Skip to content

Commit

Permalink
Merge pull request #27824 from lbajsarowicz/phpunit8/module-CatalogUr…
Browse files Browse the repository at this point in the history
…lRewrite

#27500 Prepare CatalogUrlRewrite module Tests for PHPUnit 8
  • Loading branch information
Oleksii Korshenko authored Apr 15, 2020
2 parents ea7af78 + 4e0f00e commit 7eed557
Show file tree
Hide file tree
Showing 41 changed files with 818 additions and 645 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category;

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\Category\CanonicalUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class CanonicalUrlRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
class CanonicalUrlRewriteGeneratorTest extends TestCase
{
/** @var \Magento\CatalogUrlRewrite\Model\Category\CanonicalUrlRewriteGenerator */
/** @var CanonicalUrlRewriteGenerator */
protected $canonicalUrlRewriteGenerator;

/** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */
/** @var CategoryUrlPathGenerator|MockObject */
protected $categoryUrlPathGenerator;

/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
/** @var Category|MockObject */
protected $category;

/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var UrlRewriteFactory|MockObject */
protected $urlRewriteFactory;

/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */
/** @var UrlRewrite|MockObject */
protected $urlRewrite;

protected function setUp()
protected function setUp(): void
{
$this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
$this->urlRewriteFactory = $this->getMockBuilder(UrlRewriteFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()->getMock();
$this->urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
$this->urlRewrite = $this->getMockBuilder(UrlRewrite::class)
->disableOriginalConstructor()->getMock();
$this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
$this->category = $this->getMockBuilder(Category::class)
->disableOriginalConstructor()->getMock();
$this->categoryUrlPathGenerator = $this->getMockBuilder(
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class
CategoryUrlPathGenerator::class
)->disableOriginalConstructor()->getMock();
$this->canonicalUrlRewriteGenerator = (new ObjectManager($this))->getObject(
\Magento\CatalogUrlRewrite\Model\Category\CanonicalUrlRewriteGenerator::class,
CanonicalUrlRewriteGenerator::class,
[
'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator,
'urlRewriteFactory' => $this->urlRewriteFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ChildrenCategoriesProviderTest extends \PHPUnit\Framework\TestCase
class ChildrenCategoriesProviderTest extends TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
protected $category;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
protected $select;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
protected $connection;

/** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider */
/** @var ChildrenCategoriesProvider */
protected $childrenCategoriesProvider;

protected function setUp()
protected function setUp(): void
{
$this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
$this->category = $this->getMockBuilder(Category::class)
->disableOriginalConstructor()
->setMethods(['getPath', 'getResourceCollection', 'getResource', 'getLevel', '__wakeup', 'isObjectNew'])
->getMock();
$categoryCollection = $this->getMockBuilder(
\Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection::class
AbstractCollection::class
)->disableOriginalConstructor()->setMethods(['addAttributeToSelect', 'addIdFilter'])->getMock();
$this->category->expects($this->any())->method('getPath')->willReturn('category-path');
$this->category->expects($this->any())->method('getResourceCollection')->willReturn($categoryCollection);
$categoryCollection->expects($this->any())->method('addAttributeToSelect')->willReturnSelf();
$categoryCollection->expects($this->any())->method('addIdFilter')->with(['id'])->willReturnSelf();
$this->select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
$this->select = $this->getMockBuilder(Select::class)
->disableOriginalConstructor()->setMethods(['from', 'where', 'deleteFromSelect'])->getMock();
$this->connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
$this->connection = $this->createMock(AdapterInterface::class);
$categoryResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category::class)
->disableOriginalConstructor()->getMock();
$this->category->expects($this->any())->method('getResource')->willReturn($categoryResource);
Expand All @@ -47,7 +54,7 @@ protected function setUp()
$this->select->expects($this->any())->method('from')->willReturnSelf();

$this->childrenCategoriesProvider = (new ObjectManager($this))->getObject(
\Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider::class
ChildrenCategoriesProvider::class
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,81 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryRepository;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\UrlRewrite\Model\MergeDataProvider;
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ChildrenUrlRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ChildrenUrlRewriteGeneratorTest extends TestCase
{
/** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator */
/** @var ChildrenUrlRewriteGenerator */
private $childrenUrlRewriteGenerator;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $category;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $childrenCategoriesProvider;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $categoryUrlRewriteGeneratorFactory;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $categoryUrlRewriteGenerator;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $mergeDataProvider;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $serializerMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $categoryRepository;

protected function setUp()
protected function setUp(): void
{
$this->serializerMock = $this->getMockBuilder(Json::class)
->disableOriginalConstructor()
->getMock();
$this->childrenCategoriesProvider = $this->getMockBuilder(
\Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider::class
ChildrenCategoriesProvider::class
)->disableOriginalConstructor()->getMock();
$this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
$this->category = $this->getMockBuilder(Category::class)
->disableOriginalConstructor()->getMock();
$this->categoryUrlRewriteGeneratorFactory = $this->getMockBuilder(
\Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory::class
CategoryUrlRewriteGeneratorFactory::class
)->disableOriginalConstructor()->setMethods(['create'])->getMock();
$this->categoryUrlRewriteGenerator = $this->getMockBuilder(
\Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::class
CategoryUrlRewriteGenerator::class
)->disableOriginalConstructor()->getMock();
$this->categoryRepository = $this->getMockBuilder(
\Magento\Catalog\Model\CategoryRepository::class
CategoryRepository::class
)->disableOriginalConstructor()->getMock();
$mergeDataProviderFactory = $this->createPartialMock(
\Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
MergeDataProviderFactory::class,
['create']
);
$this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider();
$this->mergeDataProvider = new MergeDataProvider();
$mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider);

$this->childrenUrlRewriteGenerator = (new ObjectManager($this))->getObject(
\Magento\CatalogUrlRewrite\Model\Category\ChildrenUrlRewriteGenerator::class,
ChildrenUrlRewriteGenerator::class,
[
'childrenCategoriesProvider' => $this->childrenCategoriesProvider,
'categoryUrlRewriteGeneratorFactory' => $this->categoryUrlRewriteGeneratorFactory,
Expand All @@ -85,7 +99,7 @@ public function testGenerate()
$saveRewritesHistory = 'flag';
$childId = 2;

$childCategory = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
$childCategory = $this->getMockBuilder(Category::class)
->disableOriginalConstructor()->getMock();
$childCategory->expects($this->once())->method('setData')
->with('save_rewrites_history', $saveRewritesHistory);
Expand All @@ -97,13 +111,13 @@ public function testGenerate()
->will($this->returnValue($saveRewritesHistory));
$this->categoryUrlRewriteGeneratorFactory->expects($this->once())->method('create')
->will($this->returnValue($this->categoryUrlRewriteGenerator));
$url1 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializerMock);
$url1 = new UrlRewrite([], $this->serializerMock);
$url1->setRequestPath('category-1')
->setStoreId(1);
$url2 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializerMock);
$url2 = new UrlRewrite([], $this->serializerMock);
$url2->setRequestPath('category-2')
->setStoreId(2);
$url3 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializerMock);
$url3 = new UrlRewrite([], $this->serializerMock);
$url3->setRequestPath('category-1')
->setStoreId(1);
$this->categoryUrlRewriteGenerator->expects($this->once())->method('generate')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,76 @@
<?php
<?php declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category;

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\UrlRewrite\Model\MergeDataProvider;
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
use Magento\UrlRewrite\Model\OptionProvider;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class CurrentUrlRewritesRegeneratorTest extends \PHPUnit\Framework\TestCase
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CurrentUrlRewritesRegeneratorTest extends TestCase
{
/** @var \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator */
/** @var CurrentUrlRewritesRegenerator */
private $currentUrlRewritesRegenerator;

/** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */
/** @var CategoryUrlPathGenerator|MockObject */
private $categoryUrlPathGenerator;

/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
/** @var Category|MockObject */
private $category;

/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var UrlRewriteFactory|MockObject */
private $urlRewriteFactory;

/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */
/** @var UrlRewrite|MockObject */
private $urlRewrite;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $mergeDataProvider;

/** @var \PHPUnit_Framework_MockObject_MockObject */
/** @var MockObject */
private $urlRewriteFinder;

protected function setUp()
protected function setUp(): void
{
$this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
$this->urlRewriteFactory = $this->getMockBuilder(UrlRewriteFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()->getMock();
$this->urlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
$this->urlRewrite = $this->getMockBuilder(UrlRewrite::class)
->disableOriginalConstructor()->getMock();
$this->category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
$this->category = $this->getMockBuilder(Category::class)
->disableOriginalConstructor()->getMock();
$this->categoryUrlPathGenerator = $this->getMockBuilder(
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class
CategoryUrlPathGenerator::class
)->disableOriginalConstructor()->getMock();
$this->urlRewriteFinder = $this->getMockBuilder(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class)
$this->urlRewriteFinder = $this->getMockBuilder(UrlRewriteFinder::class)
->disableOriginalConstructor()->getMock();
$this->urlRewriteFactory->expects($this->once())->method('create')
->willReturn($this->urlRewrite);
$mergeDataProviderFactory = $this->createPartialMock(
\Magento\UrlRewrite\Model\MergeDataProviderFactory::class,
MergeDataProviderFactory::class,
['create']
);
$this->mergeDataProvider = new \Magento\UrlRewrite\Model\MergeDataProvider;
$this->mergeDataProvider = new MergeDataProvider();
$mergeDataProviderFactory->expects($this->once())->method('create')->willReturn($this->mergeDataProvider);

$this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject(
\Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator::class,
CurrentUrlRewritesRegenerator::class,
[
'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator,
'urlRewriteFactory' => $this->urlRewriteFactory,
Expand Down Expand Up @@ -247,8 +259,8 @@ protected function getCurrentRewritesMocks($currentRewrites)
{
$rewrites = [];
foreach ($currentRewrites as $urlRewrite) {
/** @var \PHPUnit_Framework_MockObject_MockObject */
$url = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
/** @var MockObject */
$url = $this->getMockBuilder(UrlRewrite::class)
->disableOriginalConstructor()->getMock();
foreach ($urlRewrite as $key => $value) {
$url->expects($this->any())
Expand Down
Loading

0 comments on commit 7eed557

Please sign in to comment.