/** * @param $addressId * @param $isDefaultBilling * @param $isDefaultShipping * * @dataProvider getSaveDataProvider */ public function testSave($addressId, $isDefaultBilling, $isDefaultShipping) { /** @var $customer \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject */ $customer = $this->getMock('Magento\\Customer\\Model\\Customer', ['__wakeup', 'setDefaultBilling', 'setDefaultShipping', 'save', 'load'], [], '', false); $customer->expects($this->any())->method('load')->willReturnSelf(); $this->customerFactory->expects($this->any())->method('create')->willReturn($customer); /** @var $address \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject */ $address = $this->getMock('Magento\\Customer\\Model\\Address', ['__wakeup', 'getId', 'getEntityTypeId', 'getIsDefaultBilling', 'getIsDefaultShipping', 'hasDataChanges', 'validateBeforeSave', 'beforeSave', 'afterSave', 'isSaveAllowed'], [], '', false); $address->expects($this->once())->method('hasDataChanges')->willReturn(true); $address->expects($this->once())->method('isSaveAllowed')->willReturn(true); $address->expects($this->once())->method('validateBeforeSave'); $address->expects($this->once())->method('beforeSave'); $address->expects($this->once())->method('afterSave'); $address->expects($this->any())->method('getEntityTypeId')->willReturn('3'); $address->expects($this->any())->method('getId')->willReturn($addressId); $address->expects($this->any())->method('getIsDefaultShipping')->willReturn($isDefaultShipping); $address->expects($this->any())->method('getIsDefaultBilling')->willReturn($isDefaultBilling); if ($addressId && ($isDefaultBilling || $isDefaultShipping)) { if ($isDefaultBilling) { $customer->expects($this->once())->method('setDefaultBilling')->with($addressId); } if ($isDefaultShipping) { $customer->expects($this->once())->method('setDefaultShipping')->with($addressId); } $customer->expects($this->once())->method('save'); } else { $customer->expects($this->never())->method('setDefaultBilling'); $customer->expects($this->never())->method('setDefaultShipping'); $customer->expects($this->never())->method('save'); } $this->addressResource->setType('customer_address'); $this->addressResource->save($address); }
/** * SetUp */ protected function setUp() { $this->layoutMock = $this->getMock('Magento\\Framework\\View\\Layout', [], [], '', false); $this->sessionMock = $this->getMock('Magento\\Framework\\Session\\Generic', ['clearStorage', 'setData', 'getData'], [], '', false); $this->customerSessionMock = $this->getMock('Magento\\Customer\\Model\\Session', ['getCustomerGroupId', 'setCustomerGroupId', 'clearStorage', 'setCustomer'], [], '', false); $this->customerFactoryMock = $this->getMock('Magento\\Customer\\Model\\CustomerFactory', ['create'], [], '', false); $this->customerMock = $this->getMock('Magento\\Customer\\Model\\Customer', ['setGroupId', '__wakeup'], [], '', false); $this->visitorMock = $this->getMock('Magento\\Customer\\Model\\Visitor', [], [], '', false); $this->customerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->customerMock)); $this->depersonalizeCheckerMock = $this->getMock('Magento\\PageCache\\Model\\DepersonalizeChecker', [], [], '', false); $this->plugin = new \Magento\Customer\Model\Layout\DepersonalizePlugin($this->depersonalizeCheckerMock, $this->sessionMock, $this->customerSessionMock, $this->customerFactoryMock, $this->visitorMock); }
/** * SetUp */ public function setUp() { $this->layoutMock = $this->getMock('Magento\\Framework\\View\\Layout', [], [], '', false); $this->sessionMock = $this->getMock('Magento\\Framework\\Session\\Generic', ['clearStorage', 'setData', 'getData'], [], '', false); $this->customerSessionMock = $this->getMock('Magento\\Customer\\Model\\Session', ['getCustomerGroupId', 'setCustomerGroupId', 'clearStorage', 'setCustomer'], [], '', false); $this->customerFactoryMock = $this->getMock('Magento\\Customer\\Model\\CustomerFactory', ['create'], [], '', false); $this->requestMock = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false); $this->customerMock = $this->getMock('Magento\\Customer\\Model\\Customer', ['setGroupId', '__wakeup'], [], '', false); $this->moduleManagerMock = $this->getMock('Magento\\Framework\\Module\\Manager', [], [], '', false); $this->visitorMock = $this->getMock('Magento\\Customer\\Model\\Visitor', [], [], '', false); $this->customerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->customerMock)); $this->cacheConfigMock = $this->getMock('Magento\\PageCache\\Model\\Config', [], [], '', false); $this->plugin = new DepersonalizePlugin($this->sessionMock, $this->customerSessionMock, $this->customerFactoryMock, $this->requestMock, $this->moduleManagerMock, $this->visitorMock, $this->cacheConfigMock); }
/** * @param string $params * @dataProvider dataProcessNonce */ public function testProcessNonceException($params = null, $exceptionMessage = null) { $this->customerSessionMock->expects($this->any())->method('getCustomerId')->willReturn($params['customerId']); $countryCollectionMock = $this->getMockBuilder('\\Magento\\Directory\\Model\\Resource\\Country\\Collection')->disableOriginalConstructor()->getMock(); $countryCollectionMock->expects($this->any())->method('addCountryCodeFilter')->willReturn($countryCollectionMock); $this->countryFactoryMock->expects($this->any())->method('create')->willReturn($countryCollectionMock); $this->configMock->expects($this->any())->method('canUseForCountry')->willReturn($params['canUseForCountry']); $this->configMock->expects($this->any())->method('canUseCcTypeForCountry')->willReturn($params['canUseCcTypeForCountry']); $this->helperMock->expects($this->any())->method('generateCustomerId')->willReturn($params['customerId']); $this->customerMock->expects($this->any())->method('load')->willReturn($this->customerMock); $this->customerFactoryMock->expects($this->any())->method('create')->willReturn($this->customerMock); if (is_object($params['paymentMethodObj'])) { if (!$params['optionsArray']['update']) { $this->braintreePaymentMethodMock->expects($this->once())->method('create')->willReturn($params['paymentMethodObj']); } else { $this->braintreePaymentMethodMock->expects($this->once())->method('update')->willReturn($params['paymentMethodObj']); } if (!$params['paymentMethodObj']->success) { $this->errorHelperMock->expects($this->once())->method('parseBraintreeError')->willReturn(new \Magento\Framework\Phrase($exceptionMessage)); } else { $this->errorHelperMock->expects($this->never())->method('parseBraintreeError'); } } try { $this->model->processNonce($params['nonce'], $params['optionsArray'], $params['billingAddress']); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->assertEquals($exceptionMessage, $e->getMessage()); } }
public function testGetRssData() { $wishlistId = 1; $customerName = 'Customer Name'; $title = "{$customerName}'s Wishlist"; $wishlistModelMock = $this->getMock('Magento\\Wishlist\\Model\\Wishlist', ['getId', '__wakeup', 'getCustomerId', 'getItemCollection', 'getSharingCode'], [], '', false); $customerServiceMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false); $wishlistSharingUrl = 'wishlist/shared/index/1'; $locale = 'en_US'; $productUrl = 'http://product.url/'; $productName = 'Product name'; $customer = $this->getMockBuilder('Magento\\Customer\\Model\\Customer')->setMethods(['getName', '__wakeup', 'load'])->disableOriginalConstructor()->getMock(); $customer->expects($this->once())->method('load')->will($this->returnSelf()); $customer->expects($this->once())->method('getName')->will($this->returnValue('Customer Name')); $this->customerFactory->expects($this->once())->method('create')->will($this->returnValue($customer)); $this->wishlistHelperMock->expects($this->any())->method('getWishlist')->will($this->returnValue($wishlistModelMock)); $this->wishlistHelperMock->expects($this->any())->method('getCustomer')->will($this->returnValue($customerServiceMock)); $wishlistModelMock->expects($this->once())->method('getId')->will($this->returnValue($wishlistId)); $this->urlBuilderMock->expects($this->once())->method('getUrl')->will($this->returnValue($wishlistSharingUrl)); $this->scopeConfig->expects($this->any())->method('getValue')->will($this->returnValueMap([['advanced/modules_disable_output/Magento_Rss', \Magento\Framework\Store\ScopeInterface::SCOPE_STORE, null, null], [\Magento\Core\Helper\Data::XML_PATH_DEFAULT_LOCALE, \Magento\Framework\Store\ScopeInterface::SCOPE_STORE, null, $locale]])); $staticArgs = ['productName' => $productName, 'productUrl' => $productUrl]; $description = $this->processWishlistItemDescription($wishlistModelMock, $staticArgs); $expectedResult = ['title' => $title, 'description' => $title, 'link' => $wishlistSharingUrl, 'charset' => 'UTF-8', 'entries' => [0 => ['title' => $productName, 'link' => $productUrl, 'description' => $description]]]; $this->assertEquals($expectedResult, $this->model->getRssData()); }
/** * @param $customerId * @return \PHPUnit_Framework_MockObject_MockObject */ protected function prepareLoginDataMock($customerId) { $customerDataMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false); $customerDataMock->expects($this->once())->method('getId')->will($this->returnValue($customerId)); $customerMock = $this->getMock('Magento\\Customer\\Model\\Customer', [], [], '', false); $customerMock->expects($this->once())->method('getId')->will($this->returnValue($customerId)); $customerMock->expects($this->once())->method('isConfirmationRequired')->will($this->returnValue(true)); $customerMock->expects($this->never())->method('getConfirmation')->will($this->returnValue($customerId)); $this->customerFactoryMock->expects($this->once())->method('create')->will($this->returnValue($customerMock)); $customerMock->expects($this->once())->method('updateData')->with($customerDataMock)->will($this->returnSelf()); $this->_httpContextMock->expects($this->exactly(3))->method('setValue'); return $customerDataMock; }
/** * @return void */ public function testAuthenticate() { $username = '******'; $password = '******'; $customerData = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customerModel = $this->getMockBuilder('Magento\\Customer\\Model\\Customer')->disableOriginalConstructor()->getMock(); $customerModel->expects($this->once())->method('updateData')->willReturn($customerModel); $this->customerRepository->expects($this->once())->method('get')->with($username)->willReturn($customerData); $this->accountManagementHelper->expects($this->once())->method('checkIfLocked')->with($customerData); $this->accountManagementHelper->expects($this->once())->method('validatePasswordAndLockStatus')->with($customerData, $password); $this->customerFactory->expects($this->once())->method('create')->willReturn($customerModel); $this->manager->expects($this->exactly(2))->method('dispatch')->withConsecutive(['customer_customer_authenticated', ['model' => $customerModel, 'password' => $password]], ['customer_data_object_login', ['customer' => $customerData]]); $this->assertEquals($customerData, $this->accountManagement->authenticate($username, $password)); }
public function testRemoveByEmail() { $this->customer->expects($this->exactly(2))->method('loadByEmail')->with(self::CUSTOMER_EMAIL)->will($this->returnValue($this->customer)); $this->customer->expects($this->any())->method('getId')->will($this->returnValue(self::CUSTOMER_ID)); $this->customer->expects($this->any())->method('getEmail')->will($this->returnValue(self::CUSTOMER_EMAIL)); $this->customer->expects($this->any())->method('getWebsiteId')->will($this->returnValue(self::WEBSITE_ID)); $this->customer->expects($this->any())->method('setEmail')->will($this->returnValue($this->customer)); $this->customer->expects($this->any())->method('setWebsiteId')->will($this->returnValue($this->customer)); $this->customerFactory->expects($this->exactly(2))->method('create')->will($this->returnValue($this->customer)); $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID); $this->assertEquals($this->customer, $actual); $this->customerRegistry->removeByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID); $actual = $this->customerRegistry->retrieveByEmail(self::CUSTOMER_EMAIL, self::WEBSITE_ID); $this->assertEquals($this->customer, $actual); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGetList() { $sortOrder = $this->getMock('Magento\\Framework\\Api\\SortOrder', [], [], '', false); $filterGroup = $this->getMock('Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false); $filter = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false); $collection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Customer\\Collection', [], [], '', false); $searchResults = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressSearchResultsInterface', [], '', false); $searchCriteria = $this->getMockForAbstractClass('Magento\\Framework\\Api\\SearchCriteriaInterface', [], '', false); $customerModel = $this->getMock('Magento\\Customer\\Model\\Customer', ['getId', 'setId', 'setStoreId', 'getStoreId', 'getAttributeSetId', 'setAttributeSetId', 'setRpToken', 'setRpTokenCreatedAt', 'getDataModel', 'setPasswordHash', 'getCollection'], [], 'customerModel', false); $metadata = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface', [], '', false); $this->searchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults); $searchResults->expects($this->once())->method('setSearchCriteria')->with($searchCriteria); $this->customerFactory->expects($this->once())->method('create')->willReturn($customerModel); $customerModel->expects($this->once())->method('getCollection')->willReturn($collection); $this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection, 'Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->customerMetadata->expects($this->once())->method('getAllAttributesMetadata')->willReturn([$metadata]); $metadata->expects($this->once())->method('getAttributeCode')->willReturn('attribute-code'); $collection->expects($this->once())->method('addAttributeToSelect')->with('attribute-code'); $collection->expects($this->once())->method('addNameToSelect'); $collection->expects($this->at(2))->method('joinAttribute')->with('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(3))->method('joinAttribute')->with('billing_city', 'customer_address/city', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(4))->method('joinAttribute')->with('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(5))->method('joinAttribute')->with('billing_region', 'customer_address/region', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(6))->method('joinAttribute')->with('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')->willReturnSelf(); $collection->expects($this->at(7))->method('joinAttribute')->with('company', 'customer_address/company', 'default_billing', null, 'left')->willReturnSelf(); $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $collection->expects($this->once())->method('addFieldToFilter')->with([['attribute' => 'Field', 'eq' => 'Value']], []); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]); $filter->expects($this->once())->method('getConditionType')->willReturn(false); $filter->expects($this->once())->method('getField')->willReturn('Field'); $filter->expects($this->atLeastOnce())->method('getValue')->willReturn('Value'); $collection->expects($this->once())->method('addOrder')->with('Field', 'ASC'); $searchCriteria->expects($this->atLeastOnce())->method('getSortOrders')->willReturn([$sortOrder]); $sortOrder->expects($this->once())->method('getField')->willReturn('Field'); $sortOrder->expects($this->once())->method('getDirection')->willReturn(\Magento\Framework\Api\SortOrder::SORT_ASC); $searchCriteria->expects($this->once())->method('getCurrentPage')->willReturn(1); $collection->expects($this->once())->method('setCurPage')->with(1); $searchCriteria->expects($this->once())->method('getPageSize')->willReturn(10); $collection->expects($this->once())->method('setPageSize')->with(10); $collection->expects($this->once())->method('getSize')->willReturn(23); $searchResults->expects($this->once())->method('setTotalCount')->with(23); $collection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$customerModel])); $customerModel->expects($this->atLeastOnce())->method('getDataModel')->willReturn($this->customer); $searchResults->expects($this->once())->method('setItems')->with([$this->customer]); $this->assertSame($searchResults, $this->model->getList($searchCriteria)); }
/** * @return void */ public function testAuthenticate() { $username = '******'; $password = '******'; $passwordHash = '1a2b3f4c'; $customerData = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->getMock(); $customerModel = $this->getMockBuilder('Magento\\Customer\\Model\\Customer')->disableOriginalConstructor()->getMock(); $customerModel->expects($this->once())->method('updateData')->willReturn($customerModel); $this->customerRepository->expects($this->once())->method('get')->with($username)->willReturn($customerData); $this->authenticationMock->expects($this->once())->method('authenticate'); $customerSecure = $this->getMockBuilder('Magento\\Customer\\Model\\Data\\CustomerSecure')->setMethods(['getPasswordHash'])->disableOriginalConstructor()->getMock(); $customerSecure->expects($this->any())->method('getPasswordHash')->willReturn($passwordHash); $this->customerRegistry->expects($this->any())->method('retrieveSecureData')->willReturn($customerSecure); $this->customerFactory->expects($this->once())->method('create')->willReturn($customerModel); $this->manager->expects($this->exactly(2))->method('dispatch')->withConsecutive(['customer_customer_authenticated', ['model' => $customerModel, 'password' => $password]], ['customer_data_object_login', ['customer' => $customerData]]); $this->assertEquals($customerData, $this->accountManagement->authenticate($username, $password)); }