|
| 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\CatalogInventory\Ui\DataProvider\Product; |
| 9 | + |
| 10 | +use Magento\Framework\App\RequestInterface; |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\Framework\View\Element\UiComponent\ContextInterface; |
| 13 | +use Magento\Framework\View\Element\UiComponentFactory; |
| 14 | +use Magento\Framework\View\Element\UiComponentInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Checks that the product quantity filter is working correctly |
| 20 | + * |
| 21 | + * @magentoAppArea adminhtml |
| 22 | + */ |
| 23 | +class AddQuantityFilterToCollectionTest extends TestCase |
| 24 | +{ |
| 25 | + /** @var ObjectManagerInterface */ |
| 26 | + private $objectManager; |
| 27 | + |
| 28 | + /** @var UiComponentFactory */ |
| 29 | + private $componentFactory; |
| 30 | + |
| 31 | + /** @var RequestInterface */ |
| 32 | + private $request; |
| 33 | + |
| 34 | + /** |
| 35 | + * @inheritdoc |
| 36 | + */ |
| 37 | + protected function setUp(): void |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + |
| 41 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 42 | + $this->request = $this->objectManager->get(RequestInterface::class); |
| 43 | + $this->componentFactory = $this->objectManager->get(UiComponentFactory::class); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @dataProvider quantityFilterProvider |
| 48 | + * @magentoDataFixture Magento/Catalog/_files/multiple_products.php |
| 49 | + * @param array $filter |
| 50 | + * @param array $expectedProducts |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function testQuantityFilter(array $filter, array $expectedProducts): void |
| 54 | + { |
| 55 | + $this->request->setParams([ContextInterface::FILTER_VAR => $filter]); |
| 56 | + $dataProviderData = $this->getComponentProvidedData('product_listing'); |
| 57 | + $actualProducts = array_column($dataProviderData['items'], 'sku'); |
| 58 | + $this->assertEquals($expectedProducts, $actualProducts, 'Expected products do not match actual products!'); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Data provider for testQuantityFilter |
| 63 | + * |
| 64 | + * @return array |
| 65 | + */ |
| 66 | + public function quantityFilterProvider(): array |
| 67 | + { |
| 68 | + return [ |
| 69 | + 'from' => [ |
| 70 | + 'filter' => [ |
| 71 | + 'qty' => [ |
| 72 | + 'from' => 100, |
| 73 | + ], |
| 74 | + ], |
| 75 | + 'expected_products' => [ |
| 76 | + 'simple1', |
| 77 | + 'simple3', |
| 78 | + ], |
| 79 | + ], |
| 80 | + 'to' => [ |
| 81 | + 'filter' => [ |
| 82 | + 'qty' => [ |
| 83 | + 'to' => 100, |
| 84 | + ], |
| 85 | + ], |
| 86 | + 'expected_products' => [ |
| 87 | + 'simple1', |
| 88 | + 'simple2', |
| 89 | + ], |
| 90 | + ], |
| 91 | + 'both' => [ |
| 92 | + 'filter' => [ |
| 93 | + 'qty' => [ |
| 94 | + 'from' => 60, |
| 95 | + 'to' => 130, |
| 96 | + ], |
| 97 | + ], |
| 98 | + 'expected_products' => [ |
| 99 | + 'simple1', |
| 100 | + ], |
| 101 | + ], |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Call prepare method in the child components |
| 107 | + * |
| 108 | + * @param UiComponentInterface $component |
| 109 | + * @return void |
| 110 | + */ |
| 111 | + private function prepareChildComponents(UiComponentInterface $component): void |
| 112 | + { |
| 113 | + foreach ($component->getChildComponents() as $child) { |
| 114 | + $this->prepareChildComponents($child); |
| 115 | + } |
| 116 | + |
| 117 | + $component->prepare(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get component provided data |
| 122 | + * |
| 123 | + * @param string $namespace |
| 124 | + * @return array |
| 125 | + */ |
| 126 | + private function getComponentProvidedData(string $namespace): array |
| 127 | + { |
| 128 | + $component = $this->componentFactory->create($namespace); |
| 129 | + $this->prepareChildComponents($component); |
| 130 | + |
| 131 | + return $component->getContext()->getDataProvider()->getData(); |
| 132 | + } |
| 133 | +} |
0 commit comments