These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * phpDocumentor |
||
4 | * |
||
5 | * PHP Version 5.3 |
||
6 | * |
||
7 | * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com) |
||
8 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
||
9 | * @link http://phpdoc.org |
||
10 | */ |
||
11 | |||
12 | namespace phpDocumentor\Descriptor; |
||
13 | |||
14 | use \Mockery as m; |
||
15 | |||
16 | /** |
||
17 | * Tests the functionality for the FileDescriptor class. |
||
18 | */ |
||
19 | class FileDescriptorTest extends \Mockery\Adapter\Phpunit\MockeryTestCase |
||
20 | { |
||
21 | const EXAMPLE_HASH = 'a-hash-string'; |
||
22 | |||
23 | const EXAMPLE_PATH = 'a-path-string'; |
||
24 | |||
25 | const EXAMPLE_SOURCE = 'a-source-string'; |
||
26 | |||
27 | /** @var FileDescriptor $fixture */ |
||
28 | protected $fixture; |
||
29 | |||
30 | /** |
||
31 | * Creates a new (empty) fixture object. |
||
32 | */ |
||
33 | protected function setUp() |
||
34 | { |
||
35 | $this->fixture = new FileDescriptor(self::EXAMPLE_HASH); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Tests whether all collection objects and hash are properly initialized |
||
40 | * |
||
41 | * @covers phpDocumentor\Descriptor\FileDescriptor::__construct |
||
42 | */ |
||
43 | public function testInitialize() |
||
44 | { |
||
45 | $this->assertAttributeEquals(self::EXAMPLE_HASH, 'hash', $this->fixture); |
||
46 | |||
47 | $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'namespaceAliases', $this->fixture); |
||
48 | $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'includes', $this->fixture); |
||
49 | $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'constants', $this->fixture); |
||
50 | $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'functions', $this->fixture); |
||
51 | $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'classes', $this->fixture); |
||
52 | $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'interfaces', $this->fixture); |
||
53 | $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'traits', $this->fixture); |
||
54 | $this->assertAttributeInstanceOf('phpDocumentor\Descriptor\Collection', 'markers', $this->fixture); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @covers phpDocumentor\Descriptor\FileDescriptor::__construct |
||
59 | * @covers phpDocumentor\Descriptor\FileDescriptor::getHash |
||
60 | */ |
||
61 | public function testGetHash() |
||
62 | { |
||
63 | $this->assertSame(self::EXAMPLE_HASH, $this->fixture->getHash()); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @covers phpDocumentor\Descriptor\FileDescriptor::setPath |
||
68 | * @covers phpDocumentor\Descriptor\FileDescriptor::getPath |
||
69 | */ |
||
70 | public function testSetAndGetPath() |
||
71 | { |
||
72 | $this->assertSame('', $this->fixture->getPath()); |
||
73 | |||
74 | $this->fixture->setPath(self::EXAMPLE_PATH); |
||
75 | |||
76 | $this->assertSame(self::EXAMPLE_PATH, $this->fixture->getPath()); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @covers phpDocumentor\Descriptor\FileDescriptor::setSource |
||
81 | * @covers phpDocumentor\Descriptor\FileDescriptor::getSource |
||
82 | */ |
||
83 | public function testSetAndGetSource() |
||
84 | { |
||
85 | $this->assertNull($this->fixture->getSource()); |
||
86 | |||
87 | $this->fixture->setSource(self::EXAMPLE_SOURCE); |
||
88 | |||
89 | $this->assertSame(self::EXAMPLE_SOURCE, $this->fixture->getSource()); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @covers phpDocumentor\Descriptor\FileDescriptor::setNamespaceAliases |
||
94 | * @covers phpDocumentor\Descriptor\FileDescriptor::getNamespaceAliases |
||
95 | */ |
||
96 | public function testSetAndGetNamespaceAliases() |
||
97 | { |
||
98 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getNamespaceAliases()); |
||
99 | |||
100 | $mockInstance = m::mock('phpDocumentor\Descriptor\Collection'); |
||
101 | $mock = $mockInstance; |
||
102 | |||
103 | $this->fixture->setNamespaceAliases($mock); |
||
104 | |||
105 | $this->assertSame($mockInstance, $this->fixture->getNamespaceAliases()); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @covers phpDocumentor\Descriptor\FileDescriptor::setIncludes |
||
110 | * @covers phpDocumentor\Descriptor\FileDescriptor::getIncludes |
||
111 | */ |
||
112 | public function testSetAndGetIncludes() |
||
113 | { |
||
114 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getIncludes()); |
||
115 | |||
116 | $mockInstance = m::mock('phpDocumentor\Descriptor\Collection'); |
||
117 | $mock = $mockInstance; |
||
118 | |||
119 | $this->fixture->setIncludes($mock); |
||
120 | |||
121 | $this->assertSame($mockInstance, $this->fixture->getIncludes()); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @covers phpDocumentor\Descriptor\FileDescriptor::setConstants |
||
126 | * @covers phpDocumentor\Descriptor\FileDescriptor::getConstants |
||
127 | */ |
||
128 | public function testSetAndGetConstants() |
||
129 | { |
||
130 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getConstants()); |
||
131 | |||
132 | $mockInstance = m::mock('phpDocumentor\Descriptor\Collection'); |
||
133 | $mock = $mockInstance; |
||
134 | |||
135 | $this->fixture->setConstants($mock); |
||
136 | |||
137 | $this->assertSame($mockInstance, $this->fixture->getConstants()); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @covers phpDocumentor\Descriptor\FileDescriptor::setFunctions |
||
142 | * @covers phpDocumentor\Descriptor\FileDescriptor::getFunctions |
||
143 | */ |
||
144 | public function testSetAndGetFunctions() |
||
145 | { |
||
146 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getFunctions()); |
||
147 | |||
148 | $mockInstance = m::mock('phpDocumentor\Descriptor\Collection'); |
||
149 | $mock = $mockInstance; |
||
150 | |||
151 | $this->fixture->setFunctions($mock); |
||
152 | |||
153 | $this->assertSame($mockInstance, $this->fixture->getFunctions()); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @covers phpDocumentor\Descriptor\FileDescriptor::setClasses |
||
158 | * @covers phpDocumentor\Descriptor\FileDescriptor::getClasses |
||
159 | */ |
||
160 | public function testSetAndGetClasses() |
||
161 | { |
||
162 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getClasses()); |
||
163 | |||
164 | $mockInstance = m::mock('phpDocumentor\Descriptor\Collection'); |
||
165 | $mock = $mockInstance; |
||
166 | |||
167 | $this->fixture->setClasses($mock); |
||
168 | |||
169 | $this->assertSame($mockInstance, $this->fixture->getClasses()); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @covers phpDocumentor\Descriptor\FileDescriptor::setInterfaces |
||
174 | * @covers phpDocumentor\Descriptor\FileDescriptor::getInterfaces |
||
175 | */ |
||
176 | public function testSetAndGetInterfaces() |
||
177 | { |
||
178 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getInterfaces()); |
||
179 | |||
180 | $mockInstance = m::mock('phpDocumentor\Descriptor\Collection'); |
||
181 | $mock = $mockInstance; |
||
182 | |||
183 | $this->fixture->setInterfaces($mock); |
||
184 | |||
185 | $this->assertSame($mockInstance, $this->fixture->getInterfaces()); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @covers phpDocumentor\Descriptor\FileDescriptor::setTraits |
||
190 | * @covers phpDocumentor\Descriptor\FileDescriptor::getTraits |
||
191 | */ |
||
192 | public function testSetAndGetTraits() |
||
193 | { |
||
194 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getTraits()); |
||
195 | |||
196 | $mockInstance = m::mock('phpDocumentor\Descriptor\Collection'); |
||
197 | $mock = $mockInstance; |
||
198 | |||
199 | $this->fixture->setTraits($mock); |
||
200 | |||
201 | $this->assertSame($mockInstance, $this->fixture->getTraits()); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @covers phpDocumentor\Descriptor\FileDescriptor::setMarkers |
||
206 | * @covers phpDocumentor\Descriptor\FileDescriptor::getMarkers |
||
207 | */ |
||
208 | public function testSetAndGetMarkers() |
||
209 | { |
||
210 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getMarkers()); |
||
211 | |||
212 | $mockInstance = m::mock('phpDocumentor\Descriptor\Collection'); |
||
213 | $mock = $mockInstance; |
||
214 | |||
215 | $this->fixture->setMarkers($mock); |
||
216 | |||
217 | $this->assertSame($mockInstance, $this->fixture->getMarkers()); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @covers phpDocumentor\Descriptor\FileDescriptor::__construct |
||
222 | * @covers phpDocumentor\Descriptor\FileDescriptor::getAllErrors |
||
223 | */ |
||
224 | public function testIfErrorsAreInitializedToAnEmptyCollectionOnInstantiation() |
||
225 | { |
||
226 | // construct |
||
227 | $this->assertInstanceOf('phpDocumentor\Descriptor\Collection', $this->fixture->getAllErrors()); |
||
228 | |||
229 | // default returns empty array |
||
0 ignored issues
–
show
|
|||
230 | $this->assertObjectHasAttribute('items', $this->fixture->getAllErrors()); |
||
231 | |||
232 | $items = $this->fixture->getAllErrors()->getAll(); |
||
233 | $this->assertEmpty($items); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @covers phpDocumentor\Descriptor\FileDescriptor::__construct |
||
238 | * @covers phpDocumentor\Descriptor\FileDescriptor::getAllErrors |
||
239 | */ |
||
240 | public function testGetAllErrors() |
||
241 | { |
||
242 | /* |
||
243 | * constant |
||
244 | * function |
||
245 | * class |
||
246 | * property |
||
247 | * constant |
||
248 | * method |
||
249 | * interface |
||
250 | * constant |
||
251 | * method |
||
252 | * traits |
||
253 | * property |
||
254 | * method |
||
255 | */ |
||
256 | |||
257 | // setup error list |
||
258 | $errorGlobal = ['error-global']; |
||
259 | $errorClasses = ['error-class']; |
||
260 | $errorClassMethods = ['error-class-method']; |
||
261 | $errorClassConstants = ['error-class-constant']; |
||
262 | $errorClassProperties = ['error-class-property']; |
||
263 | $errorInterfaces = ['error-interface']; |
||
264 | $errorInterfacesConstants = ['error-interface-constant']; |
||
265 | $errorInterfacesMethods = ['error-interface-method']; |
||
266 | $errorTraits = ['error-traits']; |
||
267 | $errorTraitsProperties = ['error-traits-property']; |
||
268 | $errorTraitsMethods = ['error-traits-method']; |
||
269 | $errorFunctions = ['error-functions']; |
||
270 | |||
271 | // setup global check |
||
272 | $collection = new Collection($errorGlobal); |
||
273 | $this->fixture->setErrors($collection); |
||
274 | |||
275 | // setup class-property check |
||
276 | $mockClassProperties = m::mock('phpDocumentor\Descriptor\PropertyDescriptor'); |
||
277 | $mockClassProperties->shouldReceive('getErrors')->andReturn(new Collection($errorClassProperties)); |
||
0 ignored issues
–
show
The method
andReturn does only exist in Mockery\ExpectationInterface , but not in Mockery\HigherOrderMessage .
It seems like the method you are trying to call exists only in some of the possible types. Let’s take a look at an example: class A
{
public function foo() { }
}
class B extends A
{
public function bar() { }
}
/**
* @param A|B $x
*/
function someFunction($x)
{
$x->foo(); // This call is fine as the method exists in A and B.
$x->bar(); // This method only exists in B and might cause an error.
}
Available Fixes
Loading history...
|
|||
278 | |||
279 | // setup class-constant check |
||
280 | $mockClassConstants = m::mock('phpDocumentor\Descriptor\ConstantDescriptor'); |
||
281 | $mockClassConstants->shouldReceive('getErrors')->andReturn(new Collection($errorClassConstants)); |
||
282 | |||
283 | // setup class-method check |
||
284 | $mockClassMethods = m::mock('phpDocumentor\Descriptor\MethodDescriptor'); |
||
285 | $mockClassMethods->shouldReceive('getErrors')->andReturn(new Collection($errorClassMethods)); |
||
286 | |||
287 | // setup class check |
||
288 | $mockClasses = m::mock('phpDocumentor\Descriptor\ClassDescriptor'); |
||
289 | $mockClasses->shouldReceive('getProperties')->andReturn(new Collection([$mockClassProperties])); |
||
290 | $mockClasses->shouldReceive('getConstants')->andReturn(new Collection([$mockClassConstants])); |
||
291 | $mockClasses->shouldReceive('getMethods')->andReturn(new Collection([$mockClassMethods])); |
||
292 | $mockClasses->shouldReceive('getErrors')->andReturn(new Collection($errorClasses)); |
||
293 | |||
294 | $this->fixture->getClasses()->set('my-test-class', $mockClasses); |
||
295 | |||
296 | // setup interface-constant check |
||
297 | $mockInterfaceConstants = m::mock('phpDocumentor\Descriptor\ConstantDescriptor'); |
||
298 | $mockInterfaceConstants->shouldReceive('getErrors')->andReturn(new Collection($errorInterfacesConstants)); |
||
299 | |||
300 | // setup interface-method check |
||
301 | $mockInterfaceMethods = m::mock('phpDocumentor\Descriptor\MethodDescriptor'); |
||
302 | $mockInterfaceMethods->shouldReceive('getErrors')->andReturn(new Collection($errorInterfacesMethods)); |
||
303 | |||
304 | // setup interface check |
||
305 | $mockInterfaces = m::mock('phpDocumentor\Descriptor\ClassDescriptor'); |
||
306 | $mockInterfaces->shouldReceive('getProperties')->andReturn([]); |
||
307 | $mockInterfaces->shouldReceive('getConstants')->andReturn(new Collection([$mockInterfaceConstants])); |
||
308 | $mockInterfaces->shouldReceive('getMethods')->andReturn(new Collection([$mockInterfaceMethods])); |
||
309 | $mockInterfaces->shouldReceive('getErrors')->andReturn(new Collection($errorInterfaces)); |
||
310 | |||
311 | $this->fixture->getClasses()->set('my-test-interface', $mockInterfaces); |
||
312 | |||
313 | // setup traits-constant check |
||
314 | $mockTraitsProperties = m::mock('phpDocumentor\Descriptor\ConstantDescriptor'); |
||
315 | $mockTraitsProperties->shouldReceive('getErrors')->andReturn(new Collection($errorTraitsProperties)); |
||
316 | |||
317 | // setup traits-method check |
||
318 | $mockTraitsMethods = m::mock('phpDocumentor\Descriptor\MethodDescriptor'); |
||
319 | $mockTraitsMethods->shouldReceive('getErrors')->andReturn(new Collection($errorTraitsMethods)); |
||
320 | |||
321 | // setup traits check |
||
322 | $mockTraits = m::mock('phpDocumentor\Descriptor\ClassDescriptor'); |
||
323 | $mockTraits->shouldReceive('getConstants')->andReturn([]); |
||
324 | $mockTraits->shouldReceive('getProperties')->andReturn(new Collection([$mockTraitsProperties])); |
||
325 | $mockTraits->shouldReceive('getMethods')->andReturn(new Collection([$mockTraitsMethods])); |
||
326 | $mockTraits->shouldReceive('getErrors')->andReturn(new Collection($errorTraits)); |
||
327 | |||
328 | $this->fixture->getClasses()->set('my-test-traits', $mockTraits); |
||
329 | |||
330 | // setup functions check |
||
331 | $mockFunctions = m::mock('phpDocumentor\Descriptor\FunctionDescriptor'); |
||
332 | |||
333 | // create dummy instances of constants/methods |
||
334 | $mockFunctions->shouldReceive('getConstants')->andReturn([]); |
||
335 | $mockFunctions->shouldReceive('getProperties')->andReturn([]); |
||
336 | $mockFunctions->shouldReceive('getMethods')->andReturn([]); |
||
337 | $mockFunctions->shouldReceive('getErrors')->andReturn(new Collection($errorFunctions)); |
||
338 | |||
339 | $this->fixture->getClasses()->set('my-test-function', $mockFunctions); |
||
340 | |||
341 | // final merge and check |
||
342 | $expectedErrors = array_merge( |
||
343 | $errorGlobal, |
||
344 | $errorClasses, |
||
345 | $errorInterfaces, |
||
346 | $errorTraits, |
||
347 | $errorFunctions, |
||
348 | $errorClassMethods, |
||
349 | $errorClassConstants, |
||
350 | $errorClassProperties, |
||
351 | $errorInterfacesMethods, |
||
352 | $errorInterfacesConstants, |
||
353 | $errorTraitsMethods, |
||
354 | $errorTraitsProperties |
||
355 | ); |
||
356 | |||
357 | $this->assertSame($expectedErrors, $this->fixture->getAllErrors()->getAll()); |
||
358 | } |
||
359 | } |
||
360 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.