action order tests

This commit is contained in:
Etienne Roudeix
2013-09-23 14:44:52 +02:00
parent 194073fa42
commit 5bbf693e0f
4 changed files with 243 additions and 34 deletions

View File

@@ -144,6 +144,7 @@ class Order extends BaseAction implements EventSubscriberInterface
->setAddress3($deliveryAddress->getAddress3())
->setZipcode($deliveryAddress->getZipcode())
->setCity($deliveryAddress->getCity())
->setPhone($deliveryAddress->getPhone())
->setCountryId($deliveryAddress->getCountryId())
->save($con)
;
@@ -159,6 +160,7 @@ class Order extends BaseAction implements EventSubscriberInterface
->setAddress3($invoiceAddress->getAddress3())
->setZipcode($invoiceAddress->getZipcode())
->setCity($invoiceAddress->getCity())
->setPhone($invoiceAddress->getPhone())
->setCountryId($invoiceAddress->getCountryId())
->save($con)
;
@@ -284,20 +286,6 @@ class Order extends BaseAction implements EventSubscriberInterface
/* @todo */
}
/**
* @param \Thelia\Core\Event\OrderEvent $event
*/
public function setReference(OrderEvent $event)
{
$event->getOrder()->setRef($this->generateRef());
}
public function generateRef()
{
/* order addresses are unique */
return uniqid('ORD', true);
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
@@ -326,7 +314,6 @@ class Order extends BaseAction implements EventSubscriberInterface
TheliaEvents::ORDER_SET_INVOICE_ADDRESS => array("setInvoiceAddress", 128),
TheliaEvents::ORDER_SET_PAYMENT_MODULE => array("setPaymentModule", 128),
TheliaEvents::ORDER_PAY => array("create", 128),
TheliaEvents::ORDER_BEFORE_CREATE => array("setReference", 128),
TheliaEvents::ORDER_BEFORE_PAYMENT => array("sendOrderEmail", 128),
);
}

View File

