Skip to content

Commit

Permalink
Merge pull request #198 from magento-dragons/PR-8
Browse files Browse the repository at this point in the history
[Dragons] Bug Fixes
  • Loading branch information
Alexander Akimov authored Jul 29, 2016
2 parents c800ca3 + f818be8 commit f175469
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 38 deletions.
20 changes: 20 additions & 0 deletions app/code/Magento/Catalog/Api/CategoryListInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Api;

/**
* @api
*/
interface CategoryListInterface
{
/**
* Get category list
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\Catalog\Api\Data\CategorySearchResultsInterface
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Api\Data;

use Magento\Framework\Api\SearchResultsInterface;

/**
* @api
*/
interface CategorySearchResultsInterface extends SearchResultsInterface
{
/**
* Get categories
*
* @return \Magento\Catalog\Api\Data\CategoryInterface[]
*/
public function getItems();

/**
* Set categories
*
* @param \Magento\Catalog\Api\Data\CategoryInterface[] $items
* @return $this
*/
public function setItems(array $items);
}
119 changes: 119 additions & 0 deletions app/code/Magento/Catalog/Model/CategoryList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model;

use Magento\Catalog\Api\CategoryListInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategorySearchResultsInterface;
use Magento\Catalog\Api\Data\CategorySearchResultsInterfaceFactory;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\App\ObjectManager;

class CategoryList implements CategoryListInterface
{
/**
* @var CollectionFactory
*/
private $categoryCollectionFactory;

/**
* @var JoinProcessorInterface
*/
private $extensionAttributesJoinProcessor;

/**
* @var CategorySearchResultsInterfaceFactory
*/
private $categorySearchResultsFactory;

/**
* @var CategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @param CollectionFactory $categoryCollectionFactory
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
* @param CategorySearchResultsInterfaceFactory $categorySearchResultsFactory
* @param CategoryRepositoryInterface $categoryRepository
*/
public function __construct(
CollectionFactory $categoryCollectionFactory,
JoinProcessorInterface $extensionAttributesJoinProcessor,
CategorySearchResultsInterfaceFactory $categorySearchResultsFactory,
CategoryRepositoryInterface $categoryRepository
) {
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
$this->categorySearchResultsFactory = $categorySearchResultsFactory;
$this->categoryRepository = $categoryRepository;
}

/**
* {@inheritdoc}
*/
public function getList(SearchCriteriaInterface $searchCriteria)
{
/** @var Collection $collection */
$collection = $this->categoryCollectionFactory->create();
$this->extensionAttributesJoinProcessor->process($collection);

foreach ($searchCriteria->getFilterGroups() as $group) {
$this->addFilterGroupToCollection($group, $collection);
}

/** @var SortOrder $sortOrder */
$sortOrders = $searchCriteria->getSortOrders();
if ($sortOrders) {
foreach ($sortOrders as $sortOrder) {
$collection->addOrder(
$sortOrder->getField(),
($sortOrder->getDirection() === SortOrder::SORT_ASC) ? SortOrder::SORT_ASC : SortOrder::SORT_DESC
);
}
}

$collection->setCurPage($searchCriteria->getCurrentPage());
$collection->setPageSize($searchCriteria->getPageSize());

$items = [];
foreach ($collection->getAllIds() as $id) {
$items[] = $this->categoryRepository->get($id);
}

/** @var CategorySearchResultsInterface $searchResult */
$searchResult = $this->categorySearchResultsFactory->create();
$searchResult->setSearchCriteria($searchCriteria);
$searchResult->setItems($items);
$searchResult->setTotalCount($collection->getSize());
return $searchResult;
}

/**
* Add filter group to collection
*
* @param FilterGroup $filterGroup
* @param Collection $collection
* @return void
*/
private function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
{
$filters = $filterGroup->getFilters();
if ($filters) {
$fields = [];
foreach ($filters as $filter) {
$conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
$fields[] = ['attribute' => $filter->getField(), $conditionType => $filter->getValue()];
}
$collection->addFieldToFilter($fields);
}
}
}
138 changes: 138 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Model/CategoryListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Test\Unit\Model;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategorySearchResultsInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryList;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\Filter;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Catalog\Api\Data\CategorySearchResultsInterfaceFactory;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CategoryListTest extends \PHPUnit_Framework_TestCase
{
/**
* @var CategoryList
*/
protected $model;

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

/**
* @var JoinProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $extensionAttributesJoinProcessor;

/**
* @var CategorySearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $categorySearchResultsFactory;

/**
* @var CategoryRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $categoryRepository;

protected function setUp()
{
$this->categoryCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->extensionAttributesJoinProcessor = $this->getMock(JoinProcessorInterface::class);
$this->categorySearchResultsFactory = $this->getMockBuilder(CategorySearchResultsInterfaceFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->categoryRepository = $this->getMock(CategoryRepositoryInterface::class);

$this->model = (new ObjectManager($this))->getObject(
CategoryList::class,
[
'categoryCollectionFactory' => $this->categoryCollectionFactory,
'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessor,
'categorySearchResultsFactory' => $this->categorySearchResultsFactory,
'categoryRepository' => $this->categoryRepository,
]
);
}

public function testGetList()
{
$fieldName = 'field_1';
$value = 'value_1';
$conditionType = 'eq';
$currentPage = 2;
$pageSize = 1;
$totalCount = 2;
$categoryIdFirst = 1;
$categoryIdSecond = 2;

$categoryFirst = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock();
$categorySecond = $this->getMockBuilder(Category::class)->disableOriginalConstructor()->getMock();

$filter = $this->getMockBuilder(Filter::class)->disableOriginalConstructor()->getMock();
$filter->expects($this->atLeastOnce())->method('getConditionType')->willReturn($conditionType);
$filter->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
$filter->expects($this->once())->method('getValue')->willReturn($value);

$filterGroup = $this->getMockBuilder(FilterGroup::class)->disableOriginalConstructor()->getMock();
$filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]);

$sortOrder = $this->getMockBuilder(SortOrder::class)->disableOriginalConstructor()->getMock();
$sortOrder->expects($this->once())->method('getField')->willReturn($fieldName);
$sortOrder->expects($this->once())->method('getDirection')->willReturn(SortOrder::SORT_ASC);

/** @var SearchCriteriaInterface|\PHPUnit_Framework_MockObject_MockObject $searchCriteria */
$searchCriteria = $this->getMock(SearchCriteriaInterface::class);
$searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]);
$searchCriteria->expects($this->once())->method('getCurrentPage')->willReturn($currentPage);
$searchCriteria->expects($this->once())->method('getPageSize')->willReturn($pageSize);
$searchCriteria->expects($this->once())->method('getSortOrders')->willReturn([$sortOrder]);

