示例#1
0
 public function testBuild()
 {
     $productProvider = $this->getMock('Sonata\\Component\\Product\\ProductProviderInterface');
     $productManager = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface');
     $definition = new ProductDefinition($productProvider, $productManager);
     $productPool = new ProductPool();
     $productPool->addProduct('test', $definition);
     $deliveryPool = new DeliveryPool();
     $paymentPool = new PaymentPool();
     $address = $this->getMock('Sonata\\Component\\Customer\\AddressInterface');
     $addressManager = $this->getMock('Sonata\\Component\\Customer\\AddressManagerInterface');
     $addressManager->expects($this->exactly(2))->method('findOneBy')->will($this->returnValue($address));
     $basketBuilder = new BasketBuilder($productPool, $addressManager, $deliveryPool, $paymentPool);
     $basketElement_1 = $this->getMock('Sonata\\Component\\Basket\\BasketElementInterface');
     $basketElement_1->expects($this->exactly(2))->method('getProductCode')->will($this->returnValue('test'));
     $basketElement_1->expects($this->once())->method('setProductDefinition');
     $basketElements = array($basketElement_1);
     $basket = $this->getMock('Sonata\\Component\\Basket\\BasketInterface');
     $basket->expects($this->once())->method('getBasketElements')->will($this->returnValue($basketElements));
     $basket->expects($this->once())->method('getDeliveryAddressId')->will($this->returnValue(1));
     $basket->expects($this->once())->method('getDeliveryMethodCode')->will($this->returnValue('ups'));
     $basket->expects($this->once())->method('getBillingAddressId')->will($this->returnValue(2));
     $basket->expects($this->once())->method('getPaymentMethodCode')->will($this->returnValue('credit_cart'));
     $basket->expects($this->once())->method('setDeliveryAddress');
     $basket->expects($this->once())->method('setDeliveryMethod');
     $basket->expects($this->once())->method('setBillingAddress');
     $basket->expects($this->once())->method('setPaymentMethod');
     $basket->expects($this->once())->method('buildPrices');
     $basketBuilder->build($basket);
 }
示例#2
0
 /**
  * Build a basket
  *
  * @param \Sonata\Component\Basket\BasketInterface $basket
  *
  * @throws \RuntimeException
  */
 public function build(BasketInterface $basket)
 {
     $basket->setProductPool($this->productPool);
     foreach ($basket->getBasketElements() as $basketElement) {
         if ($basketElement->getProduct() === null) {
             // restore information
             if ($basketElement->getProductCode() == null) {
                 throw new \RuntimeException('The product code is empty');
             }
             $productDefinition = $this->productPool->getProduct($basketElement->getProductCode());
             $basketElement->setProductDefinition($productDefinition);
         }
     }
     // load the delivery address
     $deliveryAddressId = $basket->getDeliveryAddressId();
     if ($deliveryAddressId) {
         $address = $this->addressManager->findOneBy(array('id' => $deliveryAddressId));
         $basket->setDeliveryAddress($address);
     }
     $deliveryMethodCode = $basket->getDeliveryMethodCode();
     if ($deliveryMethodCode) {
         $basket->setDeliveryMethod($this->deliveryPool->getMethod($deliveryMethodCode));
     }
     // load the payment address
     $billingAddressId = $basket->getBillingAddressId();
     if ($billingAddressId) {
         $address = $this->addressManager->findOneBy(array('id' => $billingAddressId));
         $basket->setBillingAddress($address);
     }
     // load the payment method
     $paymentMethodCode = $basket->getPaymentMethodCode();
     if ($paymentMethodCode) {
         $basket->setPaymentMethod($this->paymentPool->getMethod($paymentMethodCode));
     }
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $choices = $this->pool->getProvider($options['product'])->getVariationsChoices($options['product'], $options['fields']);
     foreach ($choices as $choiceTitle => $choiceValues) {
         $builder->add($choiceTitle, 'choice', array_merge(array('translation_domain' => 'SonataProductBundle'), $options['field_options'], array('label' => sprintf('form_%s', $choiceTitle), 'choices' => $choiceValues)));
     }
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     if ($options['provider_name']) {
         $provider = $this->productPool->getProvider($options['provider_name']);
         $provider->buildForm($builder, $options);
     }
 }
