|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogSearch\Test\Unit\Model\Search\FilterMapper; |
| 8 | + |
| 9 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
| 10 | +use Magento\CatalogSearch\Model\Adapter\Mysql\Filter\AliasResolver; |
| 11 | +use Magento\CatalogSearch\Model\Search\FilterMapper\ExclusionStrategy; |
| 12 | +use Magento\CatalogSearch\Model\Search\FilterMapper\FilterContext; |
| 13 | +use Magento\CatalogSearch\Model\Search\FilterMapper\StaticAttributeStrategy; |
| 14 | +use Magento\CatalogSearch\Model\Search\FilterMapper\TermDropdownStrategy; |
| 15 | +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 16 | +use Magento\Framework\Search\Request\FilterInterface; |
| 17 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 18 | + |
| 19 | +class FilterContextTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var FilterContext|\PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + private $filterContext; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var AliasResolver|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + private $aliasResolver; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Magento\Eav\Model\Config|\PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + private $eavConfig; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ExclusionStrategy|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $exclusionStrategy; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var TermDropdownStrategy|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + private $termDropdownStrategy; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var StaticAttributeStrategy|\PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + private $staticAttributeStrategy; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Magento\Framework\DB\Select |
| 53 | + */ |
| 54 | + private $select; |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritDoc |
| 58 | + */ |
| 59 | + protected function setUp() |
| 60 | + { |
| 61 | + $this->eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->setMethods(['getAttribute']) |
| 64 | + ->getMock(); |
| 65 | + $this->aliasResolver = $this->getMockBuilder( |
| 66 | + AliasResolver::class |
| 67 | + ) |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->setMethods(['getAlias']) |
| 70 | + ->getMock(); |
| 71 | + $this->exclusionStrategy = $this->getMockBuilder(ExclusionStrategy::class) |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->setMethods(['apply']) |
| 74 | + ->getMock(); |
| 75 | + $this->termDropdownStrategy = $this->getMockBuilder(TermDropdownStrategy::class) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->setMethods(['apply']) |
| 78 | + ->getMock(); |
| 79 | + $this->staticAttributeStrategy = $this->getMockBuilder(StaticAttributeStrategy::class) |
| 80 | + ->disableOriginalConstructor() |
| 81 | + ->setMethods(['apply']) |
| 82 | + ->getMock(); |
| 83 | + $this->select = $this->getMockBuilder(\Magento\Framework\DB\Select::class) |
| 84 | + ->disableOriginalConstructor() |
| 85 | + ->setMethods([]) |
| 86 | + ->getMock(); |
| 87 | + $objectManager = new ObjectManager($this); |
| 88 | + $this->filterContext = $objectManager->getObject( |
| 89 | + FilterContext::class, |
| 90 | + [ |
| 91 | + 'eavConfig' => $this->eavConfig, |
| 92 | + 'aliasResolver' => $this->aliasResolver, |
| 93 | + 'exclusionStrategy' => $this->exclusionStrategy, |
| 94 | + 'termDropdownStrategy' => $this->termDropdownStrategy, |
| 95 | + 'staticAttributeStrategy' => $this->staticAttributeStrategy, |
| 96 | + ] |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + public function testApplyOnExclusionFilter() |
| 101 | + { |
| 102 | + $filter = $this->createFilterMock(); |
| 103 | + $this->exclusionStrategy->expects($this->once()) |
| 104 | + ->method('apply') |
| 105 | + ->with($filter, $this->select) |
| 106 | + ->willReturn(true); |
| 107 | + $this->eavConfig->expects($this->never())->method('getAttribute'); |
| 108 | + $this->assertTrue($this->filterContext->apply($filter, $this->select)); |
| 109 | + } |
| 110 | + |
| 111 | + public function testApplyFilterWithoutAttribute() |
| 112 | + { |
| 113 | + $filter = $this->createFilterMock('some_field'); |
| 114 | + $this->exclusionStrategy->expects($this->once()) |
| 115 | + ->method('apply') |
| 116 | + ->with($filter, $this->select) |
| 117 | + ->willReturn(false); |
| 118 | + $this->eavConfig->expects($this->once()) |
| 119 | + ->method('getAttribute') |
| 120 | + ->with(\Magento\Catalog\Model\Product::ENTITY, 'some_field') |
| 121 | + ->willReturn(null); |
| 122 | + $this->assertFalse($this->filterContext->apply($filter, $this->select)); |
| 123 | + } |
| 124 | + |
| 125 | + public function testApplyOnTermFilterBySelect() |
| 126 | + { |
| 127 | + $filter = $this->createFilterMock('select_field', FilterInterface::TYPE_TERM); |
| 128 | + $attribute = $this->createAttributeMock('select'); |
| 129 | + $this->eavConfig->expects($this->once()) |
| 130 | + ->method('getAttribute') |
| 131 | + ->with(\Magento\Catalog\Model\Product::ENTITY, 'select_field') |
| 132 | + ->willReturn($attribute); |
| 133 | + $this->exclusionStrategy->expects($this->once()) |
| 134 | + ->method('apply') |
| 135 | + ->with($filter, $this->select) |
| 136 | + ->willReturn(false); |
| 137 | + $this->termDropdownStrategy->expects($this->once()) |
| 138 | + ->method('apply') |
| 139 | + ->with($filter, $this->select) |
| 140 | + ->willReturn(true); |
| 141 | + $this->assertTrue($this->filterContext->apply($filter, $this->select)); |
| 142 | + } |
| 143 | + |
| 144 | + public function testApplyOnTermFilterByMultiSelect() |
| 145 | + { |
| 146 | + $filter = $this->createFilterMock('multiselect_field', FilterInterface::TYPE_TERM); |
| 147 | + $attribute = $this->createAttributeMock('multiselect'); |
| 148 | + $this->eavConfig->expects($this->once()) |
| 149 | + ->method('getAttribute') |
| 150 | + ->with(\Magento\Catalog\Model\Product::ENTITY, 'multiselect_field') |
| 151 | + ->willReturn($attribute); |
| 152 | + $this->exclusionStrategy->expects($this->once()) |
| 153 | + ->method('apply') |
| 154 | + ->with($filter, $this->select) |
| 155 | + ->willReturn(false); |
| 156 | + $this->termDropdownStrategy->expects($this->once()) |
| 157 | + ->method('apply') |
| 158 | + ->with($filter, $this->select) |
| 159 | + ->willReturn(true); |
| 160 | + $this->assertTrue($this->filterContext->apply($filter, $this->select)); |
| 161 | + } |
| 162 | + |
| 163 | + public function testApplyOnTermFilterByStaticAttribute() |
| 164 | + { |
| 165 | + $filter = $this->createFilterMock('multiselect_field', FilterInterface::TYPE_TERM); |
| 166 | + $attribute = $this->createAttributeMock('text', AbstractAttribute::TYPE_STATIC); |
| 167 | + $this->eavConfig->expects($this->once()) |
| 168 | + ->method('getAttribute') |
| 169 | + ->with(\Magento\Catalog\Model\Product::ENTITY, 'multiselect_field') |
| 170 | + ->willReturn($attribute); |
| 171 | + $this->exclusionStrategy->expects($this->once()) |
| 172 | + ->method('apply') |
| 173 | + ->with($filter, $this->select) |
| 174 | + ->willReturn(false); |
| 175 | + $this->staticAttributeStrategy->expects($this->once()) |
| 176 | + ->method('apply') |
| 177 | + ->with($filter, $this->select) |
| 178 | + ->willReturn(true); |
| 179 | + $this->assertTrue($this->filterContext->apply($filter, $this->select)); |
| 180 | + } |
| 181 | + |
| 182 | + public function testApplyOnTermFilterByUnknownAttributeType() |
| 183 | + { |
| 184 | + $filter = $this->createFilterMock('multiselect_field', FilterInterface::TYPE_TERM); |
| 185 | + $attribute = $this->createAttributeMock('text', 'text'); |
| 186 | + $this->eavConfig->expects($this->once()) |
| 187 | + ->method('getAttribute') |
| 188 | + ->with(\Magento\Catalog\Model\Product::ENTITY, 'multiselect_field') |
| 189 | + ->willReturn($attribute); |
| 190 | + $this->exclusionStrategy->expects($this->once()) |
| 191 | + ->method('apply') |
| 192 | + ->with($filter, $this->select) |
| 193 | + ->willReturn(false); |
| 194 | + $this->assertFalse($this->filterContext->apply($filter, $this->select)); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @param string $field |
| 199 | + * @param string $type |
| 200 | + * @return FilterInterface|\PHPUnit_Framework_MockObject_MockObject |
| 201 | + */ |
| 202 | + private function createFilterMock($field = null, $type = null) |
| 203 | + { |
| 204 | + $filter = $this->getMockBuilder(FilterInterface::class) |
| 205 | + ->setMethods(['getField', 'getType']) |
| 206 | + ->getMockForAbstractClass(); |
| 207 | + $filter->expects($this->any()) |
| 208 | + ->method('getField') |
| 209 | + ->willReturn($field); |
| 210 | + $filter->expects($this->any()) |
| 211 | + ->method('getType') |
| 212 | + ->willReturn($type); |
| 213 | + |
| 214 | + return $filter; |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * @param string|null $frontendInput |
| 219 | + * @param string|null $backendType |
| 220 | + * @return Attribute|\PHPUnit_Framework_MockObject_MockObject |
| 221 | + */ |
| 222 | + private function createAttributeMock($frontendInput = null, $backendType = null) |
| 223 | + { |
| 224 | + $attribute = $this->getMockBuilder(Attribute::class) |
| 225 | + ->disableOriginalConstructor() |
| 226 | + ->setMethods(['getFrontendInput', 'getBackendType']) |
| 227 | + ->getMock(); |
| 228 | + $attribute->expects($this->any()) |
| 229 | + ->method('getFrontendInput') |
| 230 | + ->willReturn($frontendInput); |
| 231 | + $attribute->expects($this->any()) |
| 232 | + ->method('getBackendType') |
| 233 | + ->willReturn($backendType); |
| 234 | + return $attribute; |
| 235 | + } |
| 236 | +} |
0 commit comments