Skip to content

#27500 Prepare CustomerImportExport module Tests for PHPUnit 8 #27827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CustomerImportExport\Test\Unit\Model\Export;

use Magento\Customer\Model\AddressFactory;
use Magento\Customer\Model\Config\Share;
use Magento\Customer\Model\GroupFactory;
use Magento\Customer\Model\ResourceModel\Customer;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
use Magento\CustomerImportExport\Model\Export\Address;
use Magento\CustomerImportExport\Model\Export\CustomerFactory;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Eav\Model\Entity\TypeFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Data\Collection;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Data\Collection\EntityFactory;
use Magento\Framework\DataObject;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\ImportExport\Model\Export\Adapter\AbstractAdapter;
use Magento\ImportExport\Model\Export\Factory;
use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AddressTest extends \PHPUnit\Framework\TestCase
class AddressTest extends TestCase
{
/**
* Test attribute code
Expand All @@ -22,7 +46,7 @@ class AddressTest extends \PHPUnit\Framework\TestCase
*
* @var array
*/
protected $_websites = [\Magento\Store\Model\Store::DEFAULT_STORE_ID => 'admin', 1 => 'website1'];
protected $_websites = [Store::DEFAULT_STORE_ID => 'admin', 1 => 'website1'];

/**
* Attributes array
Expand Down Expand Up @@ -67,9 +91,9 @@ class AddressTest extends \PHPUnit\Framework\TestCase
*/
protected $_model;

protected function setUp()
protected function setUp(): void
{
$storeManager = $this->createMock(\Magento\Store\Model\StoreManager::class);
$storeManager = $this->createMock(StoreManager::class);
$storeManager->expects(
$this->once()
)->method(
Expand All @@ -78,22 +102,22 @@ protected function setUp()
$this->returnCallback([$this, 'getWebsites'])
);

$this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->_model = new \Magento\CustomerImportExport\Model\Export\Address(
$this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class),
$this->_objectManager = new ObjectManager($this);
$this->_model = new Address(
$this->createMock(ScopeConfigInterface::class),
$storeManager,
$this->createMock(\Magento\ImportExport\Model\Export\Factory::class),
$this->createMock(\Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory::class),
$this->createMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class),
$this->createMock(\Magento\Eav\Model\Config::class),
$this->createMock(\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class),
$this->createMock(\Magento\CustomerImportExport\Model\Export\CustomerFactory::class),
$this->createMock(Factory::class),
$this->createMock(CollectionByPagesIteratorFactory::class),
$this->createMock(TimezoneInterface::class),
$this->createMock(Config::class),
$this->createMock(CollectionFactory::class),
$this->createMock(CustomerFactory::class),
$this->createMock(\Magento\Customer\Model\ResourceModel\Address\CollectionFactory::class),
$this->_getModelDependencies()
);
}

