Completed
Push — develop ( 9b9aa6...c1fe12 )
by Jaap
03:37
created

testCreateThrowsExceptionWhenStrategyNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2018 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Reflection\Php;
14
15
use Mockery as m;
16
use phpDocumentor\Reflection\Fqsen;
17
use phpDocumentor\Reflection\Php\Factory\DummyFactoryStrategy;
18
use PHPUnit\Framework\TestCase;
19
20
/**
21
 * Test case for ProjectFactory
22
 *
23
 * @coversDefaultClass phpDocumentor\Reflection\Php\ProjectFactory
24
 * @covers ::create
25
 * @covers ::<private>
26
 */
27
class ProjectFactoryTest extends TestCase
28
{
29
    protected function tearDown()
30
    {
31
        m::close();
32
    }
33
34
    /**
35
     * @covers ::__construct
36
     */
37
    public function testStrategiesAreChecked()
38
    {
39
        new ProjectFactory([new DummyFactoryStrategy()]);
40
        $this->assertTrue(true);
41
    }
42
43
    public function testCreate()
44
    {
45
        $someOtherStrategy = m::mock(ProjectFactoryStrategy::class);
46
        $someOtherStrategy->shouldReceive('matches')->twice()->andReturn(false);
47
        $someOtherStrategy->shouldReceive('create')->never();
48
49
        $fileStrategyMock = m::mock(ProjectFactoryStrategy::class);
50
        $fileStrategyMock->shouldReceive('matches')->twice()->andReturn(true);
51
        $fileStrategyMock->shouldReceive('create')
52
            ->twice()
53
            ->andReturnValues(
54
                [
55
                    new File(md5('some/file.php'), 'some/file.php'),
56
                    new File(md5('some/other.php'), 'some/other.php'),
57
                ]
58
            );
59
60
        $projectFactory = new ProjectFactory([$someOtherStrategy, $fileStrategyMock]);
61
62
        $files = ['some/file.php', 'some/other.php'];
63
        $project = $projectFactory->create('MyProject', $files);
64
65
        $this->assertInstanceOf(Project::class, $project);
66
67
        $projectFilePaths = array_keys($project->getFiles());
68
        $this->assertEquals($files, $projectFilePaths);
69
    }
70
71
    /**
72
     * @expectedException \OutOfBoundsException
73
     */
74
    public function testCreateThrowsExceptionWhenStrategyNotFound()
75
    {
76
        $projectFactory = new ProjectFactory([]);
77
        $projectFactory->create('MyProject', ['aa']);
78
    }
79
80
    public function testCreateProjectFromFileWithNamespacedClass()
81
    {
82
        $file = new File(md5('some/file.php'), 'some/file.php');
83
        $file->addNamespace(new Fqsen('\mySpace'));
84
        $file->addClass(new Class_(new Fqsen('\mySpace\MyClass')));
85
86
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
87
88
        $this->assertEquals('\mySpace', key($namespaces));
89
90
        /** @var Namespace_ $mySpace */
91
        $mySpace = current($namespaces);
92
93
        $this->assertInstanceOf(Namespace_::class, $mySpace);
94
        $this->assertEquals('\mySpace\MyClass', key($mySpace->getClasses()));
95
    }
96
97
    public function testWithNamespacedInterface()
98
    {
99
        $file = new File(md5('some/file.php'), 'some/file.php');
100
        $file->addNamespace(new Fqsen('\mySpace'));
101
        $file->addInterface(new Interface_(new Fqsen('\mySpace\MyInterface')));
102
103
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
104
105
        /** @var Namespace_ $mySpace */
106
        $mySpace = current($namespaces);
107
108
        $this->assertInstanceOf(Namespace_::class, $mySpace);
109
        $this->assertEquals('\mySpace\MyInterface', key($mySpace->getInterfaces()));
110
    }
111
112
    public function testWithNamespacedFunction()
113
    {
114
        $file = new File(md5('some/file.php'), 'some/file.php');
115
        $file->addNamespace(new Fqsen('\mySpace'));
116
        $file->addFunction(new Function_(new Fqsen('\mySpace\function()')));
117
118
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
119
120
        /** @var Namespace_ $mySpace */
121
        $mySpace = current($namespaces);
122
123
        $this->assertInstanceOf(Namespace_::class, $mySpace);
124
        $this->assertEquals('\mySpace\function()', key($mySpace->getFunctions()));
125
    }
126
127
    public function testWithNamespacedConstant()
128
    {
129
        $file = new File(md5('some/file.php'), 'some/file.php');
130
        $file->addNamespace(new Fqsen('\mySpace'));
131
        $file->addConstant(new Constant(new Fqsen('\mySpace::MY_CONST')));
132
133
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
134
135
        /** @var Namespace_ $mySpace */
136
        $mySpace = current($namespaces);
137
138
        $this->assertInstanceOf(Namespace_::class, $mySpace);
139
        $this->assertEquals('\mySpace::MY_CONST', key($mySpace->getConstants()));
140
    }
141
142
    public function testWithNamespacedTrait()
143
    {
144
        $file = new File(md5('some/file.php'), 'some/file.php');
145
        $file->addNamespace(new Fqsen('\mySpace'));
146
        $file->addTrait(new Trait_(new Fqsen('\mySpace\MyTrait')));
147
148
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
149
150
        /** @var Namespace_ $mySpace */
151
        $mySpace = current($namespaces);
152
153
        $this->assertInstanceOf(Namespace_::class, $mySpace);
154
        $this->assertEquals('\mySpace\MyTrait', key($mySpace->getTraits()));
155
    }
156
157
    public function testNamespaceSpreadOverMultipleFiles()
158
    {
159
        $someFile = new File(md5('some/file.php'), 'some/file.php');
160
        $someFile->addNamespace(new Fqsen('\mySpace'));
161
        $someFile->addClass(new Class_(new Fqsen('\mySpace\MyClass')));
162
163
        $otherFile = new File(md5('some/other.php'), 'some/other.php');
164
        $otherFile->addNamespace(new Fqsen('\mySpace'));
165
        $otherFile->addClass(new Class_(new Fqsen('\mySpace\OtherClass')));
166
167
        $namespaces = $this->fetchNamespacesFromMultipleFiles([$otherFile, $someFile]);
168
169
        $this->assertCount(1, $namespaces);
170
        $this->assertCount(2, current($namespaces)->getClasses());
171
    }
172
173
    public function testSingleFileMultipleNamespaces()
174
    {
175
        $someFile = new File(md5('some/file.php'), 'some/file.php');
176
        $someFile->addNamespace(new Fqsen('\mySpace'));
177
        $someFile->addClass(new Class_(new Fqsen('\mySpace\MyClass')));
178
        $someFile->addNamespace(new Fqsen('\mySpace\SubSpace'));
179
        $someFile->addClass(new Class_(new Fqsen('\mySpace\SubSpace\MyClass')));
180
181
        $namespaces = $this->fetchNamespacesFromSingleFile($someFile);
182
183
        $this->assertCount(2, $namespaces);
184
        $this->assertArrayHasKey('\mySpace', $namespaces);
185
        $this->assertArrayHasKey('\mySpace\SubSpace', $namespaces);
186
187
        $this->assertCount(1, $namespaces['\mySpace']->getClasses());
188
    }
189
190
    public function testErrorScenarioWhenFileStrategyReturnsNull()
191
    {
192
        $fileStrategyMock = m::mock(ProjectFactoryStrategy::class);
193
        $fileStrategyMock->shouldReceive('matches')->twice()->andReturn(true);
194
        $fileStrategyMock->shouldReceive('create')
195
            ->twice()
196
            ->andReturnValues(
197
                [
198
                    null,
199
                    new File(md5('some/other.php'), 'some/other.php'),
200
                ]
201
            );
202
203
        $projectFactory = new ProjectFactory([$fileStrategyMock]);
204
205
        $files = ['some/file.php', 'some/other.php'];
206
        $project = $projectFactory->create('MyProject', $files);
207
208
        $this->assertInstanceOf(Project::class, $project);
209
210
        $projectFilePaths = array_keys($project->getFiles());
211
        $this->assertEquals(['some/other.php'], $projectFilePaths);
212
    }
213
214
    /**
215
     * Uses the ProjectFactory to create a Project and returns the namespaces created by the factory.
216
     *
217
     * @return Namespace_[] Namespaces of the project
218
     */
219
    private function fetchNamespacesFromSingleFile(File $file)
220
    {
221
        return $this->fetchNamespacesFromMultipleFiles([$file]);
222
    }
223
224
    /**
225
     * Uses the ProjectFactory to create a Project and returns the namespaces created by the factory.
226
     *
227
     * @param File[] $files
228
     * @return Namespace_[] Namespaces of the project
229
     */
230
    private function fetchNamespacesFromMultipleFiles($files)
231
    {
232
        $fileStrategyMock = m::mock(ProjectFactoryStrategy::class);
233
        $fileStrategyMock->shouldReceive('matches')->times(count($files))->andReturn(true);
234
        $fileStrategyMock->shouldReceive('create')
235
            ->times(count($files))
236
            ->andReturnValues(
237
                $files
238
            );
239
240
        $projectFactory = new ProjectFactory([$fileStrategyMock]);
241
        $project = $projectFactory->create('My Project', $files);
242
243
        return $project->getNamespaces();
244
    }
245
}
246