示例#5
0
 /**
  * The validator asks each product repository to validate the related basket element.
  *
  * @param BasketInterface $basket
  * @param Constraint      $constraint
  */
 public function validate($basket, Constraint $constraint)
 {
     foreach ($basket->getBasketElements() as $pos => $basketElement) {
         // create a new ErrorElement object
         $errorElement = new ErrorElement($basket, $this->constraintValidatorFactory, $this->context, $this->context->getGroup());
         $errorElement->with('basketElements[' . $pos . ']');
         // validate the basket element through the related service provider
         $this->productPool->getProvider($basketElement->getProductCode())->validateFormBasketElement($errorElement, $basketElement, $basket);
     }
     if (count($this->context->getViolations()) > 0) {
         $this->context->addViolationAt('basketElements', $constraint->message);
     }
 }
示例#6
0
 /**
  * Returns a "add to basket" form for a specified product.
  *
  * @param ProductInterface $product A product instance
  *
  * @return FormView
  */
 public function getFormAddBasket(ProductInterface $product)
 {
     $formBuilder = $this->formFactory->createNamedBuilder('add_basket', 'form', null, array('data_class' => $this->basketElementClass, 'csrf_protection' => false));
     $this->productPool->getProvider($product)->defineAddBasketForm($product, $formBuilder, false);
     $form = $formBuilder->getForm()->createView();
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function execute(BlockContextInterface $blockContext, Response $response = null)
 {
     $product = $blockContext->getSetting('product');
     if (null === $product) {
         return $this->renderResponse($blockContext->getTemplate(), array('context' => $blockContext, 'settings' => $blockContext->getSettings(), 'block' => $blockContext->getBlock(), 'choices' => array(), 'form' => null), $response);
     }
     $fields = $blockContext->getSetting('variations_properties');
     $choices = $this->pool->getProvider($product)->getVariationsChoices($product, $fields);
     $accessor = PropertyAccess::createPropertyAccessor();
     $currentValues = array();
     foreach ($choices as $field => $values) {
         $currentValues[$field] = array_search($accessor->getValue($product, $field), $values);
     }
     $form = $this->formFactory->createBuilder('sonata_product_variation_choices', $currentValues, array('field_options' => $blockContext->getSetting('form_field_options'), 'product' => $product, 'fields' => $fields))->getForm();
     $params = array('context' => $blockContext, 'settings' => $blockContext->getSettings(), 'block' => $blockContext->getBlock(), 'choices' => $choices, 'form' => $form->createView());
     return $this->renderResponse($blockContext->getTemplate(), $params, $response);
 }
 /**
  * Deletes a product
  *
  * @ApiDoc(
  *  requirements={
  *      {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="product identifier"}
  *  },
  *  statusCodes={
  *      200="Returned when post is successfully deleted",
  *      400="Returned when an error has occurred while product deletion",
  *      404="Returned when unable to find product"
  *  }
  * )
  *
  * @param integer $id A Product identifier
  *
  * @return \FOS\RestBundle\View\View
  *
  * @throws NotFoundHttpException
  */
 public function deleteProductAction($id)
 {
     $product = $this->getProduct($id);
     $manager = $this->productPool->getManager($product);
     try {
         $manager->delete($product);
     } catch (\Exception $e) {
         return \FOS\RestBundle\View\View::create(array('error' => $e->getMessage()), 400);
     }
     return array('deleted' => true);
 }
示例#9
0
 public function testPool()
 {
     $productProvider = $this->getMock('Sonata\\Component\\Product\\ProductProviderInterface');
     $productManager1 = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface');
     $productManager2 = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface');
     // we need products from different objects to test ProductPool
     $product1 = $this->getMock('Sonata\\Component\\Product\\ProductInterface');
     $product2 = new Product();
     $productManager1->expects($this->any())->method('getClass')->will($this->returnValue($product1));
     $productManager2->expects($this->any())->method('getClass')->will($this->returnValue($product2));
     $definition1 = new ProductDefinition($productProvider, $productManager1);
     $definition2 = new ProductDefinition($productProvider, $productManager2);
     $productPool = new Pool();
     $productPool->addProduct('product1', $definition1);
     $productPool->addProduct('product2', $definition2);
     $this->assertFalse($productPool->hasProvider('grou'));
     $this->assertTrue($productPool->hasProvider('product1'));
     $this->assertTrue($productPool->hasProvider('product2'));
     $this->assertEquals($productPool->getProduct('product1'), $definition1);
     $this->assertEquals($productPool->getProduct('product2'), $definition2);
     $this->assertEquals($productPool->getProductCode($product1), 'product1');
     $this->assertEquals($productPool->getProductCode($product2), 'product2');
 }
 /**
  * Write a product, this method is used by both POST and PUT action methods.
  *
  * @param string   $provider A product provider name
  * @param Request  $request  Symfony request
  * @param int|null $id       A product identifier
  *
  * @return \FOS\RestBundle\View\View|FormInterface
  */
 protected function handleWriteProduct($provider, $request, $id = null)
 {
     $product = $id ? $this->getProduct($id) : null;
     try {
         $manager = $this->productPool->getManager($provider);
     } catch (\RuntimeException $e) {
         throw new NotFoundHttpException($e->getMessage(), $e);
     }
     $form = $this->formFactory->createNamed(null, 'sonata_product_api_form_product', $product, array('csrf_protection' => false, 'data_class' => $manager->getClass(), 'provider_name' => $provider));
     $form->bind($request);
     if ($form->isValid()) {
         $product = $form->getData();
         $product->setDescription($this->formatterPool->transform($product->getDescriptionFormatter(), $product->getRawDescription()));
         $product->setShortDescription($this->formatterPool->transform($product->getShortDescriptionFormatter(), $product->getRawShortDescription()));
         $manager->save($product);
         $view = \FOS\RestBundle\View\View::create($product);
         $serializationContext = SerializationContext::create();
         $serializationContext->setGroups(array('sonata_api_read'));
         $serializationContext->enableMaxDepthChecks();
         $view->setSerializationContext($serializationContext);
         return $view;
     }
     return $form;
 }
 /**
  * Get the Manager of the given Product.
  *
  * @param string $productType
  *
  * @return ManagerInterface
  */
 protected function getProductManager($productType)
 {
     return $this->productPool->getManager($productType);
 }
 /**
  * {@inheritdoc}
  */
 public function delete($object)
 {
     $this->pool->getManager($object)->delete($object);
 }
示例#13
0
 protected function getPreparedBasket()
 {
     $basket = new Basket();
     // create the provider mock
     $provider = $this->getMock('Sonata\\Component\\Product\\ProductProviderInterface');
     $provider->expects($this->any())->method('basketCalculatePrice')->will($this->returnValue(15));
     $provider->expects($this->any())->method('isAddableToBasket')->will($this->returnValue(true));
     // create the product manager mock
     $manager = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface');
     $manager->expects($this->any())->method('getClass')->will($this->returnValue('BasketTest_Product'));
     $definition = new ProductDefinition($provider, $manager);
     $pool = new Pool();
     $pool->addProduct('product_code', $definition);
     $basket->setProductPool($pool);
     return $basket;
 }
示例#14
0
 /**
  * {@inheritdoc}
  */
 public function configureFormFields(FormMapper $formMapper)
 {
     $formMapper->with($this->trans('order_element.form.group_main_label', array(), 'SonataOrderBundle'))->add('productType', 'choice', array('choices' => array_keys($this->productPool->getProducts())))->add('quantity')->add('price')->add('vatRate')->add('designation')->add('description', null, array('required' => false))->add('status', 'sonata_order_status', array('translation_domain' => 'SonataOrderBundle'))->add('deliveryStatus', 'sonata_product_delivery_status', array('translation_domain' => 'SonataDeliveryBundle'))->end();
 }
示例#15
0
 /**
  * Return the available stock of the product.
  *
  * @param ProductInterface $product  A product instance
  *
  * @return int
  */
 public function getProductStock(ProductInterface $product)
 {
     return $this->productPool->getProvider($product)->getStockAvailable($product);
 }