Skip to content

Commit

Permalink
Merge pull request #773 from magento-tsg/2.0-pr10
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.0 (pr10)
  • Loading branch information
kandy authored Jan 24, 2017
2 parents 1c4d908 + 67ed003 commit a4d9976
Show file tree
Hide file tree
Showing 20 changed files with 530 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Catalog\Model\ResourceModel\Category\Collection;

/**
* Category collection factory.
*/
class Factory
{
/**
Expand Down
25 changes: 24 additions & 1 deletion app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Catalog\Model\ResourceModel\Category;

use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory as CategoryFlatCollectionFactory;
use Magento\Framework\App\ObjectManager;

/**
* Category flat model
Expand Down Expand Up @@ -68,6 +70,7 @@ class Flat extends \Magento\Indexer\Model\ResourceModel\AbstractResource
* Category collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
* @deprecated
*/
protected $_categoryCollectionFactory;

Expand All @@ -78,6 +81,11 @@ class Flat extends \Magento\Indexer\Model\ResourceModel\AbstractResource
*/
protected $_categoryFactory;

/**
* @var CategoryFlatCollectionFactory
*/
private $categoryFlatCollectionFactory;

/**
* Class constructor
*
Expand Down Expand Up @@ -399,7 +407,7 @@ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $as
);
$parentPath = $this->getConnection()->fetchOne($select);

$collection = $this->_categoryCollectionFactory
$collection = $this->getCategoryFlatCollectionFactory()
->create()
->addNameToResult()
->addUrlRewriteToResult()
Expand Down Expand Up @@ -690,4 +698,19 @@ public function getProductsPosition($category)

return $this->getConnection()->fetchPairs($select, $bind);
}

/**
* Get instance of CategoryFlatCollectionFactory
*
* @return CategoryFlatCollectionFactory
*/
private function getCategoryFlatCollectionFactory()
{
if (!$this->categoryFlatCollectionFactory instanceof CategoryFlatCollectionFactory) {
$this->categoryFlatCollectionFactory = ObjectManager::getInstance()
->get(CategoryFlatCollectionFactory::class);
}

return $this->categoryFlatCollectionFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category;

use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\Flat\Collection;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Catalog\Model\ResourceModel\Category\Flat;
use Magento\Framework\DB\Select;
use Magento\Framework\DB\Adapter\AdapterInterface as Adapter;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

/**
* Category flat model test
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class FlatTest extends \PHPUnit_Framework_TestCase
{
const STORE_ID = 1;
const TABLE_NAME = 'test_table';
const PARENT_PATH = '1';
const SORTED = false;
const PARENT = 1;
const RECURSION_LEVEL = 0;

/**
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $categoryCollectionFactoryMock;

/**
* @var Collection|\PHPUnit_Framework_MockObject_MockObject
*/
private $categoryCollectionMock;

/**
* @var Flat
*/
private $model;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var Select|\PHPUnit_Framework_MockObject_MockObject
*/
private $selectMock;

/**
* @var Adapter|\PHPUnit_Framework_MockObject_MockObject
*/
private $connectionMock;

/**
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
*/
private $resourceMock;

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

/**
* @var Store|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeMock;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->objectManager = new ObjectManager($this);

$this->selectMock = $this->getMockBuilder(Select::class)
->disableOriginalConstructor()
->setMethods(['where', 'from'])
->getMock();
$this->selectMock->expects($this->once())
->method('where')
->willReturn($this->selectMock);
$this->selectMock->expects($this->once())
->method('from')
->willReturn($this->selectMock);
$this->connectionMock = $this->getMockBuilder(Adapter::class)
->getMockForAbstractClass();
$this->connectionMock->expects($this->once())
->method('select')
->willReturn($this->selectMock);
$this->connectionMock->expects($this->once())
->method('fetchOne')
->with($this->selectMock)
->willReturn(self::PARENT_PATH);
$this->resourceMock = $this->getMockBuilder(ResourceConnection::class)
->disableOriginalConstructor()
->setMethods(['getConnection', 'getTableName'])
->getMock();
$this->resourceMock->expects($this->any())
->method('getConnection')
->willReturn($this->connectionMock);
$this->resourceMock->expects($this->any())
->method('getTableName')
->willReturn(self::TABLE_NAME);
$this->contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->setMethods(['getResources'])
->getMock();
$this->contextMock->expects($this->any())
->method('getResources')
->willReturn($this->resourceMock);

$this->storeMock = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()
->setMethods(['getId'])
->getMock();
$this->storeMock->expects($this->any())
->method('getId')
->willReturn(self::STORE_ID);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
$this->storeManagerMock->expects($this->any())
->method('getStore')
->willReturn($this->storeMock);
}

public function testGetCategories()
{
$this->categoryCollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->categoryCollectionMock = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->setMethods(
[
'addNameToResult',
'addUrlRewriteToResult',
'addParentPathFilter',
'addStoreFilter',
'addIsActiveFilter',
'addAttributeToFilter',
'addSortedField',
'load'
]
)
->getMock();
$this->categoryCollectionMock->expects($this->once())
->method('addNameToResult')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addUrlRewriteToResult')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addParentPathFilter')
->with(self::PARENT_PATH)
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addStoreFilter')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addIsActiveFilter')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addSortedField')
->with(self::SORTED)
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addAttributeToFilter')
->with('include_in_menu', 1)
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('load')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionFactoryMock->expects($this->once())
->method('create')
->willReturn($this->categoryCollectionMock);

$this->model = $this->objectManager->getObject(
Flat::class,
[
'context' => $this->contextMock,
'storeManager' => $this->storeManagerMock,
]
);

$reflection = new \ReflectionClass(get_class($this->model));
$reflectionProperty = $reflection->getProperty('categoryFlatCollectionFactory');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->model, $this->categoryCollectionFactoryMock);

$this->assertEquals(
$this->model->getCategories(self::PARENT, self::RECURSION_LEVEL, self::SORTED, true),
$this->categoryCollectionMock
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,19 @@ public function fill(FixtureInterface $fixture, SimpleElement $element = null)

return $this->fillTabs($tabs, $element);
}

/**
* Return category Id.
*
* @return string
*/
public function getCategoryId()
{
$categoryId = '';
if (preg_match('/\/id\/(?<id>\d+)(?:\/)?/', $this->browser->getUrl(), $matches)) {
$categoryId = $matches['id'];
}

return $categoryId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<strategy>css selector</strategy>
<wrapper>general</wrapper>
<fields>
<use_default_name>
<selector>#group_4name_default</selector>
<input>checkbox</input>
</use_default_name>
<is_active>
<input>select</input>
</is_active>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class AssertCategoryForm extends AbstractAssertForm
* @var array
*/
protected $skippedFixtureFields = [
'parent_id'
'parent_id',
'id'
];

/**
Expand Down
Loading

0 comments on commit a4d9976

Please sign in to comment.