From 1cda933f12425a6dd1c50d4367f8ee01c80c61c3 Mon Sep 17 00:00:00 2001 From: macbre Date: Thu, 18 May 2023 11:54:23 +0100 Subject: [PATCH 01/10] composer.json: require the "ext-xmlwriter" --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 4a3ea608..f695d4c1 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "ext-gd": "*", "ext-json": "*", "ext-xml": "*", + "ext-xmlwriter": "*", "predis/predis": "^2.0", "monolog/monolog": "^3.3.1", "macbre/monolog-utils": "^3.0.0", From aff03154f05049a4dca3deb8731f702dd333ae4d Mon Sep 17 00:00:00 2001 From: macbre Date: Thu, 18 May 2023 11:54:55 +0100 Subject: [PATCH 02/10] SitemapGenerator: add the typing --- classes/utils/SitemapGenerator.class.php | 47 ++++++++++++------------ 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/classes/utils/SitemapGenerator.class.php b/classes/utils/SitemapGenerator.class.php index 77eeb9d5..9205c115 100644 --- a/classes/utils/SitemapGenerator.class.php +++ b/classes/utils/SitemapGenerator.class.php @@ -17,22 +17,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; // 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 @@ -55,7 +55,7 @@ public function __construct() * @param string $rootElement * @return XMLWriter */ - private function initXML($rootElement) + private function initXML(string $rootElement): XMLWriter { $xml = new XMLWriter(); $xml->openMemory(); @@ -77,9 +77,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' : ''); @@ -103,13 +103,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 @@ -152,9 +152,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'); @@ -170,7 +170,7 @@ private function saveIndex($fileName, $gzip = true) $xml->endElement(); } - return $this->saveXML($xml, $fileName, $gzip); + $this->saveXML($xml, $fileName, $gzip); } /** @@ -178,7 +178,7 @@ private function saveIndex($fileName, $gzip = true) * * 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) { @@ -196,7 +196,7 @@ private function addSitemap() * * @param string $name */ - public function startSitemap($name) + public function startSitemap(string $name): void { // store previously added urls $this->addSitemap(); @@ -209,7 +209,7 @@ public function startSitemap($name) /** * Closes sitemap created using startSitemap */ - public function endSitemap() + public function endSitemap(): void { // store previously added urls $this->addSitemap(); @@ -223,7 +223,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, @@ -251,7 +251,7 @@ 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; } @@ -259,7 +259,7 @@ public function countUrls() /** * 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()); @@ -276,8 +276,9 @@ 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(); From bbd63b042b8d80dd830af2ab7bbeb5abccb91592 Mon Sep 17 00:00:00 2001 From: macbre Date: Fri, 19 May 2023 10:25:00 +0100 Subject: [PATCH 03/10] Update the composer.lock file --- composer.lock | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.lock b/composer.lock index 0a876d7b..af8ca1dd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2527bd15a1ddf446276c030c05763096", + "content-hash": "0120dd275ab7b763ac65f783be698760", "packages": [ { "name": "macbre/monolog-utils", @@ -3933,7 +3933,8 @@ "ext-mbstring": "*", "ext-gd": "*", "ext-json": "*", - "ext-xml": "*" + "ext-xml": "*", + "ext-xmlwriter": "*" }, "platform-dev": [], "plugin-api-version": "2.3.0" From 93480f5493be501a99bc0ec58b0b8ec38f3963e9 Mon Sep 17 00:00:00 2001 From: macbre Date: Mon, 22 May 2023 17:40:46 +0100 Subject: [PATCH 04/10] SitemapGeneratorTest initial version --- tests/SitemapGeneratorTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/SitemapGeneratorTest.php diff --git a/tests/SitemapGeneratorTest.php b/tests/SitemapGeneratorTest.php new file mode 100644 index 00000000..62a07767 --- /dev/null +++ b/tests/SitemapGeneratorTest.php @@ -0,0 +1,25 @@ +startSitemap('pages'); + $sitemap->addUrl('/foo/bar'); + } +} + +function file_put_contents(string $fileName, string $content): int +{ + var_dump(__METHOD__, $fileName, $content); + return strlen($content); +} From 33262a16c424660a9833a391e67d802f3680ba11 Mon Sep 17 00:00:00 2001 From: macbre Date: Mon, 22 May 2023 17:41:19 +0100 Subject: [PATCH 05/10] composer.json: require ext-zlib - gzencode() call in the SitemapGenerator class --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index f695d4c1..eea9c63c 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "ext-json": "*", "ext-xml": "*", "ext-xmlwriter": "*", + "ext-zlib": "*", "predis/predis": "^2.0", "monolog/monolog": "^3.3.1", "macbre/monolog-utils": "^3.0.0", From 08490b8219b22e03141c57ea232648728a30d011 Mon Sep 17 00:00:00 2001 From: macbre Date: Mon, 22 May 2023 17:41:42 +0100 Subject: [PATCH 06/10] Move SitemapGenerator to a dedicate namespace to be able to mock file_put_contents() calls TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given /Users/macbre/git/elecena/nano/classes/utils/SitemapGenerator.class.php:189 /Users/macbre/git/elecena/nano/classes/utils/SitemapGenerator.class.php:207 /Users/macbre/git/elecena/nano/tests/SitemapGeneratorTest.php:16 --- classes/utils/SitemapGenerator.class.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/classes/utils/SitemapGenerator.class.php b/classes/utils/SitemapGenerator.class.php index 9205c115..3bf0bae4 100644 --- a/classes/utils/SitemapGenerator.class.php +++ b/classes/utils/SitemapGenerator.class.php @@ -5,6 +5,11 @@ * * @see http://www.sitemaps.org/pl/protocol.html */ +namespace Nano\Utils; + +use Exception; +use NanoApp; +use XMLWriter; class SitemapGenerator { @@ -276,11 +281,11 @@ public function save(): int * * @param string $host * @return bool - * @throws Nano\Http\ResponseException + * @throws \Nano\Http\ResponseException */ public function ping(string $host): bool { - $http = new HttpClient(); + $http = new \HttpClient(); $res = $http->get($host . '/ping', [ 'sitemap' => $this->router->formatFullUrl() . self::SITEMAP_FILE, From 52c2bbc3acb2c6946cd16873b200a54284603b09 Mon Sep 17 00:00:00 2001 From: macbre Date: Mon, 22 May 2023 17:55:38 +0100 Subject: [PATCH 07/10] SitemapGenerator: typing improved Fixes: TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given --- classes/utils/SitemapGenerator.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/utils/SitemapGenerator.class.php b/classes/utils/SitemapGenerator.class.php index 3bf0bae4..55f21900 100644 --- a/classes/utils/SitemapGenerator.class.php +++ b/classes/utils/SitemapGenerator.class.php @@ -27,8 +27,8 @@ class SitemapGenerator 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 int $urlsCount = 0; From 4f3753829d65b8aa3ab55e717de69179e18bb101 Mon Sep 17 00:00:00 2001 From: macbre Date: Mon, 22 May 2023 17:56:47 +0100 Subject: [PATCH 08/10] Add SitemapGeneratorTest --- tests/SitemapGeneratorTest.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/tests/SitemapGeneratorTest.php b/tests/SitemapGeneratorTest.php index 62a07767..ce0e2471 100644 --- a/tests/SitemapGeneratorTest.php +++ b/tests/SitemapGeneratorTest.php @@ -3,23 +3,46 @@ /** use the same namespace as the @see \Nano\Utils\SitemapGenerator is in */ namespace Nano\Utils; -use Nano\NanoBaseTest; +use Nano\AppTests\AppTestBase; /** * @covers \Nano\Utils\SitemapGenerator */ -class SitemapGeneratorTest extends NanoBaseTest +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('', $sitemapIndexXml); + $this->assertStringContainsString('http://example.org/site/sitemap-001-pages.xml.gz', $sitemapIndexXml); } } function file_put_contents(string $fileName, string $content): int { - var_dump(__METHOD__, $fileName, $content); + // var_dump(__METHOD__, $fileName, $content); + + SitemapGeneratorTest::$filesWritten[ basename($fileName) ] = $content; return strlen($content); } From fde2be386eca5b54fdb4937b167cc7b0cb293559 Mon Sep 17 00:00:00 2001 From: macbre Date: Mon, 22 May 2023 17:58:45 +0100 Subject: [PATCH 09/10] Update the lock file --- composer.lock | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.lock b/composer.lock index af8ca1dd..e413be8a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0120dd275ab7b763ac65f783be698760", + "content-hash": "b4d6c9261a7732935a1fafeae1eb995f", "packages": [ { "name": "macbre/monolog-utils", @@ -3934,7 +3934,8 @@ "ext-gd": "*", "ext-json": "*", "ext-xml": "*", - "ext-xmlwriter": "*" + "ext-xmlwriter": "*", + "ext-zlib": "*" }, "platform-dev": [], "plugin-api-version": "2.3.0" From b832beae09a7153fb53bbce98f1724dd2cfcde76 Mon Sep 17 00:00:00 2001 From: macbre Date: Mon, 22 May 2023 18:02:48 +0100 Subject: [PATCH 10/10] SitemapGeneratorTest: assert the sub-sitemap content --- tests/SitemapGeneratorTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/SitemapGeneratorTest.php b/tests/SitemapGeneratorTest.php index ce0e2471..ff288099 100644 --- a/tests/SitemapGeneratorTest.php +++ b/tests/SitemapGeneratorTest.php @@ -33,9 +33,12 @@ public function testSaveXml() $this->assertArrayHasKey('sitemap-001-pages.xml.gz', static::$filesWritten); $sitemapIndexXml = (string) static::$filesWritten['sitemap.xml']; - $this->assertStringContainsString('', $sitemapIndexXml); $this->assertStringContainsString('http://example.org/site/sitemap-001-pages.xml.gz', $sitemapIndexXml); + + $subSitemapXml = gzdecode((string) static::$filesWritten['sitemap-001-pages.xml.gz']); + $this->assertStringContainsString('', $subSitemapXml); + $this->assertStringContainsString('/foo/bar', $subSitemapXml); } }