Skip to content

Commit 4d47bb1

Browse files
committed
Add unit testing
1 parent 608d0e1 commit 4d47bb1

18 files changed

Lines changed: 606 additions & 1 deletion

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/test
21
/vendor
32
/.idea
43
/composer.lock

test/PConfigTest.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace pconfig;
4+
5+
use Exception;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class PConfigTest extends TestCase
9+
{
10+
private $basePath = __DIR__ . '/data/';
11+
12+
public function testJSON()
13+
{
14+
$this->testConfig('json');
15+
}
16+
17+
public function testINI()
18+
{
19+
$this->testConfig('ini');
20+
}
21+
22+
public function testPHP()
23+
{
24+
$this->testConfig('php');
25+
}
26+
27+
public function testYAML()
28+
{
29+
$this->testConfig('yaml');
30+
}
31+
32+
private function testConfig($type)
33+
{
34+
$config = null;
35+
try {
36+
$config = new PConfig($this->basePath . 'config.' . $type);
37+
} catch (Exception $e) {
38+
$this->fail($e->getMessage());
39+
}
40+
$this->assertNotNull($config);
41+
42+
$this->assertArrayHasKey('language', $config);
43+
$this->assertArrayHasKey("php", $config->get('language'));
44+
$this->assertArrayHasKey("php", $config['language']);
45+
$this->assertArrayHasKey("type", $config['language']['php']);
46+
$this->assertCount(3, $config['language']['php']['type']);
47+
$this->assertContains('object', $config['language']['php']['type']);
48+
49+
try {
50+
$config = new PConfig([
51+
'file' => $this->basePath . 'config.' . $type,
52+
]);
53+
} catch (Exception $e) {
54+
$this->fail($e->getMessage());
55+
}
56+
$this->assertNotNull($config);
57+
58+
$this->assertArrayHasKey('language', $config);
59+
$this->assertArrayHasKey("php", $config->get('language'));
60+
$this->assertArrayHasKey("php", $config['language']);
61+
$this->assertArrayHasKey("type", $config['language']['php']);
62+
$this->assertCount(3, $config['language']['php']['type']);
63+
$this->assertContains('object', $config['language']['php']['type']);
64+
65+
$this->assertTrue($config->set('hello.world', true));
66+
67+
$this->assertArrayHasKey('hello', $config);
68+
$this->assertArrayHasKey('world', $config->get('hello'));
69+
$this->assertArrayHasKey('world', $config['hello']);
70+
71+
$this->assertTrue($config->exists('hello'));
72+
$this->assertTrue(isset($config['hello']));
73+
$this->assertTrue($config->exists('hello.world'));
74+
$this->assertTrue(isset($config['hello.world']));
75+
$this->assertTrue(isset($config['hello']['world']));
76+
77+
$this->assertNotEquals('true', $config->get('hello.world'));
78+
$this->assertNotEquals('true', $config['hello.world']);
79+
$this->assertNotEquals('true', $config['hello']['world']);
80+
81+
$this->assertEquals(true, $config->get('hello.world'));
82+
$this->assertEquals(true, $config['hello.world']);
83+
$this->assertEquals(true, $config['hello']['world']);
84+
85+
$this->assertFalse($config->delete('hello.world1'));
86+
$this->assertTrue($config->delete('hello.world'));
87+
88+
unset($config['hello.world']);
89+
90+
$this->assertFalse($config->exists('hello.world'));
91+
$this->assertFalse(isset($config['hello.world']));
92+
$this->assertNull($config['hello.world']);
93+
94+
$config->set('array.arr', [
95+
1, 2, 3, 4
96+
]);
97+
98+
$this->assertCount(4, $config->get('array.arr'));
99+
$this->assertCount(4, $config['array.arr']);
100+
$this->assertEquals(1, $config['array.arr.0']);
101+
$this->assertEquals(4, $config->get('array.arr.3'));
102+
103+
$config->setConfig([
104+
PConfig::CONFIG_KEY_EXTRACT_SEPARATOR => '@'
105+
]);
106+
107+
$this->assertTrue($config->set('my@config', 'myconfig'));
108+
$this->assertArrayHasKey('config', $config['my']);
109+
$this->assertEquals('myconfig', $config['my@config']);
110+
$this->assertEquals('myconfig', $config['my']['config']);
111+
112+
$this->assertEquals("hello", $config->get('hello.php', 'hello'));
113+
114+
$config->setFile($this->basePath . 'config_new.' . $type);
115+
116+
try {
117+
$this->assertTrue($config->save());
118+
} catch (Exception $e) {
119+
$this->fail($e->getMessage());
120+
}
121+
}
122+
123+
}

test/data/config.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[language.php.type]
2+
0 = array
3+
1 = string
4+
2 = object

test/data/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"language": {
3+
"php": {
4+
"type": [
5+
"array",
6+
"string",
7+
"object"
8+
]
9+
}
10+
}
11+
}

test/data/config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
return [
3+
'language' => [
4+
'php' => [
5+
'type' => ['array', 'string', 'object']
6+
]
7+
]
8+
];

test/data/config.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<root>
3+
<language>
4+
<php>
5+
<type>array</type>
6+
<type>string</type>
7+
<type>object</type>
8+
</php>
9+
</language>
10+
</root>

test/data/config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language:
2+
php:
3+
type:
4+
- array
5+
- string
6+
- object

test/data/config_new.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[language.php.type]
2+
0 = array
3+
1 = string
4+
2 = object
5+
[hello]
6+
[array.arr]
7+
0 = 1
8+
1 = 2
9+
2 = 3
10+
3 = 4
11+
[my]
12+
config = myconfig

test/data/config_new.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"language": {
3+
"php": {
4+
"type": [
5+
"array",
6+
"string",
7+
"object"
8+
]
9+
}
10+
},
11+
"hello": [],
12+
"array": {
13+
"arr": [
14+
1,
15+
2,
16+
3,
17+
4
18+
]
19+
},
20+
"my": {
21+
"config": "myconfig"
22+
}
23+
}

test/data/config_new.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
return ['language' => [
3+
'php' => [
4+
'type' => [
5+
'array',
6+
'string',
7+
'object',
8+
],
9+
],
10+
],
11+
'hello' => [
12+
],
13+
'array' => [
14+
'arr' => [
15+
1,
16+
2,
17+
3,
18+
4,
19+
],
20+
],
21+
'my' => [
22+
'config' => 'myconfig',
23+
],
24+
];

0 commit comments

Comments
 (0)