From b9837f168fbe8f6ef6625978d5ea52bf359c8050 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Tue, 18 Apr 2017 14:14:25 -0400 Subject: [PATCH] 95 - Using a Fake to Intercept Email --- .../Controllers/ConcertOrdersController.php | 5 +++ app/Mail/OrderConfirmationEmail.php | 35 +++++++++++++++++++ tests/features/PurchaseTicketsTest.php | 12 ++++++- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/Mail/OrderConfirmationEmail.php diff --git a/app/Http/Controllers/ConcertOrdersController.php b/app/Http/Controllers/ConcertOrdersController.php index f0491aa..6ebf822 100644 --- a/app/Http/Controllers/ConcertOrdersController.php +++ b/app/Http/Controllers/ConcertOrdersController.php @@ -7,6 +7,8 @@ use App\Concert; use App\Reservation; use Illuminate\Http\Request; use App\Billing\PaymentGateway; +use App\Mail\OrderConfirmationEmail; +use Illuminate\Support\Facades\Mail; use App\Billing\PaymentFailedException; use App\Exceptions\NotEnoughTicketsException; @@ -32,6 +34,9 @@ class ConcertOrdersController extends Controller try { $reservation = $concert->reserveTickets(request('ticket_quantity'), request('email')); $order = $reservation->complete($this->paymentGateway, request('payment_token')); + + Mail::to($order->email)->send(new OrderConfirmationEmail($order)); + return response()->json($order, 201); } catch (PaymentFailedException $e) { $reservation->cancel(); diff --git a/app/Mail/OrderConfirmationEmail.php b/app/Mail/OrderConfirmationEmail.php new file mode 100644 index 0000000..e2751dc --- /dev/null +++ b/app/Mail/OrderConfirmationEmail.php @@ -0,0 +1,35 @@ +order = $order; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->view('view.name'); + } +} diff --git a/tests/features/PurchaseTicketsTest.php b/tests/features/PurchaseTicketsTest.php index 17471b2..3c33726 100644 --- a/tests/features/PurchaseTicketsTest.php +++ b/tests/features/PurchaseTicketsTest.php @@ -4,6 +4,8 @@ use App\Concert; use App\Facades\TicketCode; use App\Billing\PaymentGateway; use App\Billing\FakePaymentGateway; +use App\Mail\OrderConfirmationEmail; +use Illuminate\Support\Facades\Mail; use App\Facades\OrderConfirmationNumber; use App\OrderConfirmationNumberGenerator; use Illuminate\Foundation\Testing\WithoutMiddleware; @@ -53,6 +55,7 @@ class PurchaseTicketsTest extends TestCase function customer_can_purchase_tickets_to_a_published_concert() { $this->disableExceptionHandling(); + Mail::fake(); OrderConfirmationNumber::shouldReceive('generate')->andReturn('ORDERCONFIRMATION1234'); TicketCode::shouldReceive('generateFor')->andReturn('TICKETCODE1', 'TICKETCODE2', 'TICKETCODE3'); @@ -80,7 +83,14 @@ class PurchaseTicketsTest extends TestCase $this->assertEquals(9750, $this->paymentGateway->totalCharges()); $this->assertTrue($concert->hasOrderFor('john@example.com')); - $this->assertEquals(3, $concert->ordersFor('john@example.com')->first()->ticketQuantity()); + + $order = $concert->ordersFor('john@example.com')->first(); + $this->assertEquals(3, $order->ticketQuantity()); + + Mail::assertSent(OrderConfirmationEmail::class, function ($mail) use ($order) { + return $mail->hasTo('john@example.com') + && $mail->order->id == $order->id; + }); } /** @test */