|
| 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\ColumnFactory; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Magento\Catalog\Api\Data\ProductAttributeInterface; |
| 14 | +use Magento\Framework\View\Element\UiComponent\ContextInterface; |
| 15 | +use Magento\Framework\View\Element\UiComponentFactory; |
| 16 | +use Magento\Ui\Component\Listing\Columns\ColumnInterface; |
| 17 | +use Magento\Ui\Component\Filters\FilterModifier; |
| 18 | + |
| 19 | +/** |
| 20 | + * ColumnFactory test. |
| 21 | + */ |
| 22 | +class ColumnFactoryTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ColumnFactory |
| 26 | + */ |
| 27 | + private $columnFactory; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ObjectManager |
| 31 | + */ |
| 32 | + private $objectManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ProductAttributeInterface|\PHPUnit\Framework\MockObject\MockObject |
| 36 | + */ |
| 37 | + private $attribute; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var ContextInterface|\PHPUnit\Framework\MockObject\MockObject |
| 41 | + */ |
| 42 | + private $context; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var UiComponentFactory|\PHPUnit\Framework\MockObject\MockObject |
| 46 | + */ |
| 47 | + private $uiComponentFactory; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var ColumnInterface|\PHPUnit\Framework\MockObject\MockObject |
| 51 | + */ |
| 52 | + private $column; |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + protected function setUp(): void |
| 58 | + { |
| 59 | + $this->objectManager = new ObjectManager($this); |
| 60 | + |
| 61 | + $this->attribute = $this->getMockBuilder(ProductAttributeInterface::class) |
| 62 | + ->setMethods(['usesSource']) |
| 63 | + ->getMockForAbstractClass(); |
| 64 | + $this->context = $this->createMock(ContextInterface::class); |
| 65 | + $this->uiComponentFactory = $this->createMock(UiComponentFactory::class); |
| 66 | + $this->column = $this->getMockForAbstractClass(ColumnInterface::class); |
| 67 | + $this->uiComponentFactory->method('create') |
| 68 | + ->willReturn($this->column); |
| 69 | + |
| 70 | + $this->columnFactory = $this->objectManager->getObject(ColumnFactory::class, [ |
| 71 | + 'componentFactory' => $this->uiComponentFactory |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Tests the create method will return correct object. |
| 77 | + * |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function testCreatedObject(): void |
| 81 | + { |
| 82 | + $this->context->method('getRequestParam') |
| 83 | + ->with(FilterModifier::FILTER_MODIFIER, []) |
| 84 | + ->willReturn([]); |
| 85 | + |
| 86 | + $object = $this->columnFactory->create($this->attribute, $this->context); |
| 87 | + $this->assertEquals( |
| 88 | + $this->column, |
| 89 | + $object, |
| 90 | + 'Object must be the same which the ui component factory creates.' |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Tests create method with not filterable in grid attribute. |
| 96 | + * |
| 97 | + * @param array $filterModifiers |
| 98 | + * @param null|string $filter |
| 99 | + * |
| 100 | + * @return void |
| 101 | + * @dataProvider filterModifiersProvider |
| 102 | + */ |
| 103 | + public function testCreateWithNotFilterableInGridAttribute(array $filterModifiers, ?string $filter): void |
| 104 | + { |
| 105 | + $componentFactoryArgument = [ |
| 106 | + 'data' => [ |
| 107 | + 'config' => [ |
| 108 | + 'label' => __(null), |
| 109 | + 'dataType' => 'text', |
| 110 | + 'add_field' => true, |
| 111 | + 'visible' => null, |
| 112 | + 'filter' => $filter, |
| 113 | + 'component' => 'Magento_Ui/js/grid/columns/column', |
| 114 | + ], |
| 115 | + ], |
| 116 | + 'context' => $this->context, |
| 117 | + ]; |
| 118 | + |
| 119 | + $this->context->method('getRequestParam') |
| 120 | + ->with(FilterModifier::FILTER_MODIFIER, []) |
| 121 | + ->willReturn($filterModifiers); |
| 122 | + $this->attribute->method('getIsFilterableInGrid') |
| 123 | + ->willReturn(false); |
| 124 | + $this->attribute->method('getAttributeCode') |
| 125 | + ->willReturn('color'); |
| 126 | + |
| 127 | + $this->uiComponentFactory->expects($this->once()) |
| 128 | + ->method('create') |
| 129 | + ->with($this->anything(), $this->anything(), $componentFactoryArgument); |
| 130 | + |
| 131 | + $this->columnFactory->create($this->attribute, $this->context); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Filter modifiers data provider. |
| 136 | + * |
| 137 | + * @return array |
| 138 | + */ |
| 139 | + public function filterModifiersProvider(): array |
| 140 | + { |
| 141 | + return [ |
| 142 | + 'without' => [ |
| 143 | + 'filter_modifiers' => [], |
| 144 | + 'filter' => null, |
| 145 | + ], |
| 146 | + 'with' => [ |
| 147 | + 'filter_modifiers' => [ |
| 148 | + 'color' => [ |
| 149 | + 'condition_type' => 'notnull', |
| 150 | + ], |
| 151 | + ], |
| 152 | + 'filter' => 'text', |
| 153 | + ], |
| 154 | + ]; |
| 155 | + } |
| 156 | +} |
0 commit comments