Skip to content

Commit ee57401

Browse files
test: cleanup (#949)
* docs: improve testing docs and provide a pest example * refactor: replace testbench-browser-kit with testbench * refactor: apply correct order of arguments for assertEquals * refactor: import namespaces and route registration * tests: refresh router lookups after registering new routes * ci: use recommended testbench versions to avoid failing tests
1 parent 89171b8 commit ee57401

File tree

4 files changed

+71
-88
lines changed

4 files changed

+71
-88
lines changed

.github/workflows/run-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ jobs:
2020
- laravel: 12.*
2121
testbench: 10.*
2222
- laravel: 11.*
23-
testbench: 9.*
23+
testbench: ^9.9
2424
- laravel: 10.*
25-
testbench: 8.*
25+
testbench: ^8.31
2626

2727
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
2828

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"laravel/framework": "^10.0|^11.0|^12.0"
2121
},
2222
"require-dev": {
23-
"orchestra/testbench-browser-kit": "^8.5|^9.0|^10.0",
23+
"orchestra/testbench": "^8.5|^9.0|^10.0",
2424
"phpunit/phpunit": "^10.1|^11.0"
2525
},
2626
"suggest": {

tests/LaravelLocalizationTest.php

Lines changed: 67 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66
use Illuminate\Support\Facades\Request;
7+
use Illuminate\Support\Facades\Route;
8+
use Mcamara\LaravelLocalization\Facades\LaravelLocalization as LaravelLocalizationFacade;
9+
use Mcamara\LaravelLocalization\LanguageNegotiator;
710
use Mcamara\LaravelLocalization\LaravelLocalization;
11+
use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter;
12+
use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes;
13+
use Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect;
14+
15+
use function Orchestra\Testbench\refresh_router_lookups;
816

917
final class LaravelLocalizationTest extends TestCase
1018
{
@@ -25,41 +33,43 @@ protected function setRoutes($locale = false)
2533
app('laravellocalization')->setLocale($locale);
2634
}
2735

28-
app('router')->group([
29-
'prefix' => app('laravellocalization')->setLocale(),
36+
Route::group([
37+
'prefix' => LaravelLocalizationFacade::setLocale(),
3038
'middleware' => [
31-
'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes',
32-
'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter',
39+
LaravelLocalizationRoutes::class,
40+
LaravelLocalizationRedirectFilter::class,
3341
],
3442
], function () {
35-
app('router')->get('/', ['as'=> 'index', function () {
43+
Route::get('/', function () {
3644
return app('translator')->get('LaravelLocalization::routes.hello');
37-
}, ]);
45+
})->name('index');
3846

39-
app('router')->get('test', ['as'=> 'test', function () {
47+
Route::get('test', function () {
4048
return app('translator')->get('LaravelLocalization::routes.test_text');
41-
}, ]);
49+
})->name('test');
4250

43-
app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.about'), ['as'=> 'about', function () {
51+
Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.about'), function () {
4452
return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available';
45-
}, ]);
53+
})->name('about');
4654

47-
app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view'), ['as'=> 'view', function () {
55+
Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view'), function () {
4856
return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available';
49-
}, ]);
57+
})->name('view');
5058

51-
app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view_project'), ['as'=> 'view_project', function () {
59+
Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.view_project'), function () {
5260
return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available';
53-
}, ]);
61+
})->name('view_project');
5462

55-
app('router')->get(app('laravellocalization')->transRoute('LaravelLocalization::routes.manage'), ['as'=> 'manage', function () {
63+
Route::get(app('laravellocalization')->transRoute('LaravelLocalization::routes.manage'), function () {
5664
return app('laravellocalization')->getLocalizedURL('es') ?: 'Not url available';
57-
}, ]);
65+
})->name('manage');
5866
});
5967

60-
app('router')->get('/skipped', ['as'=> 'skipped', function () {
68+
Route::get('/skipped', function () {
6169
return Request::url();
62-
}, ]);
70+
})->name('skipped');
71+
72+
refresh_router_lookups(Route::getFacadeRoot());
6373
}
6474

