Skip to content

Commit 8391558

Browse files
committed
some tests wrote
1 parent 5fb650d commit 8391558

File tree

6 files changed

+194
-1
lines changed

6 files changed

+194
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ composer install
1414
php yii migrate
1515
```
1616

17+
### Unit testing
18+
```
19+
./vendor/bin/codecept run unit tests/unit/CommonUsageTest.php
20+
```
21+
1722
### Demo
1823

1924
Выбираем адреса с городом, местами и комментариями о местах

config/test_db.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
$db = require __DIR__ . '/db.php';
33
// test database! Important not to run tests on production or development databases
4-
$db['dsn'] = 'mysql:host=localhost;dbname=yii2_basic_tests';
4+
$db['dsn'] = 'mysql:host=localhost;dbname=yii2_query_relation_manager_demo';
55

66
return $db;

tests/_bootstrap.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
defined('YII_ENV') or define('YII_ENV', 'test');
4+
5+
require_once dirname(__DIR__) . '/vendor/yiisoft/yii2/Yii.php';
6+
require dirname(__DIR__) . '/vendor/autoload.php';
7+

tests/_support/UnitTester.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
/**
5+
* Inherited Methods
6+
* @method void wantToTest($text)
7+
* @method void wantTo($text)
8+
* @method void execute($callable)
9+
* @method void expectTo($prediction)
10+
* @method void expect($prediction)
11+
* @method void amGoingTo($argumentation)
12+
* @method void am($role)
13+
* @method void lookForwardTo($achieveValue)
14+
* @method void comment($description)
15+
* @method void pause()
16+
*
17+
* @SuppressWarnings(PHPMD)
18+
*/
19+
class UnitTester extends \Codeception\Actor
20+
{
21+
use _generated\UnitTesterActions;
22+
23+
/**
24+
* Define custom actions here
25+
*/
26+
}

tests/unit.suite.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Codeception Test Suite Configuration
2+
#
3+
# Suite for unit or integration tests.
4+
5+
class_name: UnitTester
6+
modules:
7+
enabled:
8+
- Asserts
9+
- Yii2:
10+
part: [orm, email, fixtures]

tests/unit/CommonUsageTest.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
namespace app\tests\unit;
4+
5+
use app\models\Address;
6+
use app\models\City;
7+
use app\models\Comment;
8+
use app\models\Place;
9+
use Smoren\Yii2\QueryRelationManager\Base\QueryRelationManagerException;
10+
use Smoren\Yii2\QueryRelationManager\Yii2\QueryRelationManager;
11+
use yii\db\Query;
12+
use yii\helpers\ArrayHelper;
13+
14+
class CommonUsageTest extends \Codeception\Test\Unit
15+
{
16+
/**
17+
* @throws QueryRelationManagerException
18+
*/
19+
public function testAddress()
20+
{
21+
$result = QueryRelationManager::select(Address::class, 'a')
22+
->withSingle('city', City::class, 'c', 'a', ['id' => 'city_id'])
23+
->withMultiple('places', Place::class, 'p', 'a', ['address_id' => 'id'])
24+
->withMultiple('comments', Comment::class, 'cm', 'p', ['place_id' => 'id'])
25+
->all();
26+
27+
expect_that(count($result) == 4);
28+
29+
$resultMap = ArrayHelper::index($result, 'id');
30+
31+
expect_that($resultMap[1]['city']['name'] == 'Moscow');
32+
expect_that($resultMap[2]['city']['name'] == 'Moscow');
33+
expect_that($resultMap[3]['city']['name'] == 'St. Petersburg');
34+
expect_that($resultMap[4]['city']['name'] == 'St. Petersburg');
35+
36+
expect_that(count($resultMap[1]['places']) == 2);
37+
expect_that(count($resultMap[2]['places']) == 1);
38+
expect_that(count($resultMap[3]['places']) == 2);
39+
expect_that(count($resultMap[4]['places']) == 1);
40+
41+
$mapPlaceIdToCommentsCount = [
42+
1 => 3,
43+
2 => 0,
44+
3 => 1,
45+
4 => 0,
46+
5 => 1,
47+
6 => 1,
48+
];
49+
50+
foreach($resultMap as $addressId => &$address) {
51+
foreach($address['places'] as $place) {
52+
expect_that(count($place['comments']) == $mapPlaceIdToCommentsCount[$place['id']]);
53+
}
54+
}
55+
unset($address);
56+
}
57+
58+
/**
59+
* @throws QueryRelationManagerException
60+
*/
61+
public function testPlace()
62+
{
63+
$result = QueryRelationManager::select(Place::class, 'p')
64+
->withSingle('address', Address::class, 'a', 'p', ['id' => 'address_id'])
65+
->withSingle('city', City::class, 'c', 'a', ['id' => 'city_id'])
66+
->withMultiple('comments', Comment::class, 'cm', 'p', ['place_id' => 'id'],
67+
'inner', 'and cm.mark >= :mark', [':mark' => 3])
68+
->modify('p', function(array &$place) {
69+
$place['comments_count'] = count($place['comments']);
70+
$place['mark_five_count'] = 0;
71+
$place['mark_average'] = 0;
72+
73+
foreach($place['comments'] as $comment) {
74+
$place['mark_average'] += $comment['mark'];
75+
if($comment['mark'] == 5) {
76+
$place['mark_five_count']++;
77+
}
78+
}
79+
80+
$place['mark_average'] /= $place['comments_count'];
81+
})
82+
->all();
83+
84+
expect_that(count($result) == 4);
85+
86+
$resultMap = ArrayHelper::index($result, 'id');
87+
88+
expect_that($resultMap[1]['address']['name'] == 'Tverskaya st., 7');
89+
expect_that($resultMap[3]['address']['name'] == 'Schipok st., 1');
90+
expect_that($resultMap[5]['address']['name'] == 'Mayakovskogo st., 12');
91+
expect_that($resultMap[6]['address']['name'] == 'Galernaya st., 3');
92+
93+
expect_that($resultMap[1]['address']['city']['name'] == 'Moscow');
94+
expect_that($resultMap[3]['address']['city']['name'] == 'Moscow');
95+
expect_that($resultMap[5]['address']['city']['name'] == 'St. Petersburg');
96+
expect_that($resultMap[6]['address']['city']['name'] == 'St. Petersburg');
97+
98+
expect_that(count($resultMap[1]['comments']) == 2);
99+
expect_that(count($resultMap[3]['comments']) == 1);
100+
expect_that(count($resultMap[5]['comments']) == 1);
101+
expect_that(count($resultMap[6]['comments']) == 1);
102+
103+
expect_that($resultMap[1]['comments_count'] == 2);
104+
expect_that($resultMap[3]['comments_count'] == 1);
105+
expect_that($resultMap[5]['comments_count'] == 1);
106+
expect_that($resultMap[6]['comments_count'] == 1);
107+
108+
expect_that($resultMap[1]['mark_five_count'] == 1);
109+
expect_that($resultMap[3]['mark_five_count'] == 1);
110+
expect_that($resultMap[5]['mark_five_count'] == 0);
111+
expect_that($resultMap[6]['mark_five_count'] == 0);
112+
113+
expect_that($resultMap[1]['mark_average'] == 4);
114+
expect_that($resultMap[3]['mark_average'] == 5);
115+
expect_that($resultMap[5]['mark_average'] == 4);
116+
expect_that($resultMap[6]['mark_average'] == 3);
117+
}
118+
119+
/**
120+
* @throws QueryRelationManagerException
121+
*/
122+
public function testCity()
123+
{
124+
$cityIds = City::find()->limit(2)->offset(1)->select('id')->column();
125+
expect_that(count($cityIds) == 2);
126+
127+
$result = QueryRelationManager::select(City::class, 'c')
128+
->withMultiple('addresses', Address::class, 'a', 'c', ['city_id' => 'id'])
129+
->filter(function(Query $q) use ($cityIds) {
130+
$q->andWhere(['c.id' => $cityIds])->orderBy(['a.id' => SORT_ASC]);
131+
})
132+
->all();
133+
134+
expect_that(count($result) == 2);
135+
expect_that(array_diff(ArrayHelper::getColumn($result, 'id'), $cityIds) == []);
136+
137+
$resultMap = ArrayHelper::index($result, 'id');
138+
139+
expect_that($resultMap[3]['name'] == 'Samara');
140+
expect_that($resultMap[2]['name'] == 'St. Petersburg');
141+
142+
expect_that(count($resultMap[3]['addresses']) == 0);
143+
expect_that(count($resultMap[2]['addresses']) == 2);
144+
}
145+
}

0 commit comments

Comments
 (0)