Skip to content

Commit

Permalink
Generate URs via URL facade instead of route() helper
Browse files Browse the repository at this point in the history
This makes it easier to to test without overriding Laravel's helpers.
  • Loading branch information
ivanvermeyen committed Mar 31, 2023
1 parent 82ee57a commit b89f5e4
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 131 deletions.
3 changes: 2 additions & 1 deletion src/LocalizedUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
use CodeZero\UrlBuilder\UrlBuilder;
use Illuminate\Support\Facades\URL;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -116,7 +117,7 @@ public function generateFromRequest(string $locale = null, $parameters = null, b
protected function generateNamedRouteURL(string $locale, array $parameters = [], bool $absolute = true): string
{
try {
return route($this->route->getName(), $parameters, $absolute, $locale);
return URL::route($this->route->getName(), $parameters, $absolute, $locale);
} catch (InvalidArgumentException $e) {
return '';
}
Expand Down
15 changes: 8 additions & 7 deletions tests/Unit/HelpersFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CodeZero\LocalizedRoutes\Tests\TestCase;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

class HelpersFileTest extends TestCase
Expand All @@ -18,9 +19,9 @@ function it_returns_localized_routes_with_locale_argument(): void
Route::get('route')->name('route');
});

$this->assertEquals(url('en/route'), route('route', [], true, null));
$this->assertEquals(url('en/route'), route('route', [], true, 'en'));
$this->assertEquals(url('nl/route'), route('route', [], true, 'nl'));
$this->assertEquals(URL::to('en/route'), URL::route('route', [], true, null));
$this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'en'));
$this->assertEquals(URL::to('nl/route'), URL::route('route', [], true, 'nl'));
}

/** @test */
Expand All @@ -35,7 +36,7 @@ function it_throws_when_route_helper_locale_is_unsupported(): void

$this->expectException(RouteNotFoundException::class);

route('route', [], true, 'wk');
URL::route('route', [], true, 'wk');
}

/** @test */
Expand All @@ -49,8 +50,8 @@ function it_uses_fallback_locale_when_route_helper_locale_is_unsupported(): void
Route::get('route')->name('route');
});

$this->assertEquals(url('en/route'), route('route', [], true, 'en'));
$this->assertEquals(url('nl/route'), route('route', [], true, 'nl'));
$this->assertEquals(url('en/route'), route('route', [], true, 'wk'));
$this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'en'));
$this->assertEquals(URL::to('nl/route'), URL::route('route', [], true, 'nl'));
$this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'wk'));
}
}
Loading

0 comments on commit b89f5e4

Please sign in to comment.