/** * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testCreateAccountWithPasswordHashWithAddressException() { $websiteId = 1; $storeId = null; $defaultStoreId = 1; $customerId = 1; $customerEmail = '*****@*****.**'; $hash = '4nj54lkj5jfi03j49f8bgujfgsd'; $address = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $address->expects($this->once())->method('setCustomerId')->with($customerId); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $store->expects($this->once())->method('getId')->willReturn($defaultStoreId); $website = $this->getMockBuilder('Magento\\Store\\Model\\Website')->disableOriginalConstructor()->getMock(); $website->expects($this->once())->method('getStoreIds')->willReturn([1, 2, 3]); $website->expects($this->once())->method('getDefaultStore')->willReturn($store); $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customer->expects($this->atLeastOnce())->method('getId')->willReturn($customerId); $customer->expects($this->once())->method('getEmail')->willReturn($customerEmail); $customer->expects($this->atLeastOnce())->method('getWebsiteId')->willReturn($websiteId); $customer->expects($this->atLeastOnce())->method('getStoreId')->willReturn($storeId); $customer->expects($this->once())->method('setStoreId')->with($defaultStoreId); $customer->expects($this->once())->method('getAddresses')->willReturn([$address]); $customer->expects($this->once())->method('setAddresses')->with(null); $this->customerRepository->expects($this->once())->method('get')->with($customerEmail)->willReturn($customer); $this->share->expects($this->once())->method('isWebsiteScope')->willReturn(true); $this->storeManager->expects($this->atLeastOnce())->method('getWebsite')->with($websiteId)->willReturn($website); $this->customerRepository->expects($this->once())->method('save')->with($customer, $hash)->willReturn($customer); $exception = new \Magento\Framework\Exception\InputException(new \Magento\Framework\Phrase('Exception message')); $this->addressRepository->expects($this->atLeastOnce())->method('save')->with($address)->willThrowException($exception); $this->customerRepository->expects($this->once())->method('delete')->with($customer); $this->accountManagement->createAccountWithPasswordHash($customer, $hash); }
public function testCreateAccountWithPasswordHashWithCustomerAddresses() { $websiteId = 1; $addressId = 2; $customerId = null; $storeId = 1; $hash = '4nj54lkj5jfi03j49f8bgujfgsd'; //Handle store $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $store->expects($this->any())->method('getWebsiteId')->willReturn($websiteId); //Handle address - existing and non-existing. Non-Existing should return null when call getId method $existingAddress = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $nonExistingAddress = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); //Ensure that existing address is not in use $this->addressRepository->expects($this->atLeastOnce())->method("save")->withConsecutive(array($this->logicalNot($this->identicalTo($existingAddress))), array($this->identicalTo($nonExistingAddress))); $existingAddress->expects($this->any())->method("getId")->willReturn($addressId); //Expects that id for existing address should be unset $existingAddress->expects($this->once())->method("setId")->with(null); //Handle Customer calls $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customer->expects($this->atLeastOnce())->method('getWebsiteId')->willReturn($websiteId); $customer->expects($this->atLeastOnce())->method('getStoreId')->willReturn($storeId); $customer->expects($this->any())->method("getId")->willReturn($customerId); //Return Customer from customer repositoryå $this->customerRepository->expects($this->atLeastOnce())->method('save')->willReturn($customer); $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($customer); $customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])->disableOriginalConstructor()->getMock(); $customerSecure->expects($this->once())->method('setRpToken')->with($hash); $customerSecure->expects($this->any())->method('getPasswordHash')->willReturn($hash); $this->customerRegistry->expects($this->any())->method('retrieveSecureData')->with($customerId)->willReturn($customerSecure); $this->random->expects($this->once())->method('getUniqueHash')->willReturn($hash); $customer->expects($this->atLeastOnce())->method('getAddresses')->willReturn([$existingAddress, $nonExistingAddress]); $this->storeManager->expects($this->atLeastOnce())->method('getStore')->willReturn($store); $this->assertSame($customer, $this->accountManagement->createAccountWithPasswordHash($customer, $hash)); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testCreateAccountWithPasswordHashWithoutException() { $websiteId = 1; $storeId = null; $defaultStoreId = 1; $customerId = 1; $customerEmail = '*****@*****.**'; $hash = '4nj54lkj5jfi03j49f8bgujfgsd'; $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu'; $templateIdentifier = 'Template Identifier'; $sender = 'Sender'; $address = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock(); $address->expects($this->once())->method('setCustomerId')->with($customerId); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $store->expects($this->once())->method('getId')->willReturn($defaultStoreId); $website = $this->getMockBuilder('Magento\\Store\\Model\\Website')->disableOriginalConstructor()->getMock(); $website->expects($this->atLeastOnce())->method('getStoreIds')->willReturn([1, 2, 3]); $website->expects($this->once())->method('getDefaultStore')->willReturn($store); $customer = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customer->expects($this->atLeastOnce())->method('getId')->willReturn($customerId); $customer->expects($this->atLeastOnce())->method('getEmail')->willReturn($customerEmail); $customer->expects($this->atLeastOnce())->method('getWebsiteId')->willReturn($websiteId); $customer->expects($this->atLeastOnce())->method('getStoreId')->willReturn($storeId); $customer->expects($this->once())->method('setStoreId')->with($defaultStoreId); $customer->expects($this->once())->method('getAddresses')->willReturn([$address]); $customer->expects($this->once())->method('setAddresses')->with(null); $this->customerRepository->expects($this->once())->method('get')->with($customerEmail)->willReturn($customer); $this->share->expects($this->once())->method('isWebsiteScope')->willReturn(true); $this->storeManager->expects($this->atLeastOnce())->method('getWebsite')->with($websiteId)->willReturn($website); $this->customerRepository->expects($this->atLeastOnce())->method('save')->willReturn($customer); $this->addressRepository->expects($this->atLeastOnce())->method('save')->with($address); $this->customerRepository->expects($this->once())->method('getById')->with($customerId)->willReturn($customer); $this->random->expects($this->once())->method('getUniqueHash')->willReturn($newLinkToken); $customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->disableOriginalConstructor()->getMock(); $customerSecure->expects($this->any())->method('setRpToken')->with($newLinkToken); $customerSecure->expects($this->any())->method('setRpTokenCreatedAt'); $this->customerRegistry->expects($this->atLeastOnce())->method('retrieveSecureData')->willReturn($customerSecure); $this->dataObjectProcessor->expects($this->once())->method('buildOutputDataArray')->willReturn([]); $this->scopeConfig->expects($this->at(1))->method('getValue')->willReturn($templateIdentifier); $this->scopeConfig->expects($this->at(2))->method('getValue')->willReturn($sender); $transport = $this->getMockBuilder('Magento\\Framework\\Mail\\TransportInterface')->getMock(); $this->transportBuilder->expects($this->once())->method('setTemplateIdentifier')->with($templateIdentifier)->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setFrom')->with($sender)->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport); $transport->expects($this->once())->method('sendMessage'); $this->accountManagement->createAccountWithPasswordHash($customer, $hash); }
/** * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testCreateAccountWithPasswordHashWithNewCustomerAndLocalizedException() { $storeId = 1; $storeName = 'store_name'; $hash = '4nj54lkj5jfi03j49f8bgujfgsd'; $customerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMockForAbstractClass(); $customerMock->expects($this->atLeastOnce())->method('getId')->willReturn(null); $customerMock->expects($this->atLeastOnce())->method('getStoreId')->willReturn($storeId); $customerMock->expects($this->once())->method('setCreatedIn')->with($storeName)->willReturnSelf(); $customerMock->expects($this->once())->method('getAddresses')->willReturn([]); $customerMock->expects($this->once())->method('setAddresses')->with(null)->willReturnSelf(); $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $storeMock->expects($this->once())->method('getName')->willReturn($storeName); $this->storeManager->expects($this->once())->method('getStore')->with($storeId)->willReturn($storeMock); $exception = new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Exception message')); $this->customerRepository->expects($this->once())->method('save')->with($customerMock, $hash)->willThrowException($exception); $this->accountManagement->createAccountWithPasswordHash($customerMock, $hash); }
/** * {@inheritdoc} */ public function createAccountWithPasswordHash(\Magento\Customer\Api\Data\CustomerInterface $customer, $hash, $redirectUrl = '') { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'createAccountWithPasswordHash'); if (!$pluginInfo) { return parent::createAccountWithPasswordHash($customer, $hash, $redirectUrl); } else { return $this->___callPlugins('createAccountWithPasswordHash', func_get_args(), $pluginInfo); } }