Files
moonwell-web/tests/Feature/SitemapRenderingFeatureTest.php
T
2026-10-20 19:50:17 +04:00

56 lines
2.2 KiB
PHP

<?php
namespace Tests\Feature;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Process\Process;
use Tests\TestCase;
class SitemapRenderingFeatureTest extends TestCase
{
#[DataProvider('shortOpenTagSettings')]
public function test_sitemap_renders_with_both_php_short_tag_settings(string $setting): void
{
$script = <<<'PHP'
require $argv[1].'/vendor/autoload.php';
$app = require $argv[1].'/bootstrap/app.php';
$app->loadEnvironmentFrom('.env.example');
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$compiledPath = sys_get_temp_dir().'/moonwell-sitemap-'.bin2hex(random_bytes(8));
mkdir($compiledPath);
config(['app.url' => 'https://moon-well.online', 'view.compiled' => $compiledPath]);
try {
$app['blade.compiler']->compile(resource_path('views/seo/sitemap.blade.php'));
echo view('seo.sitemap', [
'seo' => app(App\Services\SeoService::class),
'articles' => collect([(new App\Models\News)->forceFill([
'id' => 42,
'updated_at' => '2026-10-20 12:00:00',
])]),
])->render();
} finally {
(new Illuminate\Filesystem\Filesystem)->deleteDirectory($compiledPath);
}
PHP;
$process = new Process([PHP_BINARY, '-d', 'short_open_tag='.$setting, '-r', $script, base_path()]);
$process->run();
$this->assertTrue($process->isSuccessful(), $process->getErrorOutput().$process->getOutput());
$this->assertStringStartsWith('<?xml version="1.0" encoding="UTF-8"?>', $process->getOutput());
$xml = simplexml_load_string($process->getOutput());
$this->assertNotFalse($xml);
$xml->registerXPathNamespace('s', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$this->assertSame('https://moon-well.online/news/42', (string) $xml->xpath('//s:url[3]/s:loc')[0]);
$this->assertSame('2026-10-20T12:00:00+00:00', (string) $xml->xpath('//s:lastmod')[0]);
}
public static function shortOpenTagSettings(): array
{
return [
'production: enabled' => ['1'],
'development: disabled' => ['0'],
];
}
}