Completed
Push — master ( 02e032...3c693f )
by Christian
25s
created

AbstractSitemapServiceTestCase   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
createService() 0 1 ?
A assertSitemap() 0 4 1
B process() 0 33 6
A assertSitemapCount() 0 4 1
A getUrlIndex() 0 10 3
A assertLastmod() 0 14 4
A assertPriority() 0 13 2
A assertChangeFreq() 0 13 2
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
        $result = $this->service->execute($sitemap);
57
58
        $count = \count($this->urls);
59
        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...
60
61
        if (0 === $count) {
62
            return;
63
        }
64
65
        /** @var UrlInterface $url */
66
        foreach ($result as $url) {
67
            $index = $this->getUrlIndex($url);
68
69
            if (-1 === $index) {
70
                throw new AssertionFailedError(sprintf("The url '%s' was not expected to be called.", $url->getLoc()));
71
            }
72
73
            $data = &$this->urls[$index];
74
75
            $this->assertPriority($url, $data);
76
            $this->assertChangeFreq($url, $data);
77
            $this->assertLastmod($data, $url);
78
            ++$data['count'];
79
        }
80
81
        foreach ($this->urls as $data) {
82
            if (0 === $data['count']) {
83
                throw new AssertionFailedError(sprintf("The url '%s' was expected to be called actually was not called", $data['location']));
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param string        $location
90
     * @param int           $priority
91
     * @param string        $changeFreq
92
     * @param DateTime|null $lastMod
93
     */
94
    final protected function assertSitemap(string $location, int $priority, string $changeFreq, DateTime $lastMod = null): void
95
    {
96
        $this->urls[] = ['location' => $location, 'priority' => $priority, 'changefreq' => $changeFreq, 'lastmod' => $lastMod, 'count' => 0];
97
    }
98
99
    /**
100
     * @param int $count
101
     */
102
    final protected function assertSitemapCount(int $count): void
103
    {
104
        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...
105
    }
106
107
    /**
108
     * @param UrlInterface $url
109
     *
110
     * @return int
111
     */
112
    private function getUrlIndex(UrlInterface $url): int
113
    {
114
        foreach ($this->urls as $index => $data) {
115
            if ($url->getLoc() === $data['location']) {
116
                return $index;
117
            }
118
        }
119
120
        return -1;
121
    }
122
123
    /**
124
     * @param array|null   $data
125
     * @param UrlInterface $url
126
     */
127
    private function assertLastmod(?array $data, UrlInterface $url): void
128
    {
129
        if (null === $data['lastmod'] && null === $url->getLastMod()) {
130
            return;
131
        }
132
133
        \assert($data['lastmod'] instanceof \DateTime);
134
135
        if ($url->getLastMod() <=> $data['lastmod']) {
136
            throw new AssertionFailedError(
137
                sprintf("The url '%s' was expected with a different lastmod.", $url->getLoc())
138
            );
139
        }
140
    }
141
142
    /**
143
     * @param UrlInterface $url
144
     * @param array|null   $data
145
     */
146
    private function assertPriority(UrlInterface $url, ?array $data): void
147
    {
148
        if ($url->getPriority() !== $data['priority']) {
149
            throw new AssertionFailedError(
150
                sprintf(
151
                    "The url '%s' was expected with %s priority. %s given.",
152
                    $url->getLoc(),
153
                    $data['priority'],
154
                    $url->getPriority()
155
                )
156
            );
157
        }
158
    }
159
160
    /**
161
     * @param UrlInterface $url
162
     * @param array|null   $data
163
     */
164
    private function assertChangeFreq(UrlInterface $url, ?array $data): void
165
    {
166
        if ($url->getChangeFreq() !== $data['changefreq']) {
167
            throw new AssertionFailedError(
168
                sprintf(
169
                    "The url '%s' was expected with %s changefreq. %s given.",
170
                    $url->getLoc(),
171
                    $data['changefreq'],
172
                    $url->getChangeFreq()
173
                )
174
            );
175
        }
176
    }
177
}
178