Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from elecena/SitemapGenerator/add-tests
SitemapGenerator: add tests and improve the PHP 8.x support
- Loading branch information
Showing
4 changed files
with
89 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |