Completed
Push — develop ( 28aa0b...3fae3c )
by Jaap
13s
created

ProjectFactoryTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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-2015 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
30
    protected function tearDown()
31
    {
32
        m::close();
33
    }
34
35
    /**
36
     * @covers ::__construct
37
     */
38
    public function testStrategiesAreChecked()
39
    {
40
        new ProjectFactory(array(new DummyFactoryStrategy()));
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
                array(
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(array($someOtherStrategy, $fileStrategyMock));
61
62
        $files = array('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(array());
77
        $projectFactory->create('MyProject', array('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(array($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
    /**
191
     * Uses the ProjectFactory to create a Project and returns the namespaces created by the factory.
192
     *
193
     * @param File $file
194
     * @return Namespace_[] Namespaces of the project
195
     */
196
    private function fetchNamespacesFromSingleFile(File $file)
197
    {
198
        return $this->fetchNamespacesFromMultipleFiles(array($file));
199
    }
200
201
    /**
202
     * Uses the ProjectFactory to create a Project and returns the namespaces created by the factory.
203
     *
204
     * @param File[] $files
205
     * @return Namespace_[] Namespaces of the project
206
     */
207
208
    private function fetchNamespacesFromMultipleFiles($files)
209
    {
210
        $fileStrategyMock = m::mock(ProjectFactoryStrategy::class);
211
        $fileStrategyMock->shouldReceive('matches')->times(count($files))->andReturn(true);
212
        $fileStrategyMock->shouldReceive('create')
213
            ->times(count($files))
214
            ->andReturnValues(
215
                $files
216
            );
217
218
        $projectFactory = new ProjectFactory(array($fileStrategyMock));
219
        $project = $projectFactory->create('My Project', $files);
220
221
        return $project->getNamespaces();
222
    }
223
}
224