Skip to content

Commit

Permalink
More tests (#77)
Browse files Browse the repository at this point in the history
* Add unit test which checks against generated XML.
* Test to make sure '&' is encoded correctly in provided URLs.
  • Loading branch information
DavidGoodwin authored Jul 5, 2021
1 parent f069c2c commit b71d60f
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,51 @@ public function testWritingFile()

$this->assertTrue(file_exists($fileName));
$this->assertIsValidSitemap($fileName);
$this->assertFileExists($fileName);

unlink($fileName);

$this->assertFileNotExists($fileName);
}


public function testAgainstExpectedXml() {
$fileName = __DIR__ . '/sitemap_regular.xml';
$sitemap = new Sitemap($fileName);

$sitemap->addItem('http://example.com/test.html&q=name', (new \DateTime('2021-01-11 01:01'))->format('U'));
$sitemap->addItem('http://example.com/mylink?foo=bar', (new \DateTime('2021-01-02 03:04'))->format('U'), Sitemap::HOURLY);

$sitemap->addItem('http://example.com/mylink4', (new \DateTime('2021-01-02 03:04'))->format('U'), Sitemap::DAILY, 0.3);

$sitemap->write();

$this->assertFileExists($fileName);

$expected = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://example.com/test.html&amp;q=name</loc>
<lastmod>2021-01-11T01:01:00+00:00</lastmod>
</url>
<url>
<loc>http://example.com/mylink?foo=bar</loc>
<lastmod>2021-01-02T03:04:00+00:00</lastmod>
<changefreq>hourly</changefreq>
</url>
<url>
<loc>http://example.com/mylink4</loc>
<lastmod>2021-01-02T03:04:00+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.3</priority>
</url>
</urlset>
EOF;

$x = trim(file_get_contents($fileName));

$this->assertEquals($expected, $x);
}

public function testMultipleFiles()
Expand Down

0 comments on commit b71d60f

Please sign in to comment.