From fc088fcbd6e9ccf86005d66b5c5d79a5fce104fc Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 24 Jul 2013 17:48:10 +0200 Subject: [PATCH 01/16] start to implement cart creation process --- core/lib/Thelia/Model/Cart.php | 80 +++++++++++++++++++++++++++++ core/lib/Thelia/Model/CartQuery.php | 3 +- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/core/lib/Thelia/Model/Cart.php b/core/lib/Thelia/Model/Cart.php index 32f51484e..f7df0776f 100644 --- a/core/lib/Thelia/Model/Cart.php +++ b/core/lib/Thelia/Model/Cart.php @@ -7,4 +7,84 @@ use Thelia\Model\Base\Cart as BaseCart; class Cart extends BaseCart { + public function getCart(Request $request) + { + if ($request->cookies->has("thelia_cart")) { + //le cookie de panier existe, on le récupère + $cookie = $request->cookies->get("thelia_cart"); + + $cart = CartQuery::create()->findOneByToken($cookie); + + if ($cart) { + //le panier existe en base + $customer = $request->getSession()->getCustomerUser(); + + if ($customer) { + if($cart->getCustomerId() != $customer->getId()) { + //le customer du panier n'est pas le mm que celui connecté, il faut cloner le panier sans le customer_id + $cart = $this->duplicate($customer); + } + } else { + if ($cart->getCustomerId() != null) { + //il faut dupliquer le panier sans le customer_id + $cart = $this->duplicate(); + } + } + + } else { + $cart = $this->createCart(); + } + } else { + //le cookie de panier n'existe pas, il va falloir le créer et faire un enregistrement en base. + $cart = $this->createCart(); + } + + return $cart; + } + + public function createCart() + { + + } + + public function duplicate(Customer $customer = null) + { + $cartItems = $this->getCartItems(); + + $cart = new Cart(); + $cart->setAddressDeliveryId($this->getAddressDeliveryId()); + $cart->setAddressInvoiceId($this->getAddressInvoiceId()); + $cart->setToken($this->generateCookie()); + + if ($customer){ + $cart->setCustomer($customer); + } + // TODO : set current Currency + //$cart->setCurrency() + $cart->save(); + + foreach ($cartItems as $cartItem){ + $item = new CartItem(); + $item->setCart($cart); + $item->setProductId($cartItem->getProductId()); + $item->setQuantity($cartItem->getQuantity()); + $item->save(); + } + + return $cart; + } + + public function generateCookie() + { + $id = uniqid('', true); + + setcookie("thelia_cart", $id, time()); + + return $id; + } + + public function addItem() + { + + } } diff --git a/core/lib/Thelia/Model/CartQuery.php b/core/lib/Thelia/Model/CartQuery.php index f0b9c48ce..1d51262d2 100644 --- a/core/lib/Thelia/Model/CartQuery.php +++ b/core/lib/Thelia/Model/CartQuery.php @@ -3,7 +3,7 @@ namespace Thelia\Model; use Thelia\Model\Base\CartQuery as BaseCartQuery; - +use Symfony\Component\HttpFoundation\Request; /** * Skeleton subclass for performing query and update operations on the 'cart' table. @@ -18,4 +18,5 @@ use Thelia\Model\Base\CartQuery as BaseCartQuery; class CartQuery extends BaseCartQuery { + } // CartQuery From 72e9c139670d430c2cbb3430b4d90e7fabea2903 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 25 Jul 2013 09:38:18 +0200 Subject: [PATCH 02/16] put getCart into action classes --- core/lib/Thelia/Action/Cart.php | 55 +++++++++++++++++++++++++++++++ core/lib/Thelia/Model/Cart.php | 58 ++------------------------------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index c06ade935..49574c11c 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -91,4 +91,59 @@ class Cart implements EventSubscriberInterface "action.modifyArticle" => array("modifyArticle", 128), ); } + + public function getCart(Request $request) + { + if ($request->cookies->has("thelia_cart")) { + //le cookie de panier existe, on le récupère + $cookie = $request->cookies->get("thelia_cart"); + + $cart = CartQuery::create()->findOneByToken($cookie); + + if ($cart) { + //le panier existe en base + $customer = $request->getSession()->getCustomerUser(); + + if ($customer) { + if($cart->getCustomerId() != $customer->getId()) { + //le customer du panier n'est pas le mm que celui connecté, il faut cloner le panier sans le customer_id + $cart = $cart->duplicate($customer); + } + } else { + if ($cart->getCustomerId() != null) { + //il faut dupliquer le panier sans le customer_id + $cart = $cart->duplicate(); + } + } + + } else { + $cart = $this->createCart(); + } + } else { + //le cookie de panier n'existe pas, il va falloir le créer et faire un enregistrement en base. + $cart = $this->createCart(); + } + + return $cart; + } + + public function createCart() + { + + } + + + public function generateCookie() + { + $id = uniqid('', true); + + setcookie("thelia_cart", $id, time()); + + return $id; + } + + public function addItem() + { + + } } diff --git a/core/lib/Thelia/Model/Cart.php b/core/lib/Thelia/Model/Cart.php index f7df0776f..cf9b083c5 100644 --- a/core/lib/Thelia/Model/Cart.php +++ b/core/lib/Thelia/Model/Cart.php @@ -7,54 +7,14 @@ use Thelia\Model\Base\Cart as BaseCart; class Cart extends BaseCart { - public function getCart(Request $request) - { - if ($request->cookies->has("thelia_cart")) { - //le cookie de panier existe, on le récupère - $cookie = $request->cookies->get("thelia_cart"); - - $cart = CartQuery::create()->findOneByToken($cookie); - - if ($cart) { - //le panier existe en base - $customer = $request->getSession()->getCustomerUser(); - - if ($customer) { - if($cart->getCustomerId() != $customer->getId()) { - //le customer du panier n'est pas le mm que celui connecté, il faut cloner le panier sans le customer_id - $cart = $this->duplicate($customer); - } - } else { - if ($cart->getCustomerId() != null) { - //il faut dupliquer le panier sans le customer_id - $cart = $this->duplicate(); - } - } - - } else { - $cart = $this->createCart(); - } - } else { - //le cookie de panier n'existe pas, il va falloir le créer et faire un enregistrement en base. - $cart = $this->createCart(); - } - - return $cart; - } - - public function createCart() - { - - } - - public function duplicate(Customer $customer = null) + public function duplicate($token, Customer $customer = null) { $cartItems = $this->getCartItems(); $cart = new Cart(); $cart->setAddressDeliveryId($this->getAddressDeliveryId()); $cart->setAddressInvoiceId($this->getAddressInvoiceId()); - $cart->setToken($this->generateCookie()); + $cart->setToken($token); if ($customer){ $cart->setCustomer($customer); @@ -73,18 +33,4 @@ class Cart extends BaseCart return $cart; } - - public function generateCookie() - { - $id = uniqid('', true); - - setcookie("thelia_cart", $id, time()); - - return $id; - } - - public function addItem() - { - - } } From 652ce6a6fc3263989c015809c8855f14c2dfa393 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 25 Jul 2013 10:06:11 +0200 Subject: [PATCH 03/16] allow to create new cart --- core/lib/Thelia/Action/Cart.php | 25 ++++++++++++++++--- .../Core/HttpFoundation/Session/Session.php | 12 +++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index 49574c11c..b160c82fd 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -26,6 +26,10 @@ namespace Thelia\Action; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Core\Event\ActionEvent; +use Thelia\Core\HttpFoundation\Session\Session; +use Thelia\Model\CartQuery; +use Thelia\Model\Cart as CartModel; +use Thelia\Model\Customer; class Cart implements EventSubscriberInterface @@ -94,6 +98,10 @@ class Cart implements EventSubscriberInterface public function getCart(Request $request) { + if (null !== $cartId = $request->getSession()->getCart()){ + $cart = CartQuery::create()->findPk($cartId); + } + if ($request->cookies->has("thelia_cart")) { //le cookie de panier existe, on le récupère $cookie = $request->cookies->get("thelia_cart"); @@ -117,19 +125,30 @@ class Cart implements EventSubscriberInterface } } else { - $cart = $this->createCart(); + $cart = $this->createCart($request->getSession()); } } else { //le cookie de panier n'existe pas, il va falloir le créer et faire un enregistrement en base. - $cart = $this->createCart(); + $cart = $this->createCart($request->getSession()); } return $cart; } - public function createCart() + public function createCart(Session $session) { + $cart = new CartModel(); + $cart->setToken($this->generateCookie()); + if(null !== $customer = $session->getCustomerUser()) { + $cart->setCustomer($customer); + } + + $cart->save(); + + $session->setCart($cart->getId()); + + return $cart; } diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index ac684f6c6..2d621fbf3 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -113,4 +113,16 @@ class Session extends BaseSession { return $this->get('return_to_url', URL::getIndexPage()); } + // -- Cart ------------------------------------------------------------------ + + public function getCart() + { + return $this->get("cart_id"); + } + + public function setCart($cart_id) + { + $this->set("cart_id", $cart_id); + } + } From 08dc47fdd8fdcfcf5cf72e3e7ef520d8cd515e63 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 25 Jul 2013 10:35:04 +0200 Subject: [PATCH 04/16] add some phpdoc --- .../lib/Thelia/Core/HttpFoundation/Session/Session.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index 2d621fbf3..9c6358dbf 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -115,11 +115,21 @@ class Session extends BaseSession { // -- Cart ------------------------------------------------------------------ + /** + * retrieve cart id in session + * + * @return int cart id + */ public function getCart() { return $this->get("cart_id"); } + /** + * assign cart id in session + * + * @param $cart_id + */ public function setCart($cart_id) { $this->set("cart_id", $cart_id); From 12f78f999e8cc3fd9f8877141c2c1d2bc45e6cc7 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 25 Jul 2013 11:08:18 +0200 Subject: [PATCH 05/16] create InvalidCartException --- .../Thelia/Exception/InvalidCartException.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 core/lib/Thelia/Exception/InvalidCartException.php diff --git a/core/lib/Thelia/Exception/InvalidCartException.php b/core/lib/Thelia/Exception/InvalidCartException.php new file mode 100644 index 000000000..7eceb47d0 --- /dev/null +++ b/core/lib/Thelia/Exception/InvalidCartException.php @@ -0,0 +1,30 @@ +. */ +/* */ +/*************************************************************************************/ + + +namespace Thelia\Exception; + + +class InvalidCartException extends \RuntimeException { + +} \ No newline at end of file From 69059ea6b93f2a4dc095aabe35106c9ef6fd1d59 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Thu, 25 Jul 2013 11:14:51 +0200 Subject: [PATCH 06/16] Session::getCart return a Thelia\Model\Cart instance or null if cart is not valid --- .../Core/HttpFoundation/Session/Session.php | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index 9c6358dbf..a50ab92f5 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -25,13 +25,14 @@ namespace Thelia\Core\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Session\Session as BaseSession; use Thelia\Core\Security\User\UserInterface; -use Thelia\Form\BaseForm; -use Thelia\Model\ConfigQuery; +use Thelia\Exception\InvalidCartException; +use Thelia\Model\Base\CartQuery; +use Thelia\Model\Cart; use Thelia\Tools\URL; -class Session extends BaseSession { - - // -- Language ------------------------------------------------------------ +class Session extends BaseSession +{ + // -- Language ------------------------------------------------------------ public function getLocale() { @@ -47,34 +48,34 @@ class Session extends BaseSession { public function setCustomerUser(UserInterface $user) { - $this->set('customer_user', $user); + $this->set('customer_user', $user); } public function getCustomerUser() { - return $this->get('customer_user'); + return $this->get('customer_user'); } public function clearCustomerUser() { - return $this->remove('customer_user'); + return $this->remove('customer_user'); } // -- Admin user ----------------------------------------------------------- public function setAdminUser(UserInterface $user) { - $this->set('admin_user', $user); + $this->set('admin_user', $user); } public function getAdminUser() { - return $this->get('admin_user'); + return $this->get('admin_user'); } public function clearAdminUser() { - return $this->remove('admin_user'); + return $this->remove('admin_user'); } // -- Error form ----------------------------------------------------------- @@ -84,24 +85,24 @@ class Session extends BaseSession { */ public function setErrorFormName($formName) { - $this->set('error_form', $formName); + $this->set('error_form', $formName); } public function getErrorFormName() { - return $this->get('error_form', null); + return $this->get('error_form', null); } public function clearErrorFormName() { - return $this->remove('error_form'); + return $this->remove('error_form'); } // -- Return page ---------------------------------------------------------- public function setReturnToUrl($url) { - $this->set('return_to_url', $url); + $this->set('return_to_url', $url); } /** @@ -110,7 +111,7 @@ class Session extends BaseSession { */ public function getReturnToUrl() { - return $this->get('return_to_url', URL::getIndexPage()); + return $this->get('return_to_url', URL::getIndexPage()); } // -- Cart ------------------------------------------------------------------ @@ -122,7 +123,28 @@ class Session extends BaseSession { */ public function getCart() { - return $this->get("cart_id"); + $cart_id = $this->get("cart_id"); + $cart = null; + if ($cart_id) { + $cart = CartQuery::create()->findPk($cart_id); + try { + $this->verifyValidCart($cart); + } catch (InvalidCartException $e) { + $cart = null; + } + } + + return $cart; + } + + protected function verifyValidCart(Cart $cart) + { + $customer = $this->getCustomerUser(); + if ($customer && $cart->getCustomerId() != $customer->getId()) { + throw new InvalidCartException("customer in session and customer_id in cart are not the same"); + } else if($customer === null && $cart->getCustomerId() !== null) { + throw new InvalidCartException("Customer exists in cart and not in session"); + } } /** From 69bb513fbd2d39f875871e29e8b3d81f39410c92 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 10:43:17 +0200 Subject: [PATCH 07/16] refacto cart process --- core/lib/Thelia/Action/Cart.php | 41 ++++++++++++------- .../Core/HttpFoundation/Session/Session.php | 12 ++++-- .../Thelia/Tests/Command/BaseCommandTest.php | 29 +++++++++---- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index b160c82fd..e4e51c755 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -98,15 +98,16 @@ class Cart implements EventSubscriberInterface public function getCart(Request $request) { - if (null !== $cartId = $request->getSession()->getCart()){ - $cart = CartQuery::create()->findPk($cartId); + + if(null !== $cart = $request->getSession()->getCart()){ + return $cart; } if ($request->cookies->has("thelia_cart")) { //le cookie de panier existe, on le récupère - $cookie = $request->cookies->get("thelia_cart"); + $token = $request->cookies->get("thelia_cart"); - $cart = CartQuery::create()->findOneByToken($cookie); + $cart = CartQuery::create()->findOneByToken($token); if ($cart) { //le panier existe en base @@ -115,12 +116,12 @@ class Cart implements EventSubscriberInterface if ($customer) { if($cart->getCustomerId() != $customer->getId()) { //le customer du panier n'est pas le mm que celui connecté, il faut cloner le panier sans le customer_id - $cart = $cart->duplicate($customer); + $cart = $this->duplicateCart($cart, $request->getSession(), $customer); } } else { if ($cart->getCustomerId() != null) { //il faut dupliquer le panier sans le customer_id - $cart = $cart->duplicate(); + $cart = $this->duplicateCart($cart, $request->getSession()); } } @@ -135,7 +136,11 @@ class Cart implements EventSubscriberInterface return $cart; } - public function createCart(Session $session) + /** + * @param Session $session + * @return CartModel + */ + protected function createCart(Session $session) { $cart = new CartModel(); $cart->setToken($this->generateCookie()); @@ -152,17 +157,25 @@ class Cart implements EventSubscriberInterface } + /** + * @param CartModel $cart + * @param Session $session + * @param Customer $customer + * @return CartModel + */ + protected function duplicateCart(CartModel $cart, Session $session, Customer $customer = null) + { + $newCart = $cart->duplicate($this->generateCookie(), $customer); + $session->setCart($newCart->getId()); + + return $newCart; + } + public function generateCookie() { $id = uniqid('', true); - - setcookie("thelia_cart", $id, time()); + setcookie("thelia_cart", $id, uniqid('', true)); return $id; } - - public function addItem() - { - - } } diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index a50ab92f5..cde686ee4 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -26,7 +26,7 @@ namespace Thelia\Core\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Session\Session as BaseSession; use Thelia\Core\Security\User\UserInterface; use Thelia\Exception\InvalidCartException; -use Thelia\Model\Base\CartQuery; +use Thelia\Model\CartQuery; use Thelia\Model\Cart; use Thelia\Tools\URL; @@ -117,9 +117,9 @@ class Session extends BaseSession // -- Cart ------------------------------------------------------------------ /** - * retrieve cart id in session + * return cart if exists and is valid (checking customer) * - * @return int cart id + * @return \Thelia\Model\Cart|null */ public function getCart() { @@ -137,6 +137,12 @@ class Session extends BaseSession return $cart; } + /** + * + * + * @param \Thelia\Model\Cart $cart + * @throws \Thelia\Exception\InvalidCartException + */ protected function verifyValidCart(Cart $cart) { $customer = $this->getCustomerUser(); diff --git a/core/lib/Thelia/Tests/Command/BaseCommandTest.php b/core/lib/Thelia/Tests/Command/BaseCommandTest.php index ca9d0a632..356478ab9 100644 --- a/core/lib/Thelia/Tests/Command/BaseCommandTest.php +++ b/core/lib/Thelia/Tests/Command/BaseCommandTest.php @@ -1,12 +1,25 @@ . */ +/* */ +/*************************************************************************************/ namespace Thelia\Tests\Command; From 202d2dae99140a3fcc6795b4e5788c9994cf6859 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 10:50:03 +0200 Subject: [PATCH 08/16] initialize cart test --- core/lib/Thelia/Tests/Action/CartTest.php | 103 ++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 core/lib/Thelia/Tests/Action/CartTest.php diff --git a/core/lib/Thelia/Tests/Action/CartTest.php b/core/lib/Thelia/Tests/Action/CartTest.php new file mode 100644 index 000000000..bf7eb4376 --- /dev/null +++ b/core/lib/Thelia/Tests/Action/CartTest.php @@ -0,0 +1,103 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Tests\Action; + + +use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Thelia\Core\HttpFoundation\Request; +use Thelia\Core\HttpFoundation\Session\Session; +use Thelia\Model\Customer; + +class CartTest extends \PHPUnit_Framework_TestCase +{ + + public $session; + + public $request; + + public $actionCart; + + public $uniqid; + + + + public function setUp() + { + $this->session = new Session(new MockArraySessionStorage()); + $this->request = new Request(); + + $this->request->setSession($this->session); + + $this->uniqid = uniqid('', true); + + $this->actionCart = $this->getMock( + "\Thelia\Action\Cart", + array("generateCookie") + ); + + $this->actionCart + ->expects($this->any()) + ->method("generateCookie") + ->will($this->returnValue($this->uniqid)); + } + + public function testGetCartWithoutCustomerAndWithoutExistingCart() + { + $actionCart = $this->actionCart; + + $cart = $actionCart->getCart($this->request); + + $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNull($cart->getCustomerId()); + $this->assertNull($cart->getAddressDeliveryId()); + $this->assertNull($cart->getAddressInvoiceId()); + + } + + public function testGetCartWithCustomerAndWithoutExistingCart() + { + $actionCart = $this->actionCart; + + $request = $this->request; + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $request->getSession()->setCustomerUser($customer); + + $cart = $actionCart->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNotNull($cart->getCustomerId()); + $this->assertEquals($customer->getId(), $cart->getCustomerId()); + $this->assertNull($cart->getAddressDeliveryId()); + $this->assertNull($cart->getAddressInvoiceId()); + + } + +} \ No newline at end of file From fab90bade9dad7ba0eec4aa44e3b8043cce2e41f Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 11:06:26 +0200 Subject: [PATCH 09/16] complete cart test --- core/lib/Thelia/Action/Cart.php | 2 +- core/lib/Thelia/Tests/Action/CartTest.php | 41 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index e4e51c755..d1d5ec0dd 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -171,7 +171,7 @@ class Cart implements EventSubscriberInterface return $newCart; } - public function generateCookie() + protected function generateCookie() { $id = uniqid('', true); setcookie("thelia_cart", $id, uniqid('', true)); diff --git a/core/lib/Thelia/Tests/Action/CartTest.php b/core/lib/Thelia/Tests/Action/CartTest.php index bf7eb4376..e6647026a 100644 --- a/core/lib/Thelia/Tests/Action/CartTest.php +++ b/core/lib/Thelia/Tests/Action/CartTest.php @@ -28,6 +28,7 @@ use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Thelia\Core\HttpFoundation\Request; use Thelia\Core\HttpFoundation\Session\Session; +use Thelia\Model\Cart; use Thelia\Model\Customer; class CartTest extends \PHPUnit_Framework_TestCase @@ -73,6 +74,7 @@ class CartTest extends \PHPUnit_Framework_TestCase $this->assertNull($cart->getCustomerId()); $this->assertNull($cart->getAddressDeliveryId()); $this->assertNull($cart->getAddressInvoiceId()); + $this->assertEquals($this->uniqid, $cart->getToken()); } @@ -97,7 +99,46 @@ class CartTest extends \PHPUnit_Framework_TestCase $this->assertEquals($customer->getId(), $cart->getCustomerId()); $this->assertNull($cart->getAddressDeliveryId()); $this->assertNull($cart->getAddressInvoiceId()); + $this->assertEquals($this->uniqid, $cart->getToken()); } + public function testGetCartWithoutCustomerAndWithExistingCart() + { + $actionCart = $this->actionCart; + + $request = $this->request; + + //create a fake cart in database; + $cart = new Cart(); + $cart->setToken($this->uniqid); + $cart->save(); + + $request->cookies->set("thelia_cart", $this->uniqid); + + $getCart = $actionCart->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNull($getCart->getCustomerId()); + $this->assertNull($getCart->getAddressDeliveryId()); + $this->assertNull($getCart->getAddressInvoiceId()); + $this->assertEquals($cart->getToken(), $getCart->getToken()); + } + + public function testGetCartWithExistingCartButNotGoodCookies() + { + $actionCart = $this->actionCart; + + $request = $this->request; + + $token = "WrongToken"; + $request->cookies->set("thelia_cart", $token); + + $cart = $actionCart->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $cart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNull($cart->getCustomerId()); + $this->assertNull($cart->getAddressDeliveryId()); + $this->assertNull($cart->getAddressInvoiceId()); + $this->assertNotEquals($token, $cart->getToken()); + } + } \ No newline at end of file From 02c89c07959998f4b2eb1bf354b34874b06e8a45 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 11:12:10 +0200 Subject: [PATCH 10/16] remove use statementnot needed --- core/lib/Thelia/Tests/Action/CartTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/lib/Thelia/Tests/Action/CartTest.php b/core/lib/Thelia/Tests/Action/CartTest.php index e6647026a..41894d2f0 100644 --- a/core/lib/Thelia/Tests/Action/CartTest.php +++ b/core/lib/Thelia/Tests/Action/CartTest.php @@ -22,9 +22,6 @@ /*************************************************************************************/ namespace Thelia\Tests\Action; - -use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Thelia\Core\HttpFoundation\Request; use Thelia\Core\HttpFoundation\Session\Session; From 5f8e935a4098deb22169884928f02e0835a8162a Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 11:29:26 +0200 Subject: [PATCH 11/16] finis test for cart creation --- core/lib/Thelia/Model/Customer.php | 2 +- core/lib/Thelia/Tests/Action/CartTest.php | 74 ++++++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/core/lib/Thelia/Model/Customer.php b/core/lib/Thelia/Model/Customer.php index 4d9a78ea0..b6379380b 100755 --- a/core/lib/Thelia/Model/Customer.php +++ b/core/lib/Thelia/Model/Customer.php @@ -116,7 +116,7 @@ class Customer extends BaseCustomer implements UserInterface protected function generateRef() { - return date("YmdHisu"); + return uniqid(substr($this->getLastname(), 0, (strlen($this->getLastname()) >= 3) ? 3 : strlen($this->getLastname())), true); } public function setPassword($password) diff --git a/core/lib/Thelia/Tests/Action/CartTest.php b/core/lib/Thelia/Tests/Action/CartTest.php index 41894d2f0..3aba4762d 100644 --- a/core/lib/Thelia/Tests/Action/CartTest.php +++ b/core/lib/Thelia/Tests/Action/CartTest.php @@ -105,13 +105,13 @@ class CartTest extends \PHPUnit_Framework_TestCase $actionCart = $this->actionCart; $request = $this->request; - + $uniqid = uniqid("test1", true); //create a fake cart in database; $cart = new Cart(); - $cart->setToken($this->uniqid); + $cart->setToken($uniqid); $cart->save(); - $request->cookies->set("thelia_cart", $this->uniqid); + $request->cookies->set("thelia_cart", $uniqid); $getCart = $actionCart->getCart($request); $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); @@ -138,4 +138,72 @@ class CartTest extends \PHPUnit_Framework_TestCase $this->assertNotEquals($token, $cart->getToken()); } + public function testGetCartWithExistingCartAndCustomer() + { + $actionCart = $this->actionCart; + + $request = $this->request; + + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $uniqid = uniqid("test2", true); + //create a fake cart in database; + $cart = new Cart(); + $cart->setToken($uniqid); + $cart->setCustomer($customer); + $cart->save(); + + $request->cookies->set("thelia_cart", $uniqid); + + $request->getSession()->setCustomerUser($customer); + + $getCart = $actionCart->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNotNull($getCart->getCustomerId()); + $this->assertNull($getCart->getAddressDeliveryId()); + $this->assertNull($getCart->getAddressInvoiceId()); + $this->assertEquals($cart->getToken(), $getCart->getToken(), "token must be the same"); + $this->assertEquals($customer->getId(), $getCart->getCustomerId()); + } + + public function testGetCartWithExistinsCartAndCustomerButNotSameCustomerId() + { + $actionCart = $this->actionCart; + + $request = $this->request; + + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $uniqid = uniqid("test3", true); + //create a fake cart in database; + $cart = new Cart(); + $cart->setToken($uniqid); + + $cart->save(); + + $request->cookies->set("thelia_cart", $uniqid); + + $request->getSession()->setCustomerUser($customer); + + $getCart = $actionCart->getCart($request); + $this->assertInstanceOf("Thelia\Model\Cart", $getCart, '$cart must be an instance of cart model Thelia\Model\Cart'); + $this->assertNotNull($getCart->getCustomerId()); + $this->assertNull($getCart->getAddressDeliveryId()); + $this->assertNull($getCart->getAddressInvoiceId()); + $this->assertNotEquals($cart->getToken(), $getCart->getToken(), "token must be different"); + $this->assertEquals($customer->getId(), $getCart->getCustomerId()); + } + } \ No newline at end of file From 4b1e7ce5b0870ed6f1a6db3d5fc6b2fce5b17d4b Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 11:37:39 +0200 Subject: [PATCH 12/16] change visibility for property in cart test --- core/lib/Thelia/Tests/Action/CartTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/lib/Thelia/Tests/Action/CartTest.php b/core/lib/Thelia/Tests/Action/CartTest.php index 3aba4762d..b3ff87f8e 100644 --- a/core/lib/Thelia/Tests/Action/CartTest.php +++ b/core/lib/Thelia/Tests/Action/CartTest.php @@ -31,13 +31,13 @@ use Thelia\Model\Customer; class CartTest extends \PHPUnit_Framework_TestCase { - public $session; + protected $session; - public $request; + protected $request; - public $actionCart; + protected $actionCart; - public $uniqid; + protected $uniqid; From 747012dd3505a99ed41a5e39ee71399a9f97a31f Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 11:48:01 +0200 Subject: [PATCH 13/16] start test for session class --- .../HttpFoundation/Session/SessionTest.php | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 core/lib/Thelia/Tests/Core/HttpFoundation/Session/SessionTest.php diff --git a/core/lib/Thelia/Tests/Core/HttpFoundation/Session/SessionTest.php b/core/lib/Thelia/Tests/Core/HttpFoundation/Session/SessionTest.php new file mode 100644 index 000000000..408a4de4b --- /dev/null +++ b/core/lib/Thelia/Tests/Core/HttpFoundation/Session/SessionTest.php @@ -0,0 +1,66 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Core\HttpFoundation\Session; + +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Thelia\Core\HttpFoundation\Session\Session; +use Thelia\Model\Cart; + +class SessionTest extends \PHPUnit_Framework_TestCase +{ + + protected $session; + + public function setUp() + { + $this->session = new Session(new MockArraySessionStorage()); + } + + public function testGetCartWithoutExistingCart() + { + $session = $this->session; + + $cart = $session->getCart(); + + $this->assertNull($cart); + } + + public function testGetCartWithExistingCartWithoutCustomerConnected() + { + $session = $this->session; + + $testCart = new Cart(); + $testCart->setToken(uniqid("testSessionGetCart1", true)); + $testCart->save(); + + $session->setCart($testCart->getId()); + + $cart = $session->getCart(); + + $this->assertInstanceOf("\Thelia\Model\Cart", $cart, '$cart must be an instance of Thelia\Model\Cart'); + $this->assertEquals($testCart->getToken(), $cart->getToken()); + + } + +} \ No newline at end of file From 74fb217cb449c8cd5ed392587dce07bf5f856b2d Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 11:58:59 +0200 Subject: [PATCH 14/16] finish session test for cart part --- .../HttpFoundation/Session/SessionTest.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/core/lib/Thelia/Tests/Core/HttpFoundation/Session/SessionTest.php b/core/lib/Thelia/Tests/Core/HttpFoundation/Session/SessionTest.php index 408a4de4b..67fbfd1c2 100644 --- a/core/lib/Thelia/Tests/Core/HttpFoundation/Session/SessionTest.php +++ b/core/lib/Thelia/Tests/Core/HttpFoundation/Session/SessionTest.php @@ -26,6 +26,7 @@ namespace Thelia\Tests\Core\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Thelia\Core\HttpFoundation\Session\Session; use Thelia\Model\Cart; +use Thelia\Model\Customer; class SessionTest extends \PHPUnit_Framework_TestCase { @@ -58,9 +59,78 @@ class SessionTest extends \PHPUnit_Framework_TestCase $cart = $session->getCart(); + $this->assertNotNull($cart); $this->assertInstanceOf("\Thelia\Model\Cart", $cart, '$cart must be an instance of Thelia\Model\Cart'); $this->assertEquals($testCart->getToken(), $cart->getToken()); } + public function testGetCartWithExistingCustomerButNoCart() + { + $session = $this->session; + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john test session"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $session->setCustomerUser($customer); + + $cart = $session->getCart(); + + $this->assertNull($cart); + } + + public function testGetCartWithExistingCartAndCustomerButWithoutReferenceToCustomerInCart() + { + $session = $this->session; + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john test session"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $session->setCustomerUser($customer); + + $testCart = new Cart(); + $testCart->setToken(uniqid("testSessionGetCart2", true)); + $testCart->save(); + + $session->setCart($testCart->getId()); + + $cart = $session->getCart(); + + $this->assertNull($cart); + } + + public function testGetCartWithExistingCartAndCustomerAndReferencesEachOther() + { + $session = $this->session; + + //create a fake customer just for test. If not persists test fails ! + $customer = new Customer(); + $customer->setFirstname("john test session"); + $customer->setLastname("doe"); + $customer->setTitleId(1); + $customer->save(); + + $session->setCustomerUser($customer); + + $testCart = new Cart(); + $testCart->setToken(uniqid("testSessionGetCart3", true)); + $testCart->setCustomerId($customer->getId()); + $testCart->save(); + + $session->setCart($testCart->getId()); + + $cart = $session->getCart(); + + $this->assertNotNull($cart); + $this->assertInstanceOf("\Thelia\Model\Cart", $cart, '$cart must be an instance of Thelia\Model\Cart'); + } + } \ No newline at end of file From 9f4a2778e33bfb8547c7ebaf71863de091bc5667 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 15:40:05 +0200 Subject: [PATCH 15/16] fix endlife cookie cart --- core/lib/Thelia/Action/Cart.php | 2 +- core/lib/Thelia/Tests/Action/CartTest.php | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index d1d5ec0dd..61629bde8 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -174,7 +174,7 @@ class Cart implements EventSubscriberInterface protected function generateCookie() { $id = uniqid('', true); - setcookie("thelia_cart", $id, uniqid('', true)); + setcookie("thelia_cart", $id, time()+(60*60*24*365)); return $id; } diff --git a/core/lib/Thelia/Tests/Action/CartTest.php b/core/lib/Thelia/Tests/Action/CartTest.php index b3ff87f8e..274203d0f 100644 --- a/core/lib/Thelia/Tests/Action/CartTest.php +++ b/core/lib/Thelia/Tests/Action/CartTest.php @@ -61,6 +61,11 @@ class CartTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue($this->uniqid)); } + /** + * no cart present in session and cart_id no yet exists in cookies. + * + * In this case, a new cart instance must be create + */ public function testGetCartWithoutCustomerAndWithoutExistingCart() { $actionCart = $this->actionCart; @@ -75,6 +80,11 @@ class CartTest extends \PHPUnit_Framework_TestCase } + /** + * Customer is connected but his cart does not exists yet + * + * Cart must be created and associated to the current connected Customer + */ public function testGetCartWithCustomerAndWithoutExistingCart() { $actionCart = $this->actionCart; @@ -100,6 +110,11 @@ class CartTest extends \PHPUnit_Framework_TestCase } + /** + * Cart exists and his id put in cookies. + * + * Must return the same cart instance + */ public function testGetCartWithoutCustomerAndWithExistingCart() { $actionCart = $this->actionCart; @@ -121,6 +136,11 @@ class CartTest extends \PHPUnit_Framework_TestCase $this->assertEquals($cart->getToken(), $getCart->getToken()); } + /** + * a cart id exists in cookies but this id does not exists yet in databases + * + * a new cart must be created (different token) + */ public function testGetCartWithExistingCartButNotGoodCookies() { $actionCart = $this->actionCart; @@ -138,6 +158,9 @@ class CartTest extends \PHPUnit_Framework_TestCase $this->assertNotEquals($token, $cart->getToken()); } + /** + * + */ public function testGetCartWithExistingCartAndCustomer() { $actionCart = $this->actionCart; From a1a3b73d9cde0c763b86626a6e8f3386acfd814c Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 26 Jul 2013 15:57:22 +0200 Subject: [PATCH 16/16] add some phpdoc --- core/lib/Thelia/Tests/Action/CartTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/lib/Thelia/Tests/Action/CartTest.php b/core/lib/Thelia/Tests/Action/CartTest.php index 274203d0f..4b9712efc 100644 --- a/core/lib/Thelia/Tests/Action/CartTest.php +++ b/core/lib/Thelia/Tests/Action/CartTest.php @@ -159,7 +159,9 @@ class CartTest extends \PHPUnit_Framework_TestCase } /** + * cart and customer already exists. Cart and customer are linked. * + * cart in session must be return */ public function testGetCartWithExistingCartAndCustomer() { @@ -195,6 +197,11 @@ class CartTest extends \PHPUnit_Framework_TestCase $this->assertEquals($customer->getId(), $getCart->getCustomerId()); } + /** + * Customer is connected but cart not associated to him + * + * A new cart must be created (duplicated) containing customer id + */ public function testGetCartWithExistinsCartAndCustomerButNotSameCustomerId() { $actionCart = $this->actionCart;