/** * Test object creation * * @group order */ public function testCreate() { $orderLineHistoryFactory = new OrderLineHistoryFactory(); $orderLineHistoryFactory->setEntityNamespace('Elcodi\\Component\\Cart\\Entity\\OrderLineHistory'); $orderLineFactory = new OrderLineFactory(); $orderLineFactory->setOrderLineHistoryFactory($orderLineHistoryFactory)->setInitialOrderHistoryState('new')->setEntityNamespace('Elcodi\\Component\\Cart\\Entity\\OrderLine'); $orderLine = $orderLineFactory->create(); $orderHistoryFactory = new OrderHistoryFactory(); $orderHistoryFactory->setEntityNamespace('Elcodi\\Component\\Cart\\Entity\\OrderHistory'); $orderFactory = new OrderFactory(); $orderFactory->setOrderHistoryFactory($orderHistoryFactory)->setInitialOrderHistoryState('new')->setEntityNamespace('Elcodi\\Component\\Cart\\Entity\\Order'); $order = $orderFactory->create(); $order->addOrderLine($orderLine); $this->assertCount(1, $order->getOrderHistories()); $this->assertInstanceOf('Elcodi\\Component\\Cart\\Entity\\Interfaces\\OrderHistoryInterface', $order->getOrderHistories()->first()); $this->assertEquals('new', $order->getOrderHistories()->first()->getState()); $this->assertInstanceOf('Elcodi\\Component\\Cart\\Entity\\Interfaces\\OrderHistoryInterface', $order->getLastOrderHistory()); $this->assertSame($order->getOrderHistories()->first(), $order->getLastOrderHistory()); $this->assertCount(1, $order->getOrderLines()); $orderLine = $order->getOrderLines()->first(); $this->assertCount(1, $orderLine->getOrderLineHistories()); $this->assertInstanceOf('Elcodi\\Component\\Cart\\Entity\\Interfaces\\OrderLineHistoryInterface', $orderLine->getOrderLineHistories()->first()); $this->assertEquals($orderLine->getOrderLineHistories()->first()->getState(), 'new'); $this->assertInstanceOf('Elcodi\\Component\\Cart\\Entity\\Interfaces\\OrderLineHistoryInterface', $orderLine->getLastOrderLineHistory()); $this->assertSame($orderLine->getOrderLineHistories()->first(), $orderLine->getLastOrderLineHistory()); }
/** * Creates an instance of an entity. * * This method must return always an empty instance for related entity * * @return Order New Order instance */ public function create() { /** * @var Order $order */ $classNamespace = $this->getEntityNamespace(); $order = new $classNamespace(); $order->setQuantity(0)->setOrderLines(new ArrayCollection())->setOrderHistories(new ArrayCollection())->setCreatedAt(new DateTime()); $orderHistory = $this->orderHistoryFactory->create(); $orderHistory->setOrder($order)->setState($this->initialOrderHistoryState); $order->addOrderHistory($orderHistory)->setLastOrderHistory($orderHistory); return $order; }
/** * Subscribed on OrderLineState post change event * * This listener checks if full Order has to change its event, given the * OrderLines states * * @param OrderLineStateOnChangeEvent $event Event */ public function onOrderLineStateChange(OrderLineStateOnChangeEvent $event) { $order = $event->getOrder(); /** * If all cartLines are in same state, and last state is not same, add new line. */ $newState = null; /** * @var OrderLineInterface $line */ foreach ($order->getOrderLines() as $line) { $lastOrderLineHistory = $line->getLastOrderLineHistory(); $lastState = ""; if ($lastOrderLineHistory instanceof OrderLineHistoryInterface) { $lastState = $lastOrderLineHistory->getState(); } if (null === $newState) { $newState = $lastState; } elseif ($newState !== $lastState) { /** * not all lines in the same state */ return; } } // All order lines are in same state. $lastOrderHistory = $order->getLastOrderHistory() instanceof OrderHistoryInterface ? $order->getLastOrderHistory() : $this->orderHistoryFactory->create(); $this->orderStateEventDispatcher->dispatchOrderStatePreChangeEvent($order, $lastOrderHistory, $newState); /** * @var OrderHistoryInterface $orderHistory */ $orderHistory = $this->orderHistoryFactory->create(); $orderHistory->setState($newState)->setOrder($order); $order->addOrderHistory($orderHistory)->setLastOrderHistory($orderHistory); $this->orderStateEventDispatcher->dispatchOrderStatePreChangeEvent($order, $lastOrderHistory, $orderHistory, $newState); }