From d45ade079d4cb83df28c0e2a7283d6b87cad857e Mon Sep 17 00:00:00 2001 From: David Manners Date: Thu, 18 Jan 2018 10:22:52 +0000 Subject: [PATCH] magento-engcom/import-export-improvements#43: refactor the StorageTests - make sure that we are testing the new method for adding customer by array --- .../Import/Customer/StorageTest.php | 114 ++++++++---------- 1 file changed, 51 insertions(+), 63 deletions(-) diff --git a/app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/Customer/StorageTest.php b/app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/Customer/StorageTest.php index 71f8f17c85509..9ab4474eb2caa 100644 --- a/app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/Customer/StorageTest.php +++ b/app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/Customer/StorageTest.php @@ -26,74 +26,63 @@ class StorageTest extends \PHPUnit\Framework\TestCase protected function setUp() { - $this->_model = new \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\Storage( - $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class) - ->disableOriginalConstructor() - ->getMock(), - $this->getMockBuilder(\Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory::class) - ->disableOriginalConstructor() - ->getMock(), - $this->_getModelDependencies() - ); - $this->_model->load(); - } - - protected function tearDown() - { - unset($this->_model); - } - - /** - * Retrieve all necessary objects mocks which used inside customer storage - * - * @return array - */ - protected function _getModelDependencies() - { - $select = $this->getMockBuilder(\Magento\Framework\DB\Select::class) + /** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject $selectMock */ + $selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class) ->disableOriginalConstructor() ->setMethods(['from']) ->getMock(); - $select->expects($this->any())->method('from')->will($this->returnCallback([$this, 'validateFrom'])); + $selectMock->expects($this->any())->method('from')->will($this->returnSelf()); + + /** @var $connectionMock \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */ + $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\Pdo\Mysql::class) + ->disableOriginalConstructor() + ->setMethods(['select', 'fetchAll']) + ->getMock(); + $connectionMock->expects($this->any()) + ->method('select') + ->will($this->returnValue($selectMock)); + $connectionMock->expects($this->any()) + ->method('fetchAll') + ->will($this->returnValue([])); + + /** @var \Magento\Customer\Model\ResourceModel\Customer\Collection|\PHPUnit_Framework_MockObject_MockObject $customerCollection */ $customerCollection = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class) ->disableOriginalConstructor() - ->setMethods(['load', 'removeAttributeToSelect', 'getResource', 'getSelect']) + ->setMethods(['getConnection','getMainTable']) ->getMock(); + $customerCollection->expects($this->any()) + ->method('getConnection') + ->will($this->returnValue($connectionMock)); - $resourceStub = new \Magento\Framework\DataObject(); - $resourceStub->setEntityTable($this->_entityTable); - $customerCollection->expects($this->once())->method('getResource')->will($this->returnValue($resourceStub)); + $customerCollection->expects($this->any()) + ->method('getMainTable') + ->willReturn('customer_entity'); - $customerCollection->expects($this->once())->method('getSelect')->will($this->returnValue($select)); + /** @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject $collectionFactory */ + $collectionFactory = $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + $collectionFactory->expects($this->any()) + ->method('create') + ->willReturn($customerCollection); - $byPagesIterator = $this->createPartialMock(\stdClass::class, ['iterate']); - $byPagesIterator->expects($this->once()) - ->method('iterate') - ->will($this->returnCallback([$this, 'iterate'])); + /** @var \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory|\PHPUnit_Framework_MockObject_MockObject $byPagesIteratorFactory */ + $byPagesIteratorFactory = $this->getMockBuilder(\Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); - return [ - 'customer_collection' => $customerCollection, - 'collection_by_pages_iterator' => $byPagesIterator, - 'page_size' => 10 - ]; + $this->_model = new \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\Storage( + $collectionFactory, + $byPagesIteratorFactory + ); + $this->_model->load(); } - /** - * Iterate stub - * - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - * - * @param \Magento\Framework\Data\Collection $collection - * @param int $pageSize - * @param array $callbacks - */ - public function iterate(\Magento\Framework\Data\Collection $collection, $pageSize, array $callbacks) + protected function tearDown() { - foreach ($collection as $customer) { - foreach ($callbacks as $callback) { - call_user_func($callback, $customer); - } - } + unset($this->_model); } /** @@ -117,8 +106,7 @@ public function testAddCustomer() $customer = $this->_addCustomerToStorage(); $this->assertAttributeCount(1, $propertyName, $this->_model); - - $expectedCustomerData = [$customer->getWebsiteId() => $customer->getId()]; + $expectedCustomerData = [$customer['website_id'] => $customer['entity_id']]; $this->assertAttributeContains($expectedCustomerData, $propertyName, $this->_model); } @@ -127,19 +115,19 @@ public function testGetCustomerId() $customer = $this->_addCustomerToStorage(); $this->assertEquals( - $customer->getId(), - $this->_model->getCustomerId($customer->getEmail(), $customer->getWebsiteId()) + $customer['entity_id'], + $this->_model->getCustomerId($customer['email'], $customer['website_id']) ); - $this->assertFalse($this->_model->getCustomerId('new@test.com', $customer->getWebsiteId())); + $this->assertFalse($this->_model->getCustomerId('new@test.com', $customer['website_id'])); } /** - * @return \Magento\Framework\DataObject + * @return array */ protected function _addCustomerToStorage() { - $customer = new \Magento\Framework\DataObject(['id' => 1, 'website_id' => 1, 'email' => 'test@test.com']); - $this->_model->addCustomer($customer); + $customer = ['entity_id' => 1, 'website_id' => 1, 'email' => 'test@test.com']; + $this->_model->addCustomerByArray($customer); return $customer; }