protected function tearDown()
protected function tearDown(): void
{
unset($this->_model);
unset($this->_objectManager);
Expand All @@ -108,10 +132,10 @@ protected function _getModelDependencies()
{
$translator = $this->createMock(\stdClass::class);

$entityFactory = $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class);
$entityFactory = $this->createMock(EntityFactory::class);

/** @var $attributeCollection \Magento\Framework\Data\Collection|\PHPUnit\Framework\TestCase */
$attributeCollection = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
$attributeCollection = $this->getMockBuilder(Collection::class)
->setMethods(['getEntityTypeCode'])
->setConstructorArgs([$entityFactory])
->getMock();
Expand All @@ -125,12 +149,12 @@ protected function _getModelDependencies()
);
foreach ($this->_attributes as $attributeData) {
$arguments = $this->_objectManager->getConstructArguments(
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
['eavTypeFactory' => $this->createMock(\Magento\Eav\Model\Entity\TypeFactory::class)]
AbstractAttribute::class,
['eavTypeFactory' => $this->createMock(TypeFactory::class)]
);
$arguments['data'] = $attributeData;
$attribute = $this->getMockForAbstractClass(
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
AbstractAttribute::class,
$arguments,
'',
true,
Expand All @@ -150,7 +174,7 @@ protected function _getModelDependencies()
$this->returnCallback([$this, 'iterate'])
);

$customerCollection = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
$customerCollection = $this->getMockBuilder(AbstractDb::class)
->setMethods(['addAttributeToSelect'])
->disableOriginalConstructor()
->getMockForAbstractClass();
Expand Down Expand Up @@ -182,15 +206,16 @@ protected function _getModelDependencies()
public function getWebsites($withDefault = false)
{
$websites = [];
if (!$withDefault) {
unset($websites[0]);
}
foreach ($this->_websites as $id => $code) {
if (!$withDefault && $id == \Magento\Store\Model\Store::DEFAULT_STORE_ID) {
if (!$withDefault && $id == Store::DEFAULT_STORE_ID) {
continue;
}
$websiteData = ['id' => $id, 'code' => $code];
$websites[$id] = new \Magento\Framework\DataObject($websiteData);
$websites[$id] = new DataObject($websiteData);
}

if (!$withDefault) {
unset($websites[0]);
}

return $websites;
Expand All @@ -201,24 +226,24 @@ public function getWebsites($withDefault = false)
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param \Magento\Framework\Data\Collection\AbstractDb $collection
* @param AbstractDb $collection
* @param int $pageSize
* @param array $callbacks
*/
public function iterate(\Magento\Framework\Data\Collection\AbstractDb $collection, $pageSize, array $callbacks)
public function iterate(AbstractDb $collection, $pageSize, array $callbacks)
{
$resource = $this->createPartialMock(\Magento\Customer\Model\ResourceModel\Customer::class, ['getIdFieldName']);
$resource = $this->createPartialMock(Customer::class, ['getIdFieldName']);
$resource->expects($this->any())->method('getIdFieldName')->will($this->returnValue('id'));
$arguments = [
'data' => $this->_customerData,
'resource' => $resource,
$this->createMock(\Magento\Customer\Model\Config\Share::class),
$this->createMock(\Magento\Customer\Model\AddressFactory::class),
$this->createMock(Share::class),
$this->createMock(AddressFactory::class),
$this->createMock(\Magento\Customer\Model\ResourceModel\Address\CollectionFactory::class),
$this->createMock(\Magento\Customer\Model\GroupFactory::class),
$this->createMock(GroupFactory::class),
$this->createMock(\Magento\Customer\Model\AttributeFactory::class),
];
/** @var $customer \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject */
/** @var $customer \Magento\Customer\Model\Customer|MockObject */
$customer = $this->_objectManager->getObject(\Magento\Customer\Model\Customer::class, $arguments);

foreach ($callbacks as $callback) {
Expand All @@ -234,7 +259,7 @@ public function iterate(\Magento\Framework\Data\Collection\AbstractDb $collectio
public function testExportItem()
{
$writer = $this->getMockForAbstractClass(
\Magento\ImportExport\Model\Export\Adapter\AbstractAdapter::class,
AbstractAdapter::class,
[],
'',
false,
Expand All @@ -254,9 +279,9 @@ public function testExportItem()
$this->_model->setWriter($writer);
$this->_model->setParameters([]);

$arguments = $this->_objectManager->getConstructArguments(\Magento\Framework\Model\AbstractModel::class);
$arguments = $this->_objectManager->getConstructArguments(AbstractModel::class);
$arguments['data'] = $this->_addressData;
$item = $this->getMockForAbstractClass(\Magento\Framework\Model\AbstractModel::class, $arguments);
$item = $this->getMockForAbstractClass(AbstractModel::class, $arguments);
$this->_model->exportItem($item);
}

Expand All @@ -267,19 +292,19 @@ public function testExportItem()
*/
public function validateWriteRow(array $row)
{
$billingColumn = \Magento\CustomerImportExport\Model\Export\Address::COLUMN_NAME_DEFAULT_BILLING;
$billingColumn = Address::COLUMN_NAME_DEFAULT_BILLING;
$this->assertEquals($this->_customerData['default_billing'], $row[$billingColumn]);

$shippingColumn = \Magento\CustomerImportExport\Model\Export\Address::COLUMN_NAME_DEFAULT_SHIPPING;
$shippingColumn = Address::COLUMN_NAME_DEFAULT_SHIPPING;
$this->assertEquals($this->_customerData['default_shipping'], $row[$shippingColumn]);

$idColumn = \Magento\CustomerImportExport\Model\Export\Address::COLUMN_ADDRESS_ID;
$idColumn = Address::COLUMN_ADDRESS_ID;
$this->assertEquals($this->_addressData['id'], $row[$idColumn]);

$emailColumn = \Magento\CustomerImportExport\Model\Export\Address::COLUMN_EMAIL;
$emailColumn = Address::COLUMN_EMAIL;
$this->assertEquals($this->_customerData['email'], $row[$emailColumn]);

$websiteColumn = \Magento\CustomerImportExport\Model\Export\Address::COLUMN_WEBSITE;
$websiteColumn = Address::COLUMN_WEBSITE;
$this->assertEquals($this->_websites[$this->_customerData['website_id']], $row[$websiteColumn]);

$this->assertEquals($this->_addressData[self::ATTRIBUTE_CODE], $row[self::ATTRIBUTE_CODE]);
Expand Down
Loading