Completed
Push — master ( 6c5077...d22a37 )
by Christian
01:33
created

src/Test/AbstractSitemapServiceTestCase.php (2 issues)

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
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\SitemapBundle\Test;
13
14
use Core23\SitemapBundle\Definition\SitemapDefinitionInterface;
15
use Core23\SitemapBundle\Model\UrlInterface;
16
use Core23\SitemapBundle\Sitemap\SitemapServiceInterface;
17
use DateTime;
18
use PHPUnit\Framework\AssertionFailedError;
19
use PHPUnit\Framework\TestCase;
20
use Symfony\Component\Routing\RouterInterface;
21
22
abstract class AbstractSitemapServiceTestCase extends TestCase
23
{
24
    protected $router;
25
26
    /**
27
     * @var SitemapServiceInterface
28
     */
29
    protected $service;
30
31
    /**
32
     * @var array[]
33
     */
34
    private $urls = [];
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function setUp(): void
40
    {
41
        $this->router = $this->createMock(RouterInterface::class);
42
43
        $this->service = $this->createService();
44
    }
45
46
    /**
47
     * @return SitemapServiceInterface
48
     */
49
    abstract protected function createService(): SitemapServiceInterface;
50
51
    /**
52
     * @param SitemapDefinitionInterface $sitemap
53
     */
54
    final protected function process(SitemapDefinitionInterface $sitemap): void
55
    {
56
        /** @var UrlInterface[] $urls */
57
        $result = $this->service->execute($sitemap);
58
59
        $count = \count($this->urls);
60
        $this->assertCount($count, $result);
0 ignored issues
show
$result is of type array<integer,object<Cor...le\Model\UrlInterface>>, but the function expects a object<Countable>|object...nit\Framework\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...
61
62
        if (0 === $count) {
63
            return;
64
        }
65
66
        /** @var UrlInterface $url */
67
        foreach ($result as $url) {
68
            $index = $this->getUrlIndex($url);
69
70
            if (-1 === $index) {
71
                throw new AssertionFailedError(sprintf("The url '%s' was not expected to be called.", $url->getLoc()));
72
            }
73
74
            $data = &$this->urls[$index];
75
76
            $this->assertPriority($url, $data);
77
            $this->assertChangeFreq($url, $data);
78
            $this->assertLastmod($data, $url);
79
            ++$data['count'];
80
        }
81
82
        foreach ($this->urls as $data) {
83
            if (0 === $data['count']) {
84
                throw new AssertionFailedError(sprintf("The url '%s' was expected to be called actually was not called", $data['location']));
85
            }
86
        }
87
    }
88
89
    /**
90
     * @param string        $location
91
     * @param int           $priority
92
     * @param string        $changeFreq
93
     * @param DateTime|null $lastMod
94
     */
95
    final protected function assertSitemap(string $location, int $priority, string $changeFreq, DateTime $lastMod = null): void
96
    {
97
        $this->urls[] = ['location' => $location, 'priority' => $priority, 'changefreq' => $changeFreq, 'lastmod' => $lastMod, 'count' => 0];
98
    }
99
100
    /**
101
     * @param int $count
102
     */
103
    final protected function assertSitemapCount(int $count): void
104
    {
105
        $this->assertCount($count, $this->urls);
0 ignored issues
show
$this->urls is of type array<integer,array>, but the function expects a object<Countable>|object...nit\Framework\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...
106
    }
107
108
    /**
109
     * @param UrlInterface $url
110
     *
111
     * @return int
112
     */
113
    private function getUrlIndex(UrlInterface $url): int
114
    {
115
        foreach ($this->urls as $index => $data) {
116
            if ($url->getLoc() === $data['location']) {
117
                return $index;
118
            }
119
        }
120
121
        return -1;
122
    }
123
124
    /**
125
     * @param array|null   $data
126
     * @param UrlInterface $url
127
     */
128
    private function assertLastmod(?array $data, UrlInterface $url): void
129
    {
130
        if (null === $data['lastmod'] && null === $url->getLastMod()) {
131
            return;
132
        }
133
134
        \assert($data['lastmod'] instanceof \DateTime);
135
136
        if ($url->getLastMod() <=> $data['lastmod']) {
137
            throw new AssertionFailedError(
138
                sprintf("The url '%s' was expected with a different lastmod.", $url->getLoc())
139
            );
140
        }
141
    }
142
143
    /**
144
     * @param UrlInterface $url
145
     * @param array|null   $data
146
     */
147
    private function assertPriority(UrlInterface $url, ?array $data): void
148
    {
149
        if ($url->getPriority() !== $data['priority']) {
150
            throw new AssertionFailedError(
151
                sprintf(
152
                    "The url '%s' was expected with %s priority. %s given.",
153
                    $url->getLoc(),
154
                    $data['priority'],
155
                    $url->getPriority()
156
                )
157
            );
158
        }
159
    }
160
161
    /**
162
     * @param UrlInterface $url
163
     * @param array|null   $data
164
     */
165
    private function assertChangeFreq(UrlInterface $url, ?array $data): void
166
    {
167
        if ($url->getChangeFreq() !== $data['changefreq']) {
168
            throw new AssertionFailedError(
169
                sprintf(
170
                    "The url '%s' was expected with %s changefreq. %s given.",
171
                    $url->getLoc(),
172
                    $data['changefreq'],
173
                    $url->getChangeFreq()
174
                )
175
            );
176
        }
177
    }
178
}
179