Skip to content

#27500 PHPUnit 8 for module CatalogWidget #27702

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,111 +1,131 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogWidget\Test\Unit\Block\Product;

use Magento\Catalog\Block\Product\Widget\Html\Pager;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Visibility;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\CatalogWidget\Block\Product\ProductsList;
use Magento\CatalogWidget\Model\Rule;
use Magento\Directory\Model\Currency;
use Magento\Framework\App\Http\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\Pricing\Render;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Framework\View\Design\ThemeInterface;
use Magento\Framework\View\DesignInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Rule\Model\Condition\Combine;
use Magento\Rule\Model\Condition\Sql\Builder;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

use Magento\Widget\Helper\Conditions;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Class ProductsListTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ProductsListTest extends \PHPUnit\Framework\TestCase
class ProductsListTest extends TestCase
{
/**
* @var \Magento\CatalogWidget\Block\Product\ProductsList
* @var ProductsList
*/
protected $productsList;

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
* @var CollectionFactory|MockObject
*/
protected $collectionFactory;

/**
* @var \Magento\Catalog\Model\Product\Visibility|\PHPUnit_Framework_MockObject_MockObject
* @var Visibility|MockObject
*/
protected $visibility;

/**
* @var \Magento\Framework\App\Http\Context|\PHPUnit_Framework_MockObject_MockObject
* @var Context|MockObject
*/
protected $httpContext;

/**
* @var \Magento\Rule\Model\Condition\Sql\Builder|\PHPUnit_Framework_MockObject_MockObject
* @var Builder|MockObject
*/
protected $builder;

/**
* @var \Magento\CatalogWidget\Model\Rule|\PHPUnit_Framework_MockObject_MockObject
* @var Rule|MockObject
*/
protected $rule;

/**
* @var \Magento\Widget\Helper\Conditions|\PHPUnit_Framework_MockObject_MockObject
* @var Conditions|MockObject
*/
protected $widgetConditionsHelper;

/**
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
* @var StoreManagerInterface|MockObject
*/
protected $storeManager;

/**
* @var \Magento\Framework\View\DesignInterface
* @var DesignInterface
*/
protected $design;

/**
* @var \Magento\Framework\App\RequestInterface
* @var RequestInterface
*/
protected $request;

/**
* @var \Magento\Framework\View\LayoutInterface
* @var LayoutInterface
*/
protected $layout;

/**
* @var PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
* @var PriceCurrencyInterface|MockObject
*/
private $priceCurrency;

/**
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
* @var Json|MockObject
*/
private $serializer;

protected function setUp()
protected function setUp(): void
{
$this->collectionFactory =
$this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class)
$this->getMockBuilder(CollectionFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()->getMock();
$this->visibility = $this->getMockBuilder(\Magento\Catalog\Model\Product\Visibility::class)
$this->visibility = $this->getMockBuilder(Visibility::class)
->setMethods(['getVisibleInCatalogIds'])
->disableOriginalConstructor()
->getMock();
$this->httpContext = $this->createMock(\Magento\Framework\App\Http\Context::class);
$this->builder = $this->createMock(\Magento\Rule\Model\Condition\Sql\Builder::class);
$this->rule = $this->createMock(\Magento\CatalogWidget\Model\Rule::class);
$this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
$this->widgetConditionsHelper = $this->getMockBuilder(\Magento\Widget\Helper\Conditions::class)
$this->httpContext = $this->createMock(Context::class);
$this->builder = $this->createMock(Builder::class);
$this->rule = $this->createMock(Rule::class);
$this->serializer = $this->createMock(Json::class);
$this->widgetConditionsHelper = $this->getMockBuilder(Conditions::class)
->disableOriginalConstructor()
->getMock();
$this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
$this->design = $this->createMock(\Magento\Framework\View\DesignInterface::class);
$this->storeManager = $this->createMock(StoreManagerInterface::class);
$this->design = $this->createMock(DesignInterface::class);

$objectManagerHelper = new ObjectManagerHelper($this);
$arguments = $objectManagerHelper->getConstructArguments(
\Magento\CatalogWidget\Block\Product\ProductsList::class,
ProductsList::class,
[
'productCollectionFactory' => $this->collectionFactory,
'catalogProductVisibility' => $this->visibility,
Expand All @@ -123,20 +143,20 @@ protected function setUp()
$this->priceCurrency = $this->createMock(PriceCurrencyInterface::class);

$this->productsList = $objectManagerHelper->getObject(
\Magento\CatalogWidget\Block\Product\ProductsList::class,
ProductsList::class,
$arguments
);
$objectManagerHelper->setBackwardCompatibleProperty($this->productsList, 'priceCurrency', $this->priceCurrency);
}

public function testGetCacheKeyInfo()
{
$store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
$store = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()->setMethods(['getId'])->getMock();
$store->expects($this->once())->method('getId')->willReturn(1);
$this->storeManager->expects($this->once())->method('getStore')->willReturn($store);

$theme = $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class);
$theme = $this->createMock(ThemeInterface::class);
$theme->expects($this->once())->method('getId')->willReturn('blank');
$this->design->expects($this->once())->method('getDesignTheme')->willReturn($theme);

Expand All @@ -149,7 +169,7 @@ public function testGetCacheKeyInfo()
$this->request->expects($this->once())->method('getParam')->with('page_number')->willReturn(1);

$this->request->expects($this->once())->method('getParams')->willReturn('request_params');
$currency = $this->createMock(\Magento\Directory\Model\Currency::class);
$currency = $this->createMock(Currency::class);
$currency->expects($this->once())->method('getCode')->willReturn('USD');
$this->priceCurrency->expects($this->once())->method('getCurrency')->willReturn($currency);

Expand Down Expand Up @@ -178,13 +198,13 @@ public function testGetCacheKeyInfo()

public function testGetProductPriceHtml()
{
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
$product = $this->getMockBuilder(Product::class)
->setMethods(['getId'])
->disableOriginalConstructor()
->getMock();
$product->expects($this->once())->method('getId')->willReturn(1);

$priceRenderer = $this->getMockBuilder(\Magento\Framework\Pricing\Render::class)
$priceRenderer = $this->getMockBuilder(Render::class)
->setMethods(['render'])
->disableOriginalConstructor()
->getMock();
Expand All @@ -202,7 +222,7 @@ public function testGetProductPriceHtml()
$this->assertEquals('<html>', $this->productsList->getProductPriceHtml(
$product,
'some-price-type',
\Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
Render::ZONE_ITEM_LIST,
[
'include_container' => false,
'display_minimal_price' => false
Expand All @@ -217,7 +237,7 @@ public function testGetPagerHtmlEmpty()

public function testGetPagerHtml()
{
$collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
$collection = $this->getMockBuilder(Collection::class)
->setMethods(['getSize'])
->disableOriginalConstructor()
->getMock();
Expand All @@ -227,7 +247,7 @@ public function testGetPagerHtml()
$this->productsList->setData('products_per_page', 2);
$this->productsList->setData('product_collection', $collection);

$pagerBlock = $this->getMockBuilder(\Magento\Catalog\Block\Product\Widget\Html\Pager::class)
$pagerBlock = $this->getMockBuilder(Pager::class)
->setMethods([
'toHtml',
'setUseContainer',
Expand Down Expand Up @@ -266,7 +286,7 @@ public function testCreateCollection($pagerEnable, $productsCount, $productsPerP
{
$this->visibility->expects($this->once())->method('getVisibleInCatalogIds')
->willReturn([Visibility::VISIBILITY_IN_CATALOG, Visibility::VISIBILITY_BOTH]);
$collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
$collection = $this->getMockBuilder(Collection::class)
->setMethods([
'setVisibility',
'addMinimalPrice',
Expand Down Expand Up @@ -367,14 +387,14 @@ public function testShowPager()

public function testGetIdentities()
{
$collection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
$collection = $this->getMockBuilder(Collection::class)
->setMethods([
'addAttributeToSelect',
'getIterator',
])->disableOriginalConstructor()
->getMock();

$product = $this->createPartialMock(\Magento\Framework\DataObject\IdentityInterface::class, ['getIdentities']);
$product = $this->createPartialMock(IdentityInterface::class, ['getIdentities']);
$notProduct = $this->getMockBuilder('NotProduct')
->setMethods(['getIdentities'])
->disableOriginalConstructor()
Expand All @@ -394,11 +414,11 @@ public function testGetIdentities()
/**
* @param $collection
*
* @return \PHPUnit_Framework_MockObject_MockObject
* @return MockObject
*/
private function getConditionsForCollection($collection)
{
$conditions = $this->getMockBuilder(\Magento\Rule\Model\Condition\Combine::class)
$conditions = $this->getMockBuilder(Combine::class)
->setMethods(['collectValidatedAttributes'])
->disableOriginalConstructor()
->getMock();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
Expand All @@ -8,44 +8,58 @@
use Magento\Backend\Block\Template\Context;
use Magento\CatalogWidget\Block\Product\Widget\Conditions;
use Magento\CatalogWidget\Model\Rule;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Data\Form\Element\Fieldset;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use Magento\Framework\Registry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Framework\View\Element\BlockInterface;
use Magento\Framework\View\Element\Template\File\Resolver;
use Magento\Framework\View\Element\Template\File\Validator;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\TemplateEngineInterface;
use Magento\Framework\View\TemplateEnginePool;
use Magento\Rule\Model\Condition\Combine;
use Magento\Widget\Model\Widget\Instance;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Test class for \Magento\CatalogWidget\Block\Product\Widget\Conditions
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ConditionsTest extends \PHPUnit\Framework\TestCase
class ConditionsTest extends TestCase
{
/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

/**
* @var Registry|\PHPUnit_Framework_MockObject_MockObject
* @var Registry|MockObject
*/
private $registryMock;

/**
* @var Context|\PHPUnit_Framework_MockObject_MockObject
* @var Context|MockObject
*/
private $contextMock;

/**
* @var Rule|\PHPUnit_Framework_MockObject_MockObject
* @var Rule|MockObject
*/
protected $ruleMock;

/**
* @var LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
* @var LayoutInterface|MockObject
*/
private $layoutMock;

/**
* @var BlockInterface|\PHPUnit_Framework_MockObject_MockObject
* @var BlockInterface|MockObject
*/
private $blockMock;

Expand All @@ -57,7 +71,7 @@ class ConditionsTest extends \PHPUnit\Framework\TestCase
/**
* return void
*/
protected function setUp()
protected function setUp(): void
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->ruleMock = $this->getMockBuilder(Rule::class)
Expand Down Expand Up @@ -113,8 +127,8 @@ public function testConstructWithWidgetInstance()
{
$widgetParams = ['conditions' => 'some conditions'];

/** @var \Magento\Widget\Model\Widget\Instance|\PHPUnit_Framework_MockObject_MockObject $widgetMock */
$widgetMock = $this->getMockBuilder(\Magento\Widget\Model\Widget\Instance::class)
/** @var Instance|MockObject $widgetMock */
$widgetMock = $this->getMockBuilder(Instance::class)
->disableOriginalConstructor()
->getMock();
$widgetMock->expects($this->once())
Expand Down Expand Up @@ -185,19 +199,19 @@ public function testRender()
{
$data = ['area' => 'backend'];
$abstractElementMock = $this->createPartialMock(
\Magento\Framework\Data\Form\Element\AbstractElement::class,
AbstractElement::class,
['getContainer']
);
$eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
$scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$fieldsetMock = $this->createMock(\Magento\Framework\Data\Form\Element\Fieldset::class);
$combineMock = $this->createMock(\Magento\Rule\Model\Condition\Combine::class);
$resolverMock = $this->createMock(\Magento\Framework\View\Element\Template\File\Resolver::class);
$filesystemMock = $this->createPartialMock(\Magento\Framework\Filesystem::class, ['getDirectoryRead']);
$validatorMock = $this->createMock(\Magento\Framework\View\Element\Template\File\Validator::class);
$templateEnginePoolMock = $this->createMock(\Magento\Framework\View\TemplateEnginePool::class);
$templateEngineMock = $this->createMock(\Magento\Framework\View\TemplateEngineInterface::class);
$directoryReadMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
$eventManagerMock = $this->createMock(ManagerInterface::class);
$scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
$fieldsetMock = $this->createMock(Fieldset::class);
$combineMock = $this->createMock(Combine::class);
$resolverMock = $this->createMock(Resolver::class);
$filesystemMock = $this->createPartialMock(Filesystem::class, ['getDirectoryRead']);
$validatorMock = $this->createMock(Validator::class);
$templateEnginePoolMock = $this->createMock(TemplateEnginePool::class);
$templateEngineMock = $this->createMock(TemplateEngineInterface::class);
$directoryReadMock = $this->createMock(ReadInterface::class);

$this->ruleMock->expects($this->once())->method('getConditions')->willReturn($combineMock);
$combineMock->expects($this->once())->method('setJsFormObject')->willReturnSelf();
Expand Down
Loading