|
1
|
|
|
<?php |
|
2
|
|
|
namespace Subreality\Dilmun\Tests\Nabu; |
|
3
|
|
|
|
|
4
|
|
|
use Subreality\Dilmun\Nabu\Autoloader; |
|
5
|
|
|
use Subreality\Dilmun\Nabu\LoggerHandler\File; |
|
6
|
|
|
use Subreality\Dilmun\Tests\TestClass; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class AutoloaderTest |
|
10
|
|
|
* @package Subreality\Dilmun\Tests\Nabu |
|
11
|
|
|
*/ |
|
12
|
|
|
class AutoloaderTest extends \PHPUnit_Framework_TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public static $autoloader_log_location; |
|
15
|
|
|
|
|
16
|
|
|
public static function setUpBeforeClass() |
|
17
|
|
|
{ |
|
18
|
|
|
self::$autoloader_log_location = DILMUN_LOGS_DIR . "autoloader.log"; |
|
19
|
|
|
|
|
20
|
|
|
//Creating a pointed to the existing log file. |
|
21
|
|
|
$autoloader_file = new File("autoloader.log", DILMUN_LOGS_DIR); |
|
22
|
|
|
$autoloader_file->initialize(); |
|
23
|
|
|
|
|
24
|
|
|
//Need to clear the log for valid log writing tests |
|
25
|
|
|
$autoloader_file->clearFile(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function assertPreconditions() |
|
29
|
|
|
{ |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function setUp() |
|
33
|
|
|
{ |
|
34
|
|
|
$autoloader_array = $this->getAutoloaderFromAutoloadStack(); |
|
35
|
|
|
|
|
36
|
|
|
if (!$autoloader_array) { |
|
37
|
|
|
$loader = new Autoloader(); |
|
38
|
|
|
|
|
39
|
|
|
$loader->register(); |
|
40
|
|
|
|
|
41
|
|
|
$loader->registerPackages("Subreality\\Dilmun", DILMUN_SOURCE_DIR); |
|
42
|
|
|
$loader->registerPackages("Subreality\\Dilmun\\Tests", DILMUN_TEST_DIR); |
|
43
|
|
|
} else { |
|
44
|
|
|
//Ensuring that packages are registered in the existing autoloader queued in the autoload stack since |
|
45
|
|
|
//we're mucking about with the stack! |
|
46
|
|
|
|
|
47
|
|
|
$autoloader = $autoloader_array[0]; |
|
48
|
|
|
|
|
49
|
|
|
/** @var Autoloader $autoloader */ |
|
50
|
|
|
$autoloader->registerPackages("Subreality\\Dilmun", DILMUN_SOURCE_DIR); |
|
51
|
|
|
$autoloader->registerPackages("Subreality\\Dilmun\\Tests", DILMUN_TEST_DIR); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function tearDown() |
|
56
|
|
|
{ |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function tearDownAfterClass() |
|
60
|
|
|
{ |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testRegisterRegistersIncludeClassMethod() |
|
64
|
|
|
{ |
|
65
|
|
|
$autoloader_array = $this->getAutoloaderFromAutoloadStack(); |
|
66
|
|
|
|
|
67
|
|
|
if ($autoloader_array) { |
|
68
|
|
|
spl_autoload_unregister($autoloader_array); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->assertFalse($this->autoloaderClassExists()); |
|
72
|
|
|
|
|
73
|
|
|
$new_autoloader = new Autoloader(); |
|
74
|
|
|
|
|
75
|
|
|
$new_autoloader->register(); |
|
76
|
|
|
|
|
77
|
|
|
$autoloader_exists = $this->autoloaderClassExists(); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertTrue($autoloader_exists); |
|
80
|
|
|
|
|
81
|
|
|
$autoloader_array = $this->getAutoloaderFromAutoloadStack(); |
|
82
|
|
|
|
|
83
|
|
|
$registered_autoload_method = $autoloader_array[1]; |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals("includeClass", $registered_autoload_method); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testAutoloaderLogsNewLoadedClass() |
|
89
|
|
|
{ |
|
90
|
|
|
//We're not doing anything with this; it's an arbitrary instantiation to get something written to the log. |
|
91
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
|
92
|
|
|
$stub = new TestClass(); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$autoloader_log = file_get_contents(DILMUN_LOGS_DIR . "autoloader.log"); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertContains("TestClass", $autoloader_log); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testIncludeClassReturnsFalseWhenClassInstantiatedWithoutCorrespondingClassFile() |
|
100
|
|
|
{ |
|
101
|
|
|
$fake_class_exists = class_exists('Subreality\Dilmun\Nabu\Xkcd'); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertFalse($fake_class_exists); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testIncludeClassReturnsFalseWhenNoPackageSet() |
|
107
|
|
|
{ |
|
108
|
|
|
$autoloader_array = $this->getAutoloaderFromAutoloadStack(); |
|
109
|
|
|
|
|
110
|
|
|
if ($autoloader_array) { |
|
111
|
|
|
spl_autoload_unregister($autoloader_array); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->assertFalse($this->autoloaderClassExists()); |
|
115
|
|
|
|
|
116
|
|
|
$new_autoloader = new Autoloader(); |
|
117
|
|
|
|
|
118
|
|
|
$new_autoloader->register(); |
|
119
|
|
|
|
|
120
|
|
|
$autoloader_exists = $this->autoloaderClassExists(); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertTrue($autoloader_exists); |
|
123
|
|
|
|
|
124
|
|
|
$fake_class_exists = class_exists('Subreality\Dilmun\Nabu\Xkcd'); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertFalse($fake_class_exists); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
View Code Duplication |
private function autoloaderClassExists() |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$autoload_functions = spl_autoload_functions(); |
|
135
|
|
|
|
|
136
|
|
|
$autoloader_class_exists = false; |
|
137
|
|
|
|
|
138
|
|
|
if ($autoload_functions) { |
|
|
|
|
|
|
139
|
|
|
foreach ($autoload_functions as $function) { |
|
140
|
|
|
if ($function[0] instanceof Autoloader) { |
|
141
|
|
|
$autoloader_class_exists = true; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $autoloader_class_exists; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return mixed |
|
151
|
|
|
*/ |
|
152
|
|
View Code Duplication |
private function getAutoloaderFromAutoloadStack() |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
$autoloader_object = false; |
|
155
|
|
|
|
|
156
|
|
|
$autoload_functions = spl_autoload_functions(); |
|
157
|
|
|
|
|
158
|
|
|
if ($autoload_functions) { |
|
|
|
|
|
|
159
|
|
|
foreach ($autoload_functions as $function_array) { |
|
160
|
|
|
if ($function_array[0] instanceof Autoloader) { |
|
161
|
|
|
$autoloader_object = $function_array; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $autoloader_object; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.