/** * @dataProvider postSubmitDataProvider */ public function testPostSubmit(array $allAddresses, $formAddressKey, array $expectedAddressesData) { // Set owner for all addresses $customer = new Customer(); foreach ($allAddresses as $address) { $customer->addAddress($address); } $event = $this->getMockBuilder('Symfony\\Component\\Form\\FormEvent')->setMethods(['getData'])->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getData')->will($this->returnValue($allAddresses[$formAddressKey])); $this->subscriber->postSubmit($event); foreach ($expectedAddressesData as $addressKey => $expectedData) { /** @var CustomerAddress $address */ $address = $allAddresses[$addressKey]; $defaultTypeNames = []; /** @var AddressType $defaultType */ foreach ($address->getDefaults() as $defaultType) { $defaultTypeNames[] = $defaultType->getName(); } $this->assertEquals($expectedData['defaults'], $defaultTypeNames); } }
/** * @param Customer $customer * @param CustomerAddress $address * @return array * @throws BadRequestHttpException */ protected function update(Customer $customer, CustomerAddress $address) { $responseData = ['saved' => false, 'customer' => $customer]; if ($this->getRequest()->getMethod() == 'GET' && !$address->getId()) { if (!$customer->getAddresses()->count()) { $address->setPrimary(true); } } if ($address->getOwner() && $address->getOwner()->getId() != $customer->getId()) { throw new BadRequestHttpException('Address must belong to customer'); } elseif (!$address->getOwner()) { $customer->addAddress($address); } $form = $this->createForm(CustomerTypedAddressType::NAME, $address); $handler = new AddressHandler($form, $this->getRequest(), $this->getDoctrine()->getManagerForClass('OroB2BCustomerBundle:CustomerAddress')); if ($handler->process($address)) { $this->getDoctrine()->getManager()->flush(); $responseData['entity'] = $address; $responseData['saved'] = true; } $responseData['form'] = $form->createView(); return $responseData; }
/** * @return array */ public function submitWithFormSubscribersProvider() { $customerAddress1 = new CustomerAddress(); $customerAddress1->setTypes(new ArrayCollection([$this->billingType, $this->shippingType])); $customerAddress2 = new CustomerAddress(); $customerAddress2->setTypes(new ArrayCollection([$this->billingType, $this->shippingType]))->setDefaults(new ArrayCollection([$this->billingType, $this->shippingType])); $customerAddressExpected = new CustomerAddress(); $customerAddressExpected->setPrimary(true)->addType($this->billingType)->addType($this->shippingType)->removeType($this->billingType)->removeType($this->shippingType)->addType($this->billingType)->addType($this->shippingType)->setDefaults(new ArrayCollection([$this->billingType, $this->shippingType])); $customer = new Customer(); $customer->addAddress($customerAddress1); $customer->addAddress($customerAddress2); return ['FixCustomerAddressesDefaultSubscriber check' => ['options' => [], 'defaultData' => $customerAddress1, 'viewData' => $customerAddress1, 'submittedData' => ['types' => [AddressType::TYPE_BILLING, AddressType::TYPE_SHIPPING], 'defaults' => ['default' => [AddressType::TYPE_BILLING, AddressType::TYPE_SHIPPING]], 'primary' => true], 'expectedData' => $customerAddressExpected, 'otherAddresses' => [$customerAddress2], 'updateOwner' => $customer]]; }