Completed
Push — master ( 705157...0b8fba )
by
unknown
12s
created

tests/Sitemap/SitemapServiceManagerTest.php (1 issue)

Severity

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
/*
4
 * (c) Christian Gripp <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Core23\SitemapBundle\Tests\Sitemap;
11
12
use Core23\SitemapBundle\Definition\SitemapDefinition;
13
use Core23\SitemapBundle\Definition\SitemapDefinitionInterface;
14
use Core23\SitemapBundle\Exception\SitemapNotFoundException;
15
use Core23\SitemapBundle\Sitemap\SitemapServiceInterface;
16
use Core23\SitemapBundle\Sitemap\SitemapServiceManager;
17
use Core23\SitemapBundle\Sitemap\SitemapServiceManagerInterface;
18
use Core23\SitemapBundle\Tests\Fixtures\SitemapService;
19
use InvalidArgumentException;
20
use PHPUnit\Framework\TestCase;
21
use stdClass;
22
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
23
24
final class SitemapServiceManagerTest extends TestCase
25
{
26
    public function testItIsInstantiable(): void
27
    {
28
        $manager = new SitemapServiceManager();
29
30
        static::assertInstanceOf(SitemapServiceManagerInterface::class, $manager);
31
    }
32
33
    public function testCreationWithInvalidServices(): void
34
    {
35
        $this->expectException(InvalidArgumentException::class);
36
        $this->expectExceptionMessage('The "stdClass" service is not a valid SitemapServiceInterface');
37
38
        new SitemapServiceManager([
0 ignored issues
show
array('invalid' => new \stdClass()) is of type array<string,object<stdC...d":"object<stdClass>"}>, but the function expects a array<integer,object<Cor...temapServiceInterface>>.

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...
39
            'invalid' => new stdClass(),
40
        ]);
41
    }
42
43
    public function testGet(): void
44
    {
45
        $definition = new SitemapDefinition('my-type', []);
46
47
        $service = new SitemapService();
48
49
        $manager = new SitemapServiceManager([
50
            'my-type' => $service,
51
        ]);
52
        $result =  $manager->get($definition);
53
54
        static::assertInstanceOf(SitemapServiceInterface::class, $result);
55
        static::assertSame([
56
            'custom'           => 'foo',
57
            'use_cache'        => true,
58
            'extra_cache_keys' => [],
59
            'ttl'              => 86400,
60
        ], $definition->getSettings());
61
    }
62
63
    public function testGetWithOverride(): void
64
    {
65
        $definition = new SitemapDefinition('my-type', [
66
            'custom'           => 'bar',
67
            'use_cache'        => false,
68
            'extra_cache_keys' => ['my-key'],
69
            'ttl'              => 0,
70
        ]);
71
72
        $service = new SitemapService();
73
74
        $manager = new SitemapServiceManager([
75
            'my-type' => $service,
76
        ]);
77
        $result =  $manager->get($definition);
78
79
        static::assertInstanceOf(SitemapServiceInterface::class, $result);
80
        static::assertSame([
81
            'use_cache'        => false,
82
            'extra_cache_keys' => ['my-key'],
83
            'ttl'              => 0,
84
            'custom'           => 'bar',
85
        ], $definition->getSettings());
86
    }
87
88
    public function testGetWithInvalidOverride(): void
89
    {
90
        $this->expectException(UndefinedOptionsException::class);
91
        $this->expectExceptionMessage('The option "invalid" does not exist. Defined options are: "custom", "extra_cache_keys", "ttl", "use_cache"');
92
93
        $definition = new SitemapDefinition('my-type', [
94
            'invalid' => 'value',
95
        ]);
96
97
        $service = new SitemapService();
98
99
        $manager = new SitemapServiceManager([
100
            'my-type' => $service,
101
        ]);
102
        $manager->get($definition);
103
    }
104
105
    public function testGetWithInvalidDefinition(): void
106
    {
107
        $this->expectException(SitemapNotFoundException::class);
108
        $this->expectExceptionMessage('The sitemap service "my-type" does not exist');
109
110
        $definition = $this->prophesize(SitemapDefinitionInterface::class);
111
        $definition->getType()
112
            ->willReturn('my-type')
113
        ;
114
        $definition->getSettings()
115
            ->willReturn([])
116
        ;
117
118
        $manager = new SitemapServiceManager();
119
        $manager->get($definition->reveal());
120
    }
121
122
    public function testAddSitemap(): void
123
    {
124
        $service = $this->prophesize(SitemapServiceInterface::class);
125
126
        $manager = new SitemapServiceManager();
127
        $manager->addSitemap('my-type', $service->reveal());
128
129
        static::assertTrue(true);
130
    }
131
}
132