Completed
Push — develop ( 5e5d58...9b9aa6 )
by Mike
04:21
created

ProjectFactoryTest::testWithNamespacedConstant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
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
    public function testSilentlyFailsWhenStrategyNotFound()
72
    {
73
        $projectFactory = new ProjectFactory([]);
74
        $projectFactory->create('MyProject', ['aa']);
75
    }
76
77
    public function testCreateProjectFromFileWithNamespacedClass()
78
    {
79
        $file = new File(md5('some/file.php'), 'some/file.php');
80
        $file->addNamespace(new Fqsen('\mySpace'));
81
        $file->addClass(new Class_(new Fqsen('\mySpace\MyClass')));
82
83
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
84
85
        $this->assertEquals('\mySpace', key($namespaces));
86
87
        /** @var Namespace_ $mySpace */
88
        $mySpace = current($namespaces);
89
90
        $this->assertInstanceOf(Namespace_::class, $mySpace);
91
        $this->assertEquals('\mySpace\MyClass', key($mySpace->getClasses()));
92
    }
93
94
    public function testWithNamespacedInterface()
95
    {
96
        $file = new File(md5('some/file.php'), 'some/file.php');
97
        $file->addNamespace(new Fqsen('\mySpace'));
98
        $file->addInterface(new Interface_(new Fqsen('\mySpace\MyInterface')));
99
100
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
101
102
        /** @var Namespace_ $mySpace */
103
        $mySpace = current($namespaces);
104
105
        $this->assertInstanceOf(Namespace_::class, $mySpace);
106
        $this->assertEquals('\mySpace\MyInterface', key($mySpace->getInterfaces()));
107
    }
108
109
    public function testWithNamespacedFunction()
110
    {
111
        $file = new File(md5('some/file.php'), 'some/file.php');
112
        $file->addNamespace(new Fqsen('\mySpace'));
113
        $file->addFunction(new Function_(new Fqsen('\mySpace\function()')));
114
115
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
116
117
        /** @var Namespace_ $mySpace */
118
        $mySpace = current($namespaces);
119
120
        $this->assertInstanceOf(Namespace_::class, $mySpace);
121
        $this->assertEquals('\mySpace\function()', key($mySpace->getFunctions()));
122
    }
123
124
    public function testWithNamespacedConstant()
125
    {
126
        $file = new File(md5('some/file.php'), 'some/file.php');
127
        $file->addNamespace(new Fqsen('\mySpace'));
128
        $file->addConstant(new Constant(new Fqsen('\mySpace::MY_CONST')));
129
130
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
131
132
        /** @var Namespace_ $mySpace */
133
        $mySpace = current($namespaces);
134
135
        $this->assertInstanceOf(Namespace_::class, $mySpace);
136
        $this->assertEquals('\mySpace::MY_CONST', key($mySpace->getConstants()));
137
    }
138
139
    public function testWithNamespacedTrait()
140
    {
141
        $file = new File(md5('some/file.php'), 'some/file.php');
142
        $file->addNamespace(new Fqsen('\mySpace'));
143
        $file->addTrait(new Trait_(new Fqsen('\mySpace\MyTrait')));
144
145
        $namespaces = $this->fetchNamespacesFromSingleFile($file);
146
147
        /** @var Namespace_ $mySpace */
148
        $mySpace = current($namespaces);
149
150
        $this->assertInstanceOf(Namespace_::class, $mySpace);
151
        $this->assertEquals('\mySpace\MyTrait', key($mySpace->getTraits()));
152
    }
153
154
    public function testNamespaceSpreadOverMultipleFiles()
155
    {
156
        $someFile = new File(md5('some/file.php'), 'some/file.php');
157
        $someFile->addNamespace(new Fqsen('\mySpace'));
158
        $someFile->addClass(new Class_(new Fqsen('\mySpace\MyClass')));
159
160
        $otherFile = new File(md5('some/other.php'), 'some/other.php');
161
        $otherFile->addNamespace(new Fqsen('\mySpace'));
162
        $otherFile->addClass(new Class_(new Fqsen('\mySpace\OtherClass')));
163
164
        $namespaces = $this->fetchNamespacesFromMultipleFiles([$otherFile, $someFile]);
165
166
        $this->assertCount(1, $namespaces);
167
        $this->assertCount(2, current($namespaces)->getClasses());
168
    }
169
170
    public function testSingleFileMultipleNamespaces()
171
    {
172
        $someFile = new File(md5('some/file.php'), 'some/file.php');
173
        $someFile->addNamespace(new Fqsen('\mySpace'));
174
        $someFile->addClass(new Class_(new Fqsen('\mySpace\MyClass')));
175
        $someFile->addNamespace(new Fqsen('\mySpace\SubSpace'));
176
        $someFile->addClass(new Class_(new Fqsen('\mySpace\SubSpace\MyClass')));
177
178
        $namespaces = $this->fetchNamespacesFromSingleFile($someFile);
179
180
        $this->assertCount(2, $namespaces);
181
        $this->assertArrayHasKey('\mySpace', $namespaces);
182
        $this->assertArrayHasKey('\mySpace\SubSpace', $namespaces);
183
184
        $this->assertCount(1, $namespaces['\mySpace']->getClasses());
185
    }
186
187
    /**
188
     * Uses the ProjectFactory to create a Project and returns the namespaces created by the factory.
189
     *
190
     * @return Namespace_[] Namespaces of the project
191
     */
192
    private function fetchNamespacesFromSingleFile(File $file)
193
    {
194
        return $this->fetchNamespacesFromMultipleFiles([$file]);
195
    }
196
197
    /**
198
     * Uses the ProjectFactory to create a Project and returns the namespaces created by the factory.
199
     *
200
     * @param File[] $files
201
     * @return Namespace_[] Namespaces of the project
202
     */
203
    private function fetchNamespacesFromMultipleFiles($files)
204
    {
205
        $fileStrategyMock = m::mock(ProjectFactoryStrategy::class);
206
        $fileStrategyMock->shouldReceive('matches')->times(count($files))->andReturn(true);
207
        $fileStrategyMock->shouldReceive('create')
208
            ->times(count($files))
209
            ->andReturnValues(
210
                $files
211
            );
212
213
        $projectFactory = new ProjectFactory([$fileStrategyMock]);
214
        $project = $projectFactory->create('My Project', $files);
215
216
        return $project->getNamespaces();
217
    }
218
}
219