1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KochTest\Autoload; |
4
|
|
|
|
5
|
|
|
use Koch\Autoload\Loader; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Interface and Class definition for testing |
9
|
|
|
* the autoload skipping, when "already loaded". |
10
|
|
|
*/ |
11
|
|
|
interface ThisInterfaceExists |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
class ThisClassExists |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
class LoaderTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
public $classMapFile = 'autoloader.classmap.php'; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
// add Fixtures folder, only if not already on the include_path |
28
|
|
|
$path = realpath(__DIR__ . '/fixtures'); |
29
|
|
|
if (strpos(get_include_path(), $path) === false) { |
30
|
|
|
set_include_path($path . PATH_SEPARATOR . get_include_path()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/* |
34
|
|
|
* The APC user cache needs a reset, so that the map is generated freshly each run. |
35
|
|
|
* APC is used by readAutoloadingMapApc() / writeAutoloadingMapApc(). |
36
|
|
|
*/ |
37
|
|
|
self::apcClearCache(); |
38
|
|
|
|
39
|
|
|
Loader::setClassMapFile($this->classMapFile); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function tearDown() |
43
|
|
|
{ |
44
|
|
|
self::apcClearCache(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function apcClearCache() |
48
|
|
|
{ |
49
|
|
|
if (extension_loaded('apc') and ini_get('apc.enabled') and ini_get('apc.enable_cli')) { |
|
|
|
|
50
|
|
|
apc_clear_cache('user'); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* testMethodautoload(). |
56
|
|
|
*/ |
57
|
|
|
public function testMethodautoload() |
58
|
|
|
{ |
59
|
|
|
// workflow of autoloading |
60
|
|
|
|
61
|
|
|
// 1. existing class |
62
|
|
|
$this->assertFalse(Loader::autoload('ThisClassExists')); |
63
|
|
|
// 2. existing interface |
64
|
|
|
$this->assertFalse(Loader::autoload('ThisInterfaceExists')); |
65
|
|
|
// 3. existing trait |
66
|
|
|
if (PHP_VERSION_ID <= 50400) { |
67
|
|
|
$this->assertFalse(Loader::autoload('ClassADefinesTraitA')); |
68
|
|
|
} |
69
|
|
|
// PHP 5.4.6 Bug... trait_exists does not return anything (true|false|null). |
|
|
|
|
70
|
|
|
// So a "cannot redeclare class TraitA" fatal error is thrown. |
71
|
|
|
//$this->assertFalse(Loader::autoload('ClassBDefinesTraitA')); |
|
|
|
|
72
|
|
|
|
73
|
|
|
// 1. autoloadExclusions() |
|
|
|
|
74
|
|
|
// 2. autoloadInclusions() |
|
|
|
|
75
|
|
|
// 3. autoloadByApcOrFileMap() |
|
|
|
|
76
|
|
|
// 4. autoloadIncludePath() |
|
|
|
|
77
|
|
|
// 5. autoloadTryPathsAndMap() |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testMethodconstruct() |
81
|
|
|
{ |
82
|
|
|
// unregister first (autoloader was registered during test setup) |
83
|
|
|
$r = spl_autoload_unregister(['Koch\Autoload\Loader', 'autoload']); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// registers autoloader via constructor |
86
|
|
|
new Loader(); |
87
|
|
|
|
88
|
|
|
// test Koch Framework Autoloader is registered in the spl_autoloader_stack at first place |
89
|
|
|
$registered_autoloaders = spl_autoload_functions(); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->assertTrue(is_string($registered_autoloaders[0][0])); |
|
|
|
|
92
|
|
|
$this->assertFalse(is_object($registered_autoloaders[0][0])); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$this->assertEquals('Koch\Autoload\Loader', $registered_autoloaders[0][0]); |
|
|
|
|
95
|
|
|
$this->assertEquals('autoload', $registered_autoloaders[0][1]); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* testMethodautoloadExclusions(). |
100
|
|
|
*/ |
101
|
|
|
public function testMethodautoloadExclusions() |
102
|
|
|
{ |
103
|
|
|
// exclude "Smarty_Internal" classes |
104
|
|
|
$this->assertTrue(Loader::autoloadExclusions('Smarty_Internal_SomeClass')); |
105
|
|
|
|
106
|
|
|
// exclude "Doctrine" classes |
107
|
|
|
$this->assertTrue(Loader::autoloadExclusions('Doctrine_SomeClass')); |
108
|
|
|
$this->assertTrue(Loader::autoloadExclusions('Doctrine\SomeClass')); |
109
|
|
|
|
110
|
|
|
// but not, our own namespaced doctrine classes "Koch\Doctrine\" |
111
|
|
|
$this->assertFalse(Loader::autoloadExclusions('Koch\Doctrine\SomeClass')); |
112
|
|
|
$this->assertFalse(Loader::autoloadExclusions('Koch\Pagination\Adapter\Doctrine')); |
113
|
|
|
|
114
|
|
|
// exclude "Smarty" classes |
115
|
|
|
$this->assertTrue(Loader::autoloadExclusions('Smarty_')); |
116
|
|
|
|
117
|
|
|
// but not, our own smarty class "\Smarty" |
118
|
|
|
$this->assertFalse(Loader::autoloadExclusions('Koch\View\Renderer\Smarty')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* testMethodautoloadInclusions(). |
123
|
|
|
*/ |
124
|
|
|
public function testMethodautoloadInclusions() |
125
|
|
|
{ |
126
|
|
|
// try to load an unknown class |
127
|
|
|
$this->assertFalse(Loader::autoloadInclusions('SomeUnknownClass')); |
128
|
|
|
|
129
|
|
|
// try to load "Application_Staging" class |
130
|
|
|
// no definitions atm |
131
|
|
|
#$this->assertTrue(Loader::autoloadInclusions('Application_Staging')); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* testMethodautoloadByApcOrFileMap. |
136
|
|
|
*/ |
137
|
|
|
public function testMethodautoloadByApcOrFileMap() |
138
|
|
|
{ |
139
|
|
|
// try to load an unknown class |
140
|
|
|
$this->assertFalse(Loader::autoloadByApcOrFileMap('SomeUnknownClass')); |
141
|
|
|
|
142
|
|
|
Loader::addMapping('Captcha', realpath(__DIR__ . '/../../../framework/Koch/Captcha/Captcha.php')); |
143
|
|
|
$this->assertTrue(Loader::autoloadByApcOrFileMap('Captcha')); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* testMethodautoloadIncludePath(). |
148
|
|
|
*/ |
149
|
|
|
public function testMethodautoloadIncludePath() |
150
|
|
|
{ |
151
|
|
|
// try to load an unknown class |
152
|
|
|
$this->assertFalse(Loader::autoloadIncludePath('\Namespace\Library\SomeUnknownClass')); |
153
|
|
|
|
154
|
|
|
// set the include path to our fixtures directory, where a namespaced class exists |
155
|
|
|
$path = __DIR__ . '/fixtures/Application'; |
156
|
|
|
set_include_path($path . PATH_SEPARATOR . get_include_path()); |
157
|
|
|
|
158
|
|
|
// try to load existing namespaced class |
159
|
|
|
$this->assertTrue(Loader::autoloadIncludePath('NamespacedClass')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
View Code Duplication |
public function testMethodwriteAutoloadingMapFile() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
if (is_file($this->classMapFile)) { |
165
|
|
|
unlink($this->classMapFile); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// file will be created |
169
|
|
|
$this->assertSame([], Loader::readAutoloadingMapFile()); |
170
|
|
|
$this->assertTrue(is_file($this->classMapFile)); |
171
|
|
|
|
172
|
|
|
$array = ['class' => 'file']; |
173
|
|
|
$this->assertTrue(Loader::writeAutoloadingMapFile($array)); |
174
|
|
|
$this->assertSame($array, Loader::readAutoloadingMapFile()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
View Code Duplication |
public function testMethodreadAutoloadingMapFile() |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
if (is_file($this->classMapFile)) { |
180
|
|
|
unlink($this->classMapFile); |
181
|
|
|
} |
182
|
|
|
// file will be created |
183
|
|
|
$this->assertSame([], Loader::readAutoloadingMapFile()); |
184
|
|
|
$this->assertTrue(is_file($this->classMapFile)); |
185
|
|
|
|
186
|
|
|
$array = ['class' => 'file']; |
187
|
|
|
$this->assertTrue(Loader::writeAutoloadingMapFile($array)); |
188
|
|
|
$this->assertSame($array, Loader::readAutoloadingMapFile()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testMethodwriteAutoloadingMapApc() |
192
|
|
|
{ |
193
|
|
|
if (!extension_loaded('apc')) { |
194
|
|
|
$this->markTestSkipped('This test requires the PHP extension "apc".'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$array = ['class' => 'file']; |
198
|
|
|
$this->assertTrue(Loader::writeAutoloadingMapApc($array)); |
199
|
|
|
$this->assertSame($array, Loader::readAutoloadingMapApc()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testMethodreadAutoloadingMapApc() |
203
|
|
|
{ |
204
|
|
|
if (!extension_loaded('apc')) { |
205
|
|
|
$this->markTestSkipped(' This test requires the PHP extension "apc".'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->assertSame(apc_fetch('KF_CLASSMAP'), Loader::readAutoloadingMapApc()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
View Code Duplication |
public function testMethodaddMapping() |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$class = 'addToMappingClass'; |
214
|
|
|
$file = realpath(__DIR__ . '/fixtures/notloaded/addToMapping.php'); |
215
|
|
|
|
216
|
|
|
$this->assertTrue(Loader::addMapping($class, $file)); |
217
|
|
|
|
218
|
|
|
// test if the entry was added to the autoloader class map array |
219
|
|
|
$map = Loader::getAutoloaderClassMap(); |
220
|
|
|
// entry exists |
221
|
|
|
$this->assertTrue(true, array_key_exists($class, $map)); |
222
|
|
|
// compare entries |
223
|
|
|
$this->assertEquals($map[$class], $file); |
224
|
|
|
|
225
|
|
|
// file not loaded, just mapped |
226
|
|
|
#$this->assertFalse(class_exists($class, false)); |
|
|
|
|
227
|
|
|
|
228
|
|
|
// triggering autoload via class_exists |
229
|
|
|
// --- WARNING --- |
230
|
|
|
// The "Koch Framework Autoloader" needs to be registered BEFORE "Composers Autoloader". |
231
|
|
|
$this->assertTrue(class_exists($class, true)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
View Code Duplication |
public function testMethodincludeFileAndMap() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
$file = realpath(__DIR__ . '/fixtures/includeFileAndMap.php'); |
237
|
|
|
$class = 'includeFileAndMapClass'; |
238
|
|
|
|
239
|
|
|
Loader::includeFileAndMap($file, $class); |
240
|
|
|
|
241
|
|
|
// test if the entry was added to the autoloader class map array |
242
|
|
|
$map = Loader::getAutoloaderClassMap(); |
243
|
|
|
|
244
|
|
|
$this->assertTrue(true, array_key_exists($class, $map)); |
245
|
|
|
|
246
|
|
|
$this->assertEquals($map[$class], $file); |
247
|
|
|
|
248
|
|
|
// file already loaded |
249
|
|
|
$this->assertTrue(class_exists($class, false)); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testMethodincludeFile() |
253
|
|
|
{ |
254
|
|
|
// a) include file |
255
|
|
|
$this->assertTrue(Loader::includeFile(__DIR__ . '/fixtures/ClassForRequireFile1.php')); |
256
|
|
|
|
257
|
|
|
// b) include class |
|
|
|
|
258
|
|
|
$this->assertTrue(Loader::includeFile(__DIR__ . '/fixtures/ClassForRequireFile2.php', 'ClassForRequireFile2')); |
259
|
|
|
|
260
|
|
|
// c) include class (second parameter), but class does not exist |
261
|
|
|
$this->assertFalse(Loader::includeFile('nonExistantFile.php'), 'ThisClassDoesNotExist'); |
262
|
|
|
|
263
|
|
|
// d) file not found returns false |
264
|
|
|
$this->assertFalse(Loader::includeFile('nonExistantFile.php')); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function testsetInclusionMap() |
268
|
|
|
{ |
269
|
|
|
$classmap = ['class' => 'file']; |
270
|
|
|
Loader::setInclusionsClassMap($classmap); |
271
|
|
|
$this->assertEquals(Loader::$inclusionsClassmap, $classmap); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.