$collection = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock();
$collection->expects($this->once())
->method('addFieldToFilter')
->with([['attribute' => $fieldName, $conditionType => $value]]);
$collection->expects($this->once())->method('addOrder')->with($fieldName, SortOrder::SORT_ASC);
$collection->expects($this->once())->method('setCurPage')->with($currentPage);
$collection->expects($this->once())->method('setPageSize')->with($pageSize);
$collection->expects($this->once())->method('getSize')->willReturn($totalCount);
$collection->expects($this->once())->method('getAllIds')->willReturn([$categoryIdFirst, $categoryIdSecond]);

$searchResult = $this->getMock(CategorySearchResultsInterface::class);
$searchResult->expects($this->once())->method('setSearchCriteria')->with($searchCriteria);
$searchResult->expects($this->once())->method('setItems')->with([$categoryFirst, $categorySecond]);
$searchResult->expects($this->once())->method('setTotalCount')->with($totalCount);

$this->categoryRepository->expects($this->exactly(2))
->method('get')
->willReturnMap([
[$categoryIdFirst, $categoryFirst],
[$categoryIdSecond, $categorySecond],
])
->willReturn($categoryFirst);

$this->categorySearchResultsFactory->expects($this->once())->method('create')->willReturn($searchResult);
$this->categoryCollectionFactory->expects($this->once())->method('create')->willReturn($collection);
$this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection);

$this->assertEquals($searchResult, $this->model->getList($searchCriteria));
}
}
46 changes: 15 additions & 31 deletions app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
*/
namespace Magento\Catalog\Test\Unit\Model;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class CategoryRepositoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Catalog\Model\CategoryRepository
* @var CategoryRepository
*/
protected $model;

Expand Down Expand Up @@ -88,21 +93,16 @@ protected function setUp()
->with(\Magento\Catalog\Api\Data\CategoryInterface::class)
->willReturn($metadataMock);

$this->model = new \Magento\Catalog\Model\CategoryRepository(
$this->categoryFactoryMock,
$this->categoryResourceMock,
$this->storeManagerMock
$this->model = (new ObjectManager($this))->getObject(
CategoryRepository::class,
[
'categoryFactory' => $this->categoryFactoryMock,
'categoryResource' => $this->categoryResourceMock,
'storeManager' => $this->storeManagerMock,
'metadataPool' => $this->metadataPoolMock,
'extensibleDataObjectConverter' => $this->extensibleDataObjectConverterMock,
]
);

$this->setProperties($this->model, [
'metadataPool' => $this->metadataPoolMock
]);

// Todo: \Magento\Framework\TestFramework\Unit\Helper\ObjectManager to do this automatically (MAGETWO-49793)
$reflection = new \ReflectionClass(get_class($this->model));
$reflectionProperty = $reflection->getProperty('extensibleDataObjectConverter');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->model, $this->extensibleDataObjectConverterMock);
}

public function testGet()
Expand Down Expand Up @@ -370,20 +370,4 @@ public function testDeleteByIdentifierWithException()
);
$this->model->deleteByIdentifier($categoryId);
}

/**
* @param $object
* @param array $properties
*/
private function setProperties($object, $properties = [])
{
$reflectionClass = new \ReflectionClass(get_class($object));
foreach ($properties as $key => $value) {
if ($reflectionClass->hasProperty($key)) {
$reflectionProperty = $reflectionClass->getProperty($key);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, $value);
}
}
}
}
2 changes: 2 additions & 0 deletions app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<preference for="Magento\Catalog\Api\AttributeSetRepositoryInterface" type="Magento\Catalog\Model\Product\Attribute\SetRepository" />
<preference for="Magento\Catalog\Api\ProductManagementInterface" type="Magento\Catalog\Model\ProductManagement" />
<preference for="Magento\Catalog\Api\AttributeSetFinderInterface" type="Magento\Catalog\Model\Product\Attribute\AttributeSetFinder" />
<preference for="Magento\Catalog\Api\CategoryListInterface" type="Magento\Catalog\Model\CategoryList" />
<preference for="Magento\Catalog\Api\Data\CategorySearchResultsInterface" type="Magento\Framework\Api\SearchResults" />
<type name="Magento\Customer\Model\ResourceModel\Visitor">
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
</type>
Expand Down
Loading

0 comments on commit f175469

Please sign in to comment.