Skip to content

Commit 84e485a

Browse files
committed
Добавлены тесты
1 parent b84a6bc commit 84e485a

File tree

3 files changed

+113
-5
lines changed

3 files changed

+113
-5
lines changed

commands/WithController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ class WithController extends Controller
2525
*/
2626
public function actionCity()
2727
{
28-
$cities = City::select('c')
28+
$result = City::select('c')
2929
->with('addresses', 'a')
3030
->with('places', 'p', 'a')
3131
->all();
3232

33-
print_r($cities);
33+
print_r($result);
3434
}
3535

3636
/**
3737
* Выбираем адреса с городом, местами и комментариями, оценка которых не ниже трех
3838
* Используем упрощенный синтаксис with
3939
* Метод City::select() добавлен в класс с помощью трейта ActiveRecordTrait
40-
* @see \Smoren\Yii2\QueryRelationManager\ActiveRecord\ActiveRecordTrait
40+
* @see \Smoren\Yii2\QueryRelationManager\Yii2\ActiveRecordTrait
4141
* @throws QueryRelationManagerException
4242
*/
4343
public function actionAddress()
4444
{
45-
$cities = Address::select('a')
45+
$result = Address::select('a')
4646
->with('city', 'c')
4747
->with('places', 'p')
4848
->with(
@@ -51,6 +51,6 @@ public function actionAddress()
5151
)
5252
->all();
5353

54-
print_r($cities);
54+
print_r($result);
5555
}
5656
}

tests/unit/DataProviderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public function testCity()
125125
expect_that($this->compareResultWithCorrectMap($result, []));
126126
}
127127

128+
/**
129+
* @param array $result
130+
* @param array $correctMap
131+
* @return bool
132+
*/
128133
protected function compareResultWithCorrectMap(array $result, array $correctMap)
129134
{
130135
$resultMap = [];

tests/unit/WithSyntaxTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)