|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hyde\Framework\Testing\Feature; |
| 6 | + |
| 7 | +use Mockery; |
| 8 | +use Hyde\Hyde; |
| 9 | +use Hyde\Testing\TestCase; |
| 10 | +use Illuminate\Support\Carbon; |
| 11 | +use Illuminate\Support\Facades\File; |
| 12 | +use Illuminate\Filesystem\Filesystem; |
| 13 | + |
| 14 | +/** |
| 15 | + * High level test of the sitemap generation feature. |
| 16 | + * |
| 17 | + * It contains a setup that covers all code paths, proving 100% coverage in actual usage. |
| 18 | + * |
| 19 | + * @see \Hyde\Framework\Testing\Feature\Services\SitemapServiceTest |
| 20 | + * @see \Hyde\Framework\Testing\Feature\Commands\BuildSitemapCommandTest |
| 21 | + * |
| 22 | + * @covers \Hyde\Framework\Features\XmlGenerators\SitemapGenerator |
| 23 | + * @covers \Hyde\Framework\Actions\PostBuildTasks\GenerateSitemap |
| 24 | + * @covers \Hyde\Console\Commands\BuildSitemapCommand |
| 25 | + */ |
| 26 | +class SitemapFeatureTest extends TestCase |
| 27 | +{ |
| 28 | + public function testTheSitemapFeature() |
| 29 | + { |
| 30 | + Carbon::setTestNow('2024-01-01 12:00:00'); |
| 31 | + $filesystem = Mockery::mock(Filesystem::class)->makePartial(); |
| 32 | + $filesystem->shouldReceive('lastModified')->andReturn(Carbon::now()->timestamp); |
| 33 | + File::swap($filesystem); |
| 34 | + |
| 35 | + $this->cleanUpWhenDone('_site/sitemap.xml'); |
| 36 | + $this->setUpBroadSiteStructure(); |
| 37 | + $this->withSiteUrl(); |
| 38 | + |
| 39 | + $this->artisan('build:sitemap') |
| 40 | + ->expectsOutputToContain('Created _site/sitemap.xml') |
| 41 | + ->assertExitCode(0); |
| 42 | + |
| 43 | + $this->assertFileExists('_site/sitemap.xml'); |
| 44 | + |
| 45 | + $this->assertSameXml( |
| 46 | + '<?xml version="1.0" encoding="UTF-8"?>'."\n{$this->stripFormatting($this->expected(Hyde::version()))}\n", |
| 47 | + file_get_contents('_site/sitemap.xml') |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + protected function setUpBroadSiteStructure(): void |
| 52 | + { |
| 53 | + $this->file('_pages/about.md', "# About\n\nThis is the about page."); |
| 54 | + $this->file('_pages/contact.html', '<h1>Contact</h1><p>This is the contact page.</p>'); |
| 55 | + $this->file('_posts/hello-world.md', "# Hello, World!\n\nThis is the first post."); |
| 56 | + $this->file('_posts/second-post.md', "# Second Post\n\nThis is the second post."); |
| 57 | + $this->file('_docs/index.md', "# Documentation\n\nThis is the documentation index."); |
| 58 | + $this->file('_docs/installation.md', "# Installation\n\nThis is the installation guide."); |
| 59 | + $this->file('_docs/usage.md', "# Usage\n\nThis is the usage guide."); |
| 60 | + $this->file('_docs/404.md', "# 404\n\nThis is the 404 page."); |
| 61 | + } |
| 62 | + |
| 63 | + protected function expected(string $version): string |
| 64 | + { |
| 65 | + return <<<XML |
| 66 | + <urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" generator="HydePHP $version"> |
| 67 | + <url> |
| 68 | + <loc>https://example.com/contact.html</loc> |
| 69 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 70 | + <changefreq>weekly</changefreq> |
| 71 | + <priority>0.75</priority> |
| 72 | + </url> |
| 73 | + <url> |
| 74 | + <loc>https://example.com/404.html</loc> |
| 75 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 76 | + <changefreq>monthly</changefreq> |
| 77 | + <priority>0.25</priority> |
| 78 | + </url> |
| 79 | + <url> |
| 80 | + <loc>https://example.com/index.html</loc> |
| 81 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 82 | + <changefreq>daily</changefreq> |
| 83 | + <priority>1</priority> |
| 84 | + </url> |
| 85 | + <url> |
| 86 | + <loc>https://example.com/about.html</loc> |
| 87 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 88 | + <changefreq>daily</changefreq> |
| 89 | + <priority>0.9</priority> |
| 90 | + </url> |
| 91 | + <url> |
| 92 | + <loc>https://example.com/posts/hello-world.html</loc> |
| 93 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 94 | + <changefreq>weekly</changefreq> |
| 95 | + <priority>0.75</priority> |
| 96 | + </url> |
| 97 | + <url> |
| 98 | + <loc>https://example.com/posts/second-post.html</loc> |
| 99 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 100 | + <changefreq>weekly</changefreq> |
| 101 | + <priority>0.75</priority> |
| 102 | + </url> |
| 103 | + <url> |
| 104 | + <loc>https://example.com/docs/404.html</loc> |
| 105 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 106 | + <changefreq>monthly</changefreq> |
| 107 | + <priority>0.25</priority> |
| 108 | + </url> |
| 109 | + <url> |
| 110 | + <loc>https://example.com/docs/index.html</loc> |
| 111 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 112 | + <changefreq>daily</changefreq> |
| 113 | + <priority>1</priority> |
| 114 | + </url> |
| 115 | + <url> |
| 116 | + <loc>https://example.com/docs/installation.html</loc> |
| 117 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 118 | + <changefreq>daily</changefreq> |
| 119 | + <priority>0.9</priority> |
| 120 | + </url> |
| 121 | + <url> |
| 122 | + <loc>https://example.com/docs/usage.html</loc> |
| 123 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 124 | + <changefreq>daily</changefreq> |
| 125 | + <priority>0.9</priority> |
| 126 | + </url> |
| 127 | + <url> |
| 128 | + <loc>https://example.com/docs/search.json</loc> |
| 129 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 130 | + <changefreq>weekly</changefreq> |
| 131 | + <priority>0.5</priority> |
| 132 | + </url> |
| 133 | + <url> |
| 134 | + <loc>https://example.com/docs/search.html</loc> |
| 135 | + <lastmod>2024-01-01T12:00:00+00:00</lastmod> |
| 136 | + <changefreq>weekly</changefreq> |
| 137 | + <priority>0.5</priority> |
| 138 | + </url> |
| 139 | + </urlset> |
| 140 | + XML; |
| 141 | + } |
| 142 | + |
| 143 | + protected function stripFormatting(string $xml): string |
| 144 | + { |
| 145 | + return implode('', array_map('trim', explode("\n", $xml))); |
| 146 | + } |
| 147 | + |
| 148 | + protected function expandLines(string $xml): string |
| 149 | + { |
| 150 | + return str_replace('><', ">\n<", $xml); |
| 151 | + } |
| 152 | + |
| 153 | + protected function assertSameXml(string $expected, string $actual): void |
| 154 | + { |
| 155 | + $this->assertSame($this->expandLines($expected), $this->expandLines($actual)); |
| 156 | + } |
| 157 | +} |
0 commit comments