Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 - make sure that we are testing the new method for adding customer by array
  • Loading branch information
dmanners committed Jan 18, 2018
1 parent 5cc36b7 commit d45ade0
Showing 1 changed file with 51 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

Expand All @@ -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;
}
Expand Down

0 comments on commit d45ade0

Please sign in to comment.