Completed
Push — develop ( 392713...7e37ef )
by Jaap
20s queued 11s
created

testWhenDefaultFileIsInvalidXMLThenAnExceptionIsRaised()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 18
rs 9.6666
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\Configuration;
14
15
use Mockery\Adapter\Phpunit\MockeryTestCase;
16
use Mockery as m;
17
use org\bovigo\vfs\vfsStream;
18
use phpDocumentor\Configuration\Factory\Strategy;
19
use phpDocumentor\Configuration\Factory\Version2;
20
use phpDocumentor\Configuration\Factory\Version3;
21
use phpDocumentor\Configuration\Exception\InvalidConfigPathException;
22
use phpDocumentor\Uri;
23
24
/**
25
 * Test case for ConfigurationFactory
26
 *
27
 * @coversDefaultClass \phpDocumentor\Configuration\ConfigurationFactory
28
 * @covers ::<private>
29
 * @covers ::__construct
30
 */
31
final class ConfigurationFactoryTest extends MockeryTestCase
32
{
33
    /**
34
     * @covers ::fromUri
35
     */
36
    public function testItLoadsASpecificConfigurationFileUsingTheCorrectStrategy()
37
    {
38
        $configurationFactory = new ConfigurationFactory(
39
            [
40
                new Version3('data/xsd/phpdoc.xsd'),
41
                new Version2()
42
            ],
43
            []
44
        );
45
46
        $content = '<phpdocumentor><title>My title</title></phpdocumentor>';
47
        $configuration = $configurationFactory->fromUri(
48
            new Uri($this->givenExampleConfigurationFileWithContent($content))
49
        );
50
51
        $this->assertSame('My title', $configuration['phpdocumentor']['title']);
52
    }
53
    /**
54
     * @covers ::fromUri
55
     */
56
    public function testLoadingFromUriFailsIfFileDoesNotExist()
57
    {
58
        $this->expectException(InvalidConfigPathException::class);
59
        $this->expectExceptionMessage('File file:///does-not-exist could not be found');
60
        $configurationFactory = new ConfigurationFactory([new Version2()], []);
61
        $configurationFactory->fromUri(new Uri('/does-not-exist'));
62
    }
63
64
    /**
65
     * @covers ::fromDefaultLocations()
66
     */
67
    public function testThatTheDefaultConfigurationFilesAreLoaded()
68
    {
69
        $file = $this->givenExampleConfigurationFileWithContent(
70
            '<phpdocumentor><title>My title</title></phpdocumentor>'
71
        );
72
        $configurationFactory = new ConfigurationFactory([new Version2()], [$file]);
73
74
        $configuration = $configurationFactory->fromDefaultLocations();
75
76
        $this->assertSame('My title', $configuration['phpdocumentor']['title']);
77
    }
78
79
    /**
80
     * @covers ::fromDefaultLocations()
81
     */
82
    public function testWhenNoneOfTheDefaultsExistThatTheBakedConfigIsUsed()
83
    {
84
        $configurationFactory = new ConfigurationFactory([new Version2()], ['does_not_exist.xml']);
85
86
        $configuration = $configurationFactory->fromDefaultLocations();
87
88
        $this->assertEquals(Version3::buildDefault(), $configuration->getArrayCopy());
89
    }
90
91
    /**
92
     * @covers ::fromDefaultLocations()
93
     */
94
    public function testWhenDefaultFileIsInvalidXMLThenAnExceptionIsRaised()
95
    {
96
        $file = $this->givenExampleConfigurationFileWithContent(
97
          '<?xml version="1.0" encoding="UTF-8" ?>' .
98
          '<phpdocumentor xmlns="http://www.phpdoc.org" version="3">' .
99
            '<foo/>' .
100
          '</phpdocumentor>'
101
        );
102
        $configurationFactory = new ConfigurationFactory(
103
          [new Version3(__DIR__ . '/../../../../data/xsd/phpdoc.xsd')],
104
          [$file]
105
        );
106
107
        $this->expectException(\InvalidArgumentException::class);
108
        $this->expectExceptionMessage("Element '{http://www.phpdoc.org}foo': This element is not expected. Expected is ( {http://www.phpdoc.org}paths ).");
109
        //
110
        $configurationFactory->fromDefaultLocations();
111
    }
112
113
    /**
114
     * @covers ::addMiddleware
115
     */
116
    public function testThatMiddlewaresCanBeAddedAndAreThenApplied()
117
    {
118
        $inputValue = ['test'];
119
        $afterMiddleware1Value = ['test', 'test2'];
120
        $afterMiddleware2Value = ['test', 'test2', 'test3'];
121
122
        $middleWare1 = $this->givenAMiddlewareThatReturns($inputValue, $afterMiddleware1Value);
123
        $middleWare2 = $this->givenAMiddlewareThatReturns($afterMiddleware1Value, $afterMiddleware2Value);
124
125
        $factory = new ConfigurationFactory([$this->givenAValidStrategyThatReturns($inputValue)], []);
126
        $factory->addMiddleware($middleWare1);
127
        $factory->addMiddleware($middleWare2);
128
129
        $data = $factory->fromUri(new Uri($this->givenExampleConfigurationFileWithContent('<foo></foo>')));
130
131
        $this->assertSame($afterMiddleware2Value, $data->getArrayCopy());
132
    }
133
134
    /**
135
     * @covers ::fromUri
136
     * @expectedException \Exception
137
     * @expectedExceptionMessage No supported configuration files were found
138
     */
139
    public function testItHaltsIfNoMatchingStrategyCanBeFound()
140
    {
141
        $strategies = []; // No strategy means nothing could match ;)
142
        $configurationFactory = new ConfigurationFactory($strategies, []);
143
144
        $configurationFactory->fromUri(
145
            new Uri($this->givenExampleConfigurationFileWithContent('<foo></foo>'))
146
        );
147
    }
148
149
    /**
150
     * @covers ::__construct
151
     * @expectedException \TypeError
152
     */
153
    public function testItErrorsWhenTryingToInitializeWithSomethingOtherThanAStrategy()
154
    {
155
        new ConfigurationFactory(['this_is_not_a_strategy'], []);
0 ignored issues
show
Documentation introduced by Mike van Riel
array('this_is_not_a_strategy') is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<php...Configuration\iterable>.

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...
156
    }
157
158
    private function givenExampleConfigurationFileWithContent($content): string
159
    {
160
        vfsStream::newFile('foo.xml')
161
            ->at(vfsStream::setup('dir'))
162
            ->withContent($content);
163
164
        return vfsStream::url('dir/foo.xml');
165
    }
166
167
    private function givenAMiddlewareThatReturns($expectedInputValue, $returnValue): \Closure
168
    {
169
        return function ($value) use ($expectedInputValue, $returnValue) {
170
            $this->assertSame($expectedInputValue, $value);
171
172
            return $returnValue;
173
        };
174
    }
175
176
    private function givenAValidStrategyThatReturns($result): Strategy
177
    {
178
        /** @var m\Mock $strategy */
179
        $strategy = m::mock(Strategy::class);
180
        $strategy->shouldReceive('supports')
181
            ->once()
182
            ->with(m::type(\SimpleXMLElement::class))
183
            ->andReturn(true);
184
        $strategy
185
            ->shouldReceive('convert')
186
            ->once()
187
            ->with(m::type(\SimpleXMLElement::class))->andReturn($result);
188
189
        return $strategy;
190
    }
191
}
192