/**
  * @param bool $isWebsiteScope
  * @param array $websites
  * @param int $quoteId
  * @dataProvider dispatchDataProvider
  */
 public function testDispatch($isWebsiteScope, $websites, $quoteId)
 {
     $this->configMock->expects($this->once())->method('isWebsiteScope')->will($this->returnValue($isWebsiteScope));
     $customerDataObjectMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock();
     $customerDataObjectMock->expects($this->any())->method('getGroupId')->will($this->returnValue(1));
     $customerDataObjectMock->expects($this->any())->method('getWebsiteId')->will($this->returnValue(2));
     if ($isWebsiteScope) {
         $websites = $websites[0];
         $this->storeManagerMock->expects($this->once())->method('getWebsite')->with(2)->will($this->returnValue($websites));
     } else {
         $this->storeManagerMock->expects($this->once())->method('getWebsites')->will($this->returnValue($websites));
     }
     $origCustomerDataObjectMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock();
     $origCustomerDataObjectMock->expects($this->any())->method('getGroupId')->will($this->returnValue(2));
     $this->eventMock->expects($this->any())->method('getCustomerDataObject')->will($this->returnValue($customerDataObjectMock));
     $this->eventMock->expects($this->any())->method('getOrigCustomerDataObject')->will($this->returnValue($origCustomerDataObjectMock));
     /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\Quote $quoteMock */
     $quoteMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->setMethods(['setWebsite', 'setCustomerGroupId', 'collectTotals', '__wakeup'])->disableOriginalConstructor()->getMock();
     $websiteCount = count($websites);
     if ($quoteId) {
         $this->quoteRepositoryMock->expects($this->exactly($websiteCount))->method('getForCustomer')->will($this->returnValue($quoteMock));
         $quoteMock->expects($this->exactly($websiteCount))->method('setWebsite');
         $quoteMock->expects($this->exactly($websiteCount))->method('setCustomerGroupId');
         $quoteMock->expects($this->exactly($websiteCount))->method('collectTotals');
         $this->quoteRepositoryMock->expects($this->exactly($websiteCount))->method('save')->with($quoteMock);
     } else {
         $this->quoteRepositoryMock->expects($this->exactly($websiteCount))->method('getForCustomer')->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
         $quoteMock->expects($this->never())->method('setCustomerGroupId');
         $quoteMock->expects($this->never())->method('collectTotals');
         $this->quoteRepositoryMock->expects($this->never())->method('save');
     }
     $this->customerQuote->dispatch($this->observerMock);
 }
