setProductDefinition() публичный Метод

public setProductDefinition ( ProductDefinition $productDefinition )
$productDefinition Sonata\Component\Product\ProductDefinition
Пример #1
0
 public function testTotal()
 {
     $currency = $this->getMock('Sonata\\Component\\Currency\\Currency');
     $basket = new Basket();
     $basket->setCurrency($currency);
     $manager = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface');
     $manager->expects($this->any())->method('getClass')->will($this->returnValue('BasketTest_Product'));
     $productProvider = new ProductProviderTest($this->getMock('JMS\\Serializer\\SerializerInterface'));
     $productProvider->setCurrencyPriceCalculator(new CurrencyPriceCalculator());
     $productProvider->setEventDispatcher($this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface'));
     $productDefinition = new ProductDefinition($productProvider, $manager);
     $product = $this->getMockProduct();
     $product->expects($this->any())->method('isRecurrentPayment')->will($this->returnValue(false));
     $pool = new Pool();
     $pool->addProduct('product_code', $productDefinition);
     $basket->setProductPool($pool);
     $this->assertFalse($basket->hasProduct($product), '::hasProduct() - The product is not present in the basket');
     $basketElement = new BasketElement();
     $basketElement->setProductDefinition($productDefinition);
     $basketElement->setProduct('product_code', $product);
     $basket->addBasketElement($basketElement);
     $this->assertTrue($basket->hasProduct($product), '::hasProduct() - The product is present in the basket');
     $this->assertEquals(1, $basketElement->getQuantity(), '::getQuantity() - return 1');
     $this->assertEquals(15, $basketElement->getUnitPrice(false), '::getQuantity() - return 2');
     $this->assertEquals(15, $basketElement->getTotal(false), '::getQuantity() - return 2');
     $this->assertEquals(15, $basket->getTotal(false), '::getTotal() w/o vat return 15');
     $this->assertEquals(17.94, $basket->getTotal(true), '::getTotal() w/ vat return 18');
     $basketElement->setQuantity(2);
     $this->assertEquals(2, $basketElement->getQuantity(), '::getQuantity() - return 2');
     $this->assertEquals(15, $basketElement->getUnitPrice(false), '::getQuantity() - return 2');
     $this->assertEquals(30, $basketElement->getTotal(false), '::getQuantity() - return 2');
     $this->assertEquals(30, $basket->getTotal(false), '::getTotal() w/o vat return 30');
     $this->assertEquals(35.88, $basket->getTotal(true), '::getTotal() w/ vat return true');
     // Recurrent payments
     $this->assertEquals(0, $basket->getTotal(false, true), '::getTotal() for recurrent payments only');
     $newProduct = $this->getMockProduct();
     $newProduct->expects($this->any())->method('isRecurrentPayment')->will($this->returnValue(true));
     $basketElement = new BasketElement();
     $basketElement->setProduct('product_code', $newProduct);
     $basket->addBasketElement($basketElement);
     $this->assertEquals(30, $basket->getTotal(false, false), '::getTotal() for non-recurrent payments only');
     $basket->removeElement($basketElement);
     // Delivery
     $delivery = new Delivery();
     $basket->setDeliveryMethod($delivery);
     $this->assertEquals(150, $basket->getTotal(false), '::getTotal() - return 150');
     $this->assertEquals(179.4, $basket->getTotal(true), '::getTotal() w/o vat return 179.40');
     $this->assertEquals(29.4, $basket->getVatAmount(), '::getVatAmount() w/o vat return 29.4');
 }
 public function testGettersSetters()
 {
     $currency = $this->getMock('Sonata\\Component\\Currency\\Currency');
     $productProvider = $this->getMock('Sonata\\Component\\Product\\ProductProviderInterface');
     $productManager = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface');
     $productDefinition = new ProductDefinition($productProvider, $productManager);
     $basketElement = new BasketElement();
     $basketElement->setProductDefinition($productDefinition);
     $this->assertEquals(0, $basketElement->getVatRate());
     $this->assertEquals(0, $basketElement->getUnitPrice($currency));
     $this->assertFalse($basketElement->isValid());
     $provider = $this->getMock('Sonata\\Component\\Product\\ProductProviderInterface');
     $manager = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface');
     $productDefinition = new ProductDefinition($provider, $manager);
     // Tests getProduct
     $this->assertNull($basketElement->getProduct());
     $basketElement->setProductDefinition($productDefinition);
     $this->assertNull($basketElement->getProduct());
     $product = $this->getMock('Sonata\\Component\\Product\\ProductInterface');
     $product->expects($this->any())->method('getId')->will($this->returnValue(42));
     $basketElement->setProduct('product_code', $product);
     $this->assertEquals($product, $basketElement->getProduct());
     // Tests setProductId
     $basketElement->setProductId(42);
     $this->assertEquals(42, $basketElement->getProductId());
     $basketElement->setProductId(24);
     $this->assertNull($basketElement->getProductId());
     $manager->expects($this->any())->method('findOneBy')->will($this->returnValue($product));
     $basketElement->setProductDefinition(new ProductDefinition($provider, $manager));
     $basketElement->setProductId(42);
     $basketElement->setProduct('product_code', $product);
     // Done by the provider hereby mocked, hence we do it manually
     $this->assertEquals($product->getId(), $basketElement->getProductId());
     // Options
     $options = array('option1' => 'value1', 'option2' => 'value2');
     $basketElement->setOptions($options);
     $this->assertNull($basketElement->getOption('unexisting_option'));
     $this->assertEquals(42, $basketElement->getOption('unexisting_option', 42));
     $this->assertEquals('value1', $basketElement->getOption('option1'));
     $this->assertEquals($options, $basketElement->getOptions());
     $basketElement->setOption('option3', 'value3');
     $this->assertEquals('value3', $basketElement->getOption('option3'));
     // Other getters & setters
     $this->assertEquals($provider, $basketElement->getProductProvider());
     $this->assertEquals($manager, $basketElement->getProductManager());
     $this->assertEquals('product_code', $basketElement->getProductCode());
     $basketElement->setDelete(false);
     $this->assertFalse($basketElement->getDelete());
 }
 public function testOrderElement()
 {
     $product = $this->getMock('Sonata\\Component\\Product\\ProductInterface');
     $product->expects($this->any())->method('getId')->will($this->returnValue(42));
     $product->expects($this->any())->method('getName')->will($this->returnValue('Product name'));
     $product->expects($this->any())->method('getPrice')->will($this->returnValue(9.99));
     $product->expects($this->any())->method('getOptions')->will($this->returnValue(array('foo' => 'bar')));
     $product->expects($this->any())->method('getDescription')->will($this->returnValue('product description'));
     $productProvider = new ProductProviderTest($this->getMock('JMS\\Serializer\\SerializerInterface'));
     $productProvider->setCurrencyPriceCalculator(new CurrencyPriceCalculator());
     $productManager = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface');
     $productDefinition = new ProductDefinition($productProvider, $productManager);
     $basketElement = new BasketElement();
     $basketElement->setProduct('product_code', $product);
     $basketElement->setProductDefinition($productDefinition);
     $provider = $this->getBaseProvider();
     $orderElement = $provider->createOrderElement($basketElement);
     $this->assertInstanceOf('Sonata\\Component\\Order\\OrderElementInterface', $orderElement);
     $this->assertEquals(OrderInterface::STATUS_PENDING, $orderElement->getStatus());
     $this->assertEquals('Product name', $orderElement->getDesignation());
     $this->assertEquals(1, $orderElement->getQuantity());
 }