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

@@ -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());
// }
}