Completed
Push — develop ( ffe009...9d0b73 )
by Mike
09:33
created

givenAValidStrategyThatReturns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 15
rs 9.7666
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\DomainModel\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 testItLoadsASpecificConfigurationFile()
36
    {
37
        $configurationFactory = new ConfigurationFactory([new Version2()], []);
38
39
        $content = '<phpdocumentor><title>My title</title></phpdocumentor>';
40
        $configuration = $configurationFactory->fromUri(
41
            new Uri($this->givenExampleConfigurationFileWithContent($content))
42
        );
43
44
        $this->assertSame('My title', $configuration['phpdocumentor']['title']);
45
    }
46
47
    /**
48
     * @covers ::fromDefaultLocations()
49
     */
50
    public function testThatTheDefaultConfigurationFilesAreLoaded()
51
    {
52
        $file = $this->givenExampleConfigurationFileWithContent(
53
            '<phpdocumentor><title>My title</title></phpdocumentor>'
54
        );
55
        $configurationFactory = new ConfigurationFactory([new Version2()], [$file]);
56
57
        $configuration = $configurationFactory->fromDefaultLocations();
58
59
        $this->assertSame('My title', $configuration['phpdocumentor']['title']);
60
    }
61
62
    /**
63
     * @covers ::fromDefaultLocations()
64
     */
65
    public function testWhenNoneOfTheDefaultsExistThatTheBakedConfigIsUsed()
66
    {
67
        $configurationFactory = new ConfigurationFactory([new Version2()], ['does_not_exist.xml']);
68
69
        $configuration = $configurationFactory->fromDefaultLocations();
70
71
        $this->assertEquals(Version3::buildDefault(), $configuration->getArrayCopy());
72
    }
73
74
    /**
75
     * @covers ::addMiddleware
76
     */
77
    public function testThatMiddlewaresCanBeAddedAndAreThenApplied()
78
    {
79
        $inputValue = ['test'];
80
        $afterMiddleware1Value = ['test', 'test2'];
81
        $afterMiddleware2Value = ['test', 'test2', 'test3'];
82
83
        $middleWare1 = $this->givenAMiddlewareThatReturns($inputValue, $afterMiddleware1Value);
84
        $middleWare2 = $this->givenAMiddlewareThatReturns($afterMiddleware1Value, $afterMiddleware2Value);
85
86
        $factory = new ConfigurationFactory([$this->givenAValidStrategyThatReturns($inputValue)], []);
87
        $factory->addMiddleware($middleWare1);
88
        $factory->addMiddleware($middleWare2);
89
90
        $data = $factory->fromUri(new Uri($this->givenExampleConfigurationFileWithContent('<foo></foo>')));
91
92
        $this->assertSame($afterMiddleware2Value, $data->getArrayCopy());
93
    }
94
95
    /**
96
     * @covers ::fromUri
97
     * @expectedException \Exception
98
     * @expectedExceptionMessage No supported configuration files were found
99
     */
100
    public function testItHaltsIfNoMatchingStrategyCanBeFound()
101
    {
102
        $strategies = []; // No strategy means nothing could match ;)
103
        $configurationFactory = new ConfigurationFactory($strategies, []);
104
105
        $configurationFactory->fromUri(
106
            new Uri($this->givenExampleConfigurationFileWithContent('<foo></foo>'))
107
        );
108
    }
109
110
    /**
111
     * @covers ::__construct
112
     * @expectedException \TypeError
113
     */
114
    public function testItErrorsWhenTryingToInitializeWithSomethingOtherThanAStrategy()
115
    {
116
        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...
117
    }
118
119
    private function givenExampleConfigurationFileWithContent($content): string
120
    {
121
        vfsStream::newFile('foo.xml')
122
            ->at(vfsStream::setup('dir'))
123
            ->withContent($content);
124
125
        return vfsStream::url('dir/foo.xml');
126
    }
127
128
    private function givenAMiddlewareThatReturns($expectedInputValue, $returnValue): \Closure
129
    {
130
        return function ($value) use ($expectedInputValue, $returnValue) {
131
            $this->assertSame($expectedInputValue, $value);
132
133
            return $returnValue;
134
        };
135
    }
136
137
    private function givenAValidStrategyThatReturns($result): Strategy
138
    {
139
        /** @var m\Mock $strategy */
140
        $strategy = m::mock(Strategy::class);
141
        $strategy->shouldReceive('supports')
142
            ->once()
143
            ->with(m::type(\SimpleXMLElement::class))
144
            ->andReturn(true);
145
        $strategy
146
            ->shouldReceive('convert')
147
            ->once()
148
            ->with(m::type(\SimpleXMLElement::class))->andReturn($result);
149
150
        return $strategy;
151
    }
152
}
153