Completed
Push — develop ( 521632...eb9c4a )
by Mike
06:46
created

CommandlineOptionsMiddlewareTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 269
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 5

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testItShouldOverwriteTheDestinationFolderBasedOnTheTargetOption() 0 10 1
A testItShouldDisableTheCacheBasedOnTheForceOption() 0 9 1
A testItShouldOverwriteTheCacheFolderBasedOnTheCacheFolderOption() 0 10 1
A testItShouldOverrideTheTitleBasedOnTheTitleOption() 0 10 1
A testItShouldOverrideTheListOfTemplatesBasedOnTheTemplateOption() 0 10 1
A testItShouldAddSourceFilesForDefaultConfiguration() 0 15 1
A testItShouldAddSourceDirectoriesForDefaultConfiguration() 0 15 1
A testItShouldAddAbsoluteSourcePathsToNewApi() 0 14 1
A testItShouldAddAbsoluteSourcePathsToNewApiAndRelativeToCurrent() 0 21 1
A testItShouldRegisterExtensionsForDefaultConfiguration() 0 13 1
A testItShouldReplaceIgnoredDirectoriesForDefaultConfiguration() 0 15 1
A testItShouldOverwriteTheMarkersOfTheDefaultConfiguration() 0 13 1
A testItShouldOverwriteTheVisibilitySetInTheDefaultConfiguration() 0 13 1
A testItShouldOverwriteTheDefaultPackageNameSetInTheDefaultConfiguration() 0 13 1
A testItShouldOverwriteTheWhetherToIncludeSourcecodeInTheDefaultConfiguration() 0 20 1
A givenAConfigurationWithoutApiDefinition() 0 10 1
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-2016 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\Configuration;
14
15
use Mockery\Adapter\Phpunit\MockeryTestCase;
16
use phpDocumentor\Configuration\Factory\Version3;
17
use phpDocumentor\Dsn;
18
use phpDocumentor\Path;
19
20
/**
21
 * @coversDefaultClass \phpDocumentor\Configuration\CommandlineOptionsMiddleware
22
 * @covers ::__construct
23
 * @covers ::<private>
24
 */
