Skip to content

Commit 781099a

Browse files
feat(filterModifier): change and add some tests
1 parent 6185592 commit 781099a

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

tests/Service/Request/QueryModifier/Modifier/FilterModifierTest.php

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
class FilterModifierTest extends TestCase
3333
{
3434

35+
/**
36+
* Just to store the result of createSelectSql
37+
* @var array
38+
*/
39+
private $arr = [];
40+
3541
public function setUp()
3642
{
3743
if (!class_exists(ModifierTest::class)) {
@@ -42,6 +48,7 @@ public function setUp()
4248
<table name="modifier_test">
4349
<column name="name" type="VARCHAR"/>
4450
<column name="column2" type="VARCHAR"/>
51+
<column name="date" type="TIMESTAMP"/>
4552
<column name="relation_id" type="INTEGER"/>
4653
<foreign-key foreignTable="relation_test">
4754
<reference local="relation_id" foreign="id"/>
@@ -71,18 +78,14 @@ public function testApplyOnRelation()
7178
]));
7279
/** @var ModelCriteria $mc */
7380
$mc = new ModifierTestQuery();
74-
$modifier = $m->apply($mc);
75-
81+
$m->apply($mc);
7682
$this->assertArrayHasKey('_relationTest.name', $mc->getMap());
7783
$criterion = $mc->getMap()['_relationTest.name'];
7884
$this->assertEquals('bob', $criterion->getValue());
7985
$this->assertEquals(Criteria::NOT_EQUAL, $criterion->getComparison());
8086
$this->assertEquals('_relationTest', $criterion->getTable());
8187
$this->assertEquals('name', $criterion->getColumn());
82-
83-
$this->assertEquals($mc->toString(), "Criteria:
84-
SQL (may not be complete): SELECT FROM modifier_test INNER JOIN relation_test _relationTest ON (modifier_test.relation_id=_relationTest.id) WHERE _relationTest.name<>:p1
85-
Params: relation_test.name => 'bob'");
88+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test INNER JOIN relation_test _relationTest ON (modifier_test.relation_id=_relationTest.id) WHERE _relationTest.name<>:p1");
8689
}
8790

8891
public function testApplyWithValue()
@@ -101,10 +104,7 @@ public function testApplyWithValue()
101104
$criterion = $mc->getMap()['modifier_test.name'];
102105
$this->assertEquals('test', $criterion->getValue());
103106
$this->assertEquals('=', $criterion->getComparison());
104-
$this->assertEquals($mc->toString(), "Criteria:
105-
SQL (may not be complete): SELECT FROM modifier_test WHERE modifier_test.name=:p1
106-
Params: modifier_test.name => 'test'");
107-
//var_dump($mc->toString());
107+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test WHERE modifier_test.name=:p1");
108108
}
109109

110110
public function testApplyWithValueAndInvalidOperator()
@@ -139,9 +139,7 @@ public function testApplyWithValueAndOperator()
139139
$criterion = $mc->getMap()['modifier_test.name'];
140140
$this->assertEquals('test', $criterion->getValue());
141141
$this->assertEquals('>=', $criterion->getComparison());
142-
$this->assertEquals($mc->toString(), "Criteria:
143-
SQL (may not be complete): SELECT FROM modifier_test WHERE modifier_test.name>=:p1
144-
Params: modifier_test.name => 'test'");
142+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test WHERE modifier_test.name>=:p1");
145143
}
146144

