These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace phpDocumentor\Descriptor; |
||
4 | |||
5 | use Mockery\Adapter\Phpunit\MockeryTestCase; |
||
6 | use Mockery as m; |
||
7 | |||
8 | /** |
||
9 | * Tests for the \phpDocumentor\Descriptor\ProjectAnalyzer class. |
||
10 | */ |
||
11 | class ProjectAnalyzerTest extends MockeryTestCase |
||
12 | { |
||
13 | /** @var ProjectAnalyzer */ |
||
14 | private $fixture; |
||
15 | |||
16 | protected function setUp() |
||
17 | { |
||
18 | $this->fixture = new ProjectAnalyzer(); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::analyze |
||
23 | */ |
||
24 | public function testFilesAreCounted() |
||
25 | { |
||
26 | // Arrange |
||
27 | $projectDescriptor = $this->givenAProjectMock(); |
||
28 | $this->whenProjectDescriptorHasTheFollowingFiles($projectDescriptor, [1, 2, 3, 4]); |
||
29 | $this->whenProjectDescriptorHasTheFollowingElements($projectDescriptor, []); |
||
30 | $this->whenProjectHasTheFollowingChildrenOfRootNamespace($projectDescriptor, []); |
||
31 | |||
32 | // Act |
||
33 | $this->fixture->analyze($projectDescriptor); |
||
34 | |||
35 | // Assert |
||
36 | $this->assertAttributeSame(4, 'fileCount', $this->fixture); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::analyze |
||
41 | */ |
||
42 | public function testIfTopLevelNamespacesAreCounted() |
||
43 | { |
||
44 | // Arrange |
||
45 | $projectDescriptor = $this->givenAProjectMock(); |
||
46 | $this->whenProjectDescriptorHasTheFollowingFiles($projectDescriptor, []); |
||
47 | $this->whenProjectDescriptorHasTheFollowingElements($projectDescriptor, []); |
||
48 | $this->whenProjectHasTheFollowingChildrenOfRootNamespace($projectDescriptor, [1, 2, 3]); |
||
49 | |||
50 | // Act |
||
51 | $this->fixture->analyze($projectDescriptor); |
||
52 | |||
53 | // Assert |
||
54 | $this->assertAttributeSame(3, 'topLevelNamespaceCount', $this->fixture); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::analyze |
||
59 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::findAllElements |
||
60 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::incrementUnresolvedParentCounter |
||
61 | */ |
||
62 | public function testIfUnresolvedClassesAreCounted() |
||
63 | { |
||
64 | // Arrange |
||
65 | $classDescriptor1 = $this->givenAClassWithParent('phpDocumentor\Descriptor\ClassDescriptor'); |
||
66 | $projectDescriptor = $this->givenAProjectMock(); |
||
67 | $this->whenProjectDescriptorHasTheFollowingFiles($projectDescriptor, []); |
||
68 | $this->whenProjectDescriptorHasTheFollowingElements( |
||
69 | $projectDescriptor, |
||
70 | [ |
||
71 | 'ds1' => $classDescriptor1, |
||
72 | 'ds2' => $this->givenAClassWithParent($classDescriptor1), |
||
73 | 'ds3' => $this->givenAnInterfaceWithParent('123'), |
||
74 | ] |
||
75 | ); |
||
76 | $this->whenProjectHasTheFollowingChildrenOfRootNamespace($projectDescriptor, []); |
||
77 | |||
78 | // Act |
||
79 | $this->fixture->analyze($projectDescriptor); |
||
80 | |||
81 | // Assert |
||
82 | $this->assertAttributeSame(1, 'unresolvedParentClassesCount', $this->fixture); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::analyze |
||
87 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::findAllElements |
||
88 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::addElementToCounter |
||
89 | */ |
||
90 | public function testIfVariousDescriptorTypesAreCounted() |
||
91 | { |
||
92 | // Arrange |
||
93 | $classDescriptor1 = $this->givenAClassWithParent('phpDocumentor\Descriptor\ClassDescriptor'); |
||
94 | $projectDescriptor = $this->givenAProjectMock(); |
||
95 | $this->whenProjectDescriptorHasTheFollowingFiles($projectDescriptor, []); |
||
96 | $this->whenProjectDescriptorHasTheFollowingElements( |
||
97 | $projectDescriptor, |
||
98 | [ |
||
99 | 'ds1' => $classDescriptor1, |
||
100 | 'ds2' => $this->givenAClassWithParent($classDescriptor1), |
||
101 | 'ds3' => $this->givenAnInterfaceWithParent('123'), |
||
102 | ] |
||
103 | ); |
||
104 | $this->whenProjectHasTheFollowingChildrenOfRootNamespace($projectDescriptor, []); |
||
105 | |||
106 | // Act |
||
107 | $this->fixture->analyze($projectDescriptor); |
||
108 | |||
109 | // Assert |
||
110 | $this->assertAttributeSame( |
||
111 | [ |
||
112 | 'phpDocumentor\Descriptor\ClassDescriptor' => 2, |
||
113 | 'phpDocumentor\Descriptor\InterfaceDescriptor' => 1, |
||
114 | ], |
||
115 | 'descriptorCountByType', |
||
116 | $this->fixture |
||
117 | ); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @covers \phpDocumentor\Descriptor\ProjectAnalyzer::__toString |
||
122 | */ |
||
123 | public function testIfStringOutputContainsAllCounters() |
||
124 | { |
||
125 | // Arrange |
||
126 | $classDescriptor1 = $this->givenAClassWithParent('phpDocumentor\Descriptor\ClassDescriptor'); |
||
127 | $projectDescriptor = $this->givenAProjectMock(); |
||
128 | $this->whenProjectDescriptorHasTheFollowingFiles($projectDescriptor, [1, 2, 3, 4]); |
||
129 | $this->whenProjectDescriptorHasTheFollowingElements( |
||
130 | $projectDescriptor, |
||
131 | [ |
||
132 | 'ds1' => $classDescriptor1, |
||
133 | 'ds2' => $this->givenAClassWithParent($classDescriptor1), |
||
134 | 'ds3' => $this->givenAnInterfaceWithParent('123'), |
||
135 | ] |
||
136 | ); |
||
137 | $this->whenProjectHasTheFollowingChildrenOfRootNamespace($projectDescriptor, [1, 2, 3]); |
||
138 | $this->fixture->analyze($projectDescriptor); |
||
139 | |||
140 | $expected = <<<TEXT |
||
141 | In the ProjectDescriptor are: |
||
142 | 4 files |
||
143 | 3 top-level namespaces |
||
144 | 1 unresolvable parent classes |
||
145 | 2 phpDocumentor\Descriptor\ClassDescriptor elements |
||
146 | 1 phpDocumentor\Descriptor\InterfaceDescriptor elements |
||
147 | |||
148 | TEXT; |
||
149 | $expected = str_replace("\n", PHP_EOL, $expected); |
||
150 | |||
151 | // Act |
||
152 | $result = (string) $this->fixture; |
||
153 | |||
154 | // Assert |
||
155 | $this->assertSame($expected, $result); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns a class with the given parent set. |
||
160 | * |
||
161 | * @param string|DescriptorAbstract $parent |
||
162 | */ |
||
163 | protected function givenAClassWithParent($parent): ClassDescriptor |
||
164 | { |
||
165 | $classDescriptor1 = new ClassDescriptor(); |
||
166 | $classDescriptor1->setParent($parent); |
||
0 ignored issues
–
show
|
|||
167 | return $classDescriptor1; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param string $interfaceParent |
||
172 | */ |
||
173 | protected function givenAnInterfaceWithParent($interfaceParent): InterfaceDescriptor |
||
174 | { |
||
175 | $classDescriptor3 = new InterfaceDescriptor(); |
||
176 | $classDescriptor3->setParent($interfaceParent); |
||
0 ignored issues
–
show
$interfaceParent is of type string , but the function expects a object<phpDocumentor\Descriptor\Collection> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
177 | |||
178 | return $classDescriptor3; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Returns a mocked ProjectDescriptor object. |
||
183 | */ |
||
184 | protected function givenAProjectMock(): m\MockInterface |
||
185 | { |
||
186 | return m::mock('phpDocumentor\Descriptor\ProjectDescriptor')->shouldIgnoreMissing(); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Ensures that the ProjectDescriptor contains and returns the provided files. |
||
191 | */ |
||
192 | protected function whenProjectDescriptorHasTheFollowingFiles(m\MockInterface $projectDescriptor, array $files) |
||
193 | { |
||
194 | $projectDescriptor->shouldReceive('getFiles')->andReturn($files); |
||
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...
|
|||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Ensures that the ProjectDescriptor has an index 'elements' with the provided elements. |
||
199 | */ |
||
200 | protected function whenProjectDescriptorHasTheFollowingElements(m\MockInterface $projectDescriptor, array $elements) |
||
201 | { |
||
202 | $projectDescriptor->shouldReceive('getIndexes->get') |
||
203 | ->with('elements', m::type('phpDocumentor\Descriptor\Collection')) |
||
204 | ->andReturn(new Collection($elements)); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Ensures that the ProjectDescriptor has a root namespace with the provided array as children of that namespace. |
||
209 | */ |
||
210 | protected function whenProjectHasTheFollowingChildrenOfRootNamespace( |
||
211 | m\MockInterface $projectDescriptor, |
||
212 | array $rootNamespaceChildren |
||
213 | ) { |
||
214 | $projectDescriptor->shouldReceive('getNamespace->getChildren')->andReturn( |
||
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...
|
|||
215 | new Collection($rootNamespaceChildren) |
||
216 | ); |
||
217 | } |
||
218 | } |
||
219 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.