|
| 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\CatalogGraphQl\Test\Unit\Model\Config; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection as AttributeCollection; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory; |
| 13 | +use Magento\CatalogGraphQl\Model\Config\FilterAttributeReader; |
| 14 | +use Magento\Framework\GraphQl\Schema\Type\Entity\MapperInterface; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class FilterAttributeReaderTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var MapperInterface|MockObject |
| 22 | + */ |
| 23 | + private $mapperMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var CollectionFactory|MockObject |
| 27 | + */ |
| 28 | + private $collectionFactoryMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var FilterAttributeReader |
| 32 | + */ |
| 33 | + private $model; |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->mapperMock = $this->createMock(MapperInterface::class); |
| 38 | + $this->collectionFactoryMock = $this->createMock(AttributeCollectionFactory::class); |
| 39 | + $this->model = new FilterAttributeReader($this->mapperMock, $this->collectionFactoryMock); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @dataProvider readDataProvider |
| 44 | + * @param string $filterableAttrCode |
| 45 | + * @param string $filterableAttrInput |
| 46 | + * @param string $searchableAttrCode |
| 47 | + * @param string $searchableAttrInput |
| 48 | + * @param array $fieldsType |
| 49 | + */ |
| 50 | + public function testRead( |
| 51 | + string $filterableAttrCode, |
| 52 | + string $filterableAttrInput, |
| 53 | + string $searchableAttrCode, |
| 54 | + string $searchableAttrInput, |
| 55 | + array $fieldsType |
| 56 | + ): void { |
| 57 | + $this->mapperMock->expects(self::once()) |
| 58 | + ->method('getMappedTypes') |
| 59 | + ->with('filter_attributes') |
| 60 | + ->willReturn(['product_filter_attributes' => 'ProductAttributeFilterInput']); |
| 61 | + |
| 62 | + $filterableAttributeCollection = $this->createMock(AttributeCollection::class); |
| 63 | + $filterableAttributeCollection->expects(self::once()) |
| 64 | + ->method('addHasOptionsFilter') |
| 65 | + ->willReturnSelf(); |
| 66 | + $filterableAttributeCollection->expects(self::once()) |
| 67 | + ->method('addIsFilterableFilter') |
| 68 | + ->willReturnSelf(); |
| 69 | + $filterableAttribute = $this->createMock(Attribute::class); |
| 70 | + $filterableAttributeCollection->expects(self::once()) |
| 71 | + ->method('getItems') |
| 72 | + ->willReturn(array_filter([11 => $filterableAttribute])); |
| 73 | + $searchableAttributeCollection = $this->createMock(AttributeCollection::class); |
| 74 | + $searchableAttributeCollection->expects(self::once()) |
| 75 | + ->method('addHasOptionsFilter') |
| 76 | + ->willReturnSelf(); |
| 77 | + $searchableAttributeCollection->expects(self::once()) |
| 78 | + ->method('addIsSearchableFilter') |
| 79 | + ->willReturnSelf(); |
| 80 | + $searchableAttributeCollection->expects(self::once()) |
| 81 | + ->method('addDisplayInAdvancedSearchFilter') |
| 82 | + ->willReturnSelf(); |
| 83 | + $searchableAttribute = $this->createMock(Attribute::class); |
| 84 | + $searchableAttributeCollection->expects(self::once()) |
| 85 | + ->method('getItems') |
| 86 | + ->willReturn(array_filter([21 => $searchableAttribute])); |
| 87 | + $this->collectionFactoryMock->expects(self::exactly(2)) |
| 88 | + ->method('create') |
| 89 | + ->willReturnOnConsecutiveCalls($filterableAttributeCollection, $searchableAttributeCollection); |
| 90 | + |
| 91 | + $filterableAttribute->method('getAttributeCode') |
| 92 | + ->willReturn($filterableAttrCode); |
| 93 | + $filterableAttribute->method('getFrontendInput') |
| 94 | + ->willReturn($filterableAttrInput); |
| 95 | + $searchableAttribute->method('getAttributeCode') |
| 96 | + ->willReturn($searchableAttrCode); |
| 97 | + $searchableAttribute->method('getFrontendInput') |
| 98 | + ->willReturn($searchableAttrInput); |
| 99 | + |
| 100 | + $config = $this->model->read(); |
| 101 | + self::assertNotEmpty($config['ProductAttributeFilterInput']); |
| 102 | + self::assertCount(count($fieldsType), $config['ProductAttributeFilterInput']['fields']); |
| 103 | + foreach ($fieldsType as $attrCode => $fieldType) { |
| 104 | + self::assertEquals($fieldType, $config['ProductAttributeFilterInput']['fields'][$attrCode]['type']); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public function readDataProvider(): array |
| 109 | + { |
| 110 | + return [ |
| 111 | + [ |
| 112 | + 'price', |
| 113 | + 'price', |
| 114 | + 'sku', |
| 115 | + 'text', |
| 116 | + [ |
| 117 | + 'price' => 'FilterRangeTypeInput', |
| 118 | + 'sku' => 'FilterEqualTypeInput', |
| 119 | + ], |
| 120 | + ], |
| 121 | + [ |
| 122 | + 'date_attr', |
| 123 | + 'date', |
| 124 | + 'datetime_attr', |
| 125 | + 'datetime', |
| 126 | + [ |
| 127 | + 'date_attr' => 'FilterRangeTypeInput', |
| 128 | + 'datetime_attr' => 'FilterRangeTypeInput', |
| 129 | + ], |
| 130 | + ], |
| 131 | + [ |
| 132 | + 'select_attr', |
| 133 | + 'select', |
| 134 | + 'multiselect_attr', |
| 135 | + 'multiselect', |
| 136 | + [ |
| 137 | + 'select_attr' => 'FilterEqualTypeInput', |
| 138 | + 'multiselect_attr' => 'FilterEqualTypeInput', |
| 139 | + ], |
| 140 | + ], |
| 141 | + [ |
| 142 | + 'text_attr', |
| 143 | + 'text', |
| 144 | + 'textarea_attr', |
| 145 | + 'textarea', |
| 146 | + [ |
| 147 | + 'text_attr' => 'FilterMatchTypeInput', |
| 148 | + 'textarea_attr' => 'FilterMatchTypeInput', |
| 149 | + ], |
| 150 | + ], |
| 151 | + [ |
| 152 | + 'boolean_attr', |
| 153 | + 'boolean', |
| 154 | + 'boolean_attr', |
| 155 | + 'boolean', |
| 156 | + [ |
| 157 | + 'boolean_attr' => 'FilterEqualTypeInput', |
| 158 | + ], |
| 159 | + ], |
| 160 | + ]; |
| 161 | + } |
| 162 | +} |
0 commit comments