77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Uwil\SitemapGenerator\Tests\Unit\Localizers;
|
|
|
|
use Illuminate\Support\Facades\Route;
|
|
use Uwil\SitemapGenerator\Localizers\DefaultRouteLocalizer;
|
|
|
|
covers(DefaultRouteLocalizer::class);
|
|
|
|
describe('DefaultRouteLocalizer', function () {
|
|
it('can be used unconditionally', function () {
|
|
// GIVEN
|
|
$localizer = new DefaultRouteLocalizer();
|
|
|
|
// WHEN
|
|
$canBeUsed = $localizer->canBeUsed();
|
|
|
|
// THEN
|
|
expect($canBeUsed)->toBeTrue();
|
|
});
|
|
|
|
it('returns localized url for existing route with locale parameter', function () {
|
|
// GIVEN
|
|
Route::name('about')->get('/{locale}/about', fn () => 'about');
|
|
|
|
$localizer = new DefaultRouteLocalizer();
|
|
|
|
// WHEN
|
|
$url = $localizer->getLocalizedUrl('fr', 'about');
|
|
|
|
// THEN
|
|
expect($url)->not->toBeNull()
|
|
->and((string) $url)->toBe('http://localhost/fr/about');
|
|
});
|
|
|
|
it('returns null when route does not exist', function () {
|
|
// GIVEN
|
|
$localizer = new DefaultRouteLocalizer();
|
|
|
|
// WHEN
|
|
$url = $localizer->getLocalizedUrl('fr', 'non-existing-route');
|
|
|
|
// THEN
|
|
expect($url)->toBeNull();
|
|
});
|
|
|
|
it('does not append a locale query string when the route has no locale parameter', function () {
|
|
// GIVEN
|
|
Route::name('contact')->get('/contact', fn () => 'contact');
|
|
|
|
$localizer = new DefaultRouteLocalizer();
|
|
|
|
// WHEN
|
|
$url = $localizer->getLocalizedUrl('fr', 'contact');
|
|
|
|
// THEN
|
|
expect($url)->not->toBeNull()
|
|
->and((string) $url)->toBe('http://localhost/contact');
|
|
});
|
|
|
|
it('uses the given locale in the path for each locale when the route has a locale parameter', function () {
|
|
// GIVEN
|
|
Route::name('about')->get('/{locale}/about', fn () => 'about');
|
|
|
|
$localizer = new DefaultRouteLocalizer();
|
|
|
|
// WHEN
|
|
$fr = $localizer->getLocalizedUrl('fr', 'about');
|
|
$en = $localizer->getLocalizedUrl('en', 'about');
|
|
|
|
// THEN
|
|
expect((string) $fr)->toBe('http://localhost/fr/about')
|
|
->and((string) $en)->toBe('http://localhost/en/about');
|
|
});
|
|
});
|