2.8 - Reducing Duplication with Custom Assertions

This commit is contained in:
Adam Wathan
2016-11-10 13:21:41 -05:00
parent 76655861f2
commit 62bf031a17

View File

@@ -18,6 +18,17 @@ class PurchaseTicketsTest extends TestCase
$this->app->instance(PaymentGateway::class, $this->paymentGateway);
}
private function orderTickets($concert, $params)
{
$this->json('POST', "/concerts/{$concert->id}/orders", $params);
}
private function assertValidationError($field)
{
$this->assertResponseStatus(422);
$this->assertArrayHasKey($field, $this->decodeResponseJson());
}
/** @test */
function customer_can_purchase_concert_tickets()
{
@@ -25,7 +36,7 @@ class PurchaseTicketsTest extends TestCase
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
// Act
$this->json('POST', "/concerts/{$concert->id}/orders", [
$this->orderTickets($concert, [
'email' => 'john@example.com',
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken(),
@@ -46,13 +57,12 @@ class PurchaseTicketsTest extends TestCase
{
$concert = factory(Concert::class)->create();
$this->json('POST', "/concerts/{$concert->id}/orders", [
$this->orderTickets($concert, [
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken(),
]);
$this->assertResponseStatus(422);
$this->assertArrayHasKey('email', $this->decodeResponseJson());
$this->assertValidationError('email');
}
/** @test */
@@ -60,14 +70,13 @@ class PurchaseTicketsTest extends TestCase
{
$concert = factory(Concert::class)->create();
$this->json('POST', "/concerts/{$concert->id}/orders", [
$this->orderTickets($concert, [
'email' => 'not-an-email-address',
'ticket_quantity' => 3,
'payment_token' => $this->paymentGateway->getValidTestToken(),
]);
$this->assertResponseStatus(422);
$this->assertArrayHasKey('email', $this->decodeResponseJson());
$this->assertValidationError('email');
}
/** @test */
@@ -75,13 +84,12 @@ class PurchaseTicketsTest extends TestCase
{
$concert = factory(Concert::class)->create();
$this->json('POST', "/concerts/{$concert->id}/orders", [
$this->orderTickets($concert, [
'email' => 'john@example.com',
'payment_token' => $this->paymentGateway->getValidTestToken(),
]);
$this->assertResponseStatus(422);
$this->assertArrayHasKey('ticket_quantity', $this->decodeResponseJson());
$this->assertValidationError('ticket_quantity');
}
/** @test */
@@ -89,14 +97,13 @@ class PurchaseTicketsTest extends TestCase
{
$concert = factory(Concert::class)->create();
$this->json('POST', "/concerts/{$concert->id}/orders", [
$this->orderTickets($concert, [
'email' => 'john@example.com',
'ticket_quantity' => 0,
'payment_token' => $this->paymentGateway->getValidTestToken(),
]);
$this->assertResponseStatus(422);
$this->assertArrayHasKey('ticket_quantity', $this->decodeResponseJson());
$this->assertValidationError('ticket_quantity');
}
/** @test */
@@ -104,12 +111,11 @@ class PurchaseTicketsTest extends TestCase
{
$concert = factory(Concert::class)->create();
$this->json('POST', "/concerts/{$concert->id}/orders", [
$this->orderTickets($concert, [
'email' => 'john@example.com',
'ticket_quantity' => 3,
]);
$this->assertResponseStatus(422);
$this->assertArrayHasKey('payment_token', $this->decodeResponseJson());
$this->assertValidationError('payment_token');
}
}