mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-17 13:54:06 +00:00
2.8 - Reducing Duplication with Custom Assertions
This commit is contained in:
@@ -18,6 +18,17 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
$this->app->instance(PaymentGateway::class, $this->paymentGateway);
|
$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 */
|
/** @test */
|
||||||
function customer_can_purchase_concert_tickets()
|
function customer_can_purchase_concert_tickets()
|
||||||
{
|
{
|
||||||
@@ -25,7 +36,7 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
|
$concert = factory(Concert::class)->create(['ticket_price' => 3250]);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
$this->json('POST', "/concerts/{$concert->id}/orders", [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
'ticket_quantity' => 3,
|
'ticket_quantity' => 3,
|
||||||
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
||||||
@@ -46,13 +57,12 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create();
|
$concert = factory(Concert::class)->create();
|
||||||
|
|
||||||
$this->json('POST', "/concerts/{$concert->id}/orders", [
|
$this->orderTickets($concert, [
|
||||||
'ticket_quantity' => 3,
|
'ticket_quantity' => 3,
|
||||||
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertResponseStatus(422);
|
$this->assertValidationError('email');
|
||||||
$this->assertArrayHasKey('email', $this->decodeResponseJson());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -60,14 +70,13 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create();
|
$concert = factory(Concert::class)->create();
|
||||||
|
|
||||||
$this->json('POST', "/concerts/{$concert->id}/orders", [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'not-an-email-address',
|
'email' => 'not-an-email-address',
|
||||||
'ticket_quantity' => 3,
|
'ticket_quantity' => 3,
|
||||||
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertResponseStatus(422);
|
$this->assertValidationError('email');
|
||||||
$this->assertArrayHasKey('email', $this->decodeResponseJson());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -75,13 +84,12 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create();
|
$concert = factory(Concert::class)->create();
|
||||||
|
|
||||||
$this->json('POST', "/concerts/{$concert->id}/orders", [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertResponseStatus(422);
|
$this->assertValidationError('ticket_quantity');
|
||||||
$this->assertArrayHasKey('ticket_quantity', $this->decodeResponseJson());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -89,14 +97,13 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create();
|
$concert = factory(Concert::class)->create();
|
||||||
|
|
||||||
$this->json('POST', "/concerts/{$concert->id}/orders", [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
'ticket_quantity' => 0,
|
'ticket_quantity' => 0,
|
||||||
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertResponseStatus(422);
|
$this->assertValidationError('ticket_quantity');
|
||||||
$this->assertArrayHasKey('ticket_quantity', $this->decodeResponseJson());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
@@ -104,12 +111,11 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
{
|
{
|
||||||
$concert = factory(Concert::class)->create();
|
$concert = factory(Concert::class)->create();
|
||||||
|
|
||||||
$this->json('POST', "/concerts/{$concert->id}/orders", [
|
$this->orderTickets($concert, [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
'ticket_quantity' => 3,
|
'ticket_quantity' => 3,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertResponseStatus(422);
|
$this->assertValidationError('payment_token');
|
||||||
$this->assertArrayHasKey('payment_token', $this->decodeResponseJson());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user