Skip to content

Commit 08da413

Browse files
author
igor-chepurnoi
committed
add tests
1 parent 711841f commit 08da413

File tree

9 files changed

+299
-3
lines changed

9 files changed

+299
-3
lines changed

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ignore all test and documentation for archive
2+
/.gitattributes export-ignore
3+
/.gitignore export-ignore
4+
/.scrutinizer.yml export-ignore
5+
/.travis.yml export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/tests export-ignore
8+
/docs export-ignore

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
/composer.lock
19+
20+
# composer itself is not needed
21+
composer.phar
22+
23+
# Mac DS_Store Files
24+
.DS_Store
25+
26+
# phpunit itself is not needed
27+
phpunit.phar
28+
# local phpunit config
29+
/phpunit.xml
30+
31+
# local tests configuration
32+
/tests/data/config.local.php
33+
34+
# runtime cache
35+
/tests/runtime

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- hhvm
9+
10+
# run build against hhvm but allow them to fail
11+
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
12+
matrix:
13+
fast_finish: true
14+
allow_failures:
15+
- php: hhvm
16+
17+
# faster builds on new travis setup not using sudo
18+
sudo: false
19+
20+
# cache vendor dirs
21+
cache:
22+
directories:
23+
- $HOME/.composer/cache
24+
25+
install:
26+
- travis_retry composer self-update && composer --version
27+
- travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1"
28+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
29+
- travis_retry composer install --prefer-dist --no-interaction
30+
31+
script:
32+
- phpunit --verbose $PHPUNIT_FLAGS

ArrayQuery.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
use yii\base\Component;
77
use yii\db\QueryTrait;
88

9+
/**
10+
* Class ArrayQuery
11+
* @package yii2mod\query
12+
*/
913
class ArrayQuery extends Component
1014
{
1115
use QueryTrait;
@@ -23,7 +27,7 @@ class ArrayQuery extends Component
2327
/**
2428
* @var string the class for processing the queries
2529
*/
26-
public $queryProcessorClass = 'app\components\QueryProcessor';
30+
public $queryProcessorClass = 'yii2mod\query\QueryProcessor';
2731

2832
/**
2933
* @var QueryProcessor

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ $data = [
3737
],
3838
[
3939
'id' => 2,
40-
'name' => 'test',
41-
'username' => 'test@example.com'
40+
'username' => 'test',
41+
'email' => 'test@example.com'
4242
],
4343
];
4444

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
<testsuites>
9+
<testsuite name="Yii2mod Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

tests/ArrayQueryTest.php

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

tests/TestCase.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace yii2mod\query\tests;
4+
5+
use yii\helpers\ArrayHelper;
6+
use Yii;
7+
8+
/**
9+
* This is the base class for all yii framework unit tests.
10+
*/
11+
class TestCase extends \PHPUnit_Framework_TestCase
12+
{
13+
protected function setUp()
14+
{
15+
parent::setUp();
16+
$this->mockApplication();
17+
}
18+
19+
protected function tearDown()
20+
{
21+
$this->destroyApplication();
22+
}
23+
24+
/**
25+
* Populates Yii::$app with a new application
26+
* The application will be destroyed on tearDown() automatically.
27+
* @param array $config The application configuration, if needed
28+
* @param string $appClass name of the application class to create
29+
*/
30+
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
31+
{
32+
new $appClass(ArrayHelper::merge([
33+
'id' => 'testapp',
34+
'basePath' => __DIR__,
35+
'vendorPath' => $this->getVendorPath()
36+
], $config));
37+
}
38+
39+
/**
40+
* @return string vendor path
41+
*/
42+
protected function getVendorPath()
43+
{
44+
return dirname(__DIR__) . '/vendor';
45+
}
46+
47+
/**
48+
* Destroys application in Yii::$app by setting it to null.
49+
*/
50+
protected function destroyApplication()
51+
{
52+
Yii::$app = null;
53+
}
54+
}

tests/bootstrap.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// ensure we get report on all possible php errors
4+
error_reporting(-1);
5+
6+
define('YII_ENABLE_ERROR_HANDLER', false);
7+
define('YII_DEBUG', true);
8+
9+
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
10+
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
11+
12+
require_once(__DIR__ . '/../vendor/autoload.php');
13+
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

0 commit comments

Comments
 (0)