Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCriteriaTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class AbstractCriteriaTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $criteria;
19 
25  protected function setUp()
26  {
27  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
28  $this->criteria = $objectManager->getObject(\Magento\Framework\Data\Test\Unit\Criteria\Sample::class);
29  }
30 
41  public function testAddField($field, $alias, array $result)
42  {
43  $this->criteria->addField($field, $alias);
44  $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FIELDS]['list']);
45  }
46 
59  public function testAddFilter($name, $field, $condition, $type, array $result)
60  {
61  $this->criteria->addFilter($name, $field, $condition, $type);
62  $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FILTERS]['list']);
63  }
64 
76  public function testAddOrder($field, $direction, $unShift, array $result)
77  {
78  $this->criteria->addOrder($field, $direction, $unShift);
79  $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_ORDERS]['list']);
80  }
81 
92  public function testSetLimit($offset, $size, array $result)
93  {
94  $this->criteria->setLimit($offset, $size);
95  $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_LIMIT]);
96  }
97 
109  public function testRemoveField(array $actualField, $field, $isAlias, array $result)
110  {
111  list($name, $alias) = $actualField;
112  $this->criteria->addField($name, $alias);
113 
114  $this->criteria->removeField($field, $isAlias);
115  $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FIELDS]['list']);
116  }
117 
127  public function testRemoveAllFields(array $actualField, array $result)
128  {
129  list($name, $alias) = $actualField;
130  $this->criteria->addField($name, $alias);
131 
132  $this->criteria->removeAllFields();
133  $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FIELDS]['list']);
134  }
135 
146  public function testRemoveFilter(array $actualField, $name, array $result)
147  {
148  list($filterName, $field, $condition, $type) = $actualField;
149  $this->criteria->addFilter($filterName, $field, $condition, $type);
150 
151  $this->criteria->removeFilter($name);
152  $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FILTERS]['list']);
153  }
154 
164  public function testRemoveAllFilters(array $actualField, array $result)
165  {
166  list($filterName, $field, $condition, $type) = $actualField;
167  $this->criteria->addFilter($filterName, $field, $condition, $type);
168 
169  $this->criteria->removeAllFilters();
170  $this->assertEquals($result, $this->criteria->toArray()[CriteriaInterface::PART_FILTERS]['list']);
171  }
172 
181  public function testReset(array $result)
182  {
183  $this->criteria->reset();
184  $this->assertEquals($result, $this->criteria->toArray());
185  }
186 
192  public function dataProviderReset()
193  {
194  return [
195  [
196  'result' => [
197  'fields' => [
198  'list' => [],
199  ],
200  'filters' => [
201  'list' => [],
202  ],
203  'orders' => [
204  'list' => [],
205  ],
206  'criteria_list' => [
207  'list' => [],
208  ],
209  ],
210  ]
211  ];
212  }
213 
220  {
221  return [
222  [
223  'actualResult' => [
224  'test-filter-name',
225  'test-field-name',
226  'test-condition',
227  'test-type',
228  ],
229  'result' => [],
230  ]
231  ];
232  }
233 
239  public function dataProviderRemoveFilter()
240  {
241  return [
242  [
243  'actualResult' => [
244  'test-filter-name',
245  'test-field-name',
246  'test-condition',
247  'test-type',
248  ],
249  'name' => 'test-filter-name',
250  'result' => [],
251  ]
252  ];
253  }
254 
260  public function dataProviderRemoveAllFields()
261  {
262  return [
263  [
264  'actualField' => [
265  'test-field-name',
266  'test-field-alias',
267  ],
268  'result' => [],
269  ]
270  ];
271  }
272 
278  public function dataProviderRemoveField()
279  {
280  return [
281  [
282  'actualField' => [
283  'test-field-name',
284  null,
285  ],
286  'field' => 'test-field-name',
287  'isAlias' => false,
288  'result' => [],
289  ],
290  [
291  'actualField' => [
292  '*',
293  null,
294  ],
295  'field' => '*',
296  'isAlias' => false,
297  'result' => []
298  ],
299  [
300  'actualField' => [
301  'test-field-name',
302  'test-field-alias',
303  ],
304  'field' => 'test-field-alias',
305  'isAlias' => true,
306  'result' => []
307  ]
308  ];
309  }
310 
316  public function dataProviderSetLimit()
317  {
318  return [
319  [
320  'offset' => 99,
321  'size' => 30,
322  'result' => [99, 30],
323  ]
324  ];
325  }
326 
332  public function dataProviderAddOrder()
333  {
334  return [
335  [
336  'field' => 'test-field-name',
337  'direction' => 'desc',
338  'unShift' => false,
339  'result' => [
340 
341  'test-field-name' => 'DESC',
342  ],
343  ],
344  [
345  'field' => 'test-field-name',
346  'direction' => 'asc',
347  'unShift' => false,
348  'result' => [
349  'test-field-name' => 'ASC',
350  ]
351  ],
352  [
353  'field' => 'test-field-name',
354  'direction' => 'fail',
355  'unShift' => false,
356  'result' => [
357  'test-field-name' => 'DESC',
358  ]
359  ]
360  ];
361  }
362 
368  public function dataProviderAddFilter()
369  {
370  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
371  return [
372  [
373  'name' => 'test-filter-name',
374  'field' => 'test-field-name',
375  'condition' => 'test-condition',
376  'type' => 'test-type',
377  'result' => [
378  'test-filter-name' => $objectManager->getObject(
379  \Magento\Framework\DataObject::class,
380  [
381  'data' => [
382  'name' => 'test-filter-name',
383  'field' => 'test-field-name',
384  'condition' => 'test-condition',
385  'type' => 'test-type',
386  ]
387  ]
388  ),
389  ],
390  ]
391  ];
392  }
393 
399  public function dataProviderAddField()
400  {
401  return [
402  [
403  'field' => 'test-field-name',
404  'alias' => null,
405  'result' => [
406  'test-field-name' => 'test-field-name',
407  ],
408  ],
409  [
410  'field' => '*',
411  'alias' => null,
412  'result' => [
413  '*',
414  ],
415  ],
416  [
417  'field' => [
418  'test-field-name-1',
419  'test-field-name-2',
420  'test-field-name-3',
421  ],
422  'alias' => null,
423  'result' => [
424  'test-field-name-1' => 'test-field-name-1',
425  'test-field-name-2' => 'test-field-name-2',
426  'test-field-name-3' => 'test-field-name-3',
427  ]
428  ],
429  [
430  'field' => 'test-field-name',
431  'alias' => 'alias-test',
432  'result' => [
433  'alias-test' => 'test-field-name',
434  ]
435  ],
436  [
437  'field' => '*',
438  'alias' => null,
439  'result' => [
440  '*',
441  ]
442  ],
443  [
444  'field' => [
445  'alias-1' => 'test-field-name',
446  'alias-2' => 'test-field-name',
447  'alias-3' => 'test-field-name',
448  ],
449  'alias' => null,
450  'result' => [
451  'alias-1' => 'test-field-name',
452  'alias-2' => 'test-field-name',
453  'alias-3' => 'test-field-name',
454  ]
455  ]
456  ];
457  }
458 }
testRemoveFilter(array $actualField, $name, array $result)
$objectManager
Definition: bootstrap.php:17
testAddOrder($field, $direction, $unShift, array $result)
$type
Definition: item.phtml:13
testRemoveField(array $actualField, $field, $isAlias, array $result)
if(!trim($html)) $alias
Definition: details.phtml:20
testAddFilter($name, $field, $condition, $type, array $result)
if(!isset($_GET['name'])) $name
Definition: log.php:14