|
| 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\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\CategoryFactory; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Category; |
| 12 | +use Magento\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CategoryFilter; |
| 13 | +use Magento\Framework\Api\Filter; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for Category filter |
| 19 | + */ |
| 20 | +class CategoryFilterTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CategoryFilter |
| 24 | + */ |
| 25 | + private $model; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var CategoryFactory|MockObject |
| 29 | + */ |
| 30 | + private $categoryFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Category|MockObject |
| 34 | + */ |
| 35 | + private $categoryResourceModel; |
| 36 | + |
| 37 | + /** |
| 38 | + * @inheritdoc |
| 39 | + */ |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + parent::setUp(); |
| 43 | + $this->categoryFactory = $this->createMock(CategoryFactory::class); |
| 44 | + $this->categoryResourceModel = $this->createMock(Category::class); |
| 45 | + $this->model = new CategoryFilter( |
| 46 | + $this->categoryFactory, |
| 47 | + $this->categoryResourceModel |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test that category filter works correctly wity condition type "eq" |
| 53 | + */ |
| 54 | + public function testApplyWithConditionTypeEq(): void |
| 55 | + { |
| 56 | + $filter = new Filter(); |
| 57 | + $category = $this->createMock(\Magento\Catalog\Model\Category::class); |
| 58 | + $collection = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product\Collection::class); |
| 59 | + $filter->setConditionType('eq'); |
| 60 | + $categoryId = 1; |
| 61 | + $filter->setValue($categoryId); |
| 62 | + $this->categoryFactory->expects($this->once()) |
| 63 | + ->method('create') |
| 64 | + ->willReturn($category); |
| 65 | + $this->categoryResourceModel->expects($this->once()) |
| 66 | + ->method('load') |
| 67 | + ->with($category, $categoryId); |
| 68 | + $collection->expects($this->once()) |
| 69 | + ->method('addCategoryFilter') |
| 70 | + ->with($category); |
| 71 | + $this->model->apply($filter, $collection); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param string $condition |
| 76 | + * @dataProvider applyWithOtherSupportedConditionTypesDataProvider |
| 77 | + */ |
| 78 | + public function testApplyWithOtherSupportedConditionTypes(string $condition): void |
| 79 | + { |
| 80 | + $filter = new Filter(); |
| 81 | + $category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->onlyMethods(['getChildren']) |
| 84 | + ->addMethods(['getIsAnchor']) |
| 85 | + ->getMock(); |
| 86 | + $collection = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product\Collection::class); |
| 87 | + $filter->setConditionType($condition); |
| 88 | + $categoryId = 1; |
| 89 | + $filter->setValue($categoryId); |
| 90 | + $this->categoryFactory->expects($this->once()) |
| 91 | + ->method('create') |
| 92 | + ->willReturn($category); |
| 93 | + $this->categoryResourceModel->expects($this->once()) |
| 94 | + ->method('load') |
| 95 | + ->with($category, $categoryId); |
| 96 | + $collection->expects($this->never()) |
| 97 | + ->method('addCategoryFilter'); |
| 98 | + $collection->expects($this->once()) |
| 99 | + ->method('addCategoriesFilter') |
| 100 | + ->with([$condition => [1, 2]]); |
| 101 | + $category->expects($this->once()) |
| 102 | + ->method('getIsAnchor') |
| 103 | + ->willReturn(true); |
| 104 | + $category->expects($this->once()) |
| 105 | + ->method('getChildren') |
| 106 | + ->with(true) |
| 107 | + ->willReturn('2'); |
| 108 | + $this->model->apply($filter, $collection); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return array |
| 113 | + */ |
| 114 | + public function applyWithOtherSupportedConditionTypesDataProvider(): array |
| 115 | + { |
| 116 | + return [['neq'], ['in'], ['nin'],]; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param string $condition |
| 121 | + * @dataProvider applyWithUnsupportedConditionTypesDataProvider |
| 122 | + */ |
| 123 | + public function testApplyWithUnsupportedConditionTypes(string $condition): void |
| 124 | + { |
| 125 | + $filter = new Filter(); |
| 126 | + $collection = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product\Collection::class); |
| 127 | + $filter->setConditionType($condition); |
| 128 | + $categoryId = 1; |
| 129 | + $filter->setValue($categoryId); |
| 130 | + $this->categoryFactory->expects($this->never()) |
| 131 | + ->method('create'); |
| 132 | + $this->categoryResourceModel->expects($this->never()) |
| 133 | + ->method('load'); |
| 134 | + $collection->expects($this->never()) |
| 135 | + ->method('addCategoryFilter'); |
| 136 | + $collection->expects($this->never()) |
| 137 | + ->method('addCategoriesFilter'); |
| 138 | + $this->model->apply($filter, $collection); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @return array |
| 143 | + */ |
| 144 | + public function applyWithUnsupportedConditionTypesDataProvider(): array |
| 145 | + { |
| 146 | + return [['gteq'], ['lteq'], ['gt'], ['lt'], ['like'], ['nlike']]; |
| 147 | + } |
| 148 | +} |
0 commit comments