Skip to content

Commit f924911

Browse files
author
Valeriy Nayda
committed
Merge branch 'MAGETWO-51716' into PR-5
2 parents 95a14c3 + 949c31f commit f924911

File tree

15 files changed

+640
-18
lines changed

15 files changed

+640
-18
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Catalog\Api;
8+
9+
interface AttributeSetFinderInterface
10+
{
11+
/**
12+
* Get attribute set ids by product ids
13+
*
14+
* @param array $productIds
15+
* @return array
16+
*/
17+
public function findAttributeSetIdsByProductIds(array $productIds);
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Product\Attribute;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
10+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
11+
use Magento\Catalog\Api\AttributeSetFinderInterface;
12+
use Magento\Framework\DB\Select;
13+
14+
class AttributeSetFinder implements AttributeSetFinderInterface
15+
{
16+
/**
17+
* @var CollectionFactory
18+
*/
19+
private $productCollectionFactory;
20+
21+
/**
22+
* @param CollectionFactory $productCollectionFactory
23+
*/
24+
public function __construct(CollectionFactory $productCollectionFactory)
25+
{
26+
$this->productCollectionFactory = $productCollectionFactory;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function findAttributeSetIdsByProductIds(array $productIds)
33+
{
34+
/** @var $collection Collection */
35+
$collection = $this->productCollectionFactory->create();
36+
$select = $collection
37+
->getSelect()
38+
->reset(Select::COLUMNS)
39+
->columns(ProductInterface::ATTRIBUTE_SET_ID)
40+
->where('entity_id IN (?)', $productIds)
41+
->group(ProductInterface::ATTRIBUTE_SET_ID);
42+
$result = $collection->getConnection()->fetchCol($select);
43+
return $result;
44+
}
45+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Catalog\Test\Unit\Model\Product\Attribute;
8+
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Catalog\Model\Product\Attribute\AttributeSetFinder;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\Framework\DB\Select;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
17+
class AttributeSetFinderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var Collection|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
protected $productCollection;
23+
24+
/**
25+
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
protected $productCollectionFactory;
28+
29+
/**
30+
* @var AttributeSetFinder
31+
*/
32+
protected $attributeSetFinder;
33+
34+
protected function setUp()
35+
{
36+
$this->productCollection = $this->getMockBuilder(Collection::class)
37+
->disableOriginalConstructor()
38+
->getMock();
39+
$this->productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
40+
->setMethods(['create'])
41+
->disableOriginalConstructor()
42+
->getMock();
43+
$this->productCollectionFactory->expects($this->once())->method('create')->willReturn($this->productCollection);
44+
45+
$this->attributeSetFinder = (new ObjectManager($this))->getObject(
46+
AttributeSetFinder::class,
47+
[
48+
'productCollectionFactory' => $this->productCollectionFactory,
49+
]
50+
);
51+
}
52+
53+
public function testFindAttributeIdsByProductIds()
54+
{
55+
$productIds = [1, 2, 3];
56+
$attributeSetIds = [3, 4, 6];
57+
58+
$select = $this->getMockBuilder(Select::class)
59+
->disableOriginalConstructor()
60+
->getMock();
61+
$select->expects($this->once())->method('reset')->with(Select::COLUMNS)->willReturnSelf();
62+
$select->expects($this->once())->method('columns')->with(ProductInterface::ATTRIBUTE_SET_ID)->willReturnSelf();
63+
$select->expects($this->once())->method('where')->with('entity_id IN (?)', $productIds)->willReturnSelf();
64+
$select->expects($this->once())->method('group')->with(ProductInterface::ATTRIBUTE_SET_ID)->willReturnSelf();
65+
66+
$connection = $this->getMock(AdapterInterface::class);
67+
$connection->expects($this->once())->method('fetchCol')->with($select)->willReturn($attributeSetIds);
68+
69+
$this->productCollection->expects($this->once())->method('getSelect')->willReturn($select);
70+
$this->productCollection->expects($this->once())->method('getConnection')->willReturn($connection);
71+
72+
$this->assertEquals($attributeSetIds, $this->attributeSetFinder->findAttributeSetIdsByProductIds($productIds));
73+
}
74+
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<preference for="Magento\Catalog\Api\AttributeSetManagementInterface" type="Magento\Catalog\Model\Product\Attribute\SetManagement" />
4444
<preference for="Magento\Catalog\Api\AttributeSetRepositoryInterface" type="Magento\Catalog\Model\Product\Attribute\SetRepository" />
4545
<preference for="Magento\Catalog\Api\ProductManagementInterface" type="Magento\Catalog\Model\ProductManagement" />
46+
<preference for="Magento\Catalog\Api\AttributeSetFinderInterface" type="Magento\Catalog\Model\Product\Attribute\AttributeSetFinder" />
4647
<type name="Magento\Customer\Model\ResourceModel\Visitor">
4748
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
4849
</type>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Model\Adapter\Aggregation;
7+
8+
use Magento\Catalog\Api\AttributeSetFinderInterface;
9+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\Search\Adapter\Aggregation\AggregationResolverInterface;
12+
use Magento\Framework\Search\Request\BucketInterface;
13+
use Magento\Framework\Search\Request\Config;
14+
use Magento\Framework\Search\RequestInterface;
15+
16+
class AggregationResolver implements AggregationResolverInterface
17+
{
18+
/**
19+
* @var AttributeSetFinderInterface
20+
*/
21+
private $attributeSetFinder;
22+
23+
/**
24+
* @var ProductAttributeRepositoryInterface
25+
*/
26+
private $productAttributeRepository;
27+
28+
/**
29+
* @var SearchCriteriaBuilder
30+
*/
31+
private $searchCriteriaBuilder;
32+
33+
/**
34+
* @var Config
35+
*/
36+
private $config;
37+
38+
/**
39+
* AggregationResolver constructor
40+
*
41+
* @param AttributeSetFinderInterface $attributeSetFinder
42+
* @param ProductAttributeRepositoryInterface $productAttributeRepository
43+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
44+
* @param Config $config
45+
*/
46+
public function __construct(
47+
AttributeSetFinderInterface $attributeSetFinder,
48+
ProductAttributeRepositoryInterface $productAttributeRepository,
49+
SearchCriteriaBuilder $searchCriteriaBuilder,
50+
Config $config
51+
) {
52+
$this->attributeSetFinder = $attributeSetFinder;
53+
$this->productAttributeRepository = $productAttributeRepository;
54+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
55+
$this->config = $config;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function resolve(RequestInterface $request, array $documentIds)
62+
{
63+
$data = $this->config->get($request->getName());
64+
65+
$bucketKeys = isset($data['aggregations']) ? array_keys($data['aggregations']) : [];
66+
$attributeCodes = $this->getApplicableAttributeCodes($documentIds);
67+
68+
$resolvedAggregation = array_filter(
69+
$request->getAggregation(),
70+
function ($bucket) use ($attributeCodes, $bucketKeys) {
71+
/** @var BucketInterface $bucket */
72+
return in_array($bucket->getField(), $attributeCodes) || in_array($bucket->getName(), $bucketKeys);
73+
}
74+
);
75+
return array_values($resolvedAggregation);
76+
}
77+
78+
/**
79+
* Get applicable attributes
80+
*
81+
* @param array $documentIds
82+
* @return array
83+
*/
84+
private function getApplicableAttributeCodes(array $documentIds)
85+
{
86+
$attributeSetIds = $this->attributeSetFinder->findAttributeSetIdsByProductIds($documentIds);
87+
88+
$searchCriteria = $this->searchCriteriaBuilder
89+
->addFilter('attribute_set_id', $attributeSetIds, 'in')
90+
->create();
91+
$result = $this->productAttributeRepository->getList($searchCriteria);
92+
93+
$attributeCodes = [];
94+
foreach ($result->getItems() as $attribute) {
95+
$attributeCodes[] = $attribute->getAttributeCode();
96+
}
97+
return $attributeCodes;
98+
}
99+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Test\Unit\Model\Adapter\Aggregation;
7+
8+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
9+
use Magento\Catalog\Api\Data\ProductAttributeSearchResultsInterface;
10+
use Magento\CatalogSearch\Model\Adapter\Aggregation\AggregationResolver;
11+
use Magento\Catalog\Api\AttributeSetFinderInterface;
12+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
13+
use Magento\Framework\Api\SearchCriteriaBuilder;
14+
use Magento\Framework\Api\SearchCriteriaInterface;
15+
use Magento\Framework\Search\Request\BucketInterface;
16+
use Magento\Framework\Search\Request\Config;
17+
use Magento\Framework\Search\RequestInterface;
18+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
19+
20+
class AggregationResolverTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var AttributeSetFinderInterface|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $attributeSetFinder;
26+
27+
/**
28+
* @var ProductAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $productAttributeRepository;
31+
32+
/**
33+
* @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
private $searchCriteriaBuilder;
36+
37+
/**
38+
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $request;
41+
42+
/**
43+
* @var Config|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $config;
46+
47+
/**
48+
* @var AggregationResolver
49+
*/
50+
private $aggregationResolver;
51+
52+
protected function setUp()
53+
{
54+
$this->attributeSetFinder = $this->getMock(AttributeSetFinderInterface::class);
55+
$this->productAttributeRepository = $this->getMock(ProductAttributeRepositoryInterface::class);
56+
$this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
57+
->disableOriginalConstructor()
58+
->getMock();
59+
$this->request = $this->getMock(RequestInterface::class);
60+
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
61+
62+
$this->aggregationResolver = (new ObjectManager($this))->getObject(
63+
AggregationResolver::class,
64+
[
65+
'attributeSetFinder' => $this->attributeSetFinder,
66+
'productAttributeRepository' => $this->productAttributeRepository,
67+
'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
68+
'config' => $this->config,
69+
]
70+
);
71+
}
72+
73+
public function testResolve()
74+
{
75+
$documentIds = [1, 2, 3];
76+
$attributeSetIds = [4, 5];
77+
$requestName = 'request_name';
78+
79+
$this->attributeSetFinder
80+
->expects($this->once())
81+
->method('findAttributeSetIdsByProductIds')
82+
->with($documentIds)
83+
->willReturn($attributeSetIds);
84+
85+
$searchCriteria = $this->getMock(SearchCriteriaInterface::class);
86+
87+
$this->searchCriteriaBuilder
88+
->expects($this->once())
89+
->method('addFilter')
90+
->with('attribute_set_id', $attributeSetIds, 'in')
91+
->willReturnSelf();
92+
$this->searchCriteriaBuilder
93+
->expects($this->once())
94+
->method('create')
95+
->willReturn($searchCriteria);
96+
97+
$attributeFirst = $this->getMock(ProductAttributeInterface::class);
98+
$attributeFirst->expects($this->once())
99+
->method('getAttributeCode')
100+
->willReturn('code_1');
101+
$attributeSecond = $this->getMock(ProductAttributeInterface::class);
102+
$attributeSecond->expects($this->once())
103+
->method('getAttributeCode')
104+
->willReturn('code_2');
105+
106+
$searchResult = $this->getMock(ProductAttributeSearchResultsInterface::class);
107+
$searchResult->expects($this->once())
108+
->method('getItems')
109+
->willReturn([$attributeFirst, $attributeSecond]);
110+
111+
$this->productAttributeRepository
112+
->expects($this->once())
113+
->method('getList')
114+
->with($searchCriteria)
115+
->willReturn($searchResult);
116+
117+
$bucketFirst = $this->getMock(BucketInterface::class);
118+
$bucketFirst->expects($this->once())
119+
->method('getField')
120+
->willReturn('code_1');
121+
$bucketSecond = $this->getMock(BucketInterface::class);
122+
$bucketSecond->expects($this->once())
123+
->method('getField')
124+
->willReturn('some_another_code');
125+
$bucketThird = $this->getMock(BucketInterface::class);
126+
$bucketThird->expects($this->once())
127+
->method('getName')
128+
->willReturn('custom_not_attribute_field');
129+
130+
$this->request->expects($this->once())
131+
->method('getAggregation')
132+
->willReturn([$bucketFirst, $bucketSecond, $bucketThird]);
133+
$this->request->expects($this->once())
134+
->method('getName')
135+
->willReturn($requestName);
136+
137+
$this->config->expects($this->once())
138+
->method('get')
139+
->with($requestName)
140+
->willReturn([
141+
'aggregations' => ['custom_not_attribute_field' => []],
142+
]);
143+
144+
$this->assertEquals(
145+
[$bucketFirst, $bucketThird],
146+
$this->aggregationResolver->resolve($this->request, $documentIds)
147+
);
148+
}
149+
}

0 commit comments

Comments
 (0)