147145
public function testApplyWithoutProperty()
@@ -198,9 +196,35 @@ public function testInArray()
198196
$this->assertEquals('name', $criterion->getColumn());
199197
$this->assertEquals(["toto", "tata"], $criterion->getValue());
200198
$this->assertEquals(Criteria::IN, $criterion->getComparison());
201-
$this->assertEquals($mc->toString(), "Criteria:
202-
SQL (may not be complete): SELECT FROM modifier_test WHERE modifier_test.name IN (:p1,:p2)
203-
Params: modifier_test.name => 'toto', modifier_test.name => 'tata'");
199+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test WHERE modifier_test.name IN (:p1,:p2)");
200+
}
201+
202+
public function testMinMax()
203+
{
204+
$m = new FilterModifier(new Request([
205+
"filter" => json_encode([
206+
"property" => "date",
207+
"value" => [
208+
"min" => "01-10-2020",
209+
"max" => "20-10-2020"
210+
],
211+
]),
212+
]));
213+
/** @var ModelCriteria $mc */
214+
$mc = new ModifierTestQuery();
215+
$m->apply($mc);
216+
$this->assertArrayHasKey('modifier_test.date', $mc->getMap());
217+
/** @var RawModelCriterion $criterion */
218+
$criterion = $mc->getMap()['modifier_test.date'];
219+
$this->assertEquals('date', $criterion->getColumn());
220+
$this->assertEquals("01-10-2020", $criterion->getValue());
221+
$this->assertEquals(Criteria::GREATER_EQUAL, $criterion->getComparison());
222+
223+
$this->assertEquals(1, count($criterion->getClauses()));
224+
$this->assertEquals("20-10-2020", $criterion->getClauses()[0]->getValue());
225+
$this->assertEquals("date", $criterion->getClauses()[0]->getColumn());
226+
$this->assertEquals(Criteria::LESS_EQUAL, $criterion->getClauses()[0]->getComparison());
227+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test WHERE (modifier_test.date>=:p1 AND modifier_test.date<=:p2)");
204228
}
205229

206230
public function testLike()
@@ -222,9 +246,7 @@ public function testLike()
222246
$this->assertEquals('%foo%', $criterion->getValue());
223247
$this->assertEquals(Criteria::LIKE, $criterion->getComparison());
224248

225-
$this->assertEquals($mc->toString(), "Criteria:
226-
SQL (may not be complete): SELECT FROM modifier_test WHERE modifier_test.name LIKE :p1
227-
Params: modifier_test.name => '%foo%'");
249+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test WHERE modifier_test.name LIKE :p1");
228250
}
229251

230252
public function testValue()
@@ -244,10 +266,7 @@ public function testValue()
244266
$this->assertEquals('name', $criterion->getColumn());
245267
$this->assertEquals('foo', $criterion->getValue());
246268
$this->assertEquals('=', $criterion->getComparison());
247-
248-
$this->assertEquals($mc->toString(), "Criteria:
249-
SQL (may not be complete): SELECT FROM modifier_test WHERE modifier_test.name=:p1
250-
Params: modifier_test.name => 'foo'");
269+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test WHERE modifier_test.name=:p1");
251270
}
252271

253272
public function testValueNullWithEqualsOperator()
@@ -267,10 +286,7 @@ public function testValueNullWithEqualsOperator()
267286
$criterion = $mc->getMap()['modifier_test.name'];
268287
$this->assertNull($criterion->getValue());
269288
$this->assertEquals(Criteria::ISNULL, $criterion->getComparison());
270-
271-
$this->assertEquals($mc->toString(), "Criteria:
272-
SQL (may not be complete): SELECT FROM modifier_test WHERE modifier_test.name IS NULL
273-
Params: ");
289+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test WHERE modifier_test.name IS NULL ");
274290
}
275291

276292
public function testValueNullWithNotEqualsOperator()
@@ -290,9 +306,7 @@ public function testValueNullWithNotEqualsOperator()
290306
$criterion = $mc->getMap()['modifier_test.name'];
291307
$this->assertNull($criterion->getValue());
292308
$this->assertEquals(Criteria::ISNOTNULL, $criterion->getComparison());
293-
$this->assertEquals($mc->toString(),"Criteria:
294-
SQL (may not be complete): SELECT FROM modifier_test WHERE modifier_test.name IS NOT NULL
295-
Params: ");
309+
$this->assertEquals($mc->createSelectSql($this->arr),"SELECT FROM modifier_test WHERE modifier_test.name IS NOT NULL ");
296310
}
297311

298312
public function testValueNullWithoutOperator()
@@ -311,9 +325,6 @@ public function testValueNullWithoutOperator()
311325
$criterion = $mc->getMap()['modifier_test.name'];
312326
$this->assertNull($criterion->getValue());
313327
$this->assertEquals(Criteria::ISNULL, $criterion->getComparison());
314-
315-
$this->assertEquals($mc->toString(), "Criteria:
316-
SQL (may not be complete): SELECT FROM modifier_test WHERE modifier_test.name IS NULL
317-
Params: ");
328+
$this->assertEquals($mc->createSelectSql($this->arr), "SELECT FROM modifier_test WHERE modifier_test.name IS NULL ");
318329
}
319330
}

0 commit comments

Comments
 (0)