Completed
Push — develop ( b41f33...6cf259 )
by Mike
07:20
created

Configuration/ConfigurationFactoryTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Application\Configuration;
14
15
use Mockery\Adapter\Phpunit\MockeryTestCase;
16
use Mockery as m;
17
use org\bovigo\vfs\vfsStream;
18
use phpDocumentor\Application\Configuration\Factory\Strategy;
19
use phpDocumentor\Application\Configuration\Factory\Version3;
20
use phpDocumentor\DomainModel\Uri;
21
22
/**
23
 * Test case for ConfigurationFactory
24
 *
25
 * @coversDefaultClass \phpDocumentor\Application\Configuration\ConfigurationFactory
26
 * @covers ::<private>
27
 */
28
final class ConfigurationFactoryTest extends MockeryTestCase
29
{
30
    /**
31
     * @covers ::fromUri
32
     * @expectedException \Exception
33
     * @expectedExceptionMessage No strategy found that matches the configuration xml
34
     */
35
    public function testItHaltsIfNoMatchingStrategyCanBeFound()
36
    {
37
        $root = vfsStream::setup('dir');
38
39
        vfsStream::newFile('foo.xml')->at($root)->withContent('<foo></foo>');
40
        $uri = new Uri(vfsStream::url('dir/foo.xml'));
41
42
        $configurationFactory = new ConfigurationFactory([]);
43
        $configurationFactory->fromUri($uri);
44
    }
45
46
    /**
47
     * @covers ::__construct
48
     * @covers ::fromUri
49
     * @covers ::<private>
50
     */
51
    public function testItRegistersStrategies()
52
    {
53
        $root = vfsStream::setup('dir');
54
55
        vfsStream::newFile('foo.xml')->at($root)->withContent('<foo></foo>');
56
        $uri = new Uri(vfsStream::url('dir/foo.xml'));
57
58
        /** @var m\Mock $strategy */
59
        $strategy = m::mock(Strategy::class);
60
        $strategy->shouldReceive('supports')->once()->with(m::type(\SimpleXMLElement::class))->andReturn(true);
61
        $strategy->shouldReceive('convert')->once()->with(m::type(\SimpleXMLElement::class));
62
63
        $configurationFactory = new ConfigurationFactory([$strategy]);
0 ignored issues
show
array($strategy) is of type array<integer,object<Moc...bject<Mockery\\Mock>"}>, but the function expects a array<integer,object<php...tion\Factory\Strategy>>.

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...
64
        $configurationFactory->fromUri($uri);
65
    }
66
67
    /**
68
     * @covers ::__construct
69
     * @covers ::addMiddleware
70
     * @covers ::fromUri
71
     */
72
    public function testThatMiddlewaresAreAddedAndApplied()
73
    {
74
        $inputValue = ['test'];
75
        $afterMiddleware1Value = ['test', 'test2'];
76
        $afterMiddleware2Value = ['test', 'test2', 'test3'];
77
78
        $root = vfsStream::setup('dir');
79
        vfsStream::newFile('foo.xml')->at($root)->withContent('<foo></foo>');
80
81
        $middleWare1 = function ($value) use ($inputValue, $afterMiddleware1Value) {
82
            $this->assertSame($inputValue, $value);
83
84
            return $afterMiddleware1Value;
85
        };
86
87
        $middleWare2 = function ($value) use ($afterMiddleware1Value, $afterMiddleware2Value) {
88
            $this->assertSame($afterMiddleware1Value, $value);
89
90
            return $afterMiddleware2Value;
91
        };
92
93
        /** @var m\Mock $strategy */
94
        $strategy = m::mock(Strategy::class);
95
        $strategy->shouldReceive('supports')->once()->with(m::type(\SimpleXMLElement::class))->andReturn(true);
96
        $strategy->shouldReceive('convert')->once()->with(m::type(\SimpleXMLElement::class))->andReturn($inputValue);
97
98
        // Setup a prepopulated factory with path and cachedContents
99
        $uri = new Uri(vfsStream::url('dir/foo.xml'));
100
        $factory = new ConfigurationFactory([$strategy]);
0 ignored issues
show
array($strategy) is of type array<integer,object<Moc...bject<Mockery\\Mock>"}>, but the function expects a array<integer,object<php...tion\Factory\Strategy>>.

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...
101
        $factory->addMiddleware($middleWare1);
102
        $factory->addMiddleware($middleWare2);
103
104
        $data = $factory->fromUri($uri);
105
106
        $this->assertEquals($afterMiddleware2Value, $data->getArrayCopy());
107
    }
108
}
109