1+ <?php
2+
3+ namespace yii2mod \query \tests ;
4+
5+ use yii2mod \query \ArrayQuery ;
6+
7+ /**
8+ * Class ArrayQueryTest
9+ * @package yii2mod\query\tests
10+ */
11+ class ArrayQueryTest extends TestCase
12+ {
13+ /**
14+ * @return array
15+ */
16+ protected function getTestData ()
17+ {
18+ return [
19+ [
20+ 'id ' => 1 ,
21+ 'username ' => 'admin ' ,
22+ 'email ' => 'admin@example.com ' ,
23+ ],
24+ [
25+ 'id ' => 2 ,
26+ 'username ' => 'test ' ,
27+ 'email ' => 'test@example.com '
28+ ],
29+ [
30+ 'id ' => 3 ,
31+ 'username ' => 'guest ' ,
32+ 'email ' => 'guest@example.com '
33+ ],
34+ ];
35+ }
36+
37+ // Tests :
38+
39+ public function testWhereCondition ()
40+ {
41+ $ query = new ArrayQuery ();
42+ $ query ->from ($ this ->getTestData ());
43+ $ query ->where (['username ' => 'admin ' ]);
44+
45+ $ rows = $ query ->all ();
46+ $ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
47+ }
48+
49+ public function testLikeCondition ()
50+ {
51+ $ query = new ArrayQuery ();
52+ $ query ->from ($ this ->getTestData ());
53+ $ query ->where (['like ' , 'email ' , 'test ' ]);
54+
55+ $ rows = $ query ->all ();
56+ $ this ->assertEquals ('test ' , $ rows [0 ]['username ' ]);
57+ }
58+
59+ public function testApplyLimit ()
60+ {
61+ $ query = new ArrayQuery ();
62+ $ query ->from ($ this ->getTestData ());
63+ $ query ->where (['like ' , 'email ' , 'example.com ' ]);
64+ $ query ->limit (2 );
65+
66+ $ rows = $ query ->all ();
67+ $ this ->assertEquals (2 , count ($ rows ));
68+ $ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
69+ $ this ->assertEquals ('test ' , $ rows [1 ]['username ' ]);
70+ }
71+
72+ public function testFetchFirstRow ()
73+ {
74+ $ query = new ArrayQuery ();
75+ $ query ->from ($ this ->getTestData ());
76+
77+ $ row = $ query ->one ();
78+ $ this ->assertEquals ('admin ' , $ row ['username ' ]);
79+ }
80+
81+ public function testOrCondition ()
82+ {
83+ $ query = new ArrayQuery ();
84+ $ query ->from ($ this ->getTestData ());
85+ $ query ->where (['or ' , ['username ' => 'admin ' ], ['id ' => 3 ]]);
86+
87+ $ rows = $ query ->all ();
88+ $ this ->assertEquals (2 , count ($ rows ));
89+ $ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
90+ $ this ->assertEquals (3 , $ rows [1 ]['id ' ]);
91+ }
92+
93+ public function testBetweenCondition ()
94+ {
95+ $ query = new ArrayQuery ();
96+ $ query ->from ($ this ->getTestData ());
97+ $ query ->where (['between ' , 'id ' , 1 , 2 ]);
98+
99+ $ rows = $ query ->all ();
100+ $ this ->assertEquals (2 , count ($ rows ));
101+ $ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
102+ $ this ->assertEquals ('test ' , $ rows [1 ]['username ' ]);
103+ }
104+
105+ public function testNotCondition ()
106+ {
107+ $ query = new ArrayQuery ();
108+ $ query ->from ($ this ->getTestData ());
109+ $ query ->where (['not ' , ['username ' => 'admin ' ]]);
110+
111+ $ rows = $ query ->all ();
112+ $ this ->assertEquals (2 , count ($ rows ));
113+ $ this ->assertEquals ('test ' , $ rows [0 ]['username ' ]);
114+ $ this ->assertEquals ('guest ' , $ rows [1 ]['username ' ]);
115+ }
116+
117+ public function testInCondition ()
118+ {
119+ $ query = new ArrayQuery ();
120+ $ query ->from ($ this ->getTestData ());
121+ $ query ->where (['in ' , 'id ' , [1 , 3 ]]);
122+
123+ $ rows = $ query ->all ();
124+ $ this ->assertEquals (2 , count ($ rows ));
125+ $ this ->assertEquals ('admin ' , $ rows [0 ]['username ' ]);
126+ $ this ->assertEquals ('guest ' , $ rows [1 ]['username ' ]);
127+ }
128+
129+ public function testExistsCondition ()
130+ {
131+ $ query = new ArrayQuery ();
132+ $ query ->from ($ this ->getTestData ());
133+ $ query ->where (['username ' => 'admin ' ]);
134+
135+ $ this ->assertTrue ($ query ->exists ());
136+ }
137+ }
0 commit comments