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