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); |
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); |
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
|
|
|
|