├── .gitignore ├── README.md ├── src └── Bazaar │ └── Component │ ├── Cart │ ├── Domain │ │ └── Model │ │ │ ├── CartFactoryInterface.php │ │ │ ├── Basic │ │ │ ├── ItemBuilderFactory.php │ │ │ ├── ItemOption.php │ │ │ ├── ItemProperty.php │ │ │ ├── Item.php │ │ │ └── ItemBuilder.php │ │ │ ├── CartStorageInterface.php │ │ │ ├── CartProviderInterface.php │ │ │ ├── CartRepositoryInterface.php │ │ │ ├── AbstractItem.php │ │ │ ├── ItemInterface.php │ │ │ ├── CartInterface.php │ │ │ ├── AbstractCartFactory.php │ │ │ ├── CartProvider.php │ │ │ └── Cart.php │ ├── Tests │ │ ├── Infrastructure │ │ │ └── Persistence │ │ │ │ ├── Doctrine │ │ │ │ ├── Fixtures │ │ │ │ │ └── CustomCart.php │ │ │ │ └── CartFactoryTest.php │ │ │ │ └── Session │ │ │ │ └── CartProviderTest.php │ │ └── Domain │ │ │ └── Model │ │ │ ├── CartStorageTest.php │ │ │ └── CartProviderTest.php │ └── Infrastructure │ │ └── Persistence │ │ ├── Doctrine │ │ ├── CartFactory.php │ │ ├── Cart.php │ │ ├── Basic │ │ │ ├── ItemOption.php │ │ │ ├── ItemProperty.php │ │ │ ├── Item.php │ │ │ └── ItemBuilder.php │ │ └── CartRepository.php │ │ └── Session │ │ ├── CartStorage.php │ │ └── CartProvider.php │ └── Common │ ├── Domain │ ├── Service │ │ └── IdentityGeneratorServiceInterface.php │ └── Model │ │ └── SessionInterface.php │ └── Infrastructure │ └── Persistence │ └── Doctrine │ └── Session.php ├── phpunit.xml.dist ├── composer.json └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Bazaar 2 | ====== 3 | 4 | Web store platform. 5 | 6 | 7 | Requirements 8 | ------------ 9 | 10 | * PHP: >=5.3.2 11 | 12 | 13 | License 14 | ------- 15 | 16 | MIT, see LICENSE. 17 | 18 | 19 | Community 20 | --------- 21 | 22 | Want to get involved? Here are a few ways: 23 | 24 | * Find us in the [#dflydev](irc://irc.freenode.org/dflydev) IRC 25 | channel on irc.freenode.org. 26 | * Mention [@dflydev](http://twitter.com/dflydev) on Twitter. 27 | 28 | 29 | Not Invented Here 30 | ----------------- 31 | 32 | Much inspiration for this library was taken from [Sylius](http://sylius.org) 33 | and [Spree](http://spreecommerce.com/). 34 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/CartFactoryInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface CartFactoryInterface 20 | { 21 | /** 22 | * Create a cart. 23 | * 24 | * @return CartInterface 25 | */ 26 | public function createCart(); 27 | } 28 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Tests/Infrastructure/Persistence/Doctrine/Fixtures/CustomCart.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class CustomCart extends BaseCart 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./src/Bazaar/Component/*/Tests 6 | 7 | 8 | 9 | 10 | 11 | ./src/Bazaar/ 12 | 13 | ./src/Bazaar/Component/*/Tests 14 | ./src/Bazaar/Component/*/Resources 15 | ./src/Bazaar/Component/*/vendor 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Common/Domain/Service/IdentityGeneratorServiceInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface IdentityGeneratorServiceInterface 20 | { 21 | /** 22 | * Generate identity. 23 | * 24 | * @param string $suggestion Requested identity 25 | * 26 | * @return string 27 | */ 28 | public function generateIdentity($suggestion = null); 29 | } 30 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/Basic/ItemBuilderFactory.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class ItemBuilderFactory 20 | { 21 | /** 22 | * Create 23 | * 24 | * @param string $identifier Identifier 25 | * @param int $quantity Quantity 26 | * 27 | * @return BasicitemBuilder 28 | */ 29 | public function create($identifier, $quantity = 1) 30 | { 31 | return new ItemBuilder($identifier, $quantity); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bazaar/bazaar", 3 | "description": "Web store platform.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Dragonfly Development Inc.", 8 | "email": "info@dflydev.com", 9 | "homepage": "http://dflydev.com" 10 | }, 11 | { 12 | "name": "Beau Simensen", 13 | "email": "beau@dflydev.com", 14 | "homepage": "http://beausimensen.com" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.3.2" 19 | }, 20 | "require-dev": { 21 | "doctrine/common": "2.2.*", 22 | "doctrine/orm": "2.2.*", 23 | "symfony/http-foundation": "2.1.*" 24 | }, 25 | "autoload": { 26 | "psr-0": { "Bazaar": "src" } 27 | }, 28 | "extra": { 29 | "branch-alias": { 30 | "dev-master": "1.0-dev" 31 | } 32 | }, 33 | "minimum-stability": "beta" 34 | } 35 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/CartStorageInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface CartStorageInterface 20 | { 21 | /** 22 | * Get Cart Identifier for the current Cart. 23 | * 24 | * @return string 25 | */ 26 | public function getCurrentCartIdentifier(); 27 | 28 | /** 29 | * Set the current Cart. 30 | * 31 | * @param string $cartIdentifier 32 | */ 33 | public function setCurrentCartIdentifier($cartIdentifier); 34 | 35 | /** 36 | * Abandon the current Cart. 37 | */ 38 | public function abandonCurrentCart(); 39 | } 40 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/CartProviderInterface.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | interface CartProviderInterface 22 | { 23 | /** 24 | * Gets the active Cart 25 | * 26 | * If no cart is active a new cart will be created. 27 | * 28 | * @return CartInterface 29 | */ 30 | public function getCart(); 31 | 32 | /** 33 | * Sets the active Cart 34 | * 35 | * @param CartInterface $cart 36 | */ 37 | public function setCart(CartInterface $cart); 38 | 39 | /** 40 | * Abandon the active Cart 41 | */ 42 | public function abandonCart(); 43 | } 44 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/CartRepositoryInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface CartRepositoryInterface 20 | { 21 | /** 22 | * Find a Cart. 23 | * 24 | * @param string $cartIdentifier Cart Identifier 25 | * 26 | * @return CartInterface 27 | */ 28 | public function find($cartIdentifier); 29 | 30 | /** 31 | * Store a Cart. 32 | * 33 | * @param CartInterface $cart 34 | */ 35 | public function store(CartInterface $cart); 36 | 37 | /** 38 | * Remove a Cart. 39 | * 40 | * @param CartInterface $cart 41 | */ 42 | public function remove(CartInterface $cart); 43 | } 44 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Doctrine/CartFactory.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class CartFactory extends AbstractCartFactory 22 | { 23 | /** 24 | * Class that should be created 25 | * 26 | * @var string 27 | */ 28 | private $class = 'Bazaar\Component\Cart\Infrastructure\Persistence\Doctrine\Cart'; 29 | 30 | /** 31 | * {@inheritdoc} 32 | */ 33 | public function createCartInternal($cartIdentifier, $class = null) 34 | { 35 | $class = $class ?: $this->class; 36 | 37 | return new $class($cartIdentifier); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/AbstractItem.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | abstract class AbstractItem implements ItemInterface 20 | { 21 | private $quantity; 22 | 23 | /** 24 | * {@inheritdoc} 25 | */ 26 | public function quantity() 27 | { 28 | return $this->quantity; 29 | } 30 | 31 | /** 32 | * {@inheritdoc} 33 | */ 34 | public function setQuantity($quantity) 35 | { 36 | $this->quantity = $quantity; 37 | 38 | return $this; 39 | } 40 | 41 | /** 42 | * {@inheritdoc} 43 | */ 44 | public function incrementQuantity($quantity) 45 | { 46 | $this->quantity += $quantity; 47 | 48 | return $this; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Doctrine/Cart.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class Cart extends BaseCart 24 | { 25 | /** 26 | * {@inheritdoc} 27 | */ 28 | public function __construct($identifier) 29 | { 30 | parent::__construct($identifier); 31 | $this->items = new ArrayCollection; 32 | } 33 | 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | public function clearItems() 38 | { 39 | $this->items->clear(); 40 | 41 | return $this; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Doctrine/Basic/ItemOption.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class ItemOption extends BaseItemOption 22 | { 23 | private $item; 24 | 25 | /** 26 | * Constructor 27 | * 28 | * @param Item $item Item 29 | * @param string $name Name 30 | * @param string $presentation Presentation 31 | * @param int $value Value 32 | */ 33 | public function __construct(Item $item, $name, $presentation, $value) 34 | { 35 | parent::__construct($name, $presentation, $value); 36 | $this->item = $item; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Dragonfly Development Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Doctrine/Basic/ItemProperty.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class ItemProperty extends BaseItemProperty 22 | { 23 | private $item; 24 | 25 | /** 26 | * Constructor 27 | * 28 | * @param Item $item Item 29 | * @param string $name Name 30 | * @param string $presentation Presentation 31 | * @param int $value Value 32 | */ 33 | public function __construct(Item $item, $name, $presentation, $value = null) 34 | { 35 | parent::__construct($name, $presentation, $value); 36 | $this->item = $item; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/ItemInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface ItemInterface 20 | { 21 | /** 22 | * Quantity 23 | * 24 | * @return int 25 | */ 26 | public function quantity(); 27 | 28 | /** 29 | * Set quantity 30 | * 31 | * @param int $quantity 32 | * 33 | * @return ItemInterface 34 | */ 35 | public function setQuantity($quantity); 36 | 37 | /** 38 | * Increment quantity 39 | * 40 | * @param int $quantity 41 | * 42 | * @return ItemInterface 43 | */ 44 | public function incrementQuantity($quantity); 45 | 46 | /** 47 | * Is the specified Item the same item? 48 | * 49 | * @param ItemInterface $item 50 | * 51 | * @return bool 52 | */ 53 | public function equals(ItemInterface $item); 54 | } 55 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/Basic/ItemOption.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class ItemOption 20 | { 21 | protected $name; 22 | protected $presentation; 23 | protected $value; 24 | 25 | /** 26 | * Constructor 27 | * 28 | * @param string $name Name 29 | * @param string $presentation Presentation 30 | * @param int $value Value 31 | */ 32 | public function __construct($name, $presentation, $value) 33 | { 34 | $this->name = $name; 35 | $this->presentation = $presentation; 36 | $this->value = $value; 37 | } 38 | 39 | /** 40 | * Name 41 | * 42 | * @return string 43 | */ 44 | public function name() 45 | { 46 | return $this->name; 47 | } 48 | 49 | /** 50 | * Presentation 51 | * 52 | * @return string 53 | */ 54 | public function presentation() 55 | { 56 | return $this->presentation; 57 | } 58 | 59 | /** 60 | * Value 61 | * 62 | * @return string 63 | */ 64 | public function value() 65 | { 66 | return $this->value; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/CartInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface CartInterface 20 | { 21 | /** 22 | * Identifier. 23 | * 24 | * @return string 25 | */ 26 | public function identifier(); 27 | 28 | /** 29 | * All Items. 30 | * 31 | * @return array 32 | */ 33 | public function items(); 34 | 35 | /** 36 | * Add Item to Cart. 37 | * 38 | * @param ItemInterface $item 39 | * 40 | * @return CartInterface 41 | */ 42 | public function addItem(ItemInterface $item); 43 | 44 | /** 45 | * Updates Item in Cart. 46 | * 47 | * @param ItemInterface $item 48 | * 49 | * @return CartInterface 50 | */ 51 | public function updateItem(ItemInterface $item); 52 | 53 | /** 54 | * Remove Item from Cart. 55 | * 56 | * @param ItemInterface $item 57 | * 58 | * @return CartInterface 59 | */ 60 | public function removeItem(ItemInterface $item); 61 | 62 | /** 63 | * Clear all Items. 64 | * 65 | * @return CartInterface 66 | */ 67 | public function clearItems(); 68 | } 69 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/Basic/ItemProperty.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class ItemProperty 20 | { 21 | protected $name; 22 | protected $presentation; 23 | protected $value; 24 | 25 | /** 26 | * Constructor 27 | * 28 | * @param string $name Name 29 | * @param string $presentation Presentation 30 | * @param int $value Value 31 | */ 32 | public function __construct($name, $presentation, $value = null) 33 | { 34 | $this->name = $name; 35 | $this->presentation = $presentation; 36 | $this->value = $value; 37 | } 38 | 39 | /** 40 | * Name 41 | * 42 | * @return string 43 | */ 44 | public function name() 45 | { 46 | return $this->name; 47 | } 48 | 49 | /** 50 | * Presentation 51 | * 52 | * @return string 53 | */ 54 | public function presentation() 55 | { 56 | return $this->presentation; 57 | } 58 | 59 | /** 60 | * Value 61 | * 62 | * @return string 63 | */ 64 | public function value() 65 | { 66 | return $this->value; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Session/CartStorage.php: -------------------------------------------------------------------------------- 1 | 21 | */ 22 | class CartStorage implements CartStorageInterface 23 | { 24 | const SESSION_ATTRIBUTE_NAME = '_dflydev_bazaar.cart_id'; 25 | 26 | /** 27 | * Session 28 | * 29 | * @var SessionInterface 30 | */ 31 | protected $session; 32 | 33 | /** 34 | * Constructor 35 | * 36 | * @param SessionInterface $session 37 | */ 38 | public function __construct(SessionInterface $session) 39 | { 40 | $this->session = $session; 41 | } 42 | 43 | /** 44 | * {@inheritdoc} 45 | */ 46 | public function getCurrentCartIdentifier() 47 | { 48 | return $this->session->get(self::SESSION_ATTRIBUTE_NAME); 49 | } 50 | 51 | /** 52 | * {@inheritdoc} 53 | */ 54 | public function setCurrentCartIdentifier($cartIdentifier) 55 | { 56 | $this->session->set(self::SESSION_ATTRIBUTE_NAME, $cartIdentifier); 57 | } 58 | 59 | /** 60 | * {@inheritdoc} 61 | */ 62 | public function abandonCurrentCart() 63 | { 64 | $this->session->remove(self::SESSION_ATTRIBUTE_NAME); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Doctrine/CartRepository.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | class CartRepository implements CartRepositoryInterface 25 | { 26 | /** 27 | * Constructor 28 | * 29 | * @param SessionInterface $session Session 30 | * @param ObjectRepository $cartRepository Cart repository 31 | */ 32 | public function __construct(SessionInterface $session, ObjectRepository $cartRepository) 33 | { 34 | $this->session = $session; 35 | $this->cartRepository = $cartRepository; 36 | } 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | public function find($cartIdentifier) 41 | { 42 | return $this->cartRepository->find($cartIdentifier); 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function store(CartInterface $cart) 49 | { 50 | $this->session->persist($cart); 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function remove(CartInterface $cart) 57 | { 58 | $this->session->remove($cart); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Doctrine/Basic/Item.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | class Item extends BaseItem 25 | { 26 | private $cart; 27 | protected $options; 28 | protected $properties; 29 | 30 | /** 31 | * Constructor 32 | * 33 | * @param CartInterface $cart Cart 34 | * @param string $identifier Identifier 35 | * @param int $quantity Quantity 36 | * @param string $name Name 37 | * @param string $description Description 38 | * @param int $value Value 39 | * @param Collection $options Options 40 | * @param Collection $properties Properties 41 | */ 42 | public function __construct(CartInterface $cart, $identifier, $quantity = 1, $name = null, $description = null, $value = null, Collection $options = null, Collection $properties = null) 43 | { 44 | parent::__construct($identifier, $quantity, $name, $description, $value); 45 | $this->cart = $cart; 46 | $this->options = $options ?: new ArrayCollection; 47 | $this->properties = $properties ?: new ArrayCollection; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/AbstractCartFactory.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | abstract class AbstractCartFactory implements CartFactoryInterface 22 | { 23 | /** 24 | * Identity Generator 25 | * 26 | * @var IdentityGeneratorServiceInterface 27 | */ 28 | protected $identityGenerator; 29 | 30 | /** 31 | * Class to create 32 | * 33 | * @var string 34 | */ 35 | private $class; 36 | 37 | /** 38 | * Constructor 39 | * 40 | * @param IdentityGeneratorServiceInterface $identityGenerator Identity Generator 41 | * @param string $class Class to create 42 | */ 43 | public function __construct(IdentityGeneratorServiceInterface $identityGenerator, $class = null) 44 | { 45 | $this->identityGenerator = $identityGenerator; 46 | $this->class = $class; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function createCart() 53 | { 54 | $cartIdentifier = $this->identityGenerator->generateIdentity(); 55 | 56 | return $this->createCartInternal($cartIdentifier, $this->class); 57 | } 58 | 59 | /** 60 | * Create Cart (internal). 61 | * 62 | * @param string $cartIdentifier Cart identifier 63 | * @param string $class Class to create 64 | * 65 | * @return CartInterface 66 | */ 67 | abstract protected function createCartInternal($cartIdentifier, $class = null); 68 | } 69 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Doctrine/Basic/ItemBuilder.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class ItemBuilder extends BaseItemBuilder 24 | { 25 | protected $cart; 26 | 27 | /** 28 | * Constructor 29 | * 30 | * @param CartInterface $cart Cart 31 | * @param string $identifier Identifier 32 | * @param int $quantity Quantity 33 | * 34 | * @return ItemBuilder 35 | */ 36 | public function __construct(CartInterface $cart, $identifier, $quantity = 1) 37 | { 38 | parent::__construct($identifier, $quantity); 39 | $this->cart = $cart; 40 | } 41 | 42 | /** 43 | * Build 44 | * 45 | * @return Basic 46 | */ 47 | public function build() 48 | { 49 | $options = new ArrayCollection; 50 | $properties = new ArrayCollection; 51 | 52 | $basicItem = new Item( 53 | $this->cart, 54 | $this->identifier, 55 | $this->quantity, 56 | $this->name, 57 | $this->description, 58 | $this->value, 59 | $options, 60 | $properties 61 | ); 62 | 63 | foreach ($this->options as $args) { 64 | $options->set($args[0], new ItemOption($basicItem, $args[0], $args[1], $args[2])); 65 | } 66 | 67 | foreach ($this->properties as $args) { 68 | $properties->set($args[0], new ItemProperty($basicItem, $args[0], $args[1], $args[2])); 69 | } 70 | 71 | return $basicItem; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Tests/Infrastructure/Persistence/Doctrine/CartFactoryTest.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class CartFactoryTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Test create() method. (default class) 25 | */ 26 | public function testCreateDefaultClass() 27 | { 28 | $identityGenerator = $this->getMock('Bazaar\Component\Common\Domain\Service\IdentityGeneratorServiceInterface'); 29 | 30 | $identityGenerator 31 | ->expects($this->once()) 32 | ->method('generateIdentity') 33 | ->will($this->returnValue('asdf1234')); 34 | 35 | $cartFactory = new CartFactory($identityGenerator); 36 | 37 | $cart = $cartFactory->createCart(); 38 | 39 | $this->assertEquals('asdf1234', $cart->identifier()); 40 | } 41 | 42 | /** 43 | * Test create() method. (custom class) 44 | */ 45 | public function testCreateCustomClass() 46 | { 47 | $identityGenerator = $this->getMock('Bazaar\Component\Common\Domain\Service\IdentityGeneratorServiceInterface'); 48 | 49 | $identityGenerator 50 | ->expects($this->once()) 51 | ->method('generateIdentity') 52 | ->will($this->returnValue('asdf1234')); 53 | 54 | $cartFixtureClass = 'Bazaar\Component\Cart\Tests\Infrastructure\Persistence\Doctrine\Fixtures\CustomCart'; 55 | 56 | $cartFactory = new CartFactory($identityGenerator, $cartFixtureClass); 57 | 58 | $cart = $cartFactory->createCart(); 59 | 60 | $this->assertEquals('asdf1234', $cart->identifier()); 61 | $this->assertInstanceOf($cartFixtureClass, $cart); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Infrastructure/Persistence/Session/CartProvider.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | class CartProvider implements CartProviderInterface 25 | { 26 | const SESSION_ATTRIBUTE_NAME = '_dflydev_bazaar.cart'; 27 | 28 | /** 29 | * Cart 30 | * 31 | * @var CartInterface 32 | */ 33 | private $cart; 34 | 35 | /** 36 | * Session 37 | * 38 | * @var SessionInterface 39 | */ 40 | protected $session; 41 | 42 | /** 43 | * Constructor 44 | * 45 | * @param SessionInterface $session 46 | */ 47 | public function __construct(SessionInterface $session) 48 | { 49 | $this->session = $session; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getCart() 56 | { 57 | if (null !== $this->cart) { 58 | return $this->cart; 59 | } 60 | 61 | if ($this->session->has(self::SESSION_ATTRIBUTE_NAME)) { 62 | $this->cart = unserialize($this->session->get(self::SESSION_ATTRIBUTE_NAME)); 63 | 64 | return $this->cart; 65 | } 66 | 67 | $this->setCart(new Cart('session')); 68 | 69 | return $this->cart; 70 | } 71 | 72 | /** 73 | * {@inheritdoc} 74 | */ 75 | public function setCart(CartInterface $cart) 76 | { 77 | $this->cart = $cart; 78 | $this->session->set(self::SESSION_ATTRIBUTE_NAME, serialize($cart)); 79 | } 80 | 81 | /** 82 | * {@inheritdoc} 83 | */ 84 | public function abandonCart() 85 | { 86 | $this->cart = null; 87 | $this->session->remove(self::SESSION_ATTRIBUTE_NAME); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Tests/Domain/Model/CartStorageTest.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class CartStorageTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Setup 25 | */ 26 | public function setUp() 27 | { 28 | if (!interface_exists('Symfony\Component\HttpFoundation\Session\SessionInterface')) { 29 | $this->markTestSkipped('The Symfony HttpFoundation library is not available'); 30 | } 31 | } 32 | 33 | /** 34 | * Test getCurrentCartIdentifier() method. 35 | */ 36 | public function testGetCurrentCartIdentifier() 37 | { 38 | $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); 39 | 40 | $session 41 | ->expects($this->once()) 42 | ->method('get') 43 | ->with(CartStorage::SESSION_ATTRIBUTE_NAME) 44 | ->will($this->returnValue('asdf1234')); 45 | 46 | $cartStorage = new CartStorage($session); 47 | 48 | $this->assertEquals('asdf1234', $cartStorage->getCurrentCartIdentifier()); 49 | } 50 | /** 51 | * Test setCurrentCartIdentifier() method. 52 | */ 53 | 54 | public function testSetCurrentCartIdentifier() 55 | { 56 | $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); 57 | 58 | $session 59 | ->expects($this->once()) 60 | ->method('set') 61 | ->with(CartStorage::SESSION_ATTRIBUTE_NAME, 'asdf1234'); 62 | 63 | $cartStorage = new CartStorage($session); 64 | 65 | $cartStorage->setCurrentCartIdentifier('asdf1234'); 66 | } 67 | 68 | /** 69 | * Test abandonCurrentCart() method. 70 | */ 71 | public function testAbandonCurrentCart() 72 | { 73 | $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); 74 | 75 | $session 76 | ->expects($this->once()) 77 | ->method('remove') 78 | ->with(CartStorage::SESSION_ATTRIBUTE_NAME); 79 | 80 | $cartStorage = new CartStorage($session); 81 | 82 | $cartStorage->abandonCurrentCart(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Common/Infrastructure/Persistence/Doctrine/Session.php: -------------------------------------------------------------------------------- 1 | 21 | */ 22 | class Session implements SessionInterface 23 | { 24 | /** 25 | * @var EntityManager 26 | */ 27 | protected $entityManager; 28 | 29 | /** 30 | * Constructor 31 | * 32 | * @param EntityManager $entityManager Entity manager 33 | */ 34 | public function __construct(EntityManager $entityManager) 35 | { 36 | $this->entityManager = $entityManager; 37 | } 38 | 39 | /** 40 | * {@inheritdoc} 41 | */ 42 | public function persist($object) 43 | { 44 | $this->entityManager->persist($object); 45 | 46 | return $this; 47 | } 48 | 49 | /** 50 | * {@inheritdoc} 51 | */ 52 | public function remove($object) 53 | { 54 | $this->entityManager->remove($object); 55 | 56 | return $this; 57 | } 58 | 59 | /** 60 | * {@inheritdoc} 61 | */ 62 | public function merge($object) 63 | { 64 | return $this->entityManager->merge($object); 65 | } 66 | 67 | /** 68 | * {@inheritdoc} 69 | */ 70 | public function clear($objectName = null) 71 | { 72 | $this->entityManager->clear($objectName); 73 | 74 | return $this; 75 | } 76 | 77 | /** 78 | * {@inheritdoc} 79 | */ 80 | public function detach($object) 81 | { 82 | $this->entityManager->detach($object); 83 | 84 | return $this; 85 | } 86 | 87 | /** 88 | * {@inheritdoc} 89 | */ 90 | public function refresh($object) 91 | { 92 | $this->entityManager->refresh($object); 93 | 94 | return $this; 95 | } 96 | 97 | /** 98 | * {@inheritdoc} 99 | */ 100 | public function flush() 101 | { 102 | $this->entityManager->flush(); 103 | 104 | return $this; 105 | } 106 | 107 | /** 108 | * {@inheritdoc} 109 | */ 110 | public function transactional($func) 111 | { 112 | return $this->entityManager->transactional($func); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/CartProvider.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class CartProvider implements CartProviderInterface 22 | { 23 | /** 24 | * Cart 25 | * 26 | * @var CartInterface 27 | */ 28 | private $cart; 29 | 30 | /** 31 | * Cart Storage 32 | * 33 | * @var CartStorageInterface 34 | */ 35 | protected $cartStorage; 36 | 37 | /** 38 | * Cart Factory 39 | * 40 | * @var CartFactoryInterface 41 | */ 42 | protected $cartFactory; 43 | 44 | /** 45 | * Cart Repository 46 | * 47 | * @var CartRepository 48 | */ 49 | protected $cartRepository; 50 | 51 | /** 52 | * Constructor 53 | * 54 | * @param CartStorageInterface $cartStorage Cart Storage 55 | * @param CartFactoryInterface $cartFactory Cart Factory 56 | * @param CartRepositoryInterface $cartRepository Cart Repository 57 | */ 58 | public function __construct(CartStorageInterface $cartStorage, CartFactoryInterface $cartFactory, CartRepositoryInterface $cartRepository) 59 | { 60 | $this->cartStorage = $cartStorage; 61 | $this->cartFactory = $cartFactory; 62 | $this->cartRepository = $cartRepository; 63 | } 64 | 65 | /** 66 | * {@inheritdoc} 67 | */ 68 | public function getCart() 69 | { 70 | if (null !== $this->cart) { 71 | return $this->cart; 72 | } 73 | 74 | if ($cartIdentifier = $this->cartStorage->getCurrentCartIdentifier()) { 75 | $cart = $this->cartRepository->find($cartIdentifier); 76 | 77 | if ($cart) { 78 | $this->cart = $cart; 79 | 80 | return $this->cart; 81 | } 82 | } 83 | 84 | $cart = $this->cartFactory->createCart(); 85 | $this->cartRepository->store($cart); 86 | $this->cartStorage->setCurrentCartIdentifier($cart->identifier()); 87 | 88 | $this->cart = $cart; 89 | 90 | return $this->cart; 91 | } 92 | 93 | /** 94 | * {@inheritdoc} 95 | */ 96 | public function setCart(CartInterface $cart) 97 | { 98 | $this->cart = $cart; 99 | $this->cartStorage->setCurrentCartIdentifier($cart->identifier()); 100 | } 101 | 102 | /** 103 | * {@inheritdoc} 104 | */ 105 | public function abandonCart() 106 | { 107 | $this->cart = null; 108 | $this->cartStorage->abandonCurrentCart(); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/Cart.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class Cart implements CartInterface 20 | { 21 | protected $identifier; 22 | protected $items; 23 | 24 | /** 25 | * Constructor 26 | * 27 | * @param string $identifier 28 | */ 29 | public function __construct($identifier) 30 | { 31 | $this->identifier = $identifier; 32 | $this->items = array(); 33 | } 34 | 35 | /** 36 | * {@inheritdoc} 37 | */ 38 | public function identifier() 39 | { 40 | return $this->identifier; 41 | } 42 | 43 | /** 44 | * All Items. 45 | * 46 | * @return array 47 | */ 48 | public function items() 49 | { 50 | return array_values($this->items); 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function addItem(ItemInterface $item) 57 | { 58 | $exists = false; 59 | foreach ($this->items as $existingItem) { 60 | if ($item->equals($existingItem)) { 61 | $existingItem->incrementQuantity($item->quantity()); 62 | $exists = true; 63 | break; 64 | } 65 | } 66 | 67 | if (!$exists) { 68 | $this->items[] = $item; 69 | } 70 | 71 | return $this; 72 | } 73 | 74 | /** 75 | * {@inheritdoc} 76 | */ 77 | public function updateItem(ItemInterface $item) 78 | { 79 | $exists = false; 80 | foreach ($this->items as $existingItem) { 81 | if ($item->equals($existingItem)) { 82 | $this->removeItem($existingItem); 83 | $this->items[] = $item; 84 | 85 | return $this; 86 | } 87 | } 88 | 89 | return $this; 90 | } 91 | 92 | /** 93 | * {@inheritdoc} 94 | */ 95 | public function removeItem(ItemInterface $item) 96 | { 97 | $key = $this->searchItem($item); 98 | 99 | if (false !== $key) { 100 | unset($this->items[$key]); 101 | } 102 | 103 | return $this; 104 | } 105 | 106 | /** 107 | * {@inheritdoc} 108 | */ 109 | public function clearItems() 110 | { 111 | $this->items = array(); 112 | 113 | return $this; 114 | } 115 | 116 | /** 117 | * Search for Item 118 | * 119 | * @param ItemInterface $item 120 | * 121 | * @return mixed 122 | */ 123 | protected function searchItem(ItemInterface $item) 124 | { 125 | return array_search($item, $this->items, true); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/Basic/Item.php: -------------------------------------------------------------------------------- 1 | 21 | */ 22 | class Item extends AbstractItem 23 | { 24 | protected $identifier; 25 | protected $name; 26 | protected $description; 27 | protected $value; 28 | protected $options; 29 | protected $properties; 30 | 31 | /** 32 | * Constructor 33 | * 34 | * @param string $identifier Identifier 35 | * @param int $quantity Quantity 36 | * @param string $name Name 37 | * @param string $description Description 38 | * @param int $value Value 39 | * @param array $options Options 40 | * @param array $properties Properties 41 | */ 42 | public function __construct($identifier, $quantity = 1, $name = null, $description = null, $value = null, array $options = array(), array $properties = array()) 43 | { 44 | $this->identifier = $identifier; 45 | $this->setQuantity($quantity); 46 | $this->name = $name; 47 | $this->description = $description; 48 | $this->value = $value; 49 | $this->options = $options; 50 | $this->properties = $properties; 51 | } 52 | 53 | /** 54 | * Identifier 55 | * 56 | * @return string 57 | */ 58 | public function identifier() 59 | { 60 | return $this->identifier; 61 | } 62 | 63 | /** 64 | * Name 65 | * 66 | * @return string 67 | */ 68 | public function name() 69 | { 70 | return $this->name; 71 | } 72 | 73 | /** 74 | * Description 75 | * 76 | * @return string 77 | */ 78 | public function description() 79 | { 80 | return $this->description; 81 | } 82 | 83 | /** 84 | * Value 85 | * 86 | * @return int 87 | */ 88 | public function value() 89 | { 90 | return $this->value; 91 | } 92 | 93 | /** 94 | * Name 95 | * 96 | * @return array 97 | */ 98 | public function options() 99 | { 100 | return $this->options; 101 | } 102 | 103 | /** 104 | * Name 105 | * 106 | * @return array 107 | */ 108 | public function properties() 109 | { 110 | return $this->properties; 111 | } 112 | 113 | /** 114 | * {@inheritdoc} 115 | */ 116 | public function equals(ItemInterface $item) 117 | { 118 | if (!$item instanceof Item) { 119 | return false; 120 | } 121 | 122 | return $item->identifier() === $this->identifier(); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Domain/Model/Basic/ItemBuilder.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class ItemBuilder 20 | { 21 | protected $identifier; 22 | protected $quantity; 23 | protected $name; 24 | protected $description; 25 | protected $value; 26 | protected $options; 27 | protected $properties; 28 | 29 | /** 30 | * Constructor 31 | * 32 | * @param string $identifier Identifier 33 | * @param int $quantity Quantity 34 | * 35 | * @return ItemBuilder 36 | */ 37 | public function __construct($identifier, $quantity = 1) 38 | { 39 | $this->identifier = $identifier; 40 | $this->quantity = $quantity; 41 | $this->options = array(); 42 | $this->properties = array(); 43 | } 44 | 45 | /** 46 | * Set name 47 | * 48 | * @param int $name 49 | * 50 | * @return ItemBuilder 51 | */ 52 | public function setName($name) 53 | { 54 | $this->name = $name; 55 | 56 | return $this; 57 | } 58 | 59 | /** 60 | * Set description 61 | * 62 | * @param int $description 63 | * 64 | * @return ItemBuilder 65 | */ 66 | public function setDescription($description) 67 | { 68 | $this->description = $description; 69 | 70 | return $this; 71 | } 72 | 73 | /** 74 | * Set value 75 | * 76 | * @param int $value 77 | * 78 | * @return ItemBuilder 79 | */ 80 | public function setValue($value) 81 | { 82 | $this->value = $value; 83 | 84 | return $this; 85 | } 86 | 87 | /** 88 | * Add Option 89 | * 90 | * @param string $name Name 91 | * @param string $presentation Presentation 92 | * @param string $value Value 93 | * 94 | * @return ItemBuilder 95 | */ 96 | public function addOption($name, $presentation, $value) 97 | { 98 | $this->options[$name] = array($name, $presentation, $value); 99 | 100 | return $this; 101 | } 102 | 103 | /** 104 | * Add Property 105 | * 106 | * @param string $name Name 107 | * @param string $presentation Presentation 108 | * @param string $value Value 109 | * 110 | * @return ItemBuilder 111 | */ 112 | public function addProperty($name, $presentation, $value = null) 113 | { 114 | $this->properties[$name] = array($name, $presentation, $value); 115 | 116 | return $this; 117 | } 118 | 119 | /** 120 | * Build 121 | * 122 | * @return Basic 123 | */ 124 | public function build() 125 | { 126 | $options = array_map(function($args) { 127 | return new ItemOption($args[0], $args[1], $args[2]); 128 | }, $this->options); 129 | 130 | $properties = array_map(function($args) { 131 | return new ItemProperty($args[0], $args[1], $args[2]); 132 | }, $this->properties); 133 | 134 | return new Item( 135 | $this->identifier, 136 | $this->quantity, 137 | $this->name, 138 | $this->description, 139 | $this->value, 140 | $options, 141 | $properties 142 | ); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Common/Domain/Model/SessionInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface SessionInterface 20 | { 21 | /** 22 | * Tells the Session to make an instance managed and persistent. 23 | * 24 | * The object will be entered into the database as a result of the flush operation. 25 | * 26 | * NOTE: The persist operation always considers objects that are not yet known to 27 | * this Session as NEW. Do not pass detached objects to the persist operation. 28 | * 29 | * @param object $object The instance to make managed and persistent. 30 | * 31 | * @return SessionInterface 32 | */ 33 | public function persist($object); 34 | 35 | /** 36 | * Removes an object instance. 37 | * 38 | * A removed object will be removed from the database as a result of the flush operation. 39 | * 40 | * @param object $object The object instance to remove. 41 | * 42 | * @return SessionInterface 43 | */ 44 | public function remove($object); 45 | 46 | /** 47 | * Merges the state of a detached object into the persistence context 48 | * of this Session and returns the managed copy of the object. 49 | * The object passed to merge will not become associated/managed with this Session. 50 | * 51 | * @param object $object 52 | * 53 | * @return object 54 | */ 55 | public function merge($object); 56 | 57 | /** 58 | * Clears the Session. All objects that are currently managed 59 | * by this Session become detached. 60 | * 61 | * @param string $objectName if given, only objects of this type will get detached 62 | * 63 | * @return SessionInterface 64 | */ 65 | public function clear($objectName = null); 66 | 67 | /** 68 | * Detaches an object from the Session, causing a managed object to 69 | * become detached. Unflushed changes made to the object if any 70 | * (including removal of the object), will not be synchronized to the database. 71 | * Objects which previously referenced the detached object will continue to 72 | * reference it. 73 | * 74 | * @param object $object The object to detach. 75 | * 76 | * @return SessionInterface 77 | */ 78 | public function detach($object); 79 | 80 | /** 81 | * Refreshes the persistent state of an object from the database, 82 | * overriding any local changes that have not yet been persisted. 83 | * 84 | * @param object $object The object to refresh. 85 | * 86 | * @return SessionInterface 87 | */ 88 | public function refresh($object); 89 | 90 | /** 91 | * Flushes all changes to objects that have been queued up to now to the database. 92 | * This effectively synchronizes the in-memory state of managed objects with the 93 | * database. 94 | * 95 | * @return SessionInterface 96 | */ 97 | public function flush(); 98 | 99 | /** 100 | * Executes a function in a transaction. 101 | * 102 | * The function gets passed this Session instance as an (optional) parameter. 103 | * 104 | * {@link flush} is invoked prior to transaction commit. 105 | * 106 | * If an exception occurs during execution of the function or flushing or transaction commit, 107 | * the transaction is rolled back, the Session closed and the exception re-thrown. 108 | * 109 | * @param callable $func The function to execute transactionally. 110 | * 111 | * @return mixed Returns the non-empty value returned from the closure or true instead 112 | */ 113 | public function transactional($func); 114 | } 115 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Tests/Infrastructure/Persistence/Session/CartProviderTest.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class CartProviderTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Setup 25 | */ 26 | public function setUp() 27 | { 28 | if (!interface_exists('Symfony\Component\HttpFoundation\Session\SessionInterface')) { 29 | $this->markTestSkipped('The Symfony HttpFoundation library is not available'); 30 | } 31 | } 32 | 33 | /** 34 | * Test getCart() method. (existing cart) 35 | */ 36 | public function testGetExistingCart() 37 | { 38 | $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); 39 | 40 | $cart = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartInterface'); 41 | 42 | $cart 43 | ->expects($this->any()) 44 | ->method('identifier') 45 | ->will($this->returnValue('session')); 46 | 47 | $session 48 | ->expects($this->once()) 49 | ->method('has') 50 | ->with(CartProvider::SESSION_ATTRIBUTE_NAME) 51 | ->will($this->returnValue(true)); 52 | 53 | $session 54 | ->expects($this->once()) 55 | ->method('get') 56 | ->with(CartProvider::SESSION_ATTRIBUTE_NAME) 57 | ->will($this->returnValue(serialize($cart))); 58 | 59 | $cartProvider = new CartProvider($session); 60 | 61 | // This time should come from session. 62 | $this->assertEquals($cart->identifier(), $cartProvider->getCart()->identifier()); 63 | 64 | // This time should come from cache. 65 | $this->assertEquals($cart->identifier(), $cartProvider->getCart()->identifier()); 66 | } 67 | 68 | /** 69 | * Test getCart() method. (new cart) 70 | */ 71 | public function testGetNewCart() 72 | { 73 | $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); 74 | 75 | $session 76 | ->expects($this->once()) 77 | ->method('has') 78 | ->with(CartProvider::SESSION_ATTRIBUTE_NAME) 79 | ->will($this->returnValue(false)); 80 | 81 | $session 82 | ->expects($this->once()) 83 | ->method('set'); 84 | 85 | $cartProvider = new CartProvider($session); 86 | 87 | // This time should come from session. 88 | $this->assertEquals('session', $cartProvider->getCart()->identifier()); 89 | 90 | // This time should come from cache. 91 | $this->assertEquals('session', $cartProvider->getCart()->identifier()); 92 | } 93 | 94 | /** 95 | * Test setCart() method. 96 | */ 97 | public function testSetCart() 98 | { 99 | $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); 100 | 101 | $cart = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartInterface'); 102 | 103 | $session 104 | ->expects($this->once()) 105 | ->method('set') 106 | ->with(CartProvider::SESSION_ATTRIBUTE_NAME, serialize($cart)); 107 | 108 | $cartProvider = new CartProvider($session); 109 | 110 | $cartProvider->setCart($cart); 111 | 112 | $this->assertEquals($cart, $cartProvider->getCart()); 113 | } 114 | 115 | /** 116 | * Test abandonCart() method. 117 | */ 118 | public function testAbandonCart() 119 | { 120 | $session = $this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface'); 121 | 122 | $cart = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartInterface'); 123 | 124 | $session 125 | ->expects($this->once()) 126 | ->method('remove') 127 | ->with(CartProvider::SESSION_ATTRIBUTE_NAME); 128 | 129 | $session 130 | ->expects($this->once()) 131 | ->method('has') 132 | ->will($this->throwException(new \Exception('Called because abandon cart worked (cart is null)'))); 133 | 134 | $cartProvider = new CartProvider($session); 135 | 136 | $cartProvider->setCart($cart); 137 | 138 | $cartProvider->abandonCart(); 139 | 140 | try { 141 | $cartProvider->getCart(); 142 | 143 | $this->fail('The call to getCart did not throw the test exception'); 144 | } catch (\Exception $e) { 145 | $this->assertEquals('Called because abandon cart worked (cart is null)', $e->getMessage()); 146 | } 147 | 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/Bazaar/Component/Cart/Tests/Domain/Model/CartProviderTest.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class CartProviderTest extends \PHPUnit_Framework_TestCase 22 | { 23 | /** 24 | * Test getCart() method with valid existing cart identifier. 25 | */ 26 | public function testGetValidExistingIdentifier() 27 | { 28 | $cartStorage = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartStorageInterface'); 29 | $cartFactory = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartFactoryInterface'); 30 | $cartRepository = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartRepositoryInterface'); 31 | 32 | $cart = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartInterface'); 33 | 34 | $cartStorage 35 | ->expects($this->once()) 36 | ->method('getCurrentCartIdentifier') 37 | ->will($this->returnValue('asdf1234')); 38 | 39 | $cartRepository 40 | ->expects($this->once()) 41 | ->method('find') 42 | ->with('asdf1234'); 43 | 44 | $cartFactory 45 | ->expects($this->once()) 46 | ->method('createCart') 47 | ->will($this->returnValue($cart)); 48 | 49 | $cartRepository 50 | ->expects($this->once()) 51 | ->method('store') 52 | ->with($cart); 53 | 54 | $cart 55 | ->expects($this->once()) 56 | ->method('identifier') 57 | ->will($this->returnValue('asdf1234')); 58 | 59 | $cartStorage 60 | ->expects($this->once()) 61 | ->method('setCurrentCartIdentifier') 62 | ->with('asdf1234'); 63 | 64 | $cartProvider = new CartProvider($cartStorage, $cartFactory, $cartRepository); 65 | 66 | // This time should come from storage + repository. 67 | $this->assertEquals($cart, $cartProvider->getCart()); 68 | 69 | // This time should come from cache. 70 | $this->assertEquals($cart, $cartProvider->getCart()); 71 | } 72 | 73 | /** 74 | * Test getCart() method with invalid existing cart identifier. 75 | */ 76 | public function testGetInvalidExistingIdentifier() 77 | { 78 | $cartStorage = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartStorageInterface'); 79 | $cartFactory = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartFactoryInterface'); 80 | $cartRepository = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartRepositoryInterface'); 81 | 82 | $cart = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartInterface'); 83 | 84 | $cartStorage 85 | ->expects($this->once()) 86 | ->method('getCurrentCartIdentifier') 87 | ->will($this->returnValue('asdf1234')); 88 | 89 | $cartRepository 90 | ->expects($this->once()) 91 | ->method('find') 92 | ->with('asdf1234') 93 | ->will($this->returnValue($cart)); 94 | 95 | $cartProvider = new CartProvider($cartStorage, $cartFactory, $cartRepository); 96 | 97 | // This time should come from storage + repository. 98 | $this->assertEquals($cart, $cartProvider->getCart()); 99 | 100 | // This time should come from cache. 101 | $this->assertEquals($cart, $cartProvider->getCart()); 102 | } 103 | 104 | /** 105 | * Test setCart() method. 106 | */ 107 | public function testSetCart() 108 | { 109 | $cartStorage = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartStorageInterface'); 110 | $cartFactory = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartFactoryInterface'); 111 | $cartRepository = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartRepositoryInterface'); 112 | 113 | $cart = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartInterface'); 114 | 115 | $cartStorage 116 | ->expects($this->once()) 117 | ->method('setCurrentCartIdentifier') 118 | ->with('asdf1234'); 119 | 120 | $cartStorage 121 | ->expects($this->never()) 122 | ->method('getCurrentCartIdentifier'); 123 | 124 | $cart 125 | ->expects($this->once()) 126 | ->method('identifier') 127 | ->will($this->returnValue('asdf1234')); 128 | 129 | $cartProvider = new CartProvider($cartStorage, $cartFactory, $cartRepository); 130 | 131 | $cartProvider->setCart($cart); 132 | 133 | $this->assertEquals($cart, $cartProvider->getCart()); 134 | } 135 | 136 | /** 137 | * Test abandonCart() method. 138 | */ 139 | public function testAbandonCart() 140 | { 141 | $cartStorage = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartStorageInterface'); 142 | $cartFactory = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartFactoryInterface'); 143 | $cartRepository = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartRepositoryInterface'); 144 | 145 | $cart = $this->getMock('Bazaar\Component\Cart\Domain\Model\CartInterface'); 146 | 147 | $cartStorage 148 | ->expects($this->once()) 149 | ->method('abandonCurrentCart'); 150 | 151 | $cartStorage 152 | ->expects($this->once()) 153 | ->method('getCurrentCartIdentifier') 154 | ->will($this->throwException(new \Exception('Called because abandon cart worked (cart is null)'))); 155 | 156 | $cartProvider = new CartProvider($cartStorage, $cartFactory, $cartRepository); 157 | 158 | $cartProvider->setCart($cart); 159 | 160 | $cartProvider->abandonCart(); 161 | 162 | try { 163 | $cartProvider->getCart(); 164 | 165 | $this->fail('The call to getCart did not throw the test exception'); 166 | } catch (\Exception $e) { 167 | $this->assertEquals('Called because abandon cart worked (cart is null)', $e->getMessage()); 168 | } 169 | } 170 | } 171 | --------------------------------------------------------------------------------