1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* this file is part of magerun |
4
|
|
|
* |
5
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace N98\Util; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AutoloadHandlerTest |
12
|
|
|
* |
13
|
|
|
* @covers \N98\Util\AutoloadHandler |
14
|
|
|
* @package N98\Util |
15
|
|
|
*/ |
16
|
|
|
class AutoloadHandlerTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $cleanup = array(); |
22
|
|
|
|
23
|
|
|
public function tearDown() |
24
|
|
|
{ |
25
|
|
|
foreach ($this->cleanup as $key => $task) { |
26
|
|
|
$task(); |
27
|
|
|
unset($this->cleanup[$key]); |
28
|
|
|
} |
29
|
|
|
parent::tearDown(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @test |
34
|
|
|
*/ |
35
|
|
|
public function creation() |
36
|
|
|
{ |
37
|
|
|
$handler = $this->create(null); |
38
|
|
|
$this->assertInstanceOf(__NAMESPACE__ . '\AutoloadHandler', $handler); |
39
|
|
|
$this->assertInternalType('callable', $handler); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
* @expectedException \BadMethodCallException |
45
|
|
|
* @expectedExceptionMessage Autoload callback is not callable |
46
|
|
|
*/ |
47
|
|
|
public function noRegistrationOnCreation() |
48
|
|
|
{ |
49
|
|
|
$handler = $this->create(null, AutoloadHandler::NO_AUTO_REGISTER); |
50
|
|
|
$handler->disable(); // assertions require a disabled handler b/c of exceptions |
51
|
|
|
|
52
|
|
|
$this->assertFalse(in_array($handler, spl_autoload_functions())); |
53
|
|
|
$this->assertFalse($handler->__invoke('test')); |
54
|
|
|
$handler->register(); |
55
|
|
|
$actual = in_array($handler, spl_autoload_functions()); |
56
|
|
|
$this->assertTrue($actual); |
57
|
|
|
|
58
|
|
|
$handler->enable(); |
59
|
|
|
$handler->__invoke('test'); |
60
|
|
|
$this->fail('An expected exception was not thrown'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function create($implementation, $flags = null) |
64
|
|
|
{ |
65
|
|
|
$handler = AutoloadHandler::create($implementation, $flags); |
66
|
|
|
$this->cleanup[] = $handler->getCleanupCallback(); |
67
|
|
|
|
68
|
|
|
return $handler; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function registrationAndDeregistration() |
75
|
|
|
{ |
76
|
|
|
$calls = (object) array('retval' => true); |
77
|
|
View Code Duplication |
$assertAble = function ($className) use (&$calls) { |
|
|
|
|
78
|
|
|
$calls->log[] = array($className); |
79
|
|
|
$calls->count[$className] = 1 + @$calls->count[$className]; |
80
|
|
|
|
81
|
|
|
return $calls->retval; |
82
|
|
|
}; |
83
|
|
|
|
84
|
|
|
$handler = $this->create($assertAble); |
85
|
|
|
$this->assertTrue($handler->isEnabled()); |
86
|
|
|
$this->assertTrue($handler->__invoke("Fake")); |
87
|
|
|
|
88
|
|
|
$handler->unregister(); |
89
|
|
|
$this->assertFalse($handler->__invoke("Fake")); |
90
|
|
|
$this->assertEquals(1, $calls->count['Fake']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @test |
95
|
|
|
*/ |
96
|
|
|
public function changingCallback() |
97
|
|
|
{ |
98
|
|
|
$calls = (object) array('retval' => true); |
99
|
|
View Code Duplication |
$assertAble = function ($className) use (&$calls) { |
|
|
|
|
100
|
|
|
$calls->log[] = array($className); |
101
|
|
|
$calls->count[$className] = 1 + @$calls->count[$className]; |
102
|
|
|
|
103
|
|
|
return $calls->retval; |
104
|
|
|
}; |
105
|
|
|
|
106
|
|
|
$handler = $this->create(null, AutoloadHandler::NO_EXCEPTION); |
107
|
|
|
$this->assertFalse($handler->__invoke("Test")); |
108
|
|
|
$this->assertObjectNotHasAttribute('count', $calls); |
109
|
|
|
|
110
|
|
|
$handler->setCallback($assertAble); |
111
|
|
|
$this->assertTrue($handler->__invoke("Test")); |
112
|
|
|
$this->assertEquals(1, $calls->count["Test"]); |
113
|
|
|
|
114
|
|
|
$handler->setCallback(null); |
115
|
|
|
$this->assertFalse($handler->__invoke("Test")); |
116
|
|
|
$this->assertEquals(1, $calls->count["Test"]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @test |
121
|
|
|
*/ |
122
|
|
|
public function disablingAndEnabling() |
123
|
|
|
{ |
124
|
|
|
$handler = $this->create(null); |
125
|
|
|
$handler->setEnabled(false); |
126
|
|
|
$this->assertFalse($handler->__invoke("Test")); |
127
|
|
|
$handler->setEnabled(true); |
128
|
|
|
$this->setExpectedException('BadMethodCallException'); |
129
|
|
|
$this->assertFalse($handler->__invoke("Test")); |
130
|
|
|
$this->fail('An expected exception has not been thrown'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @test |
135
|
|
|
*/ |
136
|
|
|
public function callbackSelfReference() |
137
|
|
|
{ |
138
|
|
|
$testClass = 'MyOf' . mt_rand(1000, 9999) . 'Fake' . mt_rand(1000, 9999) . 'Class'; |
139
|
|
|
$test = $this; |
140
|
|
|
$handler = $this->create(function ($className) use (&$handler, $test, $testClass) { |
141
|
|
|
/** @var $handler AutoloadHandler */ |
142
|
|
|
$test->assertEquals($testClass, $className); |
143
|
|
|
$handler->disable(); |
144
|
|
|
}); |
145
|
|
|
$actual = class_exists($testClass); |
146
|
|
|
$isEnabled = $handler->isEnabled(); |
147
|
|
|
$this->assertEquals(1, $this->getCount()); |
148
|
|
|
$this->assertFalse($isEnabled); |
149
|
|
|
$this->assertFalse($actual); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @test |
154
|
|
|
*/ |
155
|
|
|
public function cleanupCallback() |
156
|
|
|
{ |
157
|
|
|
$calls = (object) array('retval' => true); |
158
|
|
View Code Duplication |
$assertAble = function ($className) use (&$calls) { |
|
|
|
|
159
|
|
|
$calls->log[] = array($className); |
160
|
|
|
$calls->count[$className] = 1 + @$calls->count[$className]; |
161
|
|
|
|
162
|
|
|
return $calls->retval; |
163
|
|
|
}; |
164
|
|
|
|
165
|
|
|
$handler = $this->create($assertAble, AutoloadHandler::NO_EXCEPTION); |
166
|
|
|
$cleanup = $handler->getCleanupCallback(); |
167
|
|
|
$actual = class_exists('Test'); |
168
|
|
|
$this->assertFalse($actual); |
169
|
|
|
$this->assertTrue(in_array($handler, spl_autoload_functions()), 'before cleanup'); |
170
|
|
|
$cleanup(); |
171
|
|
|
$this->assertFalse(in_array($handler, spl_autoload_functions()), 'after cleanup'); |
172
|
|
|
// calling cleanup again must not do any warnings etc. |
173
|
|
|
$cleanup(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.