1+ <?php
2+ /**
3+ * @author Aleksandar Panic
4+ * @license http://www.apache.org/licenses/LICENSE-2.0
5+ * @since 1.0.0
6+ **/
7+
8+ namespace tests \Operators ;
9+
10+
11+ use ArekX \ArrayExpression \ExpressionParser ;
12+ use ArekX \ArrayExpression \Operators \ValueOperator ;
13+ use ArekX \ArrayExpression \ValueParsers \ArrayValueParser ;
14+ use ArekX \ArrayExpression \ValueParsers \SingleValueParser ;
15+ use tests \Mocks \MockOperator ;
16+ use tests \Spies \BetweenOperatorSpy ;
17+ use tests \Spies \GetOperatorSpy ;
18+ use tests \TestCase ;
19+
20+ /**
21+ * Class BetweenOperatorTest
22+ * @package tests\Operators
23+ *
24+ */
25+ class BetweenOperatorTest extends TestCase
26+ {
27+ public function testParserIsSet ()
28+ {
29+ $ i = $ this ->createInstance ();
30+ $ parser = new ExpressionParser ();
31+ $ i ->setParser ($ parser );
32+ $ this ->assertSame ($ parser , $ i ->getParser ());
33+ }
34+
35+ public function testNameIsSet ()
36+ {
37+ $ i = $ this ->createInstance ();
38+ $ i ->configure (['between ' , ['value ' , 1 ], ['value ' , 1 ], ['value ' , 1 ]]);
39+ $ this ->assertSame ('between ' , $ i ->getName ());
40+ $ i ->configure ([-1 => 'nope ' , 1 => ['value ' , 1 ], 2 => ['value ' , 1 ], 3 => ['value ' , 1 ]]);
41+ $ this ->assertSame ('unknown ' , $ i ->getName ());
42+ }
43+
44+ public function testThrowsErrorIfNoParams ()
45+ {
46+ $ i = $ this ->createInstance ();
47+ $ this ->expectException (\InvalidArgumentException::class);
48+ $ i ->configure (['between ' ]);
49+ }
50+
51+ public function testThrowsErrorIfOnlyOneParam ()
52+ {
53+ $ i = $ this ->createInstance ();
54+ $ this ->expectException (\InvalidArgumentException::class);
55+ $ i ->configure (['between ' , ['value ' , 1 ]]);
56+ }
57+
58+ public function testThrowsErrorIfOnlyTwoParams ()
59+ {
60+ $ i = $ this ->createInstance ();
61+ $ this ->expectException (\InvalidArgumentException::class);
62+ $ i ->configure (['between ' , ['value ' , 1 ], ['value ' , 1 ]]);
63+ }
64+
65+
66+ public function testBetweenTests ()
67+ {
68+ $ i = $ this ->createInstance ();
69+
70+ $ tests = [
71+ [['between ' , ['value ' , 1 ], ['value ' , 0 ], ['value ' , 2 ]], true ],
72+ [['between ' , ['value ' , 22 ], ['value ' , 0 ], ['value ' , 2 ]], false ],
73+ [['between ' , ['value ' , -200 ], ['value ' , 0 ], ['value ' , 2 ]], false ],
74+ [['between ' , ['value ' , 0 ], ['value ' , 0 ], ['value ' , 2 ]], true ],
75+ [['between ' , ['value ' , 2 ], ['value ' , 0 ], ['value ' , 2 ]], true ],
76+ ];
77+
78+ $ value = SingleValueParser::from ("" );
79+ foreach ($ tests as $ test ) {
80+ $ i ->configure ($ test [0 ]);
81+ $ this ->assertSame ($ test [1 ], $ i ->evaluate ($ value ));
82+ }
83+ }
84+
85+ protected function createInstance (): BetweenOperatorSpy
86+ {
87+ $ parser = new ExpressionParser ();
88+ $ parser ->setType ('mock ' , MockOperator::class);
89+ $ parser ->setType ('value ' , ValueOperator::class);
90+ $ operator = new BetweenOperatorSpy ();
91+ $ operator ->setParser ($ parser );
92+
93+ return $ operator ;
94+ }
95+ }
0 commit comments