Completed
Push — develop ( 2993ce...48a3ec )
by Mike
09:32
created

  A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
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\Uri;
22
23
/**
24
 * Test case for ConfigurationFactory
25
 *
26
 * @coversDefaultClass \phpDocumentor\Configuration\ConfigurationFactory
27
 * @covers ::<private>
28
 * @covers ::__construct
29
 */
30
final class ConfigurationFactoryTest extends MockeryTestCase
31
{
32
    /**
33
     * @covers ::fromUri
34
     */
35
    public function testItLoadsASpecificConfigurationFileUsingTheCorrectStrategy()
36
    {
37
        $configurationFactory = new ConfigurationFactory(
38
            [
39
                new Version3('data/xsd/phpdoc.xsd'),
40
                new Version2()
41
            ],
42
            []
43
        );
44
45
        $content = '<phpdocumentor><title>My title</title></phpdocumentor>';
46
        $configuration = $configurationFactory->fromUri(
47
            new Uri($this->givenExampleConfigurationFileWithContent($content))
48
        );
49
50
        $this->assertSame('My title', $configuration['phpdocumentor']['title']);
51
    }
52
    /**
53
     * @covers ::fromUri
54
     */
55
    public function testLoadingFromUriFailsIfFileDoesNotExist()
56
    {
57
        $this->expectException(\InvalidArgumentException::class);
58
        $this->expectExceptionMessage('File file:///does-not-exist could not be found');
59
        $configurationFactory = new ConfigurationFactory([new Version2()], []);
60
        $configurationFactory->fromUri(new Uri('/does-not-exist'));
61
    }
62
63
    /**
64
     * @covers ::fromDefaultLocations()
65
     */
66
    public function testThatTheDefaultConfigurationFilesAreLoaded()
67
    {
68
        $file = $this->givenExampleConfigurationFileWithContent(
69
            '<phpdocumentor><title>My title</title></phpdocumentor>'
70
        );
71
        $configurationFactory = new ConfigurationFactory([new Version2()], [$file]);
72
73
        $configuration = $configurationFactory->fromDefaultLocations();
74
75
        $this->assertSame('My title', $configuration['phpdocumentor']['title']);
76
    }
77
78
    /**
79
     * @covers ::fromDefaultLocations()
80
     */
81
    public function testWhenNoneOfTheDefaultsExistThatTheBakedConfigIsUsed()
82
    {
83
        $configurationFactory = new ConfigurationFactory([new Version2()], ['does_not_exist.xml']);
84
85
        $configuration = $configurationFactory->fromDefaultLocations();
86
87
        $this->assertEquals(Version3::buildDefault(), $configuration->getArrayCopy());
88
    }
89
90
    /**
91
     * @covers ::addMiddleware
92
     */
93
    public function testThatMiddlewaresCanBeAddedAndAreThenApplied()
94
    {
95
        $inputValue = ['test'];
96
        $afterMiddleware1Value = ['test', 'test2'];
97
        $afterMiddleware2Value = ['test', 'test2', 'test3'];
98
99
        $middleWare1 = $this->givenAMiddlewareThatReturns($inputValue, $afterMiddleware1Value);
100
        $middleWare2 = $this->givenAMiddlewareThatReturns($afterMiddleware1Value, $afterMiddleware2Value);
101
102
        $factory = new ConfigurationFactory([$this->givenAValidStrategyThatReturns($inputValue)], []);
103
        $factory->addMiddleware($middleWare1);
104
        $factory->addMiddleware($middleWare2);
105
106
        $data = $factory->fromUri(new Uri($this->givenExampleConfigurationFileWithContent('<foo></foo>')));
107
108
        $this->assertSame($afterMiddleware2Value, $data->getArrayCopy());
109
    }
110
111
    /**
112
     * @covers ::fromUri
113
     * @expectedException \Exception
114
     * @expectedExceptionMessage No supported configuration files were found
115
     */
116
    public function testItHaltsIfNoMatchingStrategyCanBeFound()
117
    {
118
        $strategies = []; // No strategy means nothing could match ;)
119
        $configurationFactory = new ConfigurationFactory($strategies, []);
120
121
        $configurationFactory->fromUri(
122
            new Uri($this->givenExampleConfigurationFileWithContent('<foo></foo>'))
123
        );
124
    }
125
126
    /**
127
     * @covers ::__construct
128
     * @expectedException \TypeError
129
     */
130
    public function testItErrorsWhenTryingToInitializeWithSomethingOtherThanAStrategy()
131
    {
132
        new ConfigurationFactory(['this_is_not_a_strategy'], []);
0 ignored issues
show
Documentation introduced by
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...
133
    }
134
135
    private function givenExampleConfigurationFileWithContent($content): string
136
    {
137
        vfsStream::newFile('foo.xml')
138
            ->at(vfsStream::setup('dir'))
139
            ->withContent($content);
140
141
        return vfsStream::url('dir/foo.xml');
142
    }
143
144
    private function givenAMiddlewareThatReturns($expectedInputValue, $returnValue): \Closure
145
    {
146
        return function ($value) use ($expectedInputValue, $returnValue) {
147
            $this->assertSame($expectedInputValue, $value);
148
149
            return $returnValue;
150
        };
151
    }
152
153
    private function givenAValidStrategyThatReturns($result): Strategy
154
    {
155
        /** @var m\Mock $strategy */
156
        $strategy = m::mock(Strategy::class);
157
        $strategy->shouldReceive('supports')
158
            ->once()
159
            ->with(m::type(\SimpleXMLElement::class))
160
            ->andReturn(true);
161
        $strategy
162
            ->shouldReceive('convert')
163
            ->once()
164
            ->with(m::type(\SimpleXMLElement::class))->andReturn($result);
165
166
        return $strategy;
167
    }
168
}
169