Beispiel #1
0
 public function testGeneralSave()
 {
     $customerId = 1;
     $currentPassword = '******';
     $customerEmail = '*****@*****.**';
     $address = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->getMockForAbstractClass();
     $currentCustomerMock = $this->getCurrentCustomerMock($customerId, $address);
     $newCustomerMock = $this->getNewCustomerMock($customerId, $address);
     $currentCustomerMock->expects($this->any())->method('getEmail')->willReturn($customerEmail);
     $this->customerSession->expects($this->once())->method('getCustomerId')->willReturn($customerId);
     $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($currentCustomerMock);
     $this->validator->expects($this->once())->method('validate')->with($this->request)->willReturn(true);
     $this->request->expects($this->once())->method('isPost')->willReturn(true);
     $this->request->expects($this->exactly(3))->method('getParam')->withConsecutive(['change_email'], ['change_email'], ['change_password'])->willReturnOnConsecutiveCalls(true, true, false);
     $this->request->expects($this->once())->method('getPost')->with('current_password')->willReturn($currentPassword);
     $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($currentCustomerMock);
     $this->customerRepository->expects($this->once())->method('save')->with($newCustomerMock)->willReturnSelf();
     $this->customerExtractor->expects($this->once())->method('extract')->with('customer_account_edit', $this->request)->willReturn($newCustomerMock);
     $this->emailNotification->expects($this->once())->method('credentialsChanged')->with($currentCustomerMock, $customerEmail, false)->willReturnSelf();
     $newCustomerMock->expects($this->once())->method('getEmail')->willReturn($customerEmail);
     $this->eventManager->expects($this->once())->method('dispatch')->with('customer_account_edited', ['email' => $customerEmail]);
     $this->messageManager->expects($this->once())->method('addSuccess')->with(__('You saved the account information.'))->willReturnSelf();
     $this->resultRedirect->expects($this->once())->method('setPath')->with('customer/account')->willReturnSelf();
     $this->authenticationMock->expects($this->once())->method('authenticate')->willReturn(true);
     $this->assertSame($this->resultRedirect, $this->model->execute());
 }
 public function testExecuteWithUpdateBilling()
 {
     $this->prepareMocksForTesting(1);
     $this->customerData->expects($this->once())->method('getDefaultBilling')->willReturn(23);
     $this->prepareMocksForUpdateDefaultBilling();
     $this->customerRepository->expects($this->once())->method('save')->with($this->customerData);
     $this->emailNotification->expects($this->once())->method('credentialsChanged')->willReturnSelf();
     $this->prepareMocksForErrorMessagesProcessing();
     $this->assertSame($this->resultJson, $this->controller->execute());
 }
