예제 #1
0
 /**
  * @param LineItem $lineItem
  *
  * @return bool
  */
 public function process(LineItem $lineItem)
 {
     if (in_array($this->request->getMethod(), ['POST', 'PUT'], true)) {
         /** @var EntityManagerInterface $manager */
         $manager = $this->registry->getManagerForClass('OroB2BShoppingListBundle:LineItem');
         $manager->beginTransaction();
         // handle case for new shopping list creation
         $formName = $this->form->getName();
         $formData = $this->request->request->get($formName, []);
         if (empty($formData['shoppingList']) && !empty($formData['shoppingListLabel'])) {
             $shoppingList = $this->shoppingListManager->createCurrent($formData['shoppingListLabel']);
             $formData['shoppingList'] = $shoppingList->getId();
             $this->request->request->set($formName, $formData);
         }
         $this->form->submit($this->request);
         if ($this->form->isValid()) {
             /** @var LineItemRepository $lineItemRepository */
             $lineItemRepository = $manager->getRepository('OroB2BShoppingListBundle:LineItem');
             $existingLineItem = $lineItemRepository->findDuplicate($lineItem);
             if ($existingLineItem) {
                 $this->updateExistingLineItem($lineItem, $existingLineItem);
             } else {
                 $manager->persist($lineItem);
             }
             $manager->flush();
             $manager->commit();
             return true;
         } else {
             $manager->rollBack();
         }
     }
     return false;
 }
 /**
  * @param ShoppingList $shoppingList
  *
  * @return bool
  */
 public function process(ShoppingList $shoppingList)
 {
     $this->form->setData($shoppingList);
     if (in_array($this->request->getMethod(), ['POST', 'PUT'], true)) {
         $this->form->submit($this->request);
         if ($this->form->isValid()) {
             if ($shoppingList->getId() === null) {
                 $this->manager->setCurrent($shoppingList->getAccountUser(), $shoppingList);
             } else {
                 $em = $this->doctrine->getManagerForClass('OroB2BShoppingListBundle:ShoppingList');
                 $em->persist($shoppingList);
                 $em->flush();
             }
             return true;
         }
     }
     return false;
 }
 public function testBulkAddLineItems()
 {
     $shoppingList = new ShoppingList();
     $lineItems = [];
     for ($i = 0; $i < 10; $i++) {
         $lineItems[] = new LineItem();
     }
     $this->manager->bulkAddLineItems($lineItems, $shoppingList, 10);
     $this->assertEquals(10, $shoppingList->getLineItems()->count());
 }
 public function testProcessExistingLineItemWithNewShoppingList()
 {
     $newShoppingListId = 12;
     $lineItem = $this->getLineItem(10, null, 123, null);
     $newShoppingList = $this->getShoppingList($newShoppingListId);
     $formData = [FrontendLineItemType::NAME => ['shoppingList' => '', 'shoppingListLabel' => 'New List']];
     $this->request = Request::create('/', 'POST', $formData);
     $this->addRegistryExpectations($lineItem);
     $this->form->expects($this->once())->method('submit')->with($this->request);
     $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true));
     $this->shoppingListManager->expects($this->once())->method('createCurrent')->with($formData[FrontendLineItemType::NAME]['shoppingListLabel'])->willReturn($newShoppingList);
     $handler = new LineItemHandler($this->form, $this->request, $this->registry, $this->shoppingListManager);
     $this->assertShoppingListId('', $this->request);
     $this->assertTrue($handler->process($this->lineItem));
     $this->assertShoppingListId($newShoppingListId, $this->request);
 }
 /**
  * @param array $productIds
  * @param array $productQuantities
  * @param array $expectedLineItems
  *
  * @dataProvider itemDataProvider
  */
 public function testCreateForShoppingList(array $productIds = [], array $productQuantities = [], array $expectedLineItems = [])
 {
     /** @var \PHPUnit_Framework_MockObject_MockObject|ShoppingList $shoppingList */
     $shoppingList = $this->getMock('OroB2B\\Bundle\\ShoppingListBundle\\Entity\\ShoppingList');
     $shoppingList->expects($this->any())->method('getId')->willReturn(1);
     $accountUser = new AccountUser();
     $shoppingList->expects($this->any())->method('getAccountUser')->willReturn($accountUser);
     $this->securityFacade->expects($this->any())->method('hasLoggedUser')->willReturn(true);
     $this->securityFacade->expects($this->any())->method('isGranted')->willReturn(true);
     $this->manager->expects($this->once())->method('bulkAddLineItems')->with($this->callback(function (array $lineItems) use($expectedLineItems, $accountUser) {
         /** @var LineItem $lineItem */
         foreach ($lineItems as $key => $lineItem) {
             /** @var LineItem $expectedLineItem */
             $expectedLineItem = $expectedLineItems[$key];
             $this->assertEquals($expectedLineItem->getQuantity(), $lineItem->getQuantity());
             $this->assertEquals($accountUser, $lineItem->getAccountUser());
             $this->assertInstanceOf('OroB2B\\Bundle\\ProductBundle\\Entity\\Product', $lineItem->getProduct());
             $this->assertInstanceOf('OroB2B\\Bundle\\ProductBundle\\Entity\\ProductUnit', $lineItem->getUnit());
         }
         return true;
     }), $shoppingList, $this->isType('integer'));
     $this->handler->createForShoppingList($shoppingList, $productIds, $productQuantities);
 }
 /**
  * @param mixed $shoppingListId
  * @return ShoppingList
  */
 public function getShoppingList($shoppingListId = null)
 {
     return $this->shoppingListManager->getForCurrentUser($shoppingListId);
 }