6575
/**
@@ -138,18 +148,18 @@ protected function getEnvironmentSetUp($app)
138148

139149
public function testSetLocale(): void
140150
{
141-
$this->assertEquals(route('about'), 'http://localhost/about');
151+
$this->assertEquals('http://localhost/about', route('about'));
142152

143153
$this->refreshApplication('es');
144154
$this->assertEquals('es', app('laravellocalization')->setLocale('es'));
145155
$this->assertEquals('es', app('laravellocalization')->getCurrentLocale());
146-
$this->assertEquals(route('about'), 'http://localhost/acerca');
156+
$this->assertEquals('http://localhost/acerca', route('about'));
147157

148158
$this->refreshApplication();
149159

150160
$this->assertEquals('en', app('laravellocalization')->setLocale('en'));
151161

152-
$this->assertEquals(route('about'), 'http://localhost/about');
162+
$this->assertEquals('http://localhost/about', route('about'));
153163

154164
$this->assertNull(app('laravellocalization')->setLocale('de'));
155165
$this->assertEquals('en', app('laravellocalization')->getCurrentLocale());
@@ -250,19 +260,15 @@ public function testGetLocalizedURL(): void
250260

251261
app('laravellocalization')->setLocale('en');
252262

253-
$crawler = $this->call(
254-
'GET',
255-
self::TEST_URL.'about',
256-
[],
257-
[],
258-
[],
259-
['HTTP_ACCEPT_LANGUAGE' => 'en,es']
263+
$response = $this->get(
264+
uri: self::TEST_URL.'about',
265+
headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es']
260266
);
261267

262-
$this->assertResponseOk();
268+
$response->assertStatus(200);
263269
$this->assertEquals(
264270
self::TEST_URL.'es/acerca',
265-
$crawler->getContent()
271+
$response->getContent()
266272
);
267273

268274
$this->refreshApplication();
@@ -279,19 +285,15 @@ public function testGetLocalizedURL(): void
279285
app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test?a=1')
280286
);
281287

282-
$crawler = $this->call(
283-
'GET',
284-
app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test'),
285-
[],
286-
[],
287-
[],
288-
['HTTP_ACCEPT_LANGUAGE' => 'en,es']
288+
$response = $this->get(
289+
uri: app('laravellocalization')->getLocalizedURL('en', self::TEST_URL.'test'),
290+
headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es']
289291
);
290292

291-
$this->assertResponseOk();
293+
$response->assertStatus(200);
292294
$this->assertEquals(
293295
'Test text',
294-
$crawler->getContent()
296+
$response->getContent()
295297
);
296298

297299
$this->refreshApplication('es');
@@ -368,19 +370,15 @@ public static function getRouteNameFromAPathDataProvider(): array
368370
}
369371

370372
public function testGetLocalizedUrlForIgnoredUrls(): void {
371-
$crawler = $this->call(
372-
'GET',
373-
self::TEST_URL.'skipped',
374-
[],
375-
[],
376-
[],
377-
['HTTP_ACCEPT_LANGUAGE' => 'en,es']
373+
$response = $this->get(
374+
uri: self::TEST_URL.'skipped',
375+
headers: ['HTTP_ACCEPT_LANGUAGE' => 'en,es']
378376
);
379377

380-
$this->assertResponseOk();
378+
$response->assertStatus(200);
381379
$this->assertEquals(
382380
self::TEST_URL.'skipped',
383-
$crawler->getContent()
381+
$response->getContent()
384382
);
385383
}
386384

@@ -774,13 +772,11 @@ public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd =
774772
$request = $this->createMock(\Illuminate\Http\Request::class);
775773
$request->expects($this->any())->method('header')->with('Accept-Language')->willReturn($accept_string);
776774

777-
$negotiator = app(\Mcamara\LaravelLocalization\LanguageNegotiator::class,
778-
[
779-
'defaultLocale' => 'wrong',
780-
'supportedLanguages' => $full_config['supportedLocales'],
781-
'request' => $request
782-
]
783-
);
775+
$negotiator = app(LanguageNegotiator::class, [
776+
'defaultLocale' => 'wrong',
777+
'supportedLanguages' => $full_config['supportedLocales'],
778+
'request' => $request
779+
]);
784780

785781
$language = $negotiator->negotiateLanguage();
786782

@@ -829,13 +825,11 @@ public function testLanguageNegotiationWithMapping(): void {
829825
$request = $this->createMock(\Illuminate\Http\Request::class);
830826
$request->expects($this->any())->method('header')->with('Accept-Language')->willReturn($accept_string);
831827

832-
$negotiator = app(\Mcamara\LaravelLocalization\LanguageNegotiator::class,
833-
[
834-
'defaultLocale' => 'wrong',
835-
'supportedLanguages' => $full_config['supportedLocales'],
836-
'request' => $request
837-
]
838-
);
828+
$negotiator = app(LanguageNegotiator::class, [
829+
'defaultLocale' => 'wrong',
830+
'supportedLanguages' => $full_config['supportedLocales'],
831+
'request' => $request
832+
]);
839833

840834
$language = $negotiator->negotiateLanguage();
841835

@@ -859,39 +853,28 @@ public function testSetLocaleWithMapping(): void
859853
$this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en'));
860854
}
861855

862-
863-
864856
public function testRedirectWithHiddenDefaultLocaleInUrlAndSavedLocale()
865857
{
866-
app('router')->group([
867-
'prefix' => app('laravellocalization')->setLocale(),
858+
Route::group([
859+
'prefix' => LaravelLocalizationFacade::setLocale(),
868860
'middleware' => [
869-
'Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter',
870-
'Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect',
861+
LaravelLocalizationRedirectFilter::class,
862+
LocaleCookieRedirect::class,
871863
],
872-
], function (){
873-
app('router')->get('/', ['as'=> 'index', function () {
864+
], function () {
865+
Route::get('/', function () {
874866
return 'Index page';
875-
}, ]);
867+
})->name('index');
876868
});
877869

878870
app('config')->set('laravellocalization.hideDefaultLocaleInURL', true);
879871

880872
$savedLocale = 'es';
881873

882-
$crawler = $this->call(
883-
'GET',
884-
self::TEST_URL,
885-
[],
886-
['locale' => $savedLocale],
887-
[],
888-
[]
889-
);
890-
891-
$this->assertResponseStatus(302);
892-
$this->assertRedirectedTo(self::TEST_URL . $savedLocale);
874+
$response = $this->withUnencryptedCookie('locale', $savedLocale)->get(self::TEST_URL);
893875

894-
$localeCookie = $crawler->headers->getCookies()[0];
895-
$this->assertEquals($savedLocale, $localeCookie->getValue());
876+
$response->assertStatus(302);
877+
$response->assertRedirect(self::TEST_URL . $savedLocale);
878+
$response->assertPlainCookie('locale', $savedLocale);
896879
}
897880
}

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
66
use Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider;
7-
use Orchestra\Testbench\BrowserKit\TestCase as BaseTestCase;
7+
use Orchestra\Testbench\TestCase as BaseTestCase;
88

99
abstract class TestCase extends BaseTestCase
1010
{

0 commit comments

Comments
 (0)