123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Block\Address;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class EditTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /**
- * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressRepositoryMock;
- /**
- * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerSessionMock;
- /**
- * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $pageConfigMock;
- /**
- * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $dataObjectHelperMock;
- /**
- * @var \Magento\Customer\Api\Data\AddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressDataFactoryMock;
- /**
- * @var \Magento\Customer\Helper\Session\CurrentCustomer|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $currentCustomerMock;
- /**
- * @var \Magento\Customer\Block\Address\Edit
- */
- protected $model;
- protected function setUp()
- {
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
- ->getMock();
- $this->addressRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\AddressRepositoryInterface::class)
- ->getMock();
- $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
- ->disableOriginalConstructor()
- ->setMethods(['getAddressFormData', 'getCustomerId'])
- ->getMock();
- $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->addressDataFactoryMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterfaceFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->currentCustomerMock = $this->getMockBuilder(\Magento\Customer\Helper\Session\CurrentCustomer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->model = $this->objectManager->getObject(
- \Magento\Customer\Block\Address\Edit::class,
- [
- 'request' => $this->requestMock,
- 'addressRepository' => $this->addressRepositoryMock,
- 'customerSession' => $this->customerSessionMock,
- 'pageConfig' => $this->pageConfigMock,
- 'dataObjectHelper' => $this->dataObjectHelperMock,
- 'addressDataFactory' => $this->addressDataFactoryMock,
- 'currentCustomer' => $this->currentCustomerMock,
- ]
- );
- }
- public function testSetLayoutWithOwnAddressAndPostedData()
- {
- $addressId = 1;
- $customerId = 1;
- $title = __('Edit Address');
- $postedData = [
- 'region_id' => 1,
- 'region' => 'region',
- ];
- $newPostedData = $postedData;
- $newPostedData['region'] = $postedData;
- $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
- ->getMock();
- $this->requestMock->expects($this->once())
- ->method('getParam')
- ->with('id', null)
- ->willReturn($addressId);
- $addressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->getMock();
- $this->addressRepositoryMock->expects($this->once())
- ->method('getById')
- ->with($addressId)
- ->willReturn($addressMock);
- $addressMock->expects($this->once())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $this->customerSessionMock->expects($this->at(0))
- ->method('getCustomerId')
- ->willReturn($customerId);
- $addressMock->expects($this->exactly(2))
- ->method('getId')
- ->willReturn($addressId);
- $pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->pageConfigMock->expects($this->once())
- ->method('getTitle')
- ->willReturn($pageTitleMock);
- $pageTitleMock->expects($this->once())
- ->method('set')
- ->with($title)
- ->willReturnSelf();
- $this->customerSessionMock->expects($this->at(1))
- ->method('getAddressFormData')
- ->with(true)
- ->willReturn($postedData);
- $this->dataObjectHelperMock->expects($this->once())
- ->method('populateWithArray')
- ->with(
- $addressMock,
- $newPostedData,
- \Magento\Customer\Api\Data\AddressInterface::class
- )->willReturnSelf();
- $this->assertEquals($this->model, $this->model->setLayout($layoutMock));
- $this->assertEquals($layoutMock, $this->model->getLayout());
- }
- /**
- * @throws \Magento\Framework\Exception\LocalizedException
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function testSetLayoutWithAlienAddress()
- {
- $addressId = 1;
- $customerId = 1;
- $customerPrefix = 'prefix';
- $customerFirstName = 'firstname';
- $customerMiddlename = 'middlename';
- $customerLastname = 'lastname';
- $customerSuffix = 'suffix';
- $title = __('Add New Address');
- $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
- ->getMock();
- $this->requestMock->expects($this->once())
- ->method('getParam')
- ->with('id', null)
- ->willReturn($addressId);
- $addressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->getMock();
- $this->addressRepositoryMock->expects($this->once())
- ->method('getById')
- ->with($addressId)
- ->willReturn($addressMock);
- $addressMock->expects($this->once())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $this->customerSessionMock->expects($this->at(0))
- ->method('getCustomerId')
- ->willReturn($customerId + 1);
- $newAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->getMock();
- $this->addressDataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($newAddressMock);
- $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
- ->getMock();
- $this->currentCustomerMock->expects($this->once())
- ->method('getCustomer')
- ->willReturn($customerMock);
- $customerMock->expects($this->once())
- ->method('getPrefix')
- ->willReturn($customerPrefix);
- $customerMock->expects($this->once())
- ->method('getFirstname')
- ->willReturn($customerFirstName);
- $customerMock->expects($this->once())
- ->method('getMiddlename')
- ->willReturn($customerMiddlename);
- $customerMock->expects($this->once())
- ->method('getLastname')
- ->willReturn($customerLastname);
- $customerMock->expects($this->once())
- ->method('getSuffix')
- ->willReturn($customerSuffix);
- $newAddressMock->expects($this->once())
- ->method('setPrefix')
- ->with($customerPrefix)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setFirstname')
- ->with($customerFirstName)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setMiddlename')
- ->with($customerMiddlename)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setLastname')
- ->with($customerLastname)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setSuffix')
- ->with($customerSuffix)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('getId')
- ->willReturn(null);
- $pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->pageConfigMock->expects($this->once())
- ->method('getTitle')
- ->willReturn($pageTitleMock);
- $pageTitleMock->expects($this->once())
- ->method('set')
- ->with($title)
- ->willReturnSelf();
- $this->assertEquals($this->model, $this->model->setLayout($layoutMock));
- $this->assertEquals($layoutMock, $this->model->getLayout());
- }
- public function testSetLayoutWithoutAddressId()
- {
- $customerPrefix = 'prefix';
- $customerFirstName = 'firstname';
- $customerMiddlename = 'middlename';
- $customerLastname = 'lastname';
- $customerSuffix = 'suffix';
- $title = 'title';
- $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
- ->getMock();
- $this->requestMock->expects($this->once())
- ->method('getParam')
- ->with('id', null)
- ->willReturn('');
- $newAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->getMock();
- $this->addressDataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($newAddressMock);
- $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
- ->getMock();
- $this->currentCustomerMock->expects($this->once())
- ->method('getCustomer')
- ->willReturn($customerMock);
- $customerMock->expects($this->once())
- ->method('getPrefix')
- ->willReturn($customerPrefix);
- $customerMock->expects($this->once())
- ->method('getFirstname')
- ->willReturn($customerFirstName);
- $customerMock->expects($this->once())
- ->method('getMiddlename')
- ->willReturn($customerMiddlename);
- $customerMock->expects($this->once())
- ->method('getLastname')
- ->willReturn($customerLastname);
- $customerMock->expects($this->once())
- ->method('getSuffix')
- ->willReturn($customerSuffix);
- $newAddressMock->expects($this->once())
- ->method('setPrefix')
- ->with($customerPrefix)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setFirstname')
- ->with($customerFirstName)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setMiddlename')
- ->with($customerMiddlename)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setLastname')
- ->with($customerLastname)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setSuffix')
- ->with($customerSuffix)
- ->willReturnSelf();
- $pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->pageConfigMock->expects($this->once())
- ->method('getTitle')
- ->willReturn($pageTitleMock);
- $this->model->setData('title', $title);
- $pageTitleMock->expects($this->once())
- ->method('set')
- ->with($title)
- ->willReturnSelf();
- $this->assertEquals($this->model, $this->model->setLayout($layoutMock));
- $this->assertEquals($layoutMock, $this->model->getLayout());
- }
- public function testSetLayoutWithoutAddress()
- {
- $addressId = 1;
- $customerPrefix = 'prefix';
- $customerFirstName = 'firstname';
- $customerMiddlename = 'middlename';
- $customerLastname = 'lastname';
- $customerSuffix = 'suffix';
- $title = 'title';
- $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
- ->getMock();
- $this->requestMock->expects($this->once())
- ->method('getParam')
- ->with('id', null)
- ->willReturn($addressId);
- $this->addressRepositoryMock->expects($this->once())
- ->method('getById')
- ->with($addressId)
- ->willThrowException(
- \Magento\Framework\Exception\NoSuchEntityException::singleField('addressId', $addressId)
- );
- $newAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
- ->getMock();
- $this->addressDataFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($newAddressMock);
- $customerMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
- ->getMock();
- $this->currentCustomerMock->expects($this->once())
- ->method('getCustomer')
- ->willReturn($customerMock);
- $customerMock->expects($this->once())
- ->method('getPrefix')
- ->willReturn($customerPrefix);
- $customerMock->expects($this->once())
- ->method('getFirstname')
- ->willReturn($customerFirstName);
- $customerMock->expects($this->once())
- ->method('getMiddlename')
- ->willReturn($customerMiddlename);
- $customerMock->expects($this->once())
- ->method('getLastname')
- ->willReturn($customerLastname);
- $customerMock->expects($this->once())
- ->method('getSuffix')
- ->willReturn($customerSuffix);
- $newAddressMock->expects($this->once())
- ->method('setPrefix')
- ->with($customerPrefix)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setFirstname')
- ->with($customerFirstName)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setMiddlename')
- ->with($customerMiddlename)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setLastname')
- ->with($customerLastname)
- ->willReturnSelf();
- $newAddressMock->expects($this->once())
- ->method('setSuffix')
- ->with($customerSuffix)
- ->willReturnSelf();
- $pageTitleMock = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->pageConfigMock->expects($this->once())
- ->method('getTitle')
- ->willReturn($pageTitleMock);
- $this->model->setData('title', $title);
- $pageTitleMock->expects($this->once())
- ->method('set')
- ->with($title)
- ->willReturnSelf();
- $this->assertEquals($this->model, $this->model->setLayout($layoutMock));
- $this->assertEquals($layoutMock, $this->model->getLayout());
- }
- }
|