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+ use yii \helpers \ArrayHelper ;
13+
14+ class WithSyntaxTest extends \Codeception \Test \Unit
15+ {
16+ /**
17+ * @throws QueryRelationManagerException
18+ */
19+ public function testCity ()
20+ {
21+ $ result = City::select ('c ' )
22+ ->with ('addresses ' , 'a ' )
23+ ->with ('places ' , 'p ' , 'a ' )
24+ ->all ();
25+
26+ expect_that ($ this ->compareCityResultWithCorrectMap ($ result , [
27+ 1 => [
28+ 1 => [1 , 2 ],
29+ 2 => [3 ],
30+ ],
31+ 2 => [
32+ 3 => [4 , 5 ],
33+ 4 => [6 ],
34+ ],
35+ 3 => [],
36+ 4 => [],
37+ 5 => [],
38+ ]));
39+ }
40+
41+ /**
42+ * @throws QueryRelationManagerException
43+ */
44+ public function testAddress ()
45+ {
46+ $ result = Address::select ('a ' )
47+ ->with ('city ' , 'c ' )
48+ ->with ('places ' , 'p ' )
49+ ->with (
50+ 'comments ' , 'cm ' , 'p ' ,
51+ 'left ' , 'and cm.mark >= :mark ' , [':mark ' => 3 ]
52+ )
53+ ->all ();
54+
55+ $ addressIds = ArrayHelper::getColumn ($ result , 'id ' );
56+ sort ($ addressIds );
57+ expect_that ($ addressIds == [1 , 2 , 3 , 4 ]);
58+
59+ $ cityIds = ArrayHelper::getColumn ($ result , 'city.id ' );
60+ sort ($ cityIds );
61+ expect_that ($ cityIds == [1 , 1 , 2 , 2 ]);
62+
63+ $ placeIdToCommentMarkMap = [
64+ 1 => [3 , 5 ],
65+ 2 => [],
66+ 3 => [5 ],
67+ 4 => [],
68+ 5 => [4 ],
69+ 6 => [3 ],
70+ ];
71+
72+ foreach ($ result as $ address ) {
73+ foreach ($ address ['places ' ] as $ place ) {
74+ $ placeMarks = ArrayHelper::getColumn ($ place ['comments ' ], 'mark ' );
75+ expect_that ($ placeIdToCommentMarkMap [$ place ['id ' ]] == $ placeMarks );
76+ }
77+ }
78+ }
79+
80+ /**
81+ * @param array $result
82+ * @param array $correctMap
83+ * @return bool
84+ */
85+ protected function compareCityResultWithCorrectMap (array $ result , array $ correctMap )
86+ {
87+ $ resultMap = [];
88+ foreach ($ result as $ city ) {
89+ $ resultMap [$ city ['id ' ]] = [];
90+ foreach ($ city ['addresses ' ] as $ address ) {
91+ $ resultMap [$ city ['id ' ]][$ address ['id ' ]] = [];
92+ foreach ($ address ['places ' ] as $ place ) {
93+ $ resultMap [$ city ['id ' ]][$ address ['id ' ]][] = $ place ['id ' ];
94+ }
95+ sort ($ resultMap [$ city ['id ' ]][$ address ['id ' ]]);
96+ }
97+ ksort ($ resultMap [$ city ['id ' ]]);
98+ }
99+ ksort ($ resultMap );
100+
101+ return $ resultMap == $ correctMap ;
102+ }
103+ }
0 commit comments