/**
  * Add Product to Shopping List (product view form)
  *
  * @Route(
  *      "/add-product-from-view/{productId}",
  *      name="orob2b_shopping_list_frontend_add_product",
  *      requirements={"productId"="\d+"}
  * )
  * @AclAncestor("orob2b_shopping_list_line_item_frontend_add")
  * @ParamConverter("product", class="OroB2BProductBundle:Product", options={"id" = "productId"})
  *
  * @param Request $request
  * @param Product $product
  *
  * @return JsonResponse
  */
 public function addProductFromViewAction(Request $request, Product $product)
 {
     $shoppingListManager = $this->get('orob2b_shopping_list.shopping_list.manager');
     $shoppingList = $shoppingListManager->getForCurrentUser($request->get('shoppingListId'));
     $lineItem = (new LineItem())->setProduct($product)->setShoppingList($shoppingList)->setAccountUser($shoppingList->getAccountUser())->setOrganization($shoppingList->getOrganization());
     $form = $this->createForm(FrontendLineItemType::NAME, $lineItem);
     $handler = new LineItemHandler($form, $request, $this->getDoctrine(), $shoppingListManager);
     $isFormHandled = $handler->process($lineItem);
     if (!$isFormHandled) {
         return new JsonResponse(['successful' => false, 'message' => (string) $form->getErrors(true, false)]);
     }
     $link = $this->get('router')->generate('orob2b_shopping_list_frontend_view', ['id' => $shoppingList->getId()]);
     $translator = $this->get('translator');
     $message = $translator->trans('orob2b.shoppinglist.product.added.label');
     $linkTitle = $translator->trans('orob2b.shoppinglist.actions.view');
     $message = sprintf("%s (<a href='%s'>%s</a>).", $message, $link, $linkTitle);
     return new JsonResponse(['successful' => true, 'message' => $message]);
 }
 public function testProcessNotExistingLineItem()
 {
     $this->request = Request::create('/', 'PUT');
     $this->form->expects($this->once())->method('submit')->with($this->request);
     $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true));
     /** @var \PHPUnit_Framework_MockObject_MockObject|LineItemRepository $repository */
     $repository = $this->getMockBuilder('OroB2B\\Bundle\\ShoppingListBundle\\Entity\\Repository\\LineItemRepository')->disableOriginalConstructor()->getMock();
     $repository->expects($this->once())->method('findDuplicate')->with($this->lineItem)->will($this->returnValue(null));
     /** @var \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface $manager */
     $manager = $this->getMock('Doctrine\\ORM\\EntityManagerInterface');
     $manager->expects($this->once())->method('beginTransaction');
     $manager->expects($this->once())->method('commit');
     $manager->expects($this->never())->method('rollback');
     $manager->expects($this->once())->method('persist')->with($this->lineItem);
     $manager->expects($this->once())->method('flush');
     $manager->expects($this->once())->method('getRepository')->with(self::LINE_ITEM_SHORTCUT)->will($this->returnValue($repository));
     $this->registry->expects($this->once())->method('getManagerForClass')->with(self::LINE_ITEM_SHORTCUT)->will($this->returnValue($manager));
     $handler = new LineItemHandler($this->form, $this->request, $this->registry, $this->shoppingListManager);
     $this->assertTrue($handler->process($this->lineItem));
     $this->assertEquals([], $handler->updateSavedId([]));
 }
Ejemplo n.º 3
0
 public function testProcessNotExistingLineItem()
 {
     $this->request->expects($this->once())->method('getMethod')->will($this->returnValue('PUT'));
     $this->form->expects($this->once())->method('submit')->with($this->request);
     $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $repository = $this->getMockBuilder('OroB2B\\Bundle\\ShoppingListBundle\\Entity\\Repository\\LineItemRepository')->disableOriginalConstructor()->getMock();
     $repository->expects($this->once())->method('findDuplicate')->with($this->lineItem)->will($this->returnValue(null));
     $manager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager');
     $manager->expects($this->once())->method('getRepository')->with(self::LINE_ITEM_SHORTCUT)->will($this->returnValue($repository));
     $manager->expects($this->once())->method('persist')->with($this->lineItem);
     $manager->expects($this->once())->method('flush');
     $this->registry->expects($this->once())->method('getManagerForClass')->with(self::LINE_ITEM_SHORTCUT)->will($this->returnValue($manager));
     $handler = new LineItemHandler($this->form, $this->request, $this->registry);
     $this->assertTrue($handler->process($this->lineItem));
     $this->assertEquals([], $handler->updateSavedId([]));
 }