Skip to content

Commit 29a70d2

Browse files
committed
Add tests
1 parent 9300f67 commit 29a70d2

File tree

15 files changed

+393
-0
lines changed

15 files changed

+393
-0
lines changed

phpunit.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="true"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
<testsuites>
15+
<testsuite name="Yii2 Config Test Suite">
16+
<directory>./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
</phpunit>

tests/BootstrapTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
use codemix\yii2confload\Config;
3+
4+
class BootstrapTest extends \PHPUnit_Framework_TestCase
5+
{
6+
public function testCanBootstrapApplication()
7+
{
8+
$config = Config::bootstrap(__DIR__ . '/app', __DIR__ . '/../vendor');
9+
$web = $config->web();
10+
$expected = [
11+
'key1' => 'dotenv1',
12+
'key2' => [
13+
'web1',
14+
'web2',
15+
],
16+
'key3' => 2,
17+
'key4' => 'web4',
18+
];
19+
20+
$this->assertEquals($expected, $web);
21+
$this->assertTrue(defined('YII_DEBUG'));
22+
$this->assertTrue(defined('YII_ENV'));
23+
$this->assertTrue(defined('YII_ENV_PROD'));
24+
$this->assertEquals(0, YII_DEBUG);
25+
$this->assertEquals('dev', YII_ENV);
26+
$this->assertTrue(class_exists('Yii', false));
27+
}
28+
}

tests/ConsoleConfigTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
use codemix\yii2confload\Config;
3+
4+
class ConsoleConfigTest extends \PHPUnit_Framework_TestCase
5+
{
6+
public function testCanGetConfig()
7+
{
8+
$config = new Config(__DIR__ . '/app');
9+
$console = $config->console();
10+
$expected = [
11+
'key1' => 'dotenv1',
12+
'key2' => [
13+
'web1',
14+
'web2',
15+
],
16+
'key3' => 'console3',
17+
'key4' => [
18+
'console3',
19+
'console4',
20+
],
21+
];
22+
$this->assertEquals($expected, $console);
23+
}
24+
25+
public function testCanMergeCustomConfig()
26+
{
27+
$config = new Config(__DIR__ . '/app');
28+
$console = $config->console([
29+
'key5' => 'test',
30+
]);
31+
$expected = [
32+
'key1' => 'dotenv1',
33+
'key2' => [
34+
'web1',
35+
'web2',
36+
],
37+
'key3' => 'console3',
38+
'key4' => [
39+
'console3',
40+
'console4',
41+
],
42+
'key5' => 'test',
43+
];
44+
$this->assertEquals($expected, $console);
45+
}
46+
47+
public function testCanMergeLocalConfigByEnvVar()
48+
{
49+
$config = new Config(__DIR__ . '/app');
50+
$_ENV['ENABLE_LOCALCONF'] = 1;
51+
$console = $config->console([
52+
'key5' => 'test',
53+
]);
54+
$expected = [
55+
'key1' => 'dotenv1',
56+
'key2' => [
57+
'web1',
58+
'web2',
59+
'local1',
60+
'local2',
61+
'lconsole1',
62+
'lconsole2',
63+
],
64+
'key3' => 'lconsole3',
65+
'key4' => [
66+
'console3',
67+
'console4',
68+
],
69+
'key5' => 'test',
70+
];
71+
$this->assertEquals($expected, $console);
72+
}
73+
74+
public function testCanMergeLocalConfigByArgument()
75+
{
76+
$config = new Config(__DIR__ . '/app');
77+
$console = $config->console([
78+
'key5' => 'test',
79+
], true);
80+
$expected = [
81+
'key1' => 'dotenv1',
82+
'key2' => [
83+
'web1',
84+
'web2',
85+
'lconsole1',
86+
'lconsole2',
87+
],
88+
'key3' => 'lconsole3',
89+
'key4' => [
90+
'console3',
91+
'console4',
92+
],
93+
'key5' => 'test',
94+
];
95+
$this->assertEquals($expected, $console);
96+
}
97+
}