@@ -26,6 +26,8 @@ class Order extends BaseOrder
*/
public function preInsert(ConnectionInterface $con = null)
{
$this->setRef($this->generateRef());
$this->dispatchEvent(TheliaEvents::ORDER_BEFORE_CREATE, new OrderEvent($this));
return true;
@@ -39,6 +41,12 @@ class Order extends BaseOrder
$this->dispatchEvent(TheliaEvents::ORDER_AFTER_CREATE, new OrderEvent($this));
}
public function generateRef()
{
/* order addresses are unique */
return uniqid('ORD', true);
}
/**
* calculate the total amount
*

View File

@@ -22,6 +22,7 @@
/*************************************************************************************/
namespace Thelia\Tests\Action;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Thelia\Action\Customer;
@@ -30,11 +31,21 @@ use Thelia\Core\Event\OrderEvent;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Core\Security\SecurityContext;
use Thelia\Model\Base\AddressQuery;
use Thelia\Model\Address;
use Thelia\Model\AddressQuery;
use Thelia\Model\Base\OrderProductQuery;
use Thelia\Model\OrderStatus;
use Thelia\Model\CartQuery;
use Thelia\Model\ProductSaleElementsQuery;
use Thelia\Model\Cart;
use Thelia\Model\CartItem;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\CustomerQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Model\Order as OrderModel;
use Thelia\Model\Customer as CustomerModel;
use Thelia\Action\Order;
use Thelia\Model\ProductQuery;
use Thelia\Module\BaseModule;
/**
@@ -59,21 +70,34 @@ class OrderTest extends \PHPUnit_Framework_TestCase
*/
protected $orderEvent;
/**
* @var CustomerModel $customer
*/
protected $customer;
/**
* @var Cart $customer
*/
protected $cart;
/**
* @var CartItem[]
*/
protected $cartItems;
public function setUp()
{
$container = new ContainerBuilder();
// $session = new Session(new MockArraySessionStorage());
// $request = new Request();
// $dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface");
$session = new Session(new MockArraySessionStorage());
$request = new Request();
$dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface");
// $request->setSession($session);
$request->setSession($session);
// $container->set("event_dispatcher", $dispatcher);
// $container->set('request', $request);
// $container->set('thelia.securityContext', new SecurityContext($request));
$container->set("event_dispatcher", $dispatcher);
$container->set('request', $request);
$container->set('thelia.securityContext', new SecurityContext($request));
$this->container = $container;
@@ -82,10 +106,14 @@ class OrderTest extends \PHPUnit_Framework_TestCase
$this->orderAction = new Order($this->container);
/* load customer */
/*$this->customer = $this->loadCustomer();
$this->customer = $this->loadCustomer();
if(null === $this->customer) {
return;
}*/
}
/* fill cart */
$this->cart = $this->fillCart();
}
public function loadCustomer()
@@ -100,6 +128,55 @@ class OrderTest extends \PHPUnit_Framework_TestCase
return $customer;
}
public function fillCart()
{
$currency = CurrencyQuery::create()->findOne();
//create a fake cart in database;
$cart = new Cart();
$cart->setToken(uniqid("createorder", true))
->setCustomer($this->customer)
->setCurrency($currency)
->save();
/* add 3 items */
$productList = array();
for($i=0; $i<3; $i++) {
$pse = ProductSaleElementsQuery::create()
->filterByProduct(
ProductQuery::create()
->filterByVisible(1)
->filterById($productList, Criteria::NOT_IN)
->find()
)
->filterByQuantity(5, Criteria::GREATER_EQUAL)
->joinProductPrice('pp', Criteria::INNER_JOIN)
->addJoinCondition('pp', 'currency_id = ?', $currency->getId(), null, \PDO::PARAM_INT)
->withColumn('`pp`.price', 'price_PRICE')
->withColumn('`pp`.promo_price', 'price_PROMO_PRICE')
->findOne();
$productList[] = $pse->getProductId();
$cartItem = new CartItem();
$cartItem
->setCart($cart)
->setProduct($pse->getProduct())
->setProductSaleElements($pse)
->setQuantity($i)
->setPrice($pse->getPrice())
->setPromoPrice($pse->getPromoPrice())
->setPromo($pse->getPromo())
->setPriceEndOfLife(time() + 60*60*24*30)
->save();
$this->cartItems[] = $cartItem;
}
$this->container->get('request')->getSession()->setCart($cart->getId());
return $cart;
}
public function testSetDeliveryAddress()
{
//$validAddressId = AddressQuery::create()->findOneByCustomerId($this->customer->getId());
@@ -128,15 +205,6 @@ class OrderTest extends \PHPUnit_Framework_TestCase
public function testSetDeliveryModule()
{
/*$deliveryModule = ModuleQuery::create()
->filterByType(BaseModule::DELIVERY_MODULE_TYPE)
->filterByActivate(1)
->findOne();
if(null === $deliveryModule) {
return;
}*/
$this->orderEvent->setDeliveryModule(123);
$this->orderAction->setDeliveryModule($this->orderEvent);
@@ -158,4 +226,132 @@ class OrderTest extends \PHPUnit_Framework_TestCase
$this->orderEvent->getOrder()->getPaymentModuleId()
);
}
public function testCreate()
{
$validDeliveryAddress = AddressQuery::create()->findOneByCustomerId($this->customer->getId());
$validInvoiceAddress = AddressQuery::create()->filterById($validDeliveryAddress->getId(), Criteria::NOT_EQUAL)->findOneByCustomerId($this->customer->getId());
$deliveryModule = ModuleQuery::create()
->filterByType(BaseModule::DELIVERY_MODULE_TYPE)
->filterByActivate(1)
->findOne();
if(null === $deliveryModule) {
return;
}
$paymentModule = ModuleQuery::create()
->filterByType(BaseModule::PAYMENT_MODULE_TYPE)
->filterByActivate(1)
->findOne();
if(null === $paymentModule) {
return;
}
$this->orderEvent->getOrder()->chosenDeliveryAddress = $validDeliveryAddress->getId();
$this->orderEvent->getOrder()->chosenInvoiceAddress = $validInvoiceAddress->getId();
$this->orderEvent->getOrder()->setDeliveryModuleId($deliveryModule->getId());
$this->orderEvent->getOrder()->setPostage(20);
$this->orderEvent->getOrder()->setPaymentModuleId($paymentModule->getId());
/* memorize current stocks */
$itemsStock = array();
foreach($this->cartItems as $index => $cartItem) {
$itemsStock[$index] = $cartItem->getProductSaleElements()->getQuantity();
}
$this->orderAction->create($this->orderEvent);
$placedOrder = $this->orderEvent->getPlacedOrder();
$this->assertNotNull($placedOrder);
$this->assertNotNull($placedOrder->getId());
/* check customer */
$this->assertEquals($this->customer->getId(), $placedOrder->getCustomerId(), 'customer i does not match');
/* check delivery address */
$deliveryOrderAddress = $placedOrder->getOrderAddressRelatedByDeliveryOrderAddressId();
$this->assertEquals($validDeliveryAddress->getCustomerTitle()->getId(), $deliveryOrderAddress->getCustomerTitleId(), 'delivery address title does not match');
$this->assertEquals($validDeliveryAddress->getCompany(), $deliveryOrderAddress->getCompany(), 'delivery address company does not match');
$this->assertEquals($validDeliveryAddress->getFirstname(), $deliveryOrderAddress->getFirstname(), 'delivery address fistname does not match');
$this->assertEquals($validDeliveryAddress->getLastname(), $deliveryOrderAddress->getLastname(), 'delivery address lastname does not match');
$this->assertEquals($validDeliveryAddress->getAddress1(), $deliveryOrderAddress->getAddress1(), 'delivery address address1 does not match');
$this->assertEquals($validDeliveryAddress->getAddress2(), $deliveryOrderAddress->getAddress2(), 'delivery address address2 does not match');
$this->assertEquals($validDeliveryAddress->getAddress3(), $deliveryOrderAddress->getAddress3(), 'delivery address address3 does not match');
$this->assertEquals($validDeliveryAddress->getZipcode(), $deliveryOrderAddress->getZipcode(), 'delivery address zipcode does not match');
$this->assertEquals($validDeliveryAddress->getCity(), $deliveryOrderAddress->getCity(), 'delivery address city does not match');
$this->assertEquals($validDeliveryAddress->getPhone(), $deliveryOrderAddress->getPhone(), 'delivery address phone does not match');
$this->assertEquals($validDeliveryAddress->getCountryId(), $deliveryOrderAddress->getCountryId(), 'delivery address country does not match');
/* check invoice address */
$invoiceOrderAddress = $placedOrder->getOrderAddressRelatedByInvoiceOrderAddressId();
$this->assertEquals($validInvoiceAddress->getCustomerTitle()->getId(), $invoiceOrderAddress->getCustomerTitleId(), 'invoice address title does not match');
$this->assertEquals($validInvoiceAddress->getCompany(), $invoiceOrderAddress->getCompany(), 'invoice address company does not match');
$this->assertEquals($validInvoiceAddress->getFirstname(), $invoiceOrderAddress->getFirstname(), 'invoice address fistname does not match');
$this->assertEquals($validInvoiceAddress->getLastname(), $invoiceOrderAddress->getLastname(), 'invoice address lastname does not match');
$this->assertEquals($validInvoiceAddress->getAddress1(), $invoiceOrderAddress->getAddress1(), 'invoice address address1 does not match');
$this->assertEquals($validInvoiceAddress->getAddress2(), $invoiceOrderAddress->getAddress2(), 'invoice address address2 does not match');
$this->assertEquals($validInvoiceAddress->getAddress3(), $invoiceOrderAddress->getAddress3(), 'invoice address address3 does not match');
$this->assertEquals($validInvoiceAddress->getZipcode(), $invoiceOrderAddress->getZipcode(), 'invoice address zipcode does not match');
$this->assertEquals($validInvoiceAddress->getCity(), $invoiceOrderAddress->getCity(), 'invoice address city does not match');
$this->assertEquals($validInvoiceAddress->getPhone(), $invoiceOrderAddress->getPhone(), 'invoice address phone does not match');
$this->assertEquals($validInvoiceAddress->getCountryId(), $invoiceOrderAddress->getCountryId(), 'invoice address country does not match');
/* check currency */
$this->assertEquals($this->cart->getCurrencyId(), $placedOrder->getCurrencyId(), 'currency id does not match');
$this->assertEquals($this->cart->getCurrency()->getRate(), $placedOrder->getCurrencyRate(), 'currency rate does not match');
/* check delivery module */
$this->assertEquals(20, $placedOrder->getPostage(), 'postage does not match');
$this->assertEquals($deliveryModule->getId(), $placedOrder->getDeliveryModuleId(), 'delivery module does not match');
/* check payment module */
$this->assertEquals($paymentModule->getId(), $placedOrder->getPaymentModuleId(), 'payment module does not match');
/* check status */
$this->assertEquals(OrderStatus::CODE_NOT_PAID, $placedOrder->getOrderStatus()->getCode(), 'status does not match');
/* check lang */
$this->assertEquals($this->container->get('request')->getSession()->getLang()->getId(), $placedOrder->getLangId(), 'lang does not match');
/* check ordered product */
foreach($this->cartItems as $index => $cartItem) {
$orderProduct = OrderProductQuery::create()
->filterByOrderId($placedOrder->getId())
->filterByProductRef($cartItem->getProduct()->getRef())
->filterByProductSaleElementsRef($cartItem->getProductSaleElements()->getRef())
->filterByQuantity($cartItem->getQuantity())
->filterByPrice($cartItem->getPrice(), Criteria::LIKE)
->filterByPromoPrice($cartItem->getPromoPrice(), Criteria::LIKE)
->filterByWasNew($cartItem->getProductSaleElements()->getNewness())
->filterByWasInPromo($cartItem->getPromo())
->filterByWeight($cartItem->getProductSaleElements()->getWeight())
->findOne();
$this->assertNotNull($orderProduct);
/* check attribute combinations */
$this->assertEquals(
$cartItem->getProductSaleElements()->getAttributeCombinations()->count(),
$orderProduct->getOrderProductAttributeCombinations()->count()
);
/* check stock decrease */
$this->assertEquals(
$itemsStock[$index] - $orderProduct->getQuantity(),
$cartItem->getProductSaleElements()->getQuantity()
);
/* check tax */
$orderProductTaxList = $orderProduct->getOrderProductTaxes();
foreach($cartItem->getProduct()->getTaxRule()->getTaxDetail($validDeliveryAddress->getCountry(), $cartItem->getPromo() == 1 ? $cartItem->getPromoPrice() : $cartItem->getPrice()) as $index => $tax) {
$orderProductTax = $orderProductTaxList[$index];
$this->assertEquals($tax->getAmount(), $orderProductTax->getAmount());
}
}
}
}

View File

@@ -160,6 +160,24 @@ try {
"test@thelia.net",
"azerty"
);
for ($j = 0; $j <= 3; $j++) {
$address = new Thelia\Model\Address();
$address->setLabel($faker->text(20))
->setTitleId(rand(1,3))
->setFirstname($faker->firstname)
->setLastname($faker->lastname)
->setAddress1($faker->streetAddress)
->setAddress2($faker->streetAddress)
->setAddress3($faker->streetAddress)
->setCellphone($faker->phoneNumber)
->setPhone($faker->phoneNumber)
->setZipcode($faker->postcode)
->setCity($faker->city)
->setCountryId(64)
->setCustomer($customer)
->save()
;
}
for($i = 0; $i < 50; $i++) {
$customer = new Thelia\Model\Customer();