fix test suite issue

This commit is contained in:
Manuel Raynaud
2013-08-13 09:34:08 +02:00
parent be5d1e9ddb
commit 7f83225645
6 changed files with 243 additions and 235 deletions

View File

@@ -50,8 +50,7 @@ class Cart extends BaseAction implements EventSubscriberInterface
/**
*
* add an article in the current cart
*
* @param \Thelia\Core\Event\ActionEvent $event
* @param \Thelia\Core\Event\CartEvent $event
*/
public function addArticle(CartEvent $event)
{
@@ -85,7 +84,6 @@ class Cart extends BaseAction implements EventSubscriberInterface
$this->updateQuantity($cartItem, $quantity);
}
$this->redirect($cartAdd->getSuccessUrl($request->getUriAddingParameters(array("addCart" => 1))));
} catch (PropelException $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage()));
$message = "Failed to add this article to your cart, please try again";

View File

@@ -32,7 +32,7 @@ use Thelia\Model\ConfigQuery;
use Thelia\Model\Customer;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Core\Event\CartEvent;
use Thelia\Core\Event\Internal\CartEvent;
use Thelia\Core\Event\TheliaEvents;
trait CartTrait {

View File

@@ -14,4 +14,7 @@
<default key="_view">connexion</default>
</route>
<route id="cart.add.process" path="/cart/add" methods="post">
<default key="_controller">Thelia\Controller\Front\CartController::addArticle</default>
</route>
</routes>

View File

@@ -31,9 +31,10 @@ class CartController extends BaseFrontController
public function addArticle()
{
$cart = $this->getCart($this->getRequest);
$request = $this->getRequest();
$cart = $this->getCart($request);
$cartEvent = new CartEvent($this->getRequest(), "action.addArticle", $cart);
$cartEvent = new CartEvent($request, "action.addArticle", $cart);
$this->dispatch("action.addArticle", $cartEvent);
}

View File

@@ -41,233 +41,241 @@ use Thelia\Model\ProductSaleElementsQuery;
class CartTraitTest extends \PHPUnit_Framework_TestCase
{
public function testNoError()
protected $session;
protected $request;
protected $cartTrait;
protected $uniqid;
public function getContainer()
{
//fake assertion
$this->assertTrue(true);
$container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
$dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface");
$container->set("event_dispatcher", $dispatcher);
return $container;
}
public function setUp()
{
$this->session = new Session(new MockArraySessionStorage());
$this->request = new Request();
$this->request->setSession($this->session);
$this->uniqid = uniqid('', true);
$this->cartTrait = new MockCartTrait($this->uniqid, $this->getContainer());
}
/**
* 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()
{
$cartTrait = $this->cartTrait;
$cart = $cartTrait->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());
$this->assertEquals($this->uniqid, $cart->getToken());
}
/**
* Customer is connected but his cart does not exists yet
*
* Cart must be created and associated to the current connected Customer
*/
public function testGetCartWithCustomerAndWithoutExistingCart()
{
$cartTrait = $this->cartTrait;
$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 = $cartTrait->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());
$this->assertEquals($this->uniqid, $cart->getToken());
}
/**
* Cart exists and his id put in cookies.
*
* Must return the same cart instance
*/
public function testGetCartWithoutCustomerAndWithExistingCart()
{
$cartTrait = $this->cartTrait;
$request = $this->request;
$uniqid = uniqid("test1", true);
//create a fake cart in database;
$cart = new Cart();
$cart->setToken($uniqid);
$cart->save();
$request->cookies->set("thelia_cart", $uniqid);
$getCart = $cartTrait->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());
}
/**
* 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()
{
$cartTrait = $this->cartTrait;
$request = $this->request;
$token = "WrongToken";
$request->cookies->set("thelia_cart", $token);
$cart = $cartTrait->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());
}
/**
* cart and customer already exists. Cart and customer are linked.
*
* cart in session must be return
*/
public function testGetCartWithExistingCartAndCustomer()
{
$cartTrait = $this->cartTrait;
$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 = $cartTrait->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());
}
/**
* Customer is connected but cart not associated to him
*
* A new cart must be created (duplicated) containing customer id
*/
public function testGetCartWithExistingCartAndCustomerButNotSameCustomerId()
{
$cartTrait = $this->cartTrait;
$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 = $cartTrait->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());
}
}
/**
* Only way to mock a trait before phpunit 3.8
*
* Class MockCartTrait
* @package Thelia\Tests\Cart\CartTraitTest
*/
class MockCartTrait
{
use \Thelia\Cart\CartTrait;
public $uniqid;
public $container;
public function __construct($uniqid, $container)
{
$this->uniqid = $uniqid;
$this->container = $container;
}
public function generateCookie()
{
return $this->uniqid;
}
public function getDispatcher()
{
return $this->container->get("event_dispatcher");
}
// protected $session;
//
// protected $request;
//
// protected $actionCart;
//
// protected $uniqid;
//
//
// public function getContainer()
// {
// $container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
//
// $dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface");
//
// $container->set("event_dispatcher", $dispatcher);
//
// return $container;
// }
//
// public function setUp()
// {
// $this->session = new Session(new MockArraySessionStorage());
// $this->request = new Request();
//
// $this->request->setSession($this->session);
//
// $this->uniqid = uniqid('', true);
//
// $container = $this->getContainer();
//
//
//
// $this->actionCart = $this->getMockForTrait("\Thelia\Cart\CartTrait");
//
//
//
// $this->actionCart
// ->expects($this->any())
// ->method("generateCookie")
// ->will($this->returnValue($this->uniqid));
//
// $this->actionCart
// ->expects($this->any())
// ->method("redirect")
// ->will($this->returnValue(true))
// ;
// }
//
// /**
// * 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;
//
// $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());
// $this->assertEquals($this->uniqid, $cart->getToken());
//
// }
//
// /**
// * 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;
//
// $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());
// $this->assertEquals($this->uniqid, $cart->getToken());
//
// }
//
// /**
// * Cart exists and his id put in cookies.
// *
// * Must return the same cart instance
// */
// public function testGetCartWithoutCustomerAndWithExistingCart()
// {
// $actionCart = $this->actionCart;
//
// $request = $this->request;
// $uniqid = uniqid("test1", true);
// //create a fake cart in database;
// $cart = new Cart();
// $cart->setToken($uniqid);
// $cart->save();
//
// $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');
// $this->assertNull($getCart->getCustomerId());
// $this->assertNull($getCart->getAddressDeliveryId());
// $this->assertNull($getCart->getAddressInvoiceId());
// $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;
//
// $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());
// }
//
// /**
// * cart and customer already exists. Cart and customer are linked.
// *
// * cart in session must be return
// */
// 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());
// }
//
// /**
// * Customer is connected but cart not associated to him
// *
// * A new cart must be created (duplicated) containing customer id
// */
// public function testGetCartWithExistingCartAndCustomerButNotSameCustomerId()
// {
// $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());
// }
}

View File

@@ -9,9 +9,7 @@
{form name="thelia.cart.add" }
{* We use $INDEX_PAGE as form action to avoid mixing post and get data *}
<form action="http://thelia.local/index_dev.php?view=cart" method="post" {form_enctype form=$form}>
<input type="hidden" name="action" value="addArticle">
<form action="{url path="/cart/add" }" method="post" {form_enctype form=$form}>
{*
The form error status and the form error messages are defined in Customer action,