1+ <?php
2+
3+ namespace app \tests \unit ;
4+
5+
6+ use app \models \Address ;
7+ use app \models \City ;
8+ use Smoren \Yii2 \QueryRelationManager \Base \QueryRelationManagerException ;
9+ use Smoren \Yii2 \QueryRelationManager \Yii2 \QueryRelationDataProvider ;
10+ use Smoren \Yii2 \QueryRelationManager \Yii2 \QueryRelationManager ;
11+ use Yii ;
12+
13+ class DataProviderTest extends \Codeception \Test \Unit
14+ {
15+ /**
16+ * @throws QueryRelationManagerException
17+ */
18+ public function testCity ()
19+ {
20+ $ qrm = QueryRelationManager::select (City::class, 'c ' )
21+ ->withMultiple ('addresses ' , Address::class, 'a ' , 'c ' , ['city_id ' => 'id ' ]);
22+
23+ $ dataProvider = new QueryRelationDataProvider ([
24+ 'queryRelationManager ' => $ qrm ,
25+ 'db ' => Yii::$ app ->db ,
26+ 'pagination ' => [
27+ 'pageSize ' => 2 ,
28+ 'page ' => 0 ,
29+ ],
30+ ]);
31+
32+ $ result = $ dataProvider ->getModels ();
33+ expect_that ($ this ->compareResultWithCorrectMap ($ result , [
34+ 1 => [1 , 2 ],
35+ 2 => [3 , 4 ],
36+ ]));
37+
38+ $ dataProvider = new QueryRelationDataProvider ([
39+ 'queryRelationManager ' => $ qrm ,
40+ 'pagination ' => [
41+ 'pageSize ' => 2 ,
42+ 'page ' => 1 ,
43+ ],
44+ ]);
45+ $ result = $ dataProvider ->getModels ();
46+ expect_that ($ this ->compareResultWithCorrectMap ($ result , [
47+ 3 => [],
48+ 4 => [],
49+ ]));
50+
51+ $ dataProvider = new QueryRelationDataProvider ([
52+ 'queryRelationManager ' => $ qrm ,
53+ 'pagination ' => [
54+ 'pageSize ' => 2 ,
55+ 'page ' => 2 ,
56+ ],
57+ ]);
58+ $ result = $ dataProvider ->getModels ();
59+ expect_that ($ this ->compareResultWithCorrectMap ($ result , [
60+ 5 => [],
61+ ]));
62+
63+ $ dataProvider = new QueryRelationDataProvider ([
64+ 'queryRelationManager ' => $ qrm ,
65+ 'pagination ' => [
66+ 'pageSize ' => 2 ,
67+ 'page ' => 3 ,
68+ ],
69+ ]);
70+ $ result = $ dataProvider ->getModels ();
71+ expect_that ($ this ->compareResultWithCorrectMap ($ result , []));
72+
73+ $ dataProvider = new QueryRelationDataProvider ([
74+ 'queryRelationManager ' => $ qrm ,
75+ 'pagination ' => [
76+ 'pageSize ' => 1 ,
77+ 'page ' => 0 ,
78+ ],
79+ ]);
80+ $ result = $ dataProvider ->getModels ();
81+ expect_that ($ this ->compareResultWithCorrectMap ($ result , [
82+ 1 => [1 , 2 ],
83+ ]));
84+
85+ $ dataProvider = new QueryRelationDataProvider ([
86+ 'queryRelationManager ' => $ qrm ,
87+ 'pagination ' => [
88+ 'pageSize ' => 5 ,
89+ 'page ' => 0 ,
90+ ],
91+ ]);
92+ $ result = $ dataProvider ->getModels ();
93+ expect_that ($ this ->compareResultWithCorrectMap ($ result , [
94+ 1 => [1 , 2 ],
95+ 2 => [3 , 4 ],
96+ 3 => [],
97+ 4 => [],
98+ 5 => [],
99+ ]));
100+
101+ $ dataProvider = new QueryRelationDataProvider ([
102+ 'queryRelationManager ' => $ qrm ,
103+ 'pagination ' => [
104+ 'pageSize ' => 1000 ,
105+ 'page ' => 0 ,
106+ ],
107+ ]);
108+ $ result = $ dataProvider ->getModels ();
109+ expect_that ($ this ->compareResultWithCorrectMap ($ result , [
110+ 1 => [1 , 2 ],
111+ 2 => [3 , 4 ],
112+ 3 => [],
113+ 4 => [],
114+ 5 => [],
115+ ]));
116+
117+ $ dataProvider = new QueryRelationDataProvider ([
118+ 'queryRelationManager ' => $ qrm ,
119+ 'pagination ' => [
120+ 'pageSize ' => 1000 ,
121+ 'page ' => 1 ,
122+ ],
123+ ]);
124+ $ result = $ dataProvider ->getModels ();
125+ expect_that ($ this ->compareResultWithCorrectMap ($ result , []));
126+ }
127+
128+ protected function compareResultWithCorrectMap (array $ result , array $ correctMap )
129+ {
130+ $ resultMap = [];
131+ foreach ($ result as $ city ) {
132+ $ resultMap [$ city ['id ' ]] = [];
133+ foreach ($ city ['addresses ' ] as $ address ) {
134+ $ resultMap [$ city ['id ' ]][] = $ address ['id ' ];
135+ }
136+ sort ($ resultMap [$ city ['id ' ]]);
137+ }
138+ ksort ($ resultMap );
139+
140+ return $ resultMap == $ correctMap ;
141+ }
142+ }
0 commit comments