예제 #2
0
 /**
  * @param bool $isWebsiteScope
  * @param array $websites
  * @param int $quoteId
  * @dataProvider dispatchDataProvider
  */
 public function testDispatch($isWebsiteScope, $websites, $quoteId)
 {
     $this->configMock->expects($this->once())->method('isWebsiteScope')->will($this->returnValue($isWebsiteScope));
     $customerDataObjectMock = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\Data\\Customer')->disableOriginalConstructor()->getMock();
     $customerDataObjectMock->expects($this->any())->method('getGroupId')->will($this->returnValue(1));
     $customerDataObjectMock->expects($this->any())->method('getWebsiteId')->will($this->returnValue(2));
     if ($isWebsiteScope) {
         $websites = $websites[0];
         $this->storeManagerMock->expects($this->once())->method('getWebsite')->with(2)->will($this->returnValue($websites));
     } else {
         $this->storeManagerMock->expects($this->once())->method('getWebsites')->will($this->returnValue($websites));
     }
     $origCustomerDataObjectMock = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\Data\\Customer')->disableOriginalConstructor()->getMock();
     $origCustomerDataObjectMock->expects($this->any())->method('getGroupId')->will($this->returnValue(2));
     $this->eventMock->expects($this->any())->method('getCustomerDataObject')->will($this->returnValue($customerDataObjectMock));
     $this->eventMock->expects($this->any())->method('getOrigCustomerDataObject')->will($this->returnValue($origCustomerDataObjectMock));
     /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Sales\Model\Quote $quoteMock */
     $quoteMock = $this->getMockBuilder('Magento\\Sales\\Model\\Quote')->setMethods(array('setWebsite', 'loadByCustomer', 'getId', 'setCustomerGroupId', 'collectTotals', 'save', '__wakeup'))->disableOriginalConstructor()->getMock();
     $websiteCount = count($websites);
     $this->quoteFactoryMock->expects($this->exactly($websiteCount))->method('create')->will($this->returnValue($quoteMock));
     $quoteMock->expects($this->exactly($websiteCount))->method('setWebsite');
     $quoteMock->expects($this->exactly($websiteCount))->method('loadByCustomer');
     $quoteMock->expects($this->exactly($websiteCount))->method('getId')->will($this->returnValue($quoteId));
     if ($quoteId) {
         $quoteMock->expects($this->exactly($websiteCount))->method('setCustomerGroupId');
         $quoteMock->expects($this->exactly($websiteCount))->method('collectTotals');
         $quoteMock->expects($this->exactly($websiteCount))->method('save');
     } else {
         $quoteMock->expects($this->never())->method('setCustomerGroupId');
         $quoteMock->expects($this->never())->method('collectTotals');
         $quoteMock->expects($this->never())->method('save');
     }
     $this->customerQuote->dispatch($this->observerMock);
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testCreateAccountWithPassword()
 {
     $websiteId = 1;
     $storeId = null;
     $defaultStoreId = 1;
     $customerId = 1;
     $customerEmail = '*****@*****.**';
     $password = '******';
     $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
     $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
     $templateIdentifier = 'Template Identifier';
     $sender = 'Sender';
     $this->string->expects($this->any())->method('strlen')->willReturnCallback(function ($string) {
         return strlen($string);
     });
     $this->encryptor->expects($this->once())->method('getHash')->with($password, true)->willReturn($hash);
     $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')->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])->disableOriginalConstructor()->getMock();
     $customerSecure->expects($this->any())->method('setRpToken')->with($newLinkToken);
     $customerSecure->expects($this->any())->method('setRpTokenCreatedAt');
     $customerSecure->expects($this->any())->method('getPasswordHash')->willReturn($hash);
     $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')->with(AccountManagement::XML_PATH_REGISTER_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $defaultStoreId)->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->createAccount($customer, $password);
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testCreateAccountWithPassword()
 {
     $websiteId = 1;
     $storeId = null;
     $defaultStoreId = 1;
     $customerId = 1;
     $customerEmail = '*****@*****.**';
     $hash = '4nj54lkj5jfi03j49f8bgujfgsd';
     $newLinkToken = '2jh43j5h2345jh23lh452h345hfuzasd96ofu';
     $templateIdentifier = 'Template Identifier';
     $sender = 'Sender';
     $password = '******';
     $minPasswordLength = 5;
     $minCharacterSetsNum = 2;
     $this->scopeConfig->expects($this->any())->method('getValue')->willReturnMap([[AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH, 'default', null, $minPasswordLength], [AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER, 'default', null, $minCharacterSetsNum], [AccountManagement::XML_PATH_REGISTER_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $defaultStoreId, $templateIdentifier], [AccountManagement::XML_PATH_REGISTER_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, 1, $sender]]);
     $this->string->expects($this->any())->method('strlen')->with($password)->willReturn(iconv_strlen($password, 'UTF-8'));
     $this->encryptor->expects($this->once())->method('getHash')->with($password, true)->willReturn($hash);
     $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')->setMethods(['setRpToken', 'setRpTokenCreatedAt', 'getPasswordHash'])->disableOriginalConstructor()->getMock();
     $customerSecure->expects($this->any())->method('setRpToken')->with($newLinkToken);
     $customerSecure->expects($this->any())->method('setRpTokenCreatedAt');
     $customerSecure->expects($this->any())->method('getPasswordHash')->willReturn($hash);
     $this->customerRegistry->expects($this->atLeastOnce())->method('retrieveSecureData')->willReturn($customerSecure);
     $this->emailNotificationMock->expects($this->once())->method('newAccount')->willReturnSelf();
     $this->accountManagement->createAccount($customer, $password);
 }