Beispiel #3
0
 /**
  * @covers \Magento\Customer\Controller\Adminhtml\Index\Index::execute
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testExecuteWithExistentCustomer()
 {
     $customerId = 22;
     $addressId = 11;
     $subscription = 'true';
     $postValue = ['customer' => ['entity_id' => $customerId, 'code' => 'value', 'coolness' => false, 'disable_auto_group_change' => 'false'], 'address' => ['_template_' => '_template_', $addressId => ['entity_id' => $addressId, 'default_billing' => 'true', 'default_shipping' => 'true', 'code' => 'value', 'coolness' => false, 'region' => 'region', 'region_id' => 'region_id']], 'subscription' => $subscription];
     $filteredData = ['entity_id' => $customerId, 'code' => 'value', 'coolness' => false, 'disable_auto_group_change' => 'false'];
     $addressFilteredData = ['entity_id' => $addressId, 'default_billing' => 'true', 'default_shipping' => 'true', 'code' => 'value', 'coolness' => false, 'region' => 'region', 'region_id' => 'region_id'];
     $savedData = ['entity_id' => $customerId, 'darkness' => true, 'name' => 'Name', \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_BILLING => false, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_SHIPPING => false];
     $mergedData = ['entity_id' => $customerId, 'darkness' => true, 'name' => 'Name', 'code' => 'value', 'disable_auto_group_change' => 0, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_BILLING => $addressId, \Magento\Customer\Api\Data\CustomerInterface::DEFAULT_SHIPPING => $addressId, 'confirmation' => false, 'sendemail_store_id' => '1', 'id' => $customerId];
     $mergedAddressData = ['entity_id' => $addressId, 'default_billing' => true, 'default_shipping' => true, 'code' => 'value', 'region' => ['region' => 'region', 'region_id' => 'region_id'], 'region_id' => 'region_id', 'id' => $addressId];
     /** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $formMock */
     $attributeMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface')->disableOriginalConstructor()->getMock();
     $attributeMock->expects($this->exactly(2))->method('getAttributeCode')->willReturn('coolness');
     $attributeMock->expects($this->exactly(2))->method('getFrontendInput')->willReturn('int');
     $attributes = [$attributeMock];
     $this->requestMock->expects($this->exactly(3))->method('getPostValue')->willReturn($postValue);
     $this->requestMock->expects($this->exactly(3))->method('getPost')->willReturnMap([['customer', null, $postValue['customer']], ['address', null, $postValue['address']], ['subscription', null, $subscription]]);
     /** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject $formMock */
     $formMock = $this->getMockBuilder('Magento\\Customer\\Model\\Metadata\\Form')->disableOriginalConstructor()->getMock();
     $this->formFactoryMock->expects($this->exactly(2))->method('create')->willReturnMap([[\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'adminhtml_customer', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE, [], $formMock], [\Magento\Customer\Api\AddressMetadataInterface::ENTITY_TYPE_ADDRESS, 'adminhtml_customer_address', [], false, \Magento\Customer\Model\Metadata\Form::DONT_IGNORE_INVISIBLE, [], $formMock]]);
     $formMock->expects($this->exactly(2))->method('extractData')->willReturnMap([[$this->requestMock, 'customer', true, $filteredData], [$this->requestMock, 'address/' . $addressId, true, $addressFilteredData]]);
     /** @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject $objectMock */
     $objectMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock();
     $this->objectFactoryMock->expects($this->exactly(2))->method('create')->with(['data' => $postValue])->willReturn($objectMock);
     $objectMock->expects($this->exactly(2))->method('getData')->willReturnMap([['customer', null, $postValue['customer']], ['address/' . $addressId, null, $postValue['address'][$addressId]]]);
     $formMock->expects($this->exactly(2))->method('getAttributes')->willReturn($attributes);
     /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
     $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock();
     $this->customerDataFactoryMock->expects($this->once())->method('create')->willReturn($customerMock);
     $this->customerRepositoryMock->expects($this->once())->method('getById')->with($customerId)->willReturn($customerMock);
     $this->customerMapperMock->expects($this->once())->method('toFlatArray')->with($customerMock)->willReturn($savedData);
     $addressMock = $this->getMockBuilder('\\Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock();
     $this->addressDataFactoryMock->expects($this->once())->method('create')->willReturn($addressMock);
     $this->dataHelperMock->expects($this->exactly(2))->method('populateWithArray')->willReturnMap([[$customerMock, $mergedData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface', $this->dataHelperMock], [$addressMock, $mergedAddressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface', $this->dataHelperMock]]);
     $customerMock->expects($this->once())->method('setAddresses')->with([$addressMock])->willReturnSelf();
     $this->customerRepositoryMock->expects($this->once())->method('save')->with($customerMock)->willReturnSelf();
     $customerEmail = '*****@*****.**';
     $customerMock->expects($this->once())->method('getEmail')->willReturn($customerEmail);
     $this->emailNotificationMock->expects($this->once())->method('credentialsChanged')->with($customerMock, $customerEmail)->willReturnSelf();
     $this->authorizationMock->expects($this->once())->method('isAllowed')->with(null)->willReturn(true);
     /** @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject $subscriberMock */
     $subscriberMock = $this->getMockBuilder('Magento\\Newsletter\\Model\\Subscriber')->disableOriginalConstructor()->getMock();
     $this->subscriberFactoryMock->expects($this->once())->method('create')->with()->willReturn($subscriberMock);
     $subscriberMock->expects($this->once())->method('subscribeCustomerById')->with($customerId);
     $subscriberMock->expects($this->never())->method('unsubscribeCustomerById');
     $this->sessionMock->expects($this->once())->method('unsCustomerFormData');
     $this->registryMock->expects($this->once())->method('register')->with(\Magento\Customer\Controller\RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
     $this->messageManagerMock->expects($this->once())->method('addSuccess')->with(__('You saved the customer.'))->willReturnSelf();
     $this->requestMock->expects($this->once())->method('getParam')->with('back', false)->willReturn(true);
     /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
     $redirectMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Redirect')->disableOriginalConstructor()->getMock();
     $this->redirectFactoryMock->expects($this->once())->method('create')->with([])->willReturn($redirectMock);
     $redirectMock->expects($this->once())->method('setPath')->with('customer/*/edit', ['id' => $customerId, '_current' => true])->willReturn(true);
     $this->assertEquals($redirectMock, $this->model->execute());
 }