2026-10-14 17:32:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Uwil\SitemapGenerator\Tests\Feature;
|
|
|
|
|
|
2026-10-15 16:31:18 +02:00
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
|
|
|
use Illuminate\Support\Facades\Queue;
|
2026-10-14 17:32:12 +02:00
|
|
|
use Illuminate\Support\Facades\Route;
|
2026-10-15 14:42:20 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2026-10-14 17:32:12 +02:00
|
|
|
use Uwil\SitemapGenerator\Config\SitemapConfig;
|
|
|
|
|
use Uwil\SitemapGenerator\Console\Commands\GenerateSitemapCommand;
|
2026-10-15 16:31:18 +02:00
|
|
|
use Uwil\SitemapGenerator\Jobs\GenerateSitemapJob;
|
|
|
|
|
use Illuminate\Console\Scheduling\Event;
|
|
|
|
|
use Webmozart\Assert\Assert;
|
2026-10-14 17:32:12 +02:00
|
|
|
|
|
|
|
|
covers(GenerateSitemapCommand::class);
|
|
|
|
|
|
|
|
|
|
describe('GenerateSitemapCommand test', function () {
|
2026-10-15 14:42:20 +02:00
|
|
|
beforeEach(function () {
|
|
|
|
|
/** @var \Uwil\SitemapGenerator\Tests\TestCase $this */
|
|
|
|
|
Storage::disk($this->outputDisk)->delete($this->outputPath);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
|
/** @var \Uwil\SitemapGenerator\Tests\TestCase $this */
|
|
|
|
|
Storage::disk($this->outputDisk)->delete($this->outputPath);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-14 17:32:12 +02:00
|
|
|
it('displays the dry-run table without creating the sitemap file', function () {
|
|
|
|
|
/** @var \Uwil\SitemapGenerator\Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
Route::name('contact')->get('/{locale}/contact', fn () => 'contact');
|
|
|
|
|
config()->set(SitemapConfig::CONFIG_KEY . '.single_pages', ['contact']);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
/** @var \Illuminate\Testing\PendingCommand $command */
|
|
|
|
|
$command = $this->artisan('uwil:sitemap:refresh --dry-run');
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$command
|
|
|
|
|
->expectsOutput('Generating Sitemap..')
|
|
|
|
|
->assertSuccessful()
|
|
|
|
|
->run();
|
2026-10-15 14:42:20 +02:00
|
|
|
expect(Storage::disk($this->outputDisk)->exists($this->outputPath))->toBeFalse();
|
2026-10-14 17:32:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('generates the sitemap xml file and serves it via the GET route', function () {
|
|
|
|
|
/** @var \Uwil\SitemapGenerator\Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
$url = 'sitemap.xml';
|
|
|
|
|
Route::name('contact')->get('/{locale}/contact', fn () => 'contact');
|
|
|
|
|
config()->set(SitemapConfig::CONFIG_KEY . '.single_pages', ['contact']);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
/** @var \Illuminate\Testing\PendingCommand $command */
|
|
|
|
|
$command = $this->artisan('uwil:sitemap:refresh');
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$command
|
|
|
|
|
->expectsOutput('Generating Sitemap..')
|
|
|
|
|
->expectsOutput('A new sitemap.xml was generated')
|
|
|
|
|
->assertSuccessful()
|
|
|
|
|
->run();
|
2026-10-15 14:42:20 +02:00
|
|
|
expect(Storage::disk($this->outputDisk)->exists($this->outputPath))->toBeTrue();
|
|
|
|
|
$content = Storage::disk($this->outputDisk)->get($this->outputPath);
|
2026-10-14 17:32:12 +02:00
|
|
|
expect($content)
|
|
|
|
|
->toContain('<urlset')
|
|
|
|
|
->toContain('/fr/contact')
|
|
|
|
|
->toContain('/en/contact');
|
2026-10-15 14:42:20 +02:00
|
|
|
$response = $this->get($url);
|
|
|
|
|
$response->assertOk();
|
|
|
|
|
expect($response->streamedContent())
|
|
|
|
|
->toContain('/fr/contact');
|
2026-10-14 17:32:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns 404 when the sitemap has not been generated yet', function () {
|
|
|
|
|
/** @var \Uwil\SitemapGenerator\Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
$url = 'sitemap.xml';
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $this->get($url);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$result->assertNotFound();
|
|
|
|
|
});
|
2026-10-15 16:31:18 +02:00
|
|
|
|
|
|
|
|
it('dispatches the job to queue when using --queue option', function () {
|
|
|
|
|
/** @var \Uwil\SitemapGenerator\Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
Queue::fake();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
/** @var \Illuminate\Testing\PendingCommand $command */
|
|
|
|
|
$command = $this->artisan('uwil:sitemap:refresh --queue');
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$command
|
|
|
|
|
->expectsOutput('Generating Sitemap..')
|
|
|
|
|
->expectsOutput('Sitemap generation job dispatched to queue.')
|
|
|
|
|
->assertSuccessful()
|
|
|
|
|
->run();
|
|
|
|
|
|
|
|
|
|
Queue::assertPushed(GenerateSitemapJob::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('schedules GenerateSitemapJob with configured cron expression', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$schedule = resolve(Schedule::class);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$schedule->job(new GenerateSitemapJob())->cron('15 4 * * *');
|
|
|
|
|
$events = collect($schedule->events());
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$event = $events->first(fn ($e) => str_contains($e->description ?? '', 'GenerateSitemapJob'));
|
|
|
|
|
Assert::isInstanceOf($event, Event::class);
|
|
|
|
|
|
|
|
|
|
expect($event)->not->toBeNull()
|
|
|
|
|
->and($event->expression)->toBe('15 4 * * *');
|
|
|
|
|
});
|
2026-10-14 17:32:12 +02:00
|
|
|
});
|