77 - Stubbing the Interface

This commit is contained in:
Adam Wathan
2017-03-13 12:31:53 -04:00
parent 65c1f9a152
commit bf0cba1f77
3 changed files with 15 additions and 3 deletions

View File

@@ -11,6 +11,7 @@ class Order extends Model
public static function forTickets($tickets, $email, $amount) public static function forTickets($tickets, $email, $amount)
{ {
$order = self::create([ $order = self::create([
'confirmation_number' => app(OrderConfirmationNumberGenerator::class)->generate(),
'email' => $email, 'email' => $email,
'amount' => $amount, 'amount' => $amount,
]); ]);

View File

@@ -0,0 +1,8 @@
<?php
namespace App;
interface OrderConfirmationNumberGenerator
{
public function generate();
}

View File

@@ -3,6 +3,7 @@
use App\Concert; use App\Concert;
use App\Billing\PaymentGateway; use App\Billing\PaymentGateway;
use App\Billing\FakePaymentGateway; use App\Billing\FakePaymentGateway;
use App\OrderConfirmationNumberGenerator;
use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
@@ -51,8 +52,10 @@ class PurchaseTicketsTest extends TestCase
{ {
$this->disableExceptionHandling(); $this->disableExceptionHandling();
// The API we need in our app to be able to use a different generation strategy in our tests... $orderConfirmationNumberGenerator = Mockery::mock(OrderConfirmationNumberGenerator::class, [
// $orderConfirmationNumberGenerator->generate(); // 'ORDERCONFIRMATION1234' 'generate' => 'ORDERCONFIRMATION1234',
]);
$this->app->instance(OrderConfirmationNumberGenerator::class, $orderConfirmationNumberGenerator);
$concert = factory(Concert::class)->states('published')->create(['ticket_price' => 3250])->addTickets(3); $concert = factory(Concert::class)->states('published')->create(['ticket_price' => 3250])->addTickets(3);
@@ -65,7 +68,7 @@ class PurchaseTicketsTest extends TestCase
$this->assertResponseStatus(201); $this->assertResponseStatus(201);
$this->seeJsonSubset([ $this->seeJsonSubset([
// 'confirmation_number' => 'ORDERCONFIRMATION1234', 'confirmation_number' => 'ORDERCONFIRMATION1234',
'email' => 'john@example.com', 'email' => 'john@example.com',
'ticket_quantity' => 3, 'ticket_quantity' => 3,
'amount' => 9750, 'amount' => 9750,