|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\AnyDataset\Dataset; |
| 4 | + |
| 5 | +use ByJG\AnyDataset\Core\IteratorFilter; |
| 6 | +use ByJG\AnyDataset\Core\IteratorFilterXPathFormatter; |
| 7 | +use ByJG\AnyDataset\Core\Row; |
| 8 | +use ByJG\AnyDataset\Core\Enum\Relation; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class IteratorFilterAnydatasetTest extends TestCase |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * @var IteratorFilter |
| 16 | + */ |
| 17 | + protected $object; |
| 18 | + |
| 19 | + /** |
| 20 | + * Sets up the fixture, for example, opens a network connection. |
| 21 | + * This method is called before a test is executed. |
| 22 | + */ |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->object = new IteratorFilter(); |
| 26 | + } |
| 27 | + |
| 28 | + public function testMatch() |
| 29 | + { |
| 30 | + |
| 31 | + $collection = [ |
| 32 | + $row1 = new Row( |
| 33 | + [ |
| 34 | + 'id' => 1, |
| 35 | + 'field' => 'value1', |
| 36 | + 'field2' => 'value2', |
| 37 | + 'val' => 50, |
| 38 | + ] |
| 39 | + ), |
| 40 | + $row2 = new Row( |
| 41 | + [ |
| 42 | + 'id' => 2, |
| 43 | + 'field' => 'other1', |
| 44 | + 'field2' => 'other2', |
| 45 | + 'val' => 80, |
| 46 | + ] |
| 47 | + ), |
| 48 | + $row3 = new Row( |
| 49 | + [ |
| 50 | + 'id' => 3, |
| 51 | + 'field' => 'last1', |
| 52 | + 'field2' => 'last2', |
| 53 | + 'val' => 30, |
| 54 | + ] |
| 55 | + ), |
| 56 | + $row4 = new Row( |
| 57 | + [ |
| 58 | + 'id' => 4, |
| 59 | + 'field' => 'xy', |
| 60 | + 'field2' => 'zy', |
| 61 | + 'val' => 10, |
| 62 | + ] |
| 63 | + ), |
| 64 | + ]; |
| 65 | + |
| 66 | + $this->assertEquals($collection, $this->object->match($collection)); |
| 67 | + |
| 68 | + $this->object->addRelation('field2', Relation::EQUAL, 'other2'); |
| 69 | + $this->assertEquals([$row2], $this->object->match($collection)); |
| 70 | + |
| 71 | + $this->object->addRelationOr('field', Relation::EQUAL, 'last1'); |
| 72 | + $this->assertEquals([$row2, $row3], $this->object->match($collection)); |
| 73 | + |
| 74 | + |
| 75 | + //------------------------ |
| 76 | + |
| 77 | + $this->object = new IteratorFilter(); |
| 78 | + $this->object->addRelation('field', Relation::EQUAL, 'last1'); |
| 79 | + $this->object->addRelation('field2', Relation::EQUAL, 'last2'); |
| 80 | + $this->assertEquals([$row3], $this->object->match($collection)); |
| 81 | + |
| 82 | + // Test Greater Than |
| 83 | + $this->object = new IteratorFilter(); |
| 84 | + $this->object->addRelation('val', Relation::GREATER_THAN, 50); |
| 85 | + $this->assertEquals([$row2], $this->object->match($collection)); |
| 86 | + |
| 87 | + // Test Less Than |
| 88 | + $this->object = new IteratorFilter(); |
| 89 | + $this->object->addRelation('val', Relation::LESS_THAN, 50); |
| 90 | + $this->assertEquals([$row3, $row4], $this->object->match($collection)); |
| 91 | + |
| 92 | + // Test Greater or Equal Than |
| 93 | + $this->object = new IteratorFilter(); |
| 94 | + $this->object->addRelation('val', Relation::GREATER_OR_EQUAL_THAN, 50); |
| 95 | + $this->assertEquals([$row1, $row2], $this->object->match($collection)); |
| 96 | + |
| 97 | + // Test Less or Equal Than |
| 98 | + $this->object = new IteratorFilter(); |
| 99 | + $this->object->addRelation('val', Relation::LESS_OR_EQUAL_THAN, 50); |
| 100 | + $this->assertEquals([$row1, $row3, $row4], $this->object->match($collection)); |
| 101 | + |
| 102 | + // Test Not Equal |
| 103 | + $this->object = new IteratorFilter(); |
| 104 | + $this->object->addRelation('val', Relation::NOT_EQUAL, 50); |
| 105 | + $this->assertEquals([$row2, $row3, $row4], $this->object->match($collection)); |
| 106 | + |
| 107 | + // Test Starts With |
| 108 | + $this->object = new IteratorFilter(); |
| 109 | + $this->object->addRelation('field', Relation::STARTS_WITH, 'la'); |
| 110 | + $this->assertEquals([$row3], $this->object->match($collection)); |
| 111 | + |
| 112 | + // Test Contains |
| 113 | + $this->object = new IteratorFilter(); |
| 114 | + $this->object->addRelation('field', Relation::CONTAINS, '1'); |
| 115 | + $this->assertEquals([$row1, $row2, $row3], $this->object->match($collection)); |
| 116 | + |
| 117 | + // Test In |
| 118 | + $this->object = new IteratorFilter(); |
| 119 | + $this->object->addRelation('val', Relation::IN, [10, 30, 50]); |
| 120 | + $this->assertEquals([$row1, $row3, $row4], $this->object->match($collection)); |
| 121 | + |
| 122 | + // Test Not In |
| 123 | + $this->object = new IteratorFilter(); |
| 124 | + $this->object->addRelation('val', Relation::NOT_IN, [10, 30, 50]); |
| 125 | + $this->assertEquals([$row2], $this->object->match($collection)); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | +} |
0 commit comments