mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-17 22:54:32 +00:00
2.7 - Getting Started with Validation Testing
This commit is contained in:
@@ -44,7 +44,6 @@ class Handler extends ExceptionHandler
|
|||||||
*/
|
*/
|
||||||
public function render($request, Exception $exception)
|
public function render($request, Exception $exception)
|
||||||
{
|
{
|
||||||
throw $exception;
|
|
||||||
return parent::render($request, $exception);
|
return parent::render($request, $exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ class ConcertOrdersController extends Controller
|
|||||||
|
|
||||||
public function store($concertId)
|
public function store($concertId)
|
||||||
{
|
{
|
||||||
|
$this->validate(request(), [
|
||||||
|
'email' => 'required',
|
||||||
|
]);
|
||||||
|
|
||||||
$concert = Concert::find($concertId);
|
$concert = Concert::find($concertId);
|
||||||
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
||||||
$order = $concert->orderTickets(request('email'), request('ticket_quantity'));
|
$order = $concert->orderTickets(request('email'), request('ticket_quantity'));
|
||||||
|
|||||||
@@ -11,29 +11,47 @@ class PurchaseTicketsTest extends TestCase
|
|||||||
{
|
{
|
||||||
use DatabaseMigrations;
|
use DatabaseMigrations;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->paymentGateway = new FakePaymentGateway;
|
||||||
|
$this->app->instance(PaymentGateway::class, $this->paymentGateway);
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
function customer_can_purchase_concert_tickets()
|
function customer_can_purchase_concert_tickets()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
$paymentGateway = new FakePaymentGateway;
|
|
||||||
$this->app->instance(PaymentGateway::class, $paymentGateway);
|
|
||||||
|
|
||||||
$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->json('POST', "/concerts/{$concert->id}/orders", [
|
||||||
'email' => 'john@example.com',
|
'email' => 'john@example.com',
|
||||||
'ticket_quantity' => 3,
|
'ticket_quantity' => 3,
|
||||||
'payment_token' => $paymentGateway->getValidTestToken(),
|
'payment_token' => $this->paymentGateway->getValidTestToken(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
$this->assertResponseStatus(201);
|
$this->assertResponseStatus(201);
|
||||||
|
|
||||||
$this->assertEquals(9750, $paymentGateway->totalCharges());
|
$this->assertEquals(9750, $this->paymentGateway->totalCharges());
|
||||||
|
|
||||||
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
||||||
$this->assertNotNull($order);
|
$this->assertNotNull($order);
|
||||||
$this->assertEquals(3, $order->tickets()->count());
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user