ldc-sitemap-generator/tests/Unit/Config/SitemapConfigTest.php
Jérémy Munsch 4e8c58d5f9
refactor: refactoring et ajout de tests, la dependance pour traduire les routes est vraiment optionnelle.
- Ajout de jobs de lint, test, ci
- Refactoring de code
- montée de version sur laravel 13 uniquement et php8.5
2026-10-15 10:18:25 +02:00

166 lines
6 KiB
PHP

<?php
declare(strict_types=1);
namespace Uwil\SitemapGenerator\Unit\Integration\Config;
use Cron\CronExpression;
use Illuminate\Config\Repository as ConfigRepository;
use Uwil\SitemapGenerator\Config\SitemapConfig;
use Uwil\SitemapGenerator\Exceptions\SitemapException;
use Uwil\SitemapGenerator\Localizers\DefaultRouteLocalizer;
use Uwil\SitemapGenerator\Tests\Unit\Config\ModelClass;
covers(SitemapConfig::class);
describe('SitemapConfig', function () {
it('can be instantiated with valid configuration', function () {
/** @var \Uwil\SitemapGenerator\Tests\TestCase $this */
// GIVEN
$configRepository = new ConfigRepository([
SitemapConfig::CONFIG_KEY => [
'single_pages' => ['contact'],
'supported_locales' => ['fr', 'en'],
'cruds' => [],
'schedule' => false,
'cron' => '45 15 * * *',
'localizer' => 'default',
'output_path' => $this->testXmlPath,
],
]);
// WHEN
$sitemapConfig = new SitemapConfig($configRepository);
// THEN
expect($sitemapConfig)->toBeInstanceOf(SitemapConfig::class)
->and($sitemapConfig->singlePages)->toBe(['contact'])
->and($sitemapConfig->supportedLocales)->toBe(['fr', 'en'])
->and($sitemapConfig->cruds)->toBe([])
->and($sitemapConfig->schedule)->toBeFalse()
->and($sitemapConfig->localizer)->toBe('default')
->and($sitemapConfig->cron)->toBeInstanceOf(CronExpression::class)
->and($sitemapConfig->outputPath)->toBe($this->testXmlPath);
});
it('falls back to legacy singlePages config', function () {
// GIVEN
$configRepository = new ConfigRepository([
SitemapConfig::CONFIG_KEY => [
'singlePages' => ['legacy-page'],
'supported_locales' => ['fr'],
'cruds' => [],
'schedule' => false,
'cron' => '* * * * *',
'localizer' => 'default',
'output_path' => '/tmp/sitemap.xml',
],
]);
// WHEN
$sitemapConfig = new SitemapConfig($configRepository);
// THEN
expect($sitemapConfig->singlePages)
->toBe(['legacy-page']);
});
it('throws exception when cron expression is invalid', function () {
// GIVEN
$configRepository = new ConfigRepository([
SitemapConfig::CONFIG_KEY => [
'single_pages' => [],
'supported_locales' => ['fr'],
'cruds' => [],
'schedule' => false,
'cron' => 'invalid-cron',
'localizer' => 'default',
'output_path' => '/tmp/sitemap.xml',
],
]);
// WHEN
$action = fn () => new SitemapConfig($configRepository);
// THEN
expect($action)
->toThrow(SitemapException::class, "Cron config is not a valid expression 'invalid-cron'");
});
it('throws exception when output path does not end with .xml', function () {
// GIVEN
$configRepository = new ConfigRepository([
SitemapConfig::CONFIG_KEY => [
'single_pages' => [],
'supported_locales' => ['fr'],
'cruds' => [],
'schedule' => false,
'cron' => '* * * * *',
'localizer' => 'default',
'output_path' => '/tmp/sitemap.txt',
],
]);
// WHEN
$action = fn () => new SitemapConfig($configRepository);
// THEN
expect($action)
->toThrow(
SitemapException::class,
"Output path should indicate an XML file location '/tmp/sitemap.txt'"
);
});
it('can parse cruds configuration properly', function () {
// GIVEN
$configRepository = new ConfigRepository([
SitemapConfig::CONFIG_KEY => [
'single_pages' => [],
'supported_locales' => ['fr'],
'cruds' => [
[
'model' => ModelClass::class,
'relationFilters' => ['posts' => fn () => true],
'modelFilters' => [['is_active', '=', true]],
'allowNullRelations' => ['category'],
],
],
'schedule' => false,
'cron' => '* * * * *',
'localizer' => 'default',
'output_path' => '/tmp/sitemap.xml',
],
]);
// WHEN
$sitemapConfig = new SitemapConfig($configRepository);
// THEN
expect($sitemapConfig->cruds)->toHaveCount(1)
->and($sitemapConfig->cruds[0]->modelClass)->toBe(ModelClass::class)
->and($sitemapConfig->cruds[0]->allowNullRelations)->toBe(['category']);
});
it('returns the configured localizer instance', function () {
// GIVEN
$configRepository = new ConfigRepository([
SitemapConfig::CONFIG_KEY => [
'single_pages' => [],
'supported_locales' => ['fr'],
'cruds' => [],
'schedule' => false,
'cron' => '* * * * *',
'localizer' => 'default',
'output_path' => '/tmp/sitemap.xml',
],
]);
// WHEN
$sitemapConfig = new SitemapConfig($configRepository);
$localizer = $sitemapConfig->getConfiguredLocalizerFor(app());
// THEN
expect($localizer)->toBeInstanceOf(DefaultRouteLocalizer::class);
});
});