|
| 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 Magento\Catalog\Api\Data\ProductAttributeInterface; |
| 11 | +use Magento\Catalog\Ui\Component\FilterFactory; |
| 12 | +use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 14 | +use Magento\Framework\View\Element\UiComponent\ContextInterface; |
| 15 | +use Magento\Framework\View\Element\UiComponentFactory; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +class FilterFactoryTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Stub attribute |
| 23 | + */ |
| 24 | + private const STUB_ATTRIBUTE = [ |
| 25 | + 'attribute_code' => 'color', |
| 26 | + 'default_frontend_label' => 'Color', |
| 27 | + 'uses_source' => 'Color', |
| 28 | + 'source_model' => 'getSourceModel value', |
| 29 | + 'frontend_input' => 'select', |
| 30 | + 'all_options' => [ |
| 31 | + [ |
| 32 | + 'value' => 1, |
| 33 | + 'label' => 'Black', |
| 34 | + ], |
| 35 | + [ |
| 36 | + 'value' => 2, |
| 37 | + 'label' => 'White', |
| 38 | + ] |
| 39 | + ] |
| 40 | + ]; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var FilterFactory |
| 44 | + */ |
| 45 | + private $filterFactory; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var UiComponentFactory|MockObject |
| 49 | + */ |
| 50 | + private $componentFactoryMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * Setup environment for test |
| 54 | + */ |
| 55 | + protected function setUp() |
| 56 | + { |
| 57 | + $objectManager = new ObjectManager($this); |
| 58 | + |
| 59 | + $this->componentFactoryMock = $this->createMock(UiComponentFactory::class); |
| 60 | + |
| 61 | + $this->filterFactory = $objectManager->getObject( |
| 62 | + FilterFactory::class, |
| 63 | + [ |
| 64 | + 'componentFactory' => $this->componentFactoryMock |
| 65 | + ] |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test create() with use source attribute |
| 71 | + */ |
| 72 | + public function testCreateWithUseSourceAttribute() |
| 73 | + { |
| 74 | + $contextMock = $this->createMock(ContextInterface::class); |
| 75 | + $attributeMock = $this->getMockBuilder(ProductAttributeInterface::class) |
| 76 | + ->setMethods(['usesSource', 'getSource']) |
| 77 | + ->getMockForAbstractClass(); |
| 78 | + $attributeMock->method('getAttributeCode')->willReturn(self::STUB_ATTRIBUTE['attribute_code']); |
| 79 | + $attributeMock->method('getDefaultFrontendLabel') |
| 80 | + ->willReturn(self::STUB_ATTRIBUTE['default_frontend_label']); |
| 81 | + $attributeMock->method('usesSource')->willReturn(self::STUB_ATTRIBUTE['uses_source']); |
| 82 | + $attributeMock->method('getSourceModel')->willReturn(self::STUB_ATTRIBUTE['source_model']); |
| 83 | + $attributeMock->method('getFrontendInput')->willReturn(self::STUB_ATTRIBUTE['frontend_input']); |
| 84 | + $sourceMock = $this->createMock(SourceInterface::class); |
| 85 | + $attributeMock->method('getSource')->willReturn($sourceMock); |
| 86 | + $sourceMock->method('getAllOptions')->willReturn(self::STUB_ATTRIBUTE['all_options']); |
| 87 | + $this->componentFactoryMock->expects($this->once()) |
| 88 | + ->method('create') |
| 89 | + ->with(self::STUB_ATTRIBUTE['attribute_code'], 'filterSelect', [ |
| 90 | + 'data' => [ |
| 91 | + 'config' => [ |
| 92 | + 'options' => self::STUB_ATTRIBUTE['all_options'], |
| 93 | + 'caption' => (string)__('Select...'), |
| 94 | + 'dataScope' => self::STUB_ATTRIBUTE['attribute_code'], |
| 95 | + 'label' => self::STUB_ATTRIBUTE['default_frontend_label'], |
| 96 | + ] |
| 97 | + ], |
| 98 | + 'context' => $contextMock |
| 99 | + ]); |
| 100 | + |
| 101 | + $this->filterFactory->create($attributeMock, $contextMock); |
| 102 | + } |
| 103 | +} |
0 commit comments