Skip to content

Commit

Permalink
Fix PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evertharmeling committed Dec 13, 2023
1 parent 08806d3 commit eeae3dd
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 179 deletions.
15 changes: 15 additions & 0 deletions tests/Integration/config/7.0/framework.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
framework:
test: true
secret: '%env(APP_SECRET)%'
http_method_override: false
handle_all_throwables: true
session:
handler_id: null
cookie_secure: auto
cookie_samesite: lax
storage_factory_id: session.storage.factory.mock_file
php_errors:
log: true
router:
utf8: true
cache: null
6 changes: 6 additions & 0 deletions tests/Integration/config/7.0/messenger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
framework:
messenger:
transports:
async: 'in-memory://'
routing:
'Presta\SitemapBundle\Messenger\DumpSitemapMessage': async
9 changes: 9 additions & 0 deletions tests/Integration/config/7.0/presta_sitemap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
presta_sitemap:
default_section: static
dump_directory: "%kernel.project_dir%/public"
items_by_set: 10
alternate:
enabled: true
default_locale: en
locales: [en, fr]
i18n: symfony
3 changes: 3 additions & 0 deletions tests/Integration/config/7.0/routes/annotations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
controllers:
resource: ../../../src/Controller/
type: attribute
2 changes: 2 additions & 0 deletions tests/Integration/config/7.0/routes/presta_sitemap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
presta_sitemap:
resource: "@PrestaSitemapBundle/config/routing.yml"
7 changes: 7 additions & 0 deletions tests/Integration/config/7.0/routes/translated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
about:
path:
en: /about
fr: /a-propos
defaults: { _controller: \Presta\SitemapBundle\Tests\Integration\Controller\StaticController::about }
options:
sitemap: true
14 changes: 14 additions & 0 deletions tests/Integration/config/7.0/routes/xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>

<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="xml" path="/company">
<default key="_controller">Presta\SitemapBundle\Tests\Integration\Controller\StaticController::company</default>
<option key="sitemap">
{"priority":"0.7", "changefreq":"weekly", "section":"static"}
</option>
</route>

</routes>
6 changes: 6 additions & 0 deletions tests/Integration/config/7.0/routes/yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
yaml:
path: /contact
defaults: { _controller: \Presta\SitemapBundle\Tests\Integration\Controller\StaticController::contact }
options:
sitemap:
section: static
4 changes: 2 additions & 2 deletions tests/Integration/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function registerBundles(): iterable
yield new \Presta\SitemapBundle\PrestaSitemapBundle();
}

public function boot()
public function boot(): void
{
/* force "var" dir to be removed the first time this kernel boot */
static $cleanVarDirectory = true;
Expand Down Expand Up @@ -119,7 +119,7 @@ public function registerBundles(): iterable
yield new \Presta\SitemapBundle\PrestaSitemapBundle();
}

