Completed
Push — master ( 6c5077...d22a37 )
by Christian
01:33
created

testToXMLWithInvalidDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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