tests/EnvTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
use codemix\yii2confload\Config;
3+
4+
class EnvTest extends \PHPUnit_Framework_TestCase
5+
{
6+
public function testCanInitYiiEnvFromEnvVars()
7+
{
8+
$_ENV['YII_DEBUG'] = true;
9+
$_ENV['YII_ENV'] = 'prod';
10+
Config::initEnv();
11+
12+
$this->assertTrue(defined('YII_DEBUG'));
13+
$this->assertTrue(defined('YII_ENV'));
14+
$this->assertTrue(YII_DEBUG);
15+
$this->assertEquals('prod', YII_ENV);
16+
}
17+
18+
public function testCanInitYiiEnvFromEnvFile()
19+
{
20+
Config::initEnv(__DIR__ . '/app');
21+
22+
$this->assertTrue(defined('YII_DEBUG'));
23+
$this->assertTrue(defined('YII_ENV'));
24+
$this->assertEquals(0, YII_DEBUG);
25+
$this->assertEquals('dev', YII_ENV);
26+
}
27+
28+
public function testCanGetEnvVars()
29+
{
30+
$_ENV['TEST1'] = 987;
31+
$_ENV['TEST2'] = 'demo';
32+
33+
$this->assertEquals(987, Config::env('TEST1'));
34+
$this->assertEquals('demo', Config::env('TEST2'));
35+
$this->assertEquals('default', Config::env('TEST3', 'default'));
36+
}
37+
38+
public function testCanGetEnvVarsFromEnvFile()
39+
{
40+
Config::initEnv(__DIR__ . '/app');
41+
42+
$this->assertEquals('', Config::env('YII_DEBUG'));
43+
$this->assertEquals('dev', Config::env('YII_ENV'));
44+
$this->assertEquals('dotenv1', Config::env('VAR1'));
45+
$this->assertEquals(2, Config::env('VAR2'));
46+
$this->assertEquals('default', Config::env('TEST3', 'default'));
47+
}
48+
49+
public function testEnvFileDoesNotClearEnvVars()
50+
{
51+
$_ENV['TEST1'] = 654;
52+
$_ENV['TEST2'] = 'xyz';
53+
Config::initEnv(__DIR__ . '/app');
54+
55+
$this->assertEquals(654, Config::env('TEST1'));
56+
$this->assertEquals('xyz', Config::env('TEST2'));
57+
$this->assertEquals('dotenv1', Config::env('VAR1'));
58+
$this->assertEquals(2, Config::env('VAR2'));
59+
$this->assertEquals('default', Config::env('TEST3', 'default'));
60+
}
61+
62+
public function testEnvFileDoesNotOverrideEnvVars()
63+
{
64+
$_ENV['VAR1'] = 654;
65+
$_ENV['VAR2'] = 'xyz';
66+
Config::initEnv(__DIR__ . '/app');
67+
68+
$this->assertEquals('', Config::env('YII_DEBUG'));
69+
$this->assertEquals('dev', Config::env('YII_ENV'));
70+
$this->assertEquals(654, Config::env('VAR1'));
71+
$this->assertEquals('xyz', Config::env('VAR2'));
72+
}
73+
74+
public function testInitsYiiEnvByDefault()
75+
{
76+
$config = new Config(__DIR__ . '/app');
77+
78+
$this->assertTrue(defined('YII_DEBUG'));
79+
$this->assertTrue(defined('YII_ENV'));
80+
$this->assertEquals(0, YII_DEBUG);
81+
$this->assertEquals('dev', YII_ENV);
82+
}
83+
84+
public function testCanSuppressYiiEnvInit()
85+
{
86+
$config = new Config(__DIR__ . '/app', false);
87+
88+
$this->assertFalse(defined('YII_DEBUG'));
89+
$this->assertFalse(defined('YII_ENV'));
90+
}
91+
}

tests/WebConfigTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
use codemix\yii2confload\Config;
3+
4+
class WebConfigTest extends \PHPUnit_Framework_TestCase
5+
{
6+
public function testCanGetConfig()
7+
{
8+
$config = new Config(__DIR__ . '/app');
9+
$web = $config->web();
10+
$expected = [
11+
'key1' => 'dotenv1',
12+
'key2' => [
13+
'web1',
14+
'web2',
15+
],
16+
'key3' => 2,
17+
'key4' => 'web4',
18+
];
19+
20+
$this->assertEquals($expected, $web);
21+
}
22+
23+
public function testCanMergeCustomConfig()
24+
{
25+
$config = new Config(__DIR__ . '/app');
26+
$web = $config->web([
27+
'key5' => 'test',
28+
]);
29+
$expected = [
30+
'key1' => 'dotenv1',
31+
'key2' => [
32+
'web1',
33+
'web2',
34+
],
35+
'key3' => 2,
36+
'key4' => 'web4',
37+
'key5' => 'test',
38+
];
39+
40+
$this->assertEquals($expected, $web);
41+
}
42+
43+
public function testCanMergeLocalConfigByEnvVar()
44+
{
45+
$config = new Config(__DIR__ . '/app');
46+
$_ENV['ENABLE_LOCALCONF'] = 1;
47+
$web = $config->web([
48+
'key5' => 'test',
49+
]);
50+
$expected = [
51+
'key1' => 'dotenv1',
52+
'key2' => [
53+
'web1',
54+
'web2',
55+
'local1',
56+
'local2',
57+
],
58+
'key3' => 'local3',
59+
'key4' => 'web4',
60+
'key5' => 'test',
61+
];
62+
$this->assertEquals($expected, $web);
63+
}
64+
65+
public function testCanMergeLocalConfigByArgument()
66+
{
67+
$config = new Config(__DIR__ . '/app');
68+
$web = $config->web([
69+
'key5' => 'test',
70+
], true);
71+
$expected = [
72+
'key1' => 'dotenv1',
73+
'key2' => [
74+
'web1',
75+
'web2',
76+
'local1',
77+
'local2',
78+
],
79+
'key3' => 'local3',
80+
'key4' => 'web4',
81+
'key5' => 'test',
82+
];
83+
$this->assertEquals($expected, $web);
84+
}
85+
}

tests/app/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
YII_DEBUG=0
2+
YII_ENV=dev
3+
VAR1='dotenv1'
4+
VAR2=2

tests/app/config/console.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
return $this->web([
3+
'key3' => 'console3',
4+
'key4' => [
5+
'console3',
6+
'console4',
7+
],
8+
]);

tests/app/config/local-console.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
return [
3+
'key3' => 'lconsole3',
4+
'key2' => [
5+
'lconsole1',
6+
'lconsole2',
7+
],
8+
];

tests/app/config/local.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
return [
3+
'key3' => 'local3',
4+
'key2' => [
5+
'local1',
6+
'local2',
7+
],
8+
];

tests/app/config/web.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
return [
3+
'key1' => self::env('VAR1'),
4+
'key2' => [
5+
'web1',
6+
'web2',
7+
],
8+
'key3' => self::env('VAR2'),
9+
'key4' => 'web4',
10+
];

0 commit comments

Comments
 (0)