2.7 - Getting Started with Validation Testing

This commit is contained in:
Adam Wathan
2016-11-09 14:48:09 -05:00
parent c7837af96a
commit 4b11c973ea
3 changed files with 27 additions and 6 deletions

View File

@@ -11,29 +11,47 @@ class PurchaseTicketsTest extends TestCase
{
use DatabaseMigrations;
protected function setUp()
{
parent::setUp();
$this->paymentGateway = new FakePaymentGateway;
$this->app->instance(PaymentGateway::class, $this->paymentGateway);
}
/** @test */
function customer_can_purchase_concert_tickets()
{
// Arrange
$paymentGateway = new FakePaymentGateway;
$this->app->instance(PaymentGateway::class, $paymentGateway);
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
// Act
$this->json('POST', "/concerts/{$concert->id}/orders", [
'email' => 'john@example.com',
'ticket_quantity' => 3,
'payment_token' => $paymentGateway->getValidTestToken(),
'payment_token' => $this->paymentGateway->getValidTestToken(),
]);
// Assert
$this->assertResponseStatus(201);
$this->assertEquals(9750, $paymentGateway->totalCharges());
$this->assertEquals(9750, $this->paymentGateway->totalCharges());
$order = $concert->orders()->where('email', 'john@example.com')->first();
$this->assertNotNull($order);
$this->assertEquals(3, $order->tickets()->count());
}
/** @test */
function email_is_required_to_purchase_tickets()
{
$concert = factory(Concert::class)->create();
$this->json('POST', "/concerts/{$concert->id}/orders", [
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken(),
]);
$this->assertResponseStatus(422);
$this->assertArrayHasKey('email', $this->decodeResponseJson());
}
}