AbstractSitemapServiceTestCase   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 124
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
createService() 0 1 ?
B process() 0 33 6
A assertSitemap() 0 4 1
A assertSitemapCount() 0 4 1
A getUrlIndex() 0 10 3
A assertPriority() 0 13 2
A assertChangeFreq() 0 13 2
A assertLastmod() 0 14 6
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\MockObject\MockObject;
20
use PHPUnit\Framework\TestCase;
21
use Symfony\Component\Routing\RouterInterface;
22
23
abstract class AbstractSitemapServiceTestCase extends TestCase
24
{
25
    /**
26
     * @var MockObject&RouterInterface
27
     */
28
    protected $router;
29
30
    /**
31
     * @var SitemapServiceInterface
32
     */
33
    protected $service;
34
35
    /**
36
     * @var array[]
37
     */
38
    private $urls = [];
39
40
    protected function setUp(): void
41
    {
42
        $this->router = $this->createMock(RouterInterface::class);
43
44
        $this->service = $this->createService();
45
    }
46
47
    abstract protected function createService(): SitemapServiceInterface;
48
49
    final protected function process(SitemapDefinitionInterface $sitemap): void
50
    {
51
        $result = $this->service->execute($sitemap);
52
53
        $count = \count($this->urls);
54
        static::assertCount($count, $result);
0 ignored issues
show
Documentation introduced by core23
$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...
55
56
        if (0 === $count) {
57
            return;
58
        }
59
60
        /** @var UrlInterface $url */
61
        foreach ($result as $url) {
62
            $index = $this->getUrlIndex($url);
63
64
            if (-1 === $index) {
65
                throw new AssertionFailedError(sprintf("The url '%s' was not expected to be called.", $url->getLoc()));
66
            }
67
68
            $data = &$this->urls[$index];
69
70
            $this->assertPriority($url, $data);
71
            $this->assertChangeFreq($url, $data);
72
            $this->assertLastmod($data, $url);
73
            ++$data['count'];
74
        }
75
76
        foreach ($this->urls as $data) {
77
            if (0 === $data['count']) {
78
                throw new AssertionFailedError(sprintf("The url '%s' was expected to be called actually was not called", $data['location']));
79
            }
80
        }
81
    }
82
83
    final protected function assertSitemap(string $location, int $priority, string $changeFreq, DateTime $lastMod = null): void
84
    {
85
        $this->urls[] = ['location' => $location, 'priority' => $priority, 'changefreq' => $changeFreq, 'lastmod' => $lastMod, 'count' => 0];
86
    }
87
88
    final protected function assertSitemapCount(int $count): void
89
    {
90
        static::assertCount($count, $this->urls);
0 ignored issues
show
Documentation introduced by core23
$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...
91
    }
92
93
    private function getUrlIndex(UrlInterface $url): int
94
    {
95
        foreach ($this->urls as $index => $data) {
96
            if ($url->getLoc() === $data['location']) {
97
                return $index;
98
            }
99
        }
100
101
        return -1;
102
    }
103
104
    private function assertLastmod(?array $data, UrlInterface $url): void
105
    {
106
        if (null === $data['lastmod'] && null === $url->getLastMod()) {
107
            return;
108
        }
109
110
        \assert($data['lastmod'] instanceof \DateTime);
111
112
        if (null === $url->getLastMod() || $url->getLastMod() > $data['lastmod'] || $url->getLastMod() < $data['lastmod']) {
113
            throw new AssertionFailedError(
114
                sprintf("The url '%s' was expected with a different lastmod.", $url->getLoc())
115
            );
116
        }
117
    }
118
119
    private function assertPriority(UrlInterface $url, ?array $data): void
120
    {
121
        if ($url->getPriority() !== $data['priority']) {
122
            throw new AssertionFailedError(
123
                sprintf(
124
                    "The url '%s' was expected with %s priority. %s given.",
125
                    $url->getLoc(),
126
                    $data['priority'],
127
                    $url->getPriority()
128
                )
129
            );
130
        }
131
    }
132
133
    private function assertChangeFreq(UrlInterface $url, ?array $data): void
134
    {
135
        if ($url->getChangeFreq() !== $data['changefreq']) {
136
            throw new AssertionFailedError(
137
                sprintf(
138
                    "The url '%s' was expected with %s changefreq. %s given.",
139
                    $url->getLoc(),
140
                    $data['changefreq'],
141
                    $url->getChangeFreq()
142
                )
143
            );
144
        }
145
    }
146
}
147