diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index dec3f8c..977b6b0 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -20,9 +20,9 @@ jobs: - laravel: 12.* testbench: 10.* - laravel: 11.* - testbench: 9.* + testbench: ^9.9 - laravel: 10.* - testbench: 8.* + testbench: ^8.31 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/composer.json b/composer.json index ebb9695..1e406ca 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "laravel/framework": "^10.0|^11.0|^12.0" }, "require-dev": { - "orchestra/testbench-browser-kit": "^8.5|^9.0|^10.0", + "orchestra/testbench": "^8.5|^9.0|^10.0", "phpunit/phpunit": "^10.1|^11.0" }, "suggest": { diff --git a/tests/LaravelLocalizationTest.php b/tests/LaravelLocalizationTest.php index 9cc21fc..0a375f0 100644 --- a/tests/LaravelLocalizationTest.php +++ b/tests/LaravelLocalizationTest.php @@ -4,7 +4,15 @@ use PHPUnit\Framework\Attributes\DataProvider; use Illuminate\Support\Facades\Request; +use Illuminate\Support\Facades\Route; +use Mcamara\LaravelLocalization\Facades\LaravelLocalization as LaravelLocalizationFacade; +use Mcamara\LaravelLocalization\LanguageNegotiator; use Mcamara\LaravelLocalization\LaravelLocalization; +use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter; +use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes; +use Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect; + +use function Orchestra\Testbench\refresh_router_lookups; final class LaravelLocalizationTest extends TestCase { @@ -25,41 +33,43 @@ protected function setRoutes($locale = false) app('laravellocalization')->setLocale($locale); } - app('router')->group([ - 'prefix' => app('laravellocalization')->setLocale(), + Route::group([ + 'prefix' => LaravelLocalizationFacade::setLocale(), 'middleware' => [ - 'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes', - 'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter', + LaravelLocalizationRoutes::class, + LaravelLocalizationRedirectFilter::class, ], ], function () { - app('router')->get('/', ['as'=> 'index', function () { + Route::get('/', function () { return app('translator')->get('LaravelLocalization::routes.hello'); - }, ]); + })->name('index'); - app('router')->get('test', ['as'=> 'test', function () { + Route::get('test', function () { return app('translator')->get('LaravelLocalization::routes.test_text'); - }, ]); + })->name('test'); - app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.about'), ['as'=> 'about', function () { + Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.about'), function () { return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available'; - }, ]); + })->name('about'); - app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view'), ['as'=> 'view', function () { + Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view'), function () { return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available'; - }, ]); + })->name('view'); - app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view_project'), ['as'=> 'view_project', function () { + Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view_project'), function () { return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available'; - }, ]); + })->name('view_project'); - app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.manage'), ['as'=> 'manage', function () { + Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.manage'), function () { return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available'; - }, ]); + })->name('manage'); }); - app('router')->get('/skipped', ['as'=> 'skipped', function () { + Route::get('/skipped', function () { return Request::url(); - }, ]); + })->name('skipped'); + + refresh_router_lookups(Route::getFacadeRoot()); } /** @@ -138,18 +148,18 @@ protected function getEnvironmentSetUp($app) public function testSetLocale(): void { - $this->assertEquals(route('about'), 'http://localhost/about'); + $this->assertEquals('http://localhost/about', route('about')); $this->refreshApplication('es'); $this->assertEquals('es', app('laravellocalization')->setLocale('es')); $this->assertEquals('es', app('laravellocalization')->getCurrentLocale()); - $this->assertEquals(route('about'), 'http://localhost/acerca'); + $this->assertEquals('http://localhost/acerca', route('about')); $this->refreshApplication(); $this->assertEquals('en', app('laravellocalization')->setLocale('en')); - $this->assertEquals(route('about'), 'http://localhost/about'); + $this->assertEquals('http://localhost/about', route('about')); $this->assertNull(app('laravellocalization')->setLocale('de')); $this->assertEquals('en', app('laravellocalization')->getCurrentLocale()); @@ -250,19 +260,15 @@ public function testGetLocalizedURL(): void app('laravellocalization')->setLocale('en'); - $crawler = $this->call( - 'GET', - self::TEST_URL.'about', - [], - [], - [], - ['HTTP_ACCEPT_LANGUAGE' => 'en,es'] + $response = $this->get( + uri: self::TEST_URL.'about', + headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es'] ); - $this->assertResponseOk(); + $response->assertStatus(200); $this->assertEquals( self::TEST_URL.'es/acerca', - $crawler->getContent() + $response->getContent() ); $this->refreshApplication(); @@ -279,19 +285,15 @@ public function testGetLocalizedURL(): void app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test?a=1') ); - $crawler = $this->call( - 'GET', - app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test'), - [], - [], - [], - ['HTTP_ACCEPT_LANGUAGE' => 'en,es'] + $response = $this->get( + uri: app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test'), + headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es'] ); - $this->assertResponseOk(); + $response->assertStatus(200); $this->assertEquals( 'Test text', - $crawler->getContent() + $response->getContent() ); $this->refreshApplication('es'); @@ -368,19 +370,15 @@ public static function getRouteNameFromAPathDataProvider(): array } public function testGetLocalizedUrlForIgnoredUrls(): void { - $crawler = $this->call( - 'GET', - self::TEST_URL.'skipped', - [], - [], - [], - ['HTTP_ACCEPT_LANGUAGE' => 'en,es'] + $response = $this->get( + uri: self::TEST_URL.'skipped', + headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es'] ); - $this->assertResponseOk(); + $response->assertStatus(200); $this->assertEquals( self::TEST_URL.'skipped', - $crawler->getContent() + $response->getContent() ); } @@ -774,13 +772,11 @@ public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd = $request = $this->createMock(\Illuminate\Http\Request::class); $request->expects($this->any())->method('header')->with('Accept-Language')->willReturn($accept_string); - $negotiator = app(\Mcamara\LaravelLocalization\LanguageNegotiator::class, - [ - 'defaultLocale' => 'wrong', - 'supportedLanguages' => $full_config['supportedLocales'], - 'request' => $request - ] - ); + $negotiator = app(LanguageNegotiator::class, [ + 'defaultLocale' => 'wrong', + 'supportedLanguages' => $full_config['supportedLocales'], + 'request' => $request + ]); $language = $negotiator->negotiateLanguage(); @@ -829,13 +825,11 @@ public function testLanguageNegotiationWithMapping(): void { $request = $this->createMock(\Illuminate\Http\Request::class); $request->expects($this->any())->method('header')->with('Accept-Language')->willReturn($accept_string); - $negotiator = app(\Mcamara\LaravelLocalization\LanguageNegotiator::class, - [ - 'defaultLocale' => 'wrong', - 'supportedLanguages' => $full_config['supportedLocales'], - 'request' => $request - ] - ); + $negotiator = app(LanguageNegotiator::class, [ + 'defaultLocale' => 'wrong', + 'supportedLanguages' => $full_config['supportedLocales'], + 'request' => $request + ]); $language = $negotiator->negotiateLanguage(); @@ -859,39 +853,28 @@ public function testSetLocaleWithMapping(): void $this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en')); } - - public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale() { - app('router')->group([ - 'prefix' => app('laravellocalization')->setLocale(), + Route::group([ + 'prefix' => LaravelLocalizationFacade::setLocale(), 'middleware' => [ - 'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter', - 'Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect', + LaravelLocalizationRedirectFilter::class, + LocaleCookieRedirect::class, ], - ], function (){ - app('router')->get('/', ['as'=> 'index', function () { + ], function () { + Route::get('/', function () { return 'Index page'; - }, ]); + })->name('index'); }); app('config')->set('laravellocalization.hideDefaultLocaleInURL', true); $savedLocale = 'es'; - $crawler = $this->call( - 'GET', - self::TEST_URL, - [], - ['locale' => $savedLocale], - [], - [] - ); - - $this->assertResponseStatus(302); - $this->assertRedirectedTo(self::TEST_URL . $savedLocale); + $response = $this->withUnencryptedCookie('locale', $savedLocale)->get(self::TEST_URL); - $localeCookie = $crawler->headers->getCookies()[0]; - $this->assertEquals($savedLocale, $localeCookie->getValue()); + $response->assertStatus(302); + $response->assertRedirect(self::TEST_URL . $savedLocale); + $response->assertPlainCookie('locale', $savedLocale); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 1c94233..b0e8193 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,7 +4,7 @@ use Mcamara\LaravelLocalization\Facades\LaravelLocalization; use Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider; -use Orchestra\Testbench\BrowserKit\TestCase as BaseTestCase; +use Orchestra\Testbench\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase {