Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FilterContextTest.php
Go to the documentation of this file.
1 <?php
8 
18 
23 class FilterContextTest extends \PHPUnit\Framework\TestCase
24 {
28  private $filterContext;
29 
33  private $aliasResolver;
34 
38  private $eavConfig;
39 
43  private $exclusionStrategy;
44 
48  private $termDropdownStrategy;
49 
53  private $staticAttributeStrategy;
54 
58  private $select;
59 
63  protected function setUp()
64  {
65  $this->eavConfig = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
66  ->disableOriginalConstructor()
67  ->setMethods(['getAttribute'])
68  ->getMock();
69  $this->aliasResolver = $this->getMockBuilder(
70  AliasResolver::class
71  )
72  ->disableOriginalConstructor()
73  ->setMethods(['getAlias'])
74  ->getMock();
75  $this->exclusionStrategy = $this->getMockBuilder(ExclusionStrategy::class)
76  ->disableOriginalConstructor()
77  ->setMethods(['apply'])
78  ->getMock();
79  $this->termDropdownStrategy = $this->getMockBuilder(TermDropdownStrategy::class)
80  ->disableOriginalConstructor()
81  ->setMethods(['apply'])
82  ->getMock();
83  $this->staticAttributeStrategy = $this->getMockBuilder(StaticAttributeStrategy::class)
84  ->disableOriginalConstructor()
85  ->setMethods(['apply'])
86  ->getMock();
87  $this->select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
88  ->disableOriginalConstructor()
89  ->setMethods([])
90  ->getMock();
91  $objectManager = new ObjectManager($this);
92  $this->filterContext = $objectManager->getObject(
93  FilterContext::class,
94  [
95  'eavConfig' => $this->eavConfig,
96  'aliasResolver' => $this->aliasResolver,
97  'exclusionStrategy' => $this->exclusionStrategy,
98  'termDropdownStrategy' => $this->termDropdownStrategy,
99  'staticAttributeStrategy' => $this->staticAttributeStrategy,
100  ]
101  );
102  }
103 
104  public function testApplyOnExclusionFilter()
105  {
106  $filter = $this->createFilterMock();
107  $this->exclusionStrategy->expects($this->once())
108  ->method('apply')
109  ->with($filter, $this->select)
110  ->willReturn(true);
111  $this->eavConfig->expects($this->never())->method('getAttribute');
112  $this->assertTrue($this->filterContext->apply($filter, $this->select));
113  }
114 
116  {
117  $filter = $this->createFilterMock('some_field');
118  $this->exclusionStrategy->expects($this->once())
119  ->method('apply')
120  ->with($filter, $this->select)
121  ->willReturn(false);
122  $this->eavConfig->expects($this->once())
123  ->method('getAttribute')
124  ->with(\Magento\Catalog\Model\Product::ENTITY, 'some_field')
125  ->willReturn(null);
126  $this->assertFalse($this->filterContext->apply($filter, $this->select));
127  }
128 
130  {
131  $filter = $this->createFilterMock('select_field', FilterInterface::TYPE_TERM);
132  $attribute = $this->createAttributeMock('select');
133  $this->eavConfig->expects($this->once())
134  ->method('getAttribute')
135  ->with(\Magento\Catalog\Model\Product::ENTITY, 'select_field')
136  ->willReturn($attribute);
137  $this->exclusionStrategy->expects($this->once())
138  ->method('apply')
139  ->with($filter, $this->select)
140  ->willReturn(false);
141  $this->termDropdownStrategy->expects($this->never())
142  ->method('apply')
143  ->with($filter, $this->select)
144  ->willReturn(true);
145  $this->assertFalse($this->filterContext->apply($filter, $this->select));
146  }
147 
149  {
150  $filter = $this->createFilterMock('multiselect_field', FilterInterface::TYPE_TERM);
151  $attribute = $this->createAttributeMock('multiselect');
152  $this->eavConfig->expects($this->once())
153  ->method('getAttribute')
154  ->with(\Magento\Catalog\Model\Product::ENTITY, 'multiselect_field')
155  ->willReturn($attribute);
156  $this->exclusionStrategy->expects($this->once())
157  ->method('apply')
158  ->with($filter, $this->select)
159  ->willReturn(false);
160  $this->termDropdownStrategy->expects($this->never())
161  ->method('apply')
162  ->with($filter, $this->select)
163  ->willReturn(true);
164  $this->assertFalse($this->filterContext->apply($filter, $this->select));
165  }
166 
168  {
169  $filter = $this->createFilterMock('multiselect_field', FilterInterface::TYPE_TERM);
170  $attribute = $this->createAttributeMock('text', AbstractAttribute::TYPE_STATIC);
171  $this->eavConfig->expects($this->once())
172  ->method('getAttribute')
173  ->with(\Magento\Catalog\Model\Product::ENTITY, 'multiselect_field')
174  ->willReturn($attribute);
175  $this->exclusionStrategy->expects($this->once())
176  ->method('apply')
177  ->with($filter, $this->select)
178  ->willReturn(false);
179  $this->staticAttributeStrategy->expects($this->once())
180  ->method('apply')
181  ->with($filter, $this->select)
182  ->willReturn(true);
183  $this->assertTrue($this->filterContext->apply($filter, $this->select));
184  }
185 
187  {
188  $filter = $this->createFilterMock('multiselect_field', FilterInterface::TYPE_TERM);
189  $attribute = $this->createAttributeMock('text', 'text');
190  $this->eavConfig->expects($this->once())
191  ->method('getAttribute')
192  ->with(\Magento\Catalog\Model\Product::ENTITY, 'multiselect_field')
193  ->willReturn($attribute);
194  $this->exclusionStrategy->expects($this->once())
195  ->method('apply')
196  ->with($filter, $this->select)
197  ->willReturn(false);
198  $this->assertFalse($this->filterContext->apply($filter, $this->select));
199  }
200 
206  private function createFilterMock($field = null, $type = null)
207  {
208  $filter = $this->getMockBuilder(FilterInterface::class)
209  ->setMethods(['getField', 'getType'])
210  ->getMockForAbstractClass();
211  $filter->expects($this->any())
212  ->method('getField')
213  ->willReturn($field);
214  $filter->expects($this->any())
215  ->method('getType')
216  ->willReturn($type);
217 
218  return $filter;
219  }
220 
226  private function createAttributeMock($frontendInput = null, $backendType = null)
227  {
228  $attribute = $this->getMockBuilder(Attribute::class)
229  ->disableOriginalConstructor()
230  ->setMethods(['getFrontendInput', 'getBackendType'])
231  ->getMock();
232  $attribute->expects($this->any())
233  ->method('getFrontendInput')
234  ->willReturn($frontendInput);
235  $attribute->expects($this->any())
236  ->method('getBackendType')
237  ->willReturn($backendType);
238  return $attribute;
239  }
240 }
$objectManager
Definition: bootstrap.php:17
$type
Definition: item.phtml:13