|
1
|
|
|
<?php |
|
2
|
|
|
namespace RazonYang\Yii2\Setting\Tests\Unit; |
|
3
|
|
|
|
|
4
|
|
|
use Codeception\Test\Unit; |
|
5
|
|
|
use RazonYang\Yii2\Setting\Tests\TestManager; |
|
6
|
|
|
|
|
7
|
|
|
class ManagerTest extends Unit |
|
8
|
|
|
{ |
|
9
|
|
|
private function createManager(array $settings, $enableCache = true): TestManager |
|
10
|
|
|
{ |
|
11
|
|
|
return new TestManager([ |
|
12
|
|
|
'settings' => $settings, |
|
13
|
|
|
'cacheKey' => uniqid(), |
|
14
|
|
|
'enableCache' => $enableCache, |
|
15
|
|
|
]); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @dataProvider dataProviderGet |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testGet(array $settings, string $id, ?string $expected): void |
|
22
|
|
|
{ |
|
23
|
|
|
// empty array |
|
24
|
|
|
$manager = $this->createManager($settings, false); |
|
25
|
|
|
$this->assertSame($expected, $manager->get($id)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function dataProviderGet(): array |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
[[], 'name', null], |
|
32
|
|
|
[['name' => 'foo'], 'name', 'foo'], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testGetWithDefaultValue(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$manager = $this->createManager([]); |
|
39
|
|
|
$defaultValue = 'default value'; |
|
40
|
|
|
$this->assertSame($defaultValue, $manager->get(uniqid(), $defaultValue)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @dataProvider dataProviderGetAll |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testGetAll(array $settings): void |
|
47
|
|
|
{ |
|
48
|
|
|
$manager = $this->createManager($settings); |
|
49
|
|
|
$this->assertCount(count($settings), $manager->getAll()); |
|
50
|
|
|
foreach ($settings as $id => $value) { |
|
51
|
|
|
$this->assertEquals($value, $manager->get($id)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// create manager using same cache key |
|
55
|
|
|
$newManager = new TestManager(['cacheKey' => $manager->cacheKey]); |
|
56
|
|
|
$this->assertSame($manager->getAll(), $newManager->getAll()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function dataProviderGetAll(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
[[]], |
|
63
|
|
|
[['name' => 'foo']], |
|
64
|
|
|
[['name' => 'bar', 'age' => 0]], |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testFlushCache(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$settings = ['name' => 'foo']; |
|
71
|
|
|
$manager = $this->createManager($settings); |
|
72
|
|
|
$this->assertTrue($manager->enableCache); |
|
73
|
|
|
$this->assertSame('foo', $manager->get('name')); |
|
74
|
|
|
|
|
75
|
|
|
// change settings |
|
76
|
|
|
$manager->settings = ['name' => 'bar']; |
|
77
|
|
|
$this->assertSame('foo', $manager->get('name')); |
|
78
|
|
|
|
|
79
|
|
|
// flush cache |
|
80
|
|
|
$this->assertIsBool($manager->flushCache()); |
|
81
|
|
|
$this->assertSame('bar', $manager->get('name')); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|