123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Model\ResourceModel;
- use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class AddressRepositoryTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Directory\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $directoryData;
- /**
- * @var \Magento\Customer\Model\AddressFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressFactory;
- /**
- * @var \Magento\Customer\Model\AddressRegistry|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressRegistry;
- /**
- * @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerRegistry;
- /**
- * @var \Magento\Customer\Model\ResourceModel\Address|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressResourceModel;
- /**
- * @var \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressSearchResultsFactory;
- /**
- * @var \Magento\Customer\Model\ResourceModel\Address\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressCollectionFactory;
- /**
- * @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $extensionAttributesJoinProcessor;
- /**
- * @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customer;
- /**
- * @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $address;
- /**
- * @var \Magento\Customer\Model\ResourceModel\AddressRepository
- */
- protected $repository;
- /**
- * @var CollectionProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $collectionProcessor;
- protected function setUp()
- {
- $this->addressFactory = $this->createPartialMock(\Magento\Customer\Model\AddressFactory::class, ['create']);
- $this->addressRegistry = $this->createMock(\Magento\Customer\Model\AddressRegistry::class);
- $this->customerRegistry = $this->createMock(\Magento\Customer\Model\CustomerRegistry::class);
- $this->addressResourceModel = $this->createMock(\Magento\Customer\Model\ResourceModel\Address::class);
- $this->directoryData = $this->createMock(\Magento\Directory\Helper\Data::class);
- $this->addressSearchResultsFactory = $this->createPartialMock(
- \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory::class,
- ['create']
- );
- $this->addressCollectionFactory = $this->createPartialMock(
- \Magento\Customer\Model\ResourceModel\Address\CollectionFactory::class,
- ['create']
- );
- $this->extensionAttributesJoinProcessor = $this->getMockForAbstractClass(
- \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class,
- [],
- '',
- false
- );
- $this->customer = $this->createMock(\Magento\Customer\Model\Customer::class);
- $this->address = $this->createPartialMock(\Magento\Customer\Model\Address::class, [
- 'getId',
- 'getCountryId',
- 'getFirstname',
- 'getLastname',
- 'getStreetLine',
- 'getCity',
- 'getTelephone',
- 'getRegionId',
- 'getRegion',
- 'updateData',
- 'setCustomer',
- 'getCountryModel',
- 'getShouldIgnoreValidation',
- 'validate',
- 'save',
- 'getDataModel',
- 'getCustomerId',
- ]);
- $this->collectionProcessor = $this->getMockBuilder(CollectionProcessorInterface::class)
- ->getMockForAbstractClass();
- $this->repository = new \Magento\Customer\Model\ResourceModel\AddressRepository(
- $this->addressFactory,
- $this->addressRegistry,
- $this->customerRegistry,
- $this->addressResourceModel,
- $this->directoryData,
- $this->addressSearchResultsFactory,
- $this->addressCollectionFactory,
- $this->extensionAttributesJoinProcessor,
- $this->collectionProcessor
- );
- }
- public function testSave()
- {
- $customerId = 34;
- $addressId = 53;
- $customerAddress = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressInterface::class,
- [],
- '',
- false
- );
- $addressCollection =
- $this->createMock(\Magento\Customer\Model\ResourceModel\Address\Collection::class);
- $customerAddress->expects($this->atLeastOnce())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $customerAddress->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($addressId);
- $this->customerRegistry->expects($this->once())
- ->method('retrieve')
- ->with($customerId)
- ->willReturn($this->customer);
- $this->address->expects($this->atLeastOnce())
- ->method("getId")
- ->willReturn($addressId);
- $this->addressRegistry->expects($this->once())
- ->method('retrieve')
- ->with($addressId)
- ->willReturn(null);
- $this->addressFactory->expects($this->once())
- ->method('create')
- ->willReturn($this->address);
- $this->address->expects($this->once())
- ->method('updateData')
- ->with($customerAddress);
- $this->address->expects($this->once())
- ->method('setCustomer')
- ->with($this->customer);
- $this->address->expects($this->once())
- ->method('validate')
- ->willReturn(true);
- $this->address->expects($this->once())
- ->method('save');
- $this->addressRegistry->expects($this->once())
- ->method('push')
- ->with($this->address);
- $this->customer->expects($this->exactly(2))
- ->method('getAddressesCollection')
- ->willReturn($addressCollection);
- $addressCollection->expects($this->once())
- ->method("removeItemByKey")
- ->with($addressId);
- $addressCollection->expects($this->once())
- ->method("addItem")
- ->with($this->address);
- $this->address->expects($this->once())
- ->method('getDataModel')
- ->willReturn($customerAddress);
- $this->repository->save($customerAddress);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- */
- public function testSaveWithException()
- {
- $customerId = 34;
- $addressId = 53;
- $errors[] = __('Please enter the state/province.');
- $customerAddress = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressInterface::class,
- [],
- '',
- false
- );
- $customerAddress->expects($this->atLeastOnce())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $customerAddress->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($addressId);
- $this->customerRegistry->expects($this->once())
- ->method('retrieve')
- ->with($customerId)
- ->willReturn($this->customer);
- $this->addressRegistry->expects($this->once())
- ->method('retrieve')
- ->with($addressId)
- ->willReturn($this->address);
- $this->address->expects($this->once())
- ->method('updateData')
- ->with($customerAddress);
- $this->address->expects($this->once())
- ->method('validate')
- ->willReturn($errors);
- $this->repository->save($customerAddress);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage region is a required field.
- */
- public function testSaveWithInvalidRegion()
- {
- $customerId = 34;
- $addressId = 53;
- $errors[] = __('region is a required field.');
- $customerAddress = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressInterface::class,
- [],
- '',
- false
- );
- $customerAddress->expects($this->atLeastOnce())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $customerAddress->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($addressId);
- $this->customerRegistry->expects($this->once())
- ->method('retrieve')
- ->with($customerId)
- ->willReturn($this->customer);
- $this->addressRegistry->expects($this->once())
- ->method('retrieve')
- ->with($addressId)
- ->willReturn($this->address);
- $this->address->expects($this->once())
- ->method('updateData')
- ->with($customerAddress);
- $this->address->expects($this->never())
- ->method('getRegionId')
- ->willReturn(null);
- $this->address->expects($this->once())
- ->method('validate')
- ->willReturn($errors);
- $this->repository->save($customerAddress);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage "regionId" is required. Enter and try again.
- */
- public function testSaveWithInvalidRegionId()
- {
- $customerId = 34;
- $addressId = 53;
- $errors[] = __('"regionId" is required. Enter and try again.');
- $customerAddress = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressInterface::class,
- [],
- '',
- false
- );
- $customerAddress->expects($this->atLeastOnce())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $customerAddress->expects($this->atLeastOnce())
- ->method('getId')
- ->willReturn($addressId);
- $this->customerRegistry->expects($this->once())
- ->method('retrieve')
- ->with($customerId)
- ->willReturn($this->customer);
- $this->addressRegistry->expects($this->once())
- ->method('retrieve')
- ->with($addressId)
- ->willReturn($this->address);
- $this->address->expects($this->once())
- ->method('updateData')
- ->with($customerAddress);
- $this->address->expects($this->never())
- ->method('getRegion')
- ->willReturn('');
- $this->address->expects($this->once())
- ->method('validate')
- ->willReturn($errors);
- $this->repository->save($customerAddress);
- }
- public function testGetById()
- {
- $customerAddress = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressInterface::class,
- [],
- '',
- false
- );
- $this->addressRegistry->expects($this->once())
- ->method('retrieve')
- ->with(12)
- ->willReturn($this->address);
- $this->address->expects($this->once())
- ->method('getDataModel')
- ->willReturn($customerAddress);
- $this->assertSame($customerAddress, $this->repository->getById(12));
- }
- public function testGetList()
- {
- $collection = $this->createMock(\Magento\Customer\Model\ResourceModel\Address\Collection::class);
- $searchResults = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressSearchResultsInterface::class,
- [],
- '',
- false
- );
- $searchCriteria = $this->getMockForAbstractClass(
- \Magento\Framework\Api\SearchCriteriaInterface::class,
- [],
- '',
- false
- );
- $this->addressSearchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults);
- $this->addressCollectionFactory->expects($this->once())->method('create')->willReturn($collection);
- $this->extensionAttributesJoinProcessor->expects($this->once())
- ->method('process')
- ->with($collection, \Magento\Customer\Api\Data\AddressInterface::class);
- $this->collectionProcessor->expects($this->once())
- ->method('process')
- ->with($searchCriteria, $collection)
- ->willReturnSelf();
- $collection->expects($this->once())->method('getSize')->willReturn(23);
- $searchResults->expects($this->once())
- ->method('setTotalCount')
- ->with(23);
- $collection->expects($this->once())
- ->method('getItems')
- ->willReturn([$this->address]);
- $this->address->expects($this->once())
- ->method('getId')
- ->willReturn(12);
- $customerAddress = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressInterface::class,
- [],
- '',
- false
- );
- $this->addressRegistry->expects($this->once())
- ->method('retrieve')
- ->with(12)
- ->willReturn($this->address);
- $this->address->expects($this->once())
- ->method('getDataModel')
- ->willReturn($customerAddress);
- $searchResults->expects($this->once())
- ->method('setItems')
- ->with([$customerAddress]);
- $searchResults->expects($this->once())
- ->method('setSearchCriteria')
- ->with($searchCriteria);
- $this->assertSame($searchResults, $this->repository->getList($searchCriteria));
- }
- public function testDelete()
- {
- $addressId = 12;
- $customerId = 43;
- $addressCollection = $this->createMock(\Magento\Customer\Model\ResourceModel\Address\Collection::class);
- $customerAddress = $this->getMockForAbstractClass(
- \Magento\Customer\Api\Data\AddressInterface::class,
- [],
- '',
- false
- );
- $customerAddress->expects($this->once())
- ->method('getId')
- ->willReturn($addressId);
- $this->address->expects($this->once())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $this->addressRegistry->expects($this->once())
- ->method('retrieve')
- ->with($addressId)
- ->willReturn($this->address);
- $this->customerRegistry->expects($this->once())
- ->method('retrieve')
- ->with($customerId)
- ->willReturn($this->customer);
- $this->customer->expects($this->once())
- ->method('getAddressesCollection')
- ->willReturn($addressCollection);
- $addressCollection->expects($this->once())
- ->method('clear');
- $this->addressResourceModel->expects($this->once())
- ->method('delete')
- ->with($this->address);
- $this->addressRegistry->expects($this->once())
- ->method('remove')
- ->with($addressId);
- $this->assertTrue($this->repository->delete($customerAddress));
- }
- public function testDeleteById()
- {
- $addressId = 12;
- $customerId = 43;
- $this->address->expects($this->once())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $addressCollection = $this->createMock(\Magento\Customer\Model\ResourceModel\Address\Collection::class);
- $this->addressRegistry->expects($this->once())
- ->method('retrieve')
- ->with($addressId)
- ->willReturn($this->address);
- $this->customerRegistry->expects($this->once())
- ->method('retrieve')
- ->with($customerId)
- ->willReturn($this->customer);
- $this->customer->expects($this->once())
- ->method('getAddressesCollection')
- ->willReturn($addressCollection);
- $addressCollection->expects($this->once())
- ->method('removeItemByKey')
- ->with($addressId);
- $this->addressResourceModel->expects($this->once())
- ->method('delete')
- ->with($this->address);
- $this->addressRegistry->expects($this->once())
- ->method('remove')
- ->with($addressId);
- $this->assertTrue($this->repository->deleteById($addressId));
- }
- }
|