1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Alejandro Peña Florentín ([email protected]). |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Tight\Tests; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Description of ModuleLoaderTest |
31
|
|
|
* |
32
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
33
|
|
|
*/ |
34
|
|
|
class ModuleLoaderTest extends \PHPUnit_Framework_TestCase |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
private $loader; |
38
|
|
|
private $testModule; |
39
|
|
|
|
40
|
|
|
public function setUp() { |
41
|
|
|
|
42
|
|
|
$this->loader = new \Tight\Modules\ModuleLoader(new \Tight\Tight); |
43
|
|
|
$this->testModule = new TestLoaderModule("TestModule"); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function tearDown() { |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @test |
52
|
|
|
*/ |
53
|
|
|
public function testModuleLoaderGetModules() { |
54
|
|
|
$expected = []; |
55
|
|
|
$this->assertEquals($expected, $this->loader->getModules()); |
56
|
|
|
$this->assertNull($this->loader->getModule("undefinedModule")); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
* @depends testModuleLoaderGetModules |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function testModuleLoaderAdd() { |
64
|
|
|
$key = $this->testModule->getModuleName(); |
65
|
|
|
$expected = [$key => $this->testModule]; |
66
|
|
|
$this->loader->add($this->testModule); |
67
|
|
|
$this->assertEquals($expected, $this->loader->getModules()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @test |
72
|
|
|
* @depends testModuleLoaderAdd |
73
|
|
|
* @expectedException \Tight\Exception\ModuleException |
74
|
|
|
*/ |
75
|
|
|
public function testModuleLoaderAddWithException() { |
76
|
|
|
$this->loader->add($this->testModule); |
77
|
|
|
$this->setExpectedException("\Tight\Exception\ModuleException"); |
78
|
|
|
$this->loader->add($this->testModule); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @test |
83
|
|
|
* @depends testModuleLoaderAdd |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function testModuleLoaderGetModule() { |
86
|
|
|
$this->loader->add($this->testModule); |
87
|
|
|
$modName = $this->testModule->getModuleName(); |
88
|
|
|
$expected = $this->testModule; |
89
|
|
|
$this->assertEquals($expected, $this->loader->getModule($modName)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
* @depends testModuleLoaderAdd |
95
|
|
|
*/ |
96
|
|
|
public function testModuleLoaderRemove() { |
97
|
|
|
$this->loader->add($this->testModule); |
98
|
|
|
$this->assertTrue($this->loader->remove($this->testModule->getModuleName())); |
99
|
|
|
$this->assertFalse($this->loader->remove($this->testModule->getModuleName())); |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* @test |
103
|
|
|
* @depends testModuleLoaderAdd |
104
|
|
|
*/ |
105
|
|
|
public function testModuleLoaderGetModulesInfo(){ |
106
|
|
|
$expected[0] = [ |
|
|
|
|
107
|
|
|
"name" => "TestModule", |
108
|
|
|
"version"=> "v1.0" |
109
|
|
|
]; |
110
|
|
|
$this->assertEquals([],$this->loader->getModulesInfo()); |
111
|
|
|
$this->loader->add($this->testModule); |
112
|
|
|
$this->assertEquals($expected,$this->loader->getModulesInfo()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
class TestLoaderModule extends \Tight\Modules\AbstractModule |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
|
119
|
|
|
public function onConfigChange() { |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function onLoad() { |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function onRemove() { |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.