-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from magento-dragons/PR-8
[Dragons] Bug Fixes
- Loading branch information
Showing
14 changed files
with
467 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
29 changes: 29 additions & 0 deletions
29
app/code/Magento/Catalog/Api/Data/CategorySearchResultsInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
138
app/code/Magento/Catalog/Test/Unit/Model/CategoryListTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.