25
final class CommandlineOptionsMiddlewareTest extends MockeryTestCase
26
{
27
    /**
28
     * @covers ::__invoke
29
     */
30
    public function testItShouldOverwriteTheDestinationFolderBasedOnTheTargetOption()
31
    {
32
        $expected = '/abc';
33
        $configuration = ['phpdocumentor' => ['paths' => ['output' => '/tmp']]];
34
35
        $middleware = new CommandlineOptionsMiddleware(['target' => $expected]);
36
        $newConfiguration = $middleware($configuration);
37
38
        $this->assertEquals(new Dsn($expected), $newConfiguration['phpdocumentor']['paths']['output']);
39
    }
40
41
    /**
42
     * @covers ::__invoke
43
     */
44
    public function testItShouldDisableTheCacheBasedOnTheForceOption()
45
    {
46
        $configuration = ['phpdocumentor' => ['use-cache' => true]];
47
48
        $middleware = new CommandlineOptionsMiddleware(['force' => true]);
49
        $newConfiguration = $middleware($configuration);
50
51
        $this->assertFalse($newConfiguration['phpdocumentor']['use-cache']);
52
    }
53
54
    /**
55
     * @covers ::__invoke
56
     */
57
    public function testItShouldOverwriteTheCacheFolderBasedOnTheCacheFolderOption()
58
    {
59
        $expected = '/abc';
60
        $configuration = ['phpdocumentor' => ['paths' => ['cache' => '/tmp']]];
61
62
        $middleware = new CommandlineOptionsMiddleware(['cache-folder' => $expected]);
63
        $newConfiguration = $middleware->__invoke($configuration);
64
65
        $this->assertEquals(new Path($expected), $newConfiguration['phpdocumentor']['paths']['cache']);
66
    }
67
68
    /**
69
     * @covers ::__invoke
70
     */
71
    public function testItShouldOverrideTheTitleBasedOnTheTitleOption()
72
    {
73
        $expected = 'phpDocumentor3';
74
        $configuration = ['phpdocumentor' => ['title' => 'phpDocumentor2']];
75
76
        $middleware = new CommandlineOptionsMiddleware(['title' => $expected]);
77
        $newConfiguration = $middleware($configuration);
78
79
        $this->assertSame($expected, $newConfiguration['phpdocumentor']['title']);
80
    }
81
82
    /**
83
     * @covers ::__invoke
84
     */
85
    public function testItShouldOverrideTheListOfTemplatesBasedOnTheTemplateOption()
86
    {
87
        $expected = 'clean';
88
        $configuration = ['phpdocumentor' => ['templates' => [['name' => 'responsive']]]];
89
90
        $middleware = new CommandlineOptionsMiddleware(['template' => $expected]);
91
        $newConfiguration = $middleware($configuration);
92
93
        $this->assertSame([['name' => $expected]], $newConfiguration['phpdocumentor']['templates']);
94
    }
95
96
    /**
97
     * @covers ::__invoke
98
     */
99
    public function testItShouldAddSourceFilesForDefaultConfiguration()
100
    {
101
        $configuration = $this->givenAConfigurationWithoutApiDefinition();
102
103
        $middleware = new CommandlineOptionsMiddleware(['filename' => ['./src/index.php']]);
104
        $newConfiguration = $middleware($configuration);
105
106
        $this->assertEquals(
107
            [
108
                'dsn' => new Dsn('file://.'),
109
                'paths' => [new Path('./src/index.php')],
110
            ],
111
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['source']
112
        );
113
    }
114
115
    /**
116
     * @covers ::__invoke
117
     */
118
    public function testItShouldAddSourceDirectoriesForDefaultConfiguration()
119
    {
120
        $configuration = $this->givenAConfigurationWithoutApiDefinition();
121
122
        $middleware = new CommandlineOptionsMiddleware(['directory' => ['./src']]);
123
        $newConfiguration = $middleware($configuration);
124
125
        $this->assertEquals(
126
            [
127
                'dsn' => new Dsn('file://.'),
128
                'paths' => [new Path('./src')],
129
            ],
130
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['source']
131
        );
132
    }
133
134
    public function testItShouldAddAbsoluteSourcePathsToNewApi()
135
    {
136
        $configuration = Version3::buildDefault();
137
        $middleware = new CommandlineOptionsMiddleware(['directory' => ['/src']]);
138
        $newConfiguration = $middleware($configuration);
139
140
        $this->assertEquals(
141
            [
142
                'dsn' => new Dsn('file:///src'),
143
                'paths' => [new Path('./')],
144
            ],
145
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['source']
146
        );
147
    }
148
149
    public function testItShouldAddAbsoluteSourcePathsToNewApiAndRelativeToCurrent()
150
    {
151
        $configuration = Version3::buildDefault();
152
        $middleware = new CommandlineOptionsMiddleware(['directory' => ['/src', './localSrc']]);
153
        $newConfiguration = $middleware($configuration);
154
155
        $this->assertEquals(
156
            [
157
                'dsn' => new Dsn('file:///src'),
158
                'paths' => [new Path('./')],
159
            ],
160
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['source']
161
        );
162
        $this->assertEquals(
163
            [
164
                'dsn' => new Dsn('file://.'),
165
                'paths' => [new Path('./localSrc')],
166
            ],
167
            current($newConfiguration['phpdocumentor']['versions'])['api'][1]['source']
168
        );
169
    }
170
171
172
    /**
173
     * @covers ::__invoke
174
     */
175
    public function testItShouldRegisterExtensionsForDefaultConfiguration()
176
    {
177
        $configuration = $this->givenAConfigurationWithoutApiDefinition();
178
179
        $extensions = ['php7', 'php5'];
180
        $middleware = new CommandlineOptionsMiddleware(['extensions' => $extensions]);
181
        $newConfiguration = $middleware($configuration);
182
183
        $this->assertEquals(
184
            $extensions,
185
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['extensions']
186
        );
187
    }
188
189
    /**
190
     * @covers ::__invoke
191
     */
192
    public function testItShouldReplaceIgnoredDirectoriesForDefaultConfiguration()
193
    {
194
        $configuration = $this->givenAConfigurationWithoutApiDefinition();
195
196
        $middleware = new CommandlineOptionsMiddleware(['ignore' => ['./src']]);
197
        $newConfiguration = $middleware($configuration);
198
199
        $this->assertEquals(
200
            [
201
                'paths' => [new Path('./src')],
202
                'hidden' => true
203
            ],
204
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['ignore']
205
        );
206
    }
207
208
    /**
209
     * @covers ::__invoke
210
     */
211
    public function testItShouldOverwriteTheMarkersOfTheDefaultConfiguration()
212
    {
213
        $configuration = $this->givenAConfigurationWithoutApiDefinition();
214
215
        $markers = ['FIXME2', 'TODOSOMETIME'];
216
        $middleware = new CommandlineOptionsMiddleware(['markers' => $markers]);
217
        $newConfiguration = $middleware($configuration);
218
219
        $this->assertEquals(
220
            $markers,
221
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['markers']
222
        );
223
    }
224
225
    /**
226
     * @covers ::__invoke
227
     */
228
    public function testItShouldOverwriteTheVisibilitySetInTheDefaultConfiguration()
229
    {
230
        $configuration = $this->givenAConfigurationWithoutApiDefinition();
231
232
        $visibility = ['public'];
233
        $middleware = new CommandlineOptionsMiddleware(['visibility' => $visibility]);
234
        $newConfiguration = $middleware($configuration);
235
236
        $this->assertEquals(
237
            $visibility,
238
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['visibility']
239
        );
240
    }
241
242
    /**
243
     * @covers ::__invoke
244
     */
245
    public function testItShouldOverwriteTheDefaultPackageNameSetInTheDefaultConfiguration()
246
    {
247
        $configuration = $this->givenAConfigurationWithoutApiDefinition();
248
249
        $defaultPackageName = ['public'];
250
        $middleware = new CommandlineOptionsMiddleware(['defaultpackagename' => $defaultPackageName]);
251
        $newConfiguration = $middleware($configuration);
252
253
        $this->assertEquals(
254
            $defaultPackageName,
255
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['default-package-name']
256
        );
257
    }
258
259
    /**
260
     * @covers ::__invoke
261
     */
262
    public function testItShouldOverwriteTheWhetherToIncludeSourcecodeInTheDefaultConfiguration()
263
    {
264
        $configuration = $this->givenAConfigurationWithoutApiDefinition();
265
266
        $middleware = new CommandlineOptionsMiddleware(['sourcecode' => true]);
267
        $newConfiguration = $middleware($configuration);
268
269
        $this->assertEquals(
270
            true,
271
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['include-source']
272
        );
273
274
        $middleware = new CommandlineOptionsMiddleware(['sourcecode' => false]);
275
        $newConfiguration = $middleware($configuration);
276
277
        $this->assertEquals(
278
            false,
279
            current($newConfiguration['phpdocumentor']['versions'])['api'][0]['include-source']
280
        );
281
    }
282
283
    private function givenAConfigurationWithoutApiDefinition(): array
284
    {
285
        $configuration = Version3::buildDefault();
286
287
        // wipe version so that middleware needs to re-add the api key
288
        unset($configuration['phpdocumentor']['versions']);
289
        $configuration['phpdocumentor']['versions'] = ['1.0.0' => []];
290
291
        return $configuration;
292
    }
293
}
294