Fixed testsuite

This commit is contained in:
Rob Gloudemans
2017-05-20 21:52:06 +02:00
parent 195cc095f5
commit ce6f0a8d85
3 changed files with 57 additions and 35 deletions

View File

@@ -15,15 +15,20 @@
"illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*" "illuminate/events": "5.1.*|5.2.*|5.3.*|5.4.*"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~4.0", "phpunit/phpunit": "~5.0",
"mockery/mockery": "~0.9.0", "mockery/mockery": "~0.9.0",
"orchestra/testbench": "~3.0" "orchestra/testbench": "~3.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Gloudemans\\Shoppingcart\\": "src/" "Gloudemans\\Shoppingcart\\": "src/"
} }
}, },
"autoload-dev": {
"psr-4": {
"Gloudemans\\Tests\\Shoppingcart\\": "tests/"
}
},
"suggest": { "suggest": {
"gloudemans/notify": "Simple flash notifications for Laravel" "gloudemans/notify": "Simple flash notifications for Laravel"
}, },

View File

@@ -1,11 +1,12 @@
<?php <?php
namespace Gloudemans\Tests\Shoppingcart;
use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\Cart;
use PHPUnit_Framework_Assert as PHPUnit; use PHPUnit_Framework_Assert as PHPUnit;
trait CartAssertions trait CartAssertions
{ {
/** /**
* Assert that the cart contains the given number of items. * Assert that the cart contains the given number of items.
* *
@@ -31,5 +32,4 @@ trait CartAssertions
PHPUnit::assertCount($rows, $cart->content(), "Expected the cart to contain {$rows} rows, but got {$actual}."); PHPUnit::assertCount($rows, $cart->content(), "Expected the cart to contain {$rows} rows, but got {$actual}.");
} }
}
}

View File

@@ -1,10 +1,21 @@
<?php <?php
namespace Gloudemans\Tests\Shoppingcart;
use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartItem;
use Gloudemans\Shoppingcart\CartItemOptions;
use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\Buyable;
use Gloudemans\Shoppingcart\Contracts\Taxable; use Gloudemans\Shoppingcart\Contracts\Taxable;
use Gloudemans\Shoppingcart\ShoppingcartServiceProvider;
use Illuminate\Auth\Events\Logout;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Session\SessionManager;
use Illuminate\Support\Collection;
use Mockery;
use PHPUnit\Framework\Assert;
class CartTest extends Orchestra\Testbench\TestCase class CartTest extends \Orchestra\Testbench\TestCase
{ {
use CartAssertions; use CartAssertions;
@@ -16,7 +27,7 @@ class CartTest extends Orchestra\Testbench\TestCase
*/ */
protected function getPackageProviders($app) protected function getPackageProviders($app)
{ {
return [\Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class]; return [ShoppingcartServiceProvider::class];
} }
/** /**
@@ -38,7 +49,17 @@ class CartTest extends Orchestra\Testbench\TestCase
'prefix' => '', 'prefix' => '',
]); ]);
} }
protected function setUp()
{
parent::setUp();
$this->app->afterResolving('migrator', function ($migrator) {
$migrator->path(realpath(__DIR__.'/../database/migrations'));
});
}
/** @test */ /** @test */
public function it_has_a_default_instance() public function it_has_a_default_instance()
{ {
@@ -89,7 +110,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$cartItem = $cart->add($item); $cartItem = $cart->add($item);
$this->assertInstanceOf(\Gloudemans\Shoppingcart\CartItem::class, $cartItem); $this->assertInstanceOf(CartItem::class, $cartItem);
$this->assertEquals('027c91341fd5cf4d2579b49c4b6a90da', $cartItem->rowId); $this->assertEquals('027c91341fd5cf4d2579b49c4b6a90da', $cartItem->rowId);
} }
@@ -122,7 +143,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$this->assertTrue(is_array($cartItems)); $this->assertTrue(is_array($cartItems));
$this->assertCount(2, $cartItems); $this->assertCount(2, $cartItems);
$this->assertContainsOnlyInstancesOf(\Gloudemans\Shoppingcart\CartItem::class, $cartItems); $this->assertContainsOnlyInstancesOf(CartItem::class, $cartItems);
} }
/** @test */ /** @test */
@@ -179,7 +200,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$cartItem = $cart->get('07d5da5550494c62daf9993cf954303f'); $cartItem = $cart->get('07d5da5550494c62daf9993cf954303f');
$this->assertInstanceOf(\Gloudemans\Shoppingcart\CartItem::class, $cartItem); $this->assertInstanceOf(CartItem::class, $cartItem);
$this->assertEquals('XL', $cartItem->options->size); $this->assertEquals('XL', $cartItem->options->size);
$this->assertEquals('red', $cartItem->options->color); $this->assertEquals('red', $cartItem->options->color);
} }
@@ -426,7 +447,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$this->assertInstanceOf(\Gloudemans\Shoppingcart\CartItem::class, $cartItem); $this->assertInstanceOf(CartItem::class, $cartItem);
} }
/** @test */ /** @test */
@@ -442,7 +463,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$content = $cart->content(); $content = $cart->content();
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $content); $this->assertInstanceOf(Collection::class, $content);
$this->assertCount(2, $content); $this->assertCount(2, $content);
} }
@@ -453,7 +474,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$content = $cart->content(); $content = $cart->content();
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $content); $this->assertInstanceOf(Collection::class, $content);
$this->assertCount(0, $content); $this->assertCount(0, $content);
} }
@@ -470,7 +491,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$content = $cart->content(); $content = $cart->content();
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $content); $this->assertInstanceOf(Collection::class, $content);
$this->assertEquals([ $this->assertEquals([
'027c91341fd5cf4d2579b49c4b6a90da' => [ '027c91341fd5cf4d2579b49c4b6a90da' => [
'rowId' => '027c91341fd5cf4d2579b49c4b6a90da', 'rowId' => '027c91341fd5cf4d2579b49c4b6a90da',
@@ -480,7 +501,7 @@ class CartTest extends Orchestra\Testbench\TestCase
'price' => 10.00, 'price' => 10.00,
'tax' => 2.10, 'tax' => 2.10,
'subtotal' => 10.0, 'subtotal' => 10.0,
'options' => new \Gloudemans\Shoppingcart\CartItemOptions, 'options' => new CartItemOptions,
], ],
'370d08585360f5c568b18d1f2e4ca1df' => [ '370d08585360f5c568b18d1f2e4ca1df' => [
'rowId' => '370d08585360f5c568b18d1f2e4ca1df', 'rowId' => '370d08585360f5c568b18d1f2e4ca1df',
@@ -490,7 +511,7 @@ class CartTest extends Orchestra\Testbench\TestCase
'price' => 10.00, 'price' => 10.00,
'tax' => 2.10, 'tax' => 2.10,
'subtotal' => 10.0, 'subtotal' => 10.0,
'options' => new \Gloudemans\Shoppingcart\CartItemOptions, 'options' => new CartItemOptions,
] ]
], $content->toArray()); ], $content->toArray());
} }
@@ -556,9 +577,9 @@ class CartTest extends Orchestra\Testbench\TestCase
return $cartItem->name == 'Some item'; return $cartItem->name == 'Some item';
}); });
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $cartItem); $this->assertInstanceOf(Collection::class, $cartItem);
$this->assertCount(1, $cartItem); $this->assertCount(1, $cartItem);
$this->assertInstanceOf(\Gloudemans\Shoppingcart\CartItem::class, $cartItem->first()); $this->assertInstanceOf(CartItem::class, $cartItem->first());
$this->assertEquals(1, $cartItem->first()->id); $this->assertEquals(1, $cartItem->first()->id);
} }
@@ -579,7 +600,7 @@ class CartTest extends Orchestra\Testbench\TestCase
return $cartItem->name == 'Some item'; return $cartItem->name == 'Some item';
}); });
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $cartItem); $this->assertInstanceOf(Collection::class, $cartItem);
} }
/** @test */ /** @test */
@@ -597,9 +618,9 @@ class CartTest extends Orchestra\Testbench\TestCase
return $cartItem->options->color == 'red'; return $cartItem->options->color == 'red';
}); });
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $cartItem); $this->assertInstanceOf(Collection::class, $cartItem);
$this->assertCount(1, $cartItem); $this->assertCount(1, $cartItem);
$this->assertInstanceOf(\Gloudemans\Shoppingcart\CartItem::class, $cartItem->first()); $this->assertInstanceOf(CartItem::class, $cartItem->first());
$this->assertEquals(1, $cartItem->first()->id); $this->assertEquals(1, $cartItem->first()->id);
} }
@@ -614,7 +635,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$this->assertContains('Gloudemans_Shoppingcart_Contracts_Buyable', PHPUnit_Framework_Assert::readAttribute($cartItem, 'associatedModel')); $this->assertContains('Gloudemans_Shoppingcart_Contracts_Buyable', Assert::readAttribute($cartItem, 'associatedModel'));
} }
/** @test */ /** @test */
@@ -630,7 +651,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da');
$this->assertEquals(get_class($model), PHPUnit_Framework_Assert::readAttribute($cartItem, 'associatedModel')); $this->assertEquals(get_class($model), Assert::readAttribute($cartItem, 'associatedModel'));
} }
/** /**
@@ -839,7 +860,6 @@ class CartTest extends Orchestra\Testbench\TestCase
{ {
$this->artisan('migrate', [ $this->artisan('migrate', [
'--database' => 'testing', '--database' => 'testing',
'--realpath' => realpath(__DIR__.'/../database/migrations'),
]); ]);
$this->expectsEvents('cart.stored'); $this->expectsEvents('cart.stored');
@@ -854,7 +874,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$serialized = serialize($cart->content()); $serialized = serialize($cart->content());
$this->seeInDatabase('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]); $this->assertDatabaseHas('shoppingcart', ['identifier' => $identifier, 'instance' => 'default', 'content' => $serialized]);
} }
/** /**
@@ -866,7 +886,6 @@ class CartTest extends Orchestra\Testbench\TestCase
{ {
$this->artisan('migrate', [ $this->artisan('migrate', [
'--database' => 'testing', '--database' => 'testing',
'--realpath' => realpath(__DIR__.'/../database/migrations'),
]); ]);
$this->expectsEvents('cart.stored'); $this->expectsEvents('cart.stored');
@@ -887,7 +906,6 @@ class CartTest extends Orchestra\Testbench\TestCase
{ {
$this->artisan('migrate', [ $this->artisan('migrate', [
'--database' => 'testing', '--database' => 'testing',
'--realpath' => realpath(__DIR__.'/../database/migrations'),
]); ]);
$this->expectsEvents('cart.restored'); $this->expectsEvents('cart.restored');
@@ -908,7 +926,7 @@ class CartTest extends Orchestra\Testbench\TestCase
$this->assertItemsInCart(1, $cart); $this->assertItemsInCart(1, $cart);
$this->dontSeeInDatabase('shoppingcart', ['identifier' => $identifier, 'instance' => 'default']); $this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => 'default']);
} }
/** @test */ /** @test */
@@ -916,7 +934,6 @@ class CartTest extends Orchestra\Testbench\TestCase
{ {
$this->artisan('migrate', [ $this->artisan('migrate', [
'--database' => 'testing', '--database' => 'testing',
'--realpath' => realpath(__DIR__.'/../database/migrations'),
]); ]);
$cart = $this->getCart(); $cart = $this->getCart();
@@ -956,15 +973,15 @@ class CartTest extends Orchestra\Testbench\TestCase
{ {
$this->app['config']->set('cart.destroy_on_logout', true); $this->app['config']->set('cart.destroy_on_logout', true);
$session = Mockery::mock(\Illuminate\Session\SessionManager::class); $session = Mockery::mock(SessionManager::class);
$session->shouldReceive('forget')->once()->with('cart'); $session->shouldReceive('forget')->once()->with('cart');
$this->app->instance(\Illuminate\Session\SessionManager::class, $session); $this->app->instance(SessionManager::class, $session);
$user = Mockery::mock(\Illuminate\Contracts\Auth\Authenticatable::class); $user = Mockery::mock(Authenticatable::class);
event(new \Illuminate\Auth\Events\Logout($user)); event(new Logout($user));
} }
/** /**
@@ -1019,4 +1036,4 @@ class CartTest extends Orchestra\Testbench\TestCase
class ModelStub { class ModelStub {
public $someValue = 'Some value'; public $someValue = 'Some value';
public function find($id) { return $this; } public function find($id) { return $this; }
} }