public function testAddProductToOrder() { $mgr = $this->createOrderManager(); $order = $mgr->createOrder('test'); $product = new Product(); $product->setName('test product'); $product->setPrice(10); $mgr->addProductToOrder($order, $product); $items = $order->getItems(); $this->assertSame(1, count($items)); $item = $items[0]; $this->assertSame($product, $item->getProduct()); $this->assertSame(1, $item->getQuantity()); $this->assertSame(10, $item->getPrice()); $this->assertSame(10, $item->getPrice('subtotal')); $this->assertSame('test product', $item->getName()); $this->assertSame(10, $order->getPrice()); // add the same product again to increase the quantity $existingItem = $mgr->addProductToOrder($order, $product); $this->assertSame($existingItem, $item); $items = $order->getItems(); $this->assertSame(1, count($items)); $this->assertSame(2, $existingItem->getQuantity()); $this->assertSame(20, $item->getPrice('subtotal')); $this->assertSame(20, $order->getPrice()); //$this->assertSame(OrderEvents::UPDATE_ITEM, $mgr->getEventDispatcher()->getLastEventName(), 'a OrderEvents::UPDATE_ITEM event should be triggered'); //$event = $mgr->getEventDispatcher()->getLastEvent(); //$this->assertInstanceOf('Vespolina\Entity\Order\ItemInterface', $event->getSubject()); // specifiy the quantity when adding a product to the order $mgr->addProductToOrder($order, $product, array(), 2); $this->assertSame(4, $existingItem->getQuantity(), 'passing the quantity should add to the existing quantity'); $this->assertSame(40, $item->getPrice('subtotal')); $this->assertSame(40, $order->getPrice()); //$this->assertSame(OrderEvents::UPDATE_ITEM, $mgr->getEventDispatcher()->getLastEventName(), 'a OrderEvents::UPDATE_ITEM event should be triggered'); //$event = $mgr->getEventDispatcher()->getLastEvent(); //$this->assertInstanceOf('Vespolina\Entity\Order\ItemInterface', $event->getSubject()); $optionSet1 = array('color' => 'blue', 'size' => 'small'); $optionSet2 = array('color' => 'red', 'size' => 'small'); $option1Item = $mgr->addProductToOrder($order, $product, $optionSet1); $this->assertNotSame($option1Item, $existingItem, 'different options for the same product should be different items'); $items = $order->getItems(); $this->assertSame(2, count($items)); $option2Item = $mgr->addProductToOrder($order, $product, $optionSet2, 3); $this->assertNotSame($option1Item, $option2Item, 'different options for the same product should be different items'); $items = $order->getItems(); $this->assertSame(3, count($items)); $this->assertSame(3, $option2Item->getQuantity()); //$this->assertSame(OrderEvents::INIT_ITEM, $mgr->getEventDispatcher()->getLastEventName(), 'a OrderEvents::INIT_ITEM event should be triggered'); //$event = $mgr->getEventDispatcher()->getLastEvent(); //$this->assertInstanceOf('Vespolina\Entity\Order\ItemInterface', $event->getSubject()); }
public function testVariations() { $product = new Product(); $this->assertFalse($product->hasVariations(), 'there should not be any variations when initialized'); $this->assertEmpty($product->getVariations(), 'there should not be any variations when initialized'); $product->setPrice(3); $variation = $product->useVariation('variant1')->setPrice(4); $this->assertSame($product, $variation->getParent(), 'the original Product class should be the variation parent'); $product->useVariation('variant2')->setPrice(7); $this->assertSame(3, $product->getPrice()); $this->assertSame(4, $product->useVariation('variant1')->getPrice()); $this->assertSame(7, $product->useVariation('variant2')->getPrice()); $this->assertTrue($product->hasVariations()); $variations = $product->getVariations(); $this->assertCount(2, $variations); $this->assertInstanceof('Vespolina\\Entity\\Product\\Product', $variations['variant1']); // next goal is to be able to select a product variation and add it to the cart }