public function boot()
public function boot(): void
{
/* force "var" dir to be removed the first time this kernel boot */
static $cleanVarDirectory = true;
Expand Down
192 changes: 192 additions & 0 deletions tests/Integration/tests/BaseSitemapTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

/*
* This file is part of the PrestaSitemapBundle package.
*
* (c) PrestaConcept <https://prestaconcept.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presta\SitemapBundle\Tests\Integration\Tests;

use PHPUnit\Framework\Assert;
use Presta\SitemapBundle\Tests\Integration\Kernel;
use SimpleXMLElement;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

abstract class BaseSitemapTestCase extends WebTestCase
{
protected static function assertIndex(string $xml, bool $gzip = false): void
{
$index = simplexml_load_string($xml);
$index->registerXPathNamespace('sm', 'http://www.sitemaps.org/schemas/sitemap/0.9');

self::assertIndexContainsSectionLink($index, 'static', $gzip);
self::assertIndexContainsSectionLink($index, 'blog', $gzip);
self::assertIndexContainsSectionLink($index, 'archives', $gzip);
self::assertIndexContainsSectionLink($index, 'archives_0', $gzip);
}

protected static function assertStaticSection(string $xml): void
{
$static = simplexml_load_string($xml);
$static->registerXPathNamespace('sm', 'http://www.sitemaps.org/schemas/sitemap/0.9');

if (Kernel::VERSION_ID >= 50100) {
self::assertSectionContainsCountUrls($static, 'static', 4);
} else {
self::assertSectionContainsCountUrls($static, 'static', 3);
}

$annotations = self::assertSectionContainsPath($static, 'static', '/');
self::assertUrlConcrete($annotations, 'static', 0.5, 'daily');
$xml = self::assertSectionContainsPath($static, 'static', '/company');
self::assertUrlConcrete($xml, 'static', 0.7, 'weekly');
$yaml = self::assertSectionContainsPath($static, 'static', '/contact');
self::assertUrlConcrete($yaml, 'static', 0.5, 'daily');

if (Kernel::VERSION_ID >= 50100) {
$translated = self::assertSectionContainsPath($static, 'static', '/about');
self::assertUrlConcrete($translated, 'static', 0.5, 'daily');
}
}

protected static function assertBlogSection(string $xml): void
{
$blog = simplexml_load_string($xml);
$blog->registerXPathNamespace('sm', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$blog->registerXPathNamespace('image', 'http://www.google.com/schemas/sitemap-image/1.1');
$blog->registerXPathNamespace('video', 'http://www.google.com/schemas/sitemap-video/1.1');

self::assertSectionContainsCountUrls($blog, 'blog', 5);
$list = self::assertSectionContainsPath($blog, 'blog', '/blog');
self::assertUrlConcrete($list, 'blog', 0.5, 'daily');
$postWithoutMedia = self::assertSectionContainsPath($blog, 'blog', '/blog/post-without-media');
self::assertUrlConcrete($postWithoutMedia, 'blog', 0.5, 'daily');
$postWithOneImage = self::assertSectionContainsPath($blog, 'blog', '/blog/post-with-one-image');
self::assertUrlHasImage($postWithOneImage, 'blog', 'http://lorempixel.com/400/200/technics/1');
$postWithAVideo = self::assertSectionContainsPath($blog, 'blog', '/blog/post-with-a-video');
self::assertUrlHasVideo($postWithAVideo, 'blog', 'https://www.youtube.com/watch?v=j6IKRxH8PTg');
$postWithMultimedia = self::assertSectionContainsPath($blog, 'blog', '/blog/post-with-multimedia');
self::assertUrlHasImage($postWithMultimedia, 'blog', 'http://lorempixel.com/400/200/technics/2');
self::assertUrlHasImage($postWithMultimedia, 'blog', 'http://lorempixel.com/400/200/technics/3');
self::assertUrlHasVideo($postWithMultimedia, 'blog', 'https://www.youtube.com/watch?v=JugaMuswrmk');
}

protected static function assertArchivesSection(string $xml): void
{
$archives = simplexml_load_string($xml);
$archives->registerXPathNamespace('sm', 'http://www.sitemaps.org/schemas/sitemap/0.9');

self::assertSectionContainsCountUrls($archives, 'archive', 10);
Assert::assertCount(
10,
$urls = $archives->xpath('//sm:urlset/sm:url[ sm:loc[ contains(text(), "/archive?i=") ] ]'),
'Sitemap section "archives" contains 10 elements'
);
foreach ($urls as $url) {
self::assertUrlConcrete($url, 'archives', 0.5, 'daily');
}
}

private static function assertIndexContainsSectionLink(
SimpleXMLElement $xml,
string $name,
bool $gzip = false
): SimpleXMLElement {
$loc = sprintf('http://localhost/sitemap.%s.xml', $name);
if ($gzip) {
$loc .= '.gz';
}
$section = $xml->xpath(
sprintf('//sm:sitemapindex/sm:sitemap[ sm:loc[ text() = "%s" ] ]', $loc)
);
Assert::assertCount(
1,
$section,
'Sitemap index contains a link to "' . $loc . '"'
);

return reset($section);
}

private static function assertSectionContainsCountUrls(SimpleXMLElement $xml, string $section, int $count): void
{
Assert::assertCount(
$count,
$xml->xpath('//sm:urlset/sm:url'),
'Sitemap section "' . $section . '" contains ' . $count . ' elements'
);
}

private static function assertSectionContainsPath(
SimpleXMLElement $xml,
string $section,
string $path
): SimpleXMLElement {
$loc = sprintf('http://localhost/%s', ltrim($path, '/'));
$url = $xml->xpath(
sprintf('//sm:urlset/sm:url[ sm:loc[ text() = "%s" ] ]', $loc)
);
Assert::assertCount(
1,
$url,
'Sitemap section "' . $section . '" contains a link to "' . $loc . '"'
);

return reset($url);
}

private static function assertUrlConcrete(
SimpleXMLElement $url,
string $section,
float $priority,
string $changefreq
): void {
$loc = (string)$url->loc;
$locationMessage = 'Sitemap URL "' . $loc . '" of section "' . $section . '"';
Assert::assertInstanceOf(
\DateTime::class,
\DateTime::createFromFormat(DATE_ATOM, $url->lastmod),
$locationMessage . ' has valid lastmod attribute.'
);
Assert::assertSame(
number_format($priority, 1),
(string)$url->priority,
$locationMessage . ' priority attribute is has expected.'
);
Assert::assertSame(
$changefreq,
(string)$url->changefreq,
$locationMessage . ' changefreq priority is has expected.'
);
}

private static function assertUrlHasImage(SimpleXMLElement $url, string $section, string $loc): void
{
$urlLoc = (string)$url->loc;
Assert::assertCount(
1,
$url->xpath(
sprintf('//image:image[ image:loc[ text() = "%s" ] ]', $loc)
),
'Sitemap URL "' . $urlLoc . '" of section "' . $section . '" has image "' . $loc . '"'
);
}

private static function assertUrlHasVideo(SimpleXMLElement $url, string $section, string $loc): void
{
$urlLoc = (string)$url->loc;
Assert::assertCount(
1,
$url->xpath(
sprintf('//video:video[ video:content_loc[ text() = "%s" ] ]', $loc)
),
'Sitemap URL "' . $urlLoc . '" of section "' . $section . '" has video "' . $loc . '"'
);
}
}
Loading

0 comments on commit eeae3dd

Please sign in to comment.