|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Christian Gripp <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Core23\SitemapBundle\Tests\Generator; |
|
11
|
|
|
|
|
12
|
|
|
use Core23\SitemapBundle\Definition\DefintionManagerInterface; |
|
13
|
|
|
use Core23\SitemapBundle\Definition\SitemapDefinitionInterface; |
|
14
|
|
|
use Core23\SitemapBundle\Generator\SitemapGenerator; |
|
15
|
|
|
use Core23\SitemapBundle\Model\Url; |
|
16
|
|
|
use Core23\SitemapBundle\Model\UrlInterface; |
|
17
|
|
|
use Core23\SitemapBundle\Sitemap\SitemapServiceInterface; |
|
18
|
|
|
use Core23\SitemapBundle\Sitemap\SitemapServiceManagerInterface; |
|
19
|
|
|
use Core23\SitemapBundle\Tests\Fixtures\InvalidArgumentException; |
|
20
|
|
|
use DateTime; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
use Prophecy\Argument; |
|
23
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
24
|
|
|
use RuntimeException; |
|
25
|
|
|
|
|
26
|
|
|
final class SitemapGeneratorTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
private $sitemapServiceManager; |
|
29
|
|
|
|
|
30
|
|
|
private $defintionManager; |
|
31
|
|
|
|
|
32
|
|
|
public static function setUpBeforeClass(): void |
|
33
|
|
|
{ |
|
34
|
|
|
date_default_timezone_set('UTC'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function setUp(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->sitemapServiceManager = $this->prophesize(SitemapServiceManagerInterface::class); |
|
40
|
|
|
$this->defintionManager = $this->prophesize(DefintionManagerInterface::class); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testToXMLWithInvalidDefinition(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$expected = '<?xml version="1.0" encoding="UTF-8"?>'; |
|
46
|
|
|
$expected .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '; |
|
47
|
|
|
$expected .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; |
|
48
|
|
|
$expected .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; |
|
49
|
|
|
$expected .= '</urlset>'; |
|
50
|
|
|
|
|
51
|
|
|
$definition = $this->prophesize(SitemapDefinitionInterface::class); |
|
52
|
|
|
|
|
53
|
|
|
$this->sitemapServiceManager->get($definition) |
|
54
|
|
|
->willReturn(null) |
|
55
|
|
|
; |
|
56
|
|
|
|
|
57
|
|
|
$this->defintionManager->getAll() |
|
58
|
|
|
->willReturn([ |
|
59
|
|
|
'dummy' => $definition->reveal(), |
|
60
|
|
|
]) |
|
61
|
|
|
; |
|
62
|
|
|
|
|
63
|
|
|
$generator = new SitemapGenerator( |
|
64
|
|
|
$this->sitemapServiceManager->reveal(), |
|
65
|
|
|
$this->defintionManager->reveal() |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
static::assertSame($expected, $generator->toXML()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testToXMLWithNoEntries(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$expected = '<?xml version="1.0" encoding="UTF-8"?>'; |
|
74
|
|
|
$expected .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '; |
|
75
|
|
|
$expected .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; |
|
76
|
|
|
$expected .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; |
|
77
|
|
|
$expected .= '</urlset>'; |
|
78
|
|
|
|
|
79
|
|
|
$this->defintionManager->getAll() |
|
80
|
|
|
->willReturn([]) |
|
81
|
|
|
; |
|
82
|
|
|
|
|
83
|
|
|
$generator = new SitemapGenerator( |
|
84
|
|
|
$this->sitemapServiceManager->reveal(), |
|
85
|
|
|
$this->defintionManager->reveal() |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
static::assertSame($expected, $generator->toXML()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testToXML(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$expected = '<?xml version="1.0" encoding="UTF-8"?>'; |
|
94
|
|
|
$expected .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '; |
|
95
|
|
|
$expected .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; |
|
96
|
|
|
$expected .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; |
|
97
|
|
|
$expected .= '<url><loc>http://core23.de</loc><lastmod>2017-12-23T00:00:00+00:00</lastmod><changefreq>daily</changefreq><priority>80</priority></url>'; |
|
98
|
|
|
$expected .= '</urlset>'; |
|
99
|
|
|
|
|
100
|
|
|
$definition = $this->prophesize(SitemapDefinitionInterface::class); |
|
101
|
|
|
|
|
102
|
|
|
$url = $this->prophesize(UrlInterface::class); |
|
103
|
|
|
$url->getChangeFreq() |
|
104
|
|
|
->willReturn(Url::FREQUENCE_DAILY) |
|
105
|
|
|
; |
|
106
|
|
|
$url->getLastMod() |
|
107
|
|
|
->willReturn(new DateTime('2017-12-23 00:00:00')) |
|
108
|
|
|
; |
|
109
|
|
|
$url->getLoc() |
|
110
|
|
|
->willReturn('http://core23.de') |
|
111
|
|
|
; |
|
112
|
|
|
$url->getPriority() |
|
113
|
|
|
->willReturn(80) |
|
114
|
|
|
; |
|
115
|
|
|
|
|
116
|
|
|
$sitemap = $this->prophesize(SitemapServiceInterface::class); |
|
117
|
|
|
$sitemap->execute($definition) |
|
118
|
|
|
->willReturn([ |
|
119
|
|
|
$url->reveal(), |
|
120
|
|
|
]) |
|
121
|
|
|
; |
|
122
|
|
|
|
|
123
|
|
|
$this->sitemapServiceManager->get($definition) |
|
124
|
|
|
->willReturn($sitemap) |
|
125
|
|
|
; |
|
126
|
|
|
|
|
127
|
|
|
$this->defintionManager->getAll() |
|
128
|
|
|
->willReturn([ |
|
129
|
|
|
'dummy' => $definition->reveal(), |
|
130
|
|
|
]) |
|
131
|
|
|
; |
|
132
|
|
|
|
|
133
|
|
|
$generator = new SitemapGenerator( |
|
134
|
|
|
$this->sitemapServiceManager->reveal(), |
|
135
|
|
|
$this->defintionManager->reveal() |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
static::assertSame($expected, $generator->toXML()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testToXMLWithExistingCache(): void |
|
142
|
|
|
{ |
|
143
|
|
|
$xmlEntry = '<url><loc>http://core23.de</loc><lastmod>2017-12-23T00:00:00+00:00</lastmod><changefreq>daily</changefreq><priority>80</priority></url>'; |
|
144
|
|
|
|
|
145
|
|
|
$expected = '<?xml version="1.0" encoding="UTF-8"?>'; |
|
146
|
|
|
$expected .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '; |
|
147
|
|
|
$expected .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; |
|
148
|
|
|
$expected .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; |
|
149
|
|
|
$expected .= $xmlEntry; |
|
150
|
|
|
$expected .= '</urlset>'; |
|
151
|
|
|
|
|
152
|
|
|
$definition = $this->prophesize(SitemapDefinitionInterface::class); |
|
153
|
|
|
$definition->getTtl() |
|
154
|
|
|
->willReturn(90) |
|
155
|
|
|
; |
|
156
|
|
|
|
|
157
|
|
|
$url = $this->prophesize(UrlInterface::class); |
|
158
|
|
|
$url->getChangeFreq() |
|
159
|
|
|
->willReturn(Url::FREQUENCE_DAILY) |
|
160
|
|
|
; |
|
161
|
|
|
$url->getLastMod() |
|
162
|
|
|
->willReturn(new DateTime('2017-12-23 00:00:00')) |
|
163
|
|
|
; |
|
164
|
|
|
$url->getLoc() |
|
165
|
|
|
->willReturn('http://core23.de') |
|
166
|
|
|
; |
|
167
|
|
|
$url->getPriority() |
|
168
|
|
|
->willReturn(80) |
|
169
|
|
|
; |
|
170
|
|
|
|
|
171
|
|
|
$sitemap = $this->prophesize(SitemapServiceInterface::class); |
|
172
|
|
|
|
|
173
|
|
|
$this->sitemapServiceManager->get($definition) |
|
174
|
|
|
->willReturn($sitemap) |
|
175
|
|
|
; |
|
176
|
|
|
|
|
177
|
|
|
$this->defintionManager->getAll() |
|
178
|
|
|
->willReturn([ |
|
179
|
|
|
'dummy' => $definition->reveal(), |
|
180
|
|
|
]) |
|
181
|
|
|
; |
|
182
|
|
|
|
|
183
|
|
|
$cache = $this->prophesize(CacheInterface::class); |
|
184
|
|
|
$cache->has(Argument::containingString('Sitemap_')) |
|
185
|
|
|
->willReturn(true) |
|
186
|
|
|
; |
|
187
|
|
|
$cache->get(Argument::containingString('Sitemap_')) |
|
188
|
|
|
->willReturn($xmlEntry) |
|
189
|
|
|
; |
|
190
|
|
|
|
|
191
|
|
|
$generator = new SitemapGenerator( |
|
192
|
|
|
$this->sitemapServiceManager->reveal(), |
|
193
|
|
|
$this->defintionManager->reveal(), |
|
194
|
|
|
$cache->reveal() |
|
195
|
|
|
); |
|
196
|
|
|
|
|
197
|
|
|
static::assertSame($expected, $generator->toXML()); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function testToXMLWithExpiredCache(): void |
|
201
|
|
|
{ |
|
202
|
|
|
$xmlEntry = '<url><loc>http://core23.de</loc><lastmod>2017-12-23T00:00:00+00:00</lastmod><changefreq>daily</changefreq><priority>80</priority></url>'; |
|
203
|
|
|
|
|
204
|
|
|
$expected = '<?xml version="1.0" encoding="UTF-8"?>'; |
|
205
|
|
|
$expected .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '; |
|
206
|
|
|
$expected .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; |
|
207
|
|
|
$expected .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; |
|
208
|
|
|
$expected .= $xmlEntry; |
|
209
|
|
|
$expected .= '</urlset>'; |
|
210
|
|
|
|
|
211
|
|
|
$definition = $this->prophesize(SitemapDefinitionInterface::class); |
|
212
|
|
|
$definition->getTtl() |
|
213
|
|
|
->willReturn(90) |
|
214
|
|
|
; |
|
215
|
|
|
|
|
216
|
|
|
$url = $this->prophesize(UrlInterface::class); |
|
217
|
|
|
$url->getChangeFreq() |
|
218
|
|
|
->willReturn(Url::FREQUENCE_DAILY) |
|
219
|
|
|
; |
|
220
|
|
|
$url->getLastMod() |
|
221
|
|
|
->willReturn(new DateTime('2017-12-23')) |
|
222
|
|
|
; |
|
223
|
|
|
$url->getLoc() |
|
224
|
|
|
->willReturn('http://core23.de') |
|
225
|
|
|
; |
|
226
|
|
|
$url->getPriority() |
|
227
|
|
|
->willReturn(80) |
|
228
|
|
|
; |
|
229
|
|
|
|
|
230
|
|
|
$sitemap = $this->prophesize(SitemapServiceInterface::class); |
|
231
|
|
|
$sitemap->execute($definition) |
|
232
|
|
|
->willReturn([ |
|
233
|
|
|
$url->reveal(), |
|
234
|
|
|
]) |
|
235
|
|
|
; |
|
236
|
|
|
|
|
237
|
|
|
$this->sitemapServiceManager->get($definition) |
|
238
|
|
|
->willReturn($sitemap) |
|
239
|
|
|
; |
|
240
|
|
|
|
|
241
|
|
|
$this->defintionManager->getAll() |
|
242
|
|
|
->willReturn([ |
|
243
|
|
|
'dummy' => $definition->reveal(), |
|
244
|
|
|
]) |
|
245
|
|
|
; |
|
246
|
|
|
|
|
247
|
|
|
$cache = $this->prophesize(CacheInterface::class); |
|
248
|
|
|
$cache->has(Argument::containingString('Sitemap_')) |
|
249
|
|
|
->willReturn(false) |
|
250
|
|
|
; |
|
251
|
|
|
$cache->set(Argument::containingString('Sitemap_'), $xmlEntry, 90) |
|
252
|
|
|
->shouldBeCalled() |
|
253
|
|
|
; |
|
254
|
|
|
|
|
255
|
|
|
$generator = new SitemapGenerator( |
|
256
|
|
|
$this->sitemapServiceManager->reveal(), |
|
257
|
|
|
$this->defintionManager->reveal(), |
|
258
|
|
|
$cache->reveal() |
|
259
|
|
|
); |
|
260
|
|
|
|
|
261
|
|
|
static::assertSame($expected, $generator->toXML()); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function testToXMLWithCacheException(): void |
|
265
|
|
|
{ |
|
266
|
|
|
$this->expectException(RuntimeException::class); |
|
267
|
|
|
$this->expectExceptionMessage('Error accessing cache'); |
|
268
|
|
|
|
|
269
|
|
|
$definition = $this->prophesize(SitemapDefinitionInterface::class); |
|
270
|
|
|
|
|
271
|
|
|
$this->sitemapServiceManager->get($definition) |
|
272
|
|
|
->willReturn(null) |
|
273
|
|
|
; |
|
274
|
|
|
|
|
275
|
|
|
$this->defintionManager->getAll() |
|
276
|
|
|
->willReturn([ |
|
277
|
|
|
'dummy' => $definition->reveal(), |
|
278
|
|
|
]) |
|
279
|
|
|
; |
|
280
|
|
|
|
|
281
|
|
|
$cache = $this->prophesize(CacheInterface::class); |
|
282
|
|
|
$cache->has(Argument::containingString('Sitemap_')) |
|
283
|
|
|
->willThrow(InvalidArgumentException::class) |
|
284
|
|
|
; |
|
285
|
|
|
|
|
286
|
|
|
$generator = new SitemapGenerator( |
|
287
|
|
|
$this->sitemapServiceManager->reveal(), |
|
288
|
|
|
$this->defintionManager->reveal(), |
|
289
|
|
|
$cache->reveal() |
|
290
|
|
|
); |
|
291
|
|
|
|
|
292
|
|
|
$generator->toXML(); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|