Skip to content

Commit 7553225

Browse files
committed
[Catalog] Cover Component/FilterFactory by Unit Test
1 parent 1ad65a3 commit 7553225

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\Ui\Component;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use Magento\Catalog\Ui\Component\FilterFactory;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
13+
use Magento\Framework\View\Element\UiComponentFactory;
14+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
15+
use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface;
16+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
17+
18+
class FilterFactoryTest extends TestCase
19+
{
20+
/**
21+
* @var FilterFactory
22+
*/
23+
private $filterFactory;
24+
25+
/**
26+
* @var UiComponentFactory|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $componentFactoryMock;
29+
30+
/**
31+
* Setup environment for test
32+
*/
33+
protected function setUp()
34+
{
35+
$objectManager = new ObjectManagerHelper($this);
36+
37+
$this->componentFactoryMock = $this->createMock(UiComponentFactory::class);
38+
39+
$this->filterFactory = $objectManager->getObject(
40+
FilterFactory::class,
41+
[
42+
'componentFactory' => $this->componentFactoryMock
43+
]
44+
);
45+
}
46+
47+
/**
48+
* Test create() with use source attribute
49+
*/
50+
public function testCreateWithUseSourceAttribute()
51+
{
52+
$contextMock = $this->createMock(ContextInterface::class);
53+
$attributeMock = $this->getMockBuilder(ProductAttributeInterface::class)
54+
->setMethods(['usesSource', 'getSource'])
55+
->getMockForAbstractClass();
56+
$attributeMock->method('getAttributeCode')->willReturn('color');
57+
$attributeMock->method('getDefaultFrontendLabel')->willReturn('Color');
58+
$attributeMock->method('usesSource')->willReturn(true);
59+
$attributeMock->method('getSourceModel')->willReturn('getSourceModel value');
60+
$attributeMock->method('getFrontendInput')->willReturn('select');
61+
$sourceMock = $this->createMock(SourceInterface::class);
62+
$attributeMock->method('getSource')->willReturn($sourceMock);
63+
$sourceMock->method('getAllOptions')->willReturn(
64+
[
65+
[
66+
'value' => 1,
67+
'label' => 'Black',
68+
],
69+
[
70+
'value' => 2,
71+
'label' => 'White',
72+
]
73+
]
74+
);
75+
$this->componentFactoryMock->expects($this->once())
76+
->method('create')
77+
->with('color', 'filterSelect', [
78+
'data' => [
79+
'config' => [
80+
'options' => [
81+
[
82+
'value' => 1,
83+
'label' => 'Black',
84+
],
85+
[
86+
'value' => 2,
87+
'label' => 'White',
88+
]
89+
],
90+
'caption' => (string)__('Select...'),
91+
'dataScope' => 'color',
92+
'label' => (string)__('Color'),
93+
]
94+
],
95+
'context' => $contextMock
96+
]);
97+
98+
$this->filterFactory->create($attributeMock, $contextMock);
99+
}
100+
}

0 commit comments

Comments
 (0)