Skip to content

Commit

Permalink
Merge pull request #236 from elecena/SitemapGenerator/add-tests
Browse files Browse the repository at this point in the history
SitemapGenerator: add tests and improve the PHP 8.x support
  • Loading branch information
macbre committed May 22, 2023
2 parents 820f291 + b832bea commit e23a306
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 28 deletions.
58 changes: 32 additions & 26 deletions classes/utils/SitemapGenerator.class.php
Expand Up @@ -5,6 +5,11 @@
*
* @see http://www.sitemaps.org/pl/protocol.html
*/
namespace Nano\Utils;

use Exception;
use NanoApp;
use XMLWriter;

class SitemapGenerator
{
Expand All @@ -17,22 +22,22 @@ class SitemapGenerator

const USE_GZIP = true; // see issue #11

private $app;
private $debug;
private $dir;
private $router;
private NanoApp $app;
private \Nano\Debug $debug;
private string $dir;
private \Nano\Router $router;

// list of sitemap's URLs
private $urls;
/* @var string [] list of sitemap's URLs */
private array $urls = [];

// count URLs in all sitemaps
private $urlsCount = 0;
private int $urlsCount = 0;

// name of the currently built sitemap
private $currentSitemap = '';
private string $currentSitemap = '';

// list of sitemaps to be stored in sitemapindex file
private $sitemaps = [];
private array $sitemaps = [];

/**
* @throws Exception
Expand All @@ -55,7 +60,7 @@ public function __construct()
* @param string $rootElement
* @return XMLWriter
*/
private function initXML($rootElement)
private function initXML(string $rootElement): XMLWriter
{
$xml = new XMLWriter();
$xml->openMemory();
Expand All @@ -77,9 +82,9 @@ private function initXML($rootElement)
* @param XMLWriter $xml
* @param string $fileName
* @param bool $gzip
* @return int
* @return int bytes saved
*/
private function saveXML(XMLWriter $xml, $fileName, $gzip = true)
private function saveXML(XMLWriter $xml, string $fileName, bool $gzip = true): int
{
$fileName = basename($fileName) . ($gzip ? '.gz' : '');

Expand All @@ -103,13 +108,13 @@ private function saveXML(XMLWriter $xml, $fileName, $gzip = true)
*
* @param string $fileName
* @param bool $gzip
* @return bool|int
* @return null|int
*/
private function saveSitemap($fileName, $gzip = true)
private function saveSitemap(string $fileName, bool $gzip = true): ?int
{
// nothing to store
if (empty($this->urls)) {
return false;
return null;
}

// generate XML
Expand Down Expand Up @@ -152,9 +157,9 @@ private function saveSitemap($fileName, $gzip = true)
*
* @param string $fileName
* @param bool $gzip
* @return int
* @return void
*/
private function saveIndex($fileName, $gzip = true)
private function saveIndex(string $fileName, bool $gzip = true): void
{
// generate XML
$xml = $this->initXML('sitemapindex');
Expand All @@ -170,15 +175,15 @@ private function saveIndex($fileName, $gzip = true)
$xml->endElement();
}

return $this->saveXML($xml, $fileName, $gzip);
$this->saveXML($xml, $fileName, $gzip);
}

/**
* Add new entry in sitemap index
*
* Saves sitemap with the current set of URLs and adds an entry to sitemaps index
*/
private function addSitemap()
private function addSitemap(): void
{
// nothing to store
if (count($this->urls) === 0) {
Expand All @@ -196,7 +201,7 @@ private function addSitemap()
*
* @param string $name
*/
public function startSitemap($name)
public function startSitemap(string $name): void
{
// store previously added urls
$this->addSitemap();
Expand All @@ -209,7 +214,7 @@ public function startSitemap($name)
/**
* Closes sitemap created using startSitemap
*/
public function endSitemap()
public function endSitemap(): void
{
// store previously added urls
$this->addSitemap();
Expand All @@ -223,7 +228,7 @@ public function endSitemap()
* @param bool|string|int $lastmod
* @param bool|int $priority
*/
public function addUrl($url, $lastmod = false, $priority = false /* 0.5 is the default value */)
public function addUrl(string $url, $lastmod = false, $priority = false /* 0.5 is the default value */): void
{
$entry = [
'url' => $url,
Expand Down Expand Up @@ -251,15 +256,15 @@ public function addUrl($url, $lastmod = false, $priority = false /* 0.5 is the d
/**
* Return number of URLs added to the list
*/
public function countUrls()
public function countUrls(): int
{
return $this->urlsCount;
}

/**
* Save all remaining links in the sitemap and generate sitemap index
*/
public function save()
public function save(): int
{
$this->debug->log(__METHOD__ . ": number of URLs - " . $this->countUrls());

Expand All @@ -276,10 +281,11 @@ public function save()
*
* @param string $host
* @return bool
* @throws \Nano\Http\ResponseException
*/
public function ping($host)
public function ping(string $host): bool
{
$http = new HttpClient();
$http = new \HttpClient();

$res = $http->get($host . '/ping', [
'sitemap' => $this->router->formatFullUrl() . self::SITEMAP_FILE,
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -17,6 +17,8 @@
"ext-gd": "*",
"ext-json": "*",
"ext-xml": "*",
"ext-xmlwriter": "*",
"ext-zlib": "*",
"predis/predis": "^2.0",
"monolog/monolog": "^3.3.1",
"macbre/monolog-utils": "^3.0.0",
Expand Down
6 changes: 4 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions tests/SitemapGeneratorTest.php
@@ -0,0 +1,51 @@
<?php

/** use the same namespace as the @see \Nano\Utils\SitemapGenerator is in */
namespace Nano\Utils;

use Nano\AppTests\AppTestBase;

/**
* @covers \Nano\Utils\SitemapGenerator
*/
class SitemapGeneratorTest extends AppTestBase
{
public static array $filesWritten = [];

public function setUp(): void
{
parent::setUp();
static::$filesWritten = [];
}

public function testSaveXml()
{
$sitemap = new SitemapGenerator();
$sitemap->startSitemap('pages');
$sitemap->addUrl('/foo/bar');
$urlsCount = $sitemap->save();

$this->assertEquals(1, $urlsCount);

// assert the given files were saved
$this->assertCount(2, static::$filesWritten);
$this->assertArrayHasKey('sitemap.xml', static::$filesWritten);
$this->assertArrayHasKey('sitemap-001-pages.xml.gz', static::$filesWritten);

$sitemapIndexXml = (string) static::$filesWritten['sitemap.xml'];
$this->assertStringContainsString('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', $sitemapIndexXml);
$this->assertStringContainsString('<loc>http://example.org/site/sitemap-001-pages.xml.gz</loc>', $sitemapIndexXml);

$subSitemapXml = gzdecode((string) static::$filesWritten['sitemap-001-pages.xml.gz']);
$this->assertStringContainsString('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', $subSitemapXml);
$this->assertStringContainsString('<loc>/foo/bar</loc>', $subSitemapXml);
}
}

function file_put_contents(string $fileName, string $content): int
{
// var_dump(__METHOD__, $fileName, $content);

SitemapGeneratorTest::$filesWritten[ basename($fileName) ] = $content;
return strlen($content);
}

0 comments on commit e23a306

Please sign in to comment.