Skip to content

Commit 1b49057

Browse files
Add support Laravel 11 and PHPUnit 10 (#107)
* Bump PHPUnit dependencies * Ignore PHPUnit cache folder * Adopt PHP attributes in test classes * Add return types to test methods * Define test classes as `final` * Update .gitignore * Support only Laravel 10 and newer * Migrate PHPUnit XML * Add instructions to upgrade to Laravel 11 * Add Laravel 11 middleware instructions to README --------- Co-authored-by: Shift <[email protected]>
1 parent fff1b46 commit 1b49057

16 files changed

+370
-337
lines changed

.github/workflows/run-tests.yml

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,17 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
php: [ 8.0, 8.1 ]
12-
laravel: [ 8.*, 9.* , 10.*]
11+
php: [ 8.1, 8.2, 8.3 ]
12+
laravel: [ 10.* , 11.*]
1313
dependency-version: [ prefer-stable ]
1414
exclude:
15-
- laravel: 10.*
16-
php: 8.0
15+
- laravel: 11.*
16+
php: 8.1
1717
include:
18-
- laravel: 7.*
19-
php: 7.2
20-
testbench: 5.*
21-
- laravel: 7.*
22-
php: 8.0
23-
testbench: 5.*
24-
- laravel: 8.*
25-
php: 7.3
26-
testbench: 6.*
27-
- laravel: 8.*
28-
testbench: 6.*
29-
- laravel: 9.*
30-
testbench: 7.*
3118
- laravel: 10.*
3219
testbench: 8.*
20+
- laravel: 11.*
21+
testbench: 9.*
3322

3423
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
3524

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
/phpunit.xml
33
composer.lock
4+
/.phpunit.cache
45
/.phpunit.result.cache

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Laravel Localized Routes
22

33
[![GitHub release](https://img.shields.io/github/release/codezero-be/laravel-localized-routes.svg?style=flat-square)](https://github.com/codezero-be/laravel-localized-routes/releases)
4-
[![Laravel](https://img.shields.io/badge/laravel-10-red?style=flat-square&logo=laravel&logoColor=white)](https://laravel.com)
4+
[![Laravel](https://img.shields.io/badge/laravel-11-red?style=flat-square&logo=laravel&logoColor=white)](https://laravel.com)
55
[![License](https://img.shields.io/packagist/l/codezero/laravel-localized-routes.svg?style=flat-square)](LICENSE.md)
66
[![Build Status](https://img.shields.io/github/actions/workflow/status/codezero-be/laravel-localized-routes/run-tests.yml?style=flat-square&logo=github&logoColor=white&label=tests)](https://github.com/codezero-be/laravel-localized-routes/actions)
77
[![Code Coverage](https://img.shields.io/codacy/coverage/a5db8a1321664e67900c96eadc575ece/master?style=flat-square)](https://app.codacy.com/gh/codezero-be/laravel-localized-routes)
@@ -53,8 +53,8 @@ A convenient way to set up and use localized routes in a Laravel app.
5353

5454
## ✅ Requirements
5555

56-
- PHP >= 7.2.5
57-
- Laravel >= 7.0
56+
- PHP >= 8.1
57+
- Laravel >= 10
5858
- Composer ^2.3 (for [codezero/composer-preload-files](https://github.com/codezero-be/composer-preload-files))
5959

6060
## ⬆ Upgrade
@@ -153,30 +153,43 @@ Route::localized(function () {
153153
## 🧩 Add Middleware to Update App Locale
154154

155155
By default, the app locale will always be what you configured in `config/app.php`.
156-
To automatically update the app locale, you need to register the middleware.
156+
To automatically update the app locale, you need to register the middleware in the `web` middleware group.
157+
Make sure to add it after `StartSession` and before `SubstituteBindings`.
157158

158-
Add the middleware to the `web` middleware group in `app/Http/Kernel.php`:
159+
The order of the middleware is important if you are using localized route keys (translated slugs)!
160+
The session needs to be active when setting the locale, and the locale needs to be set when substituting the route bindings.
161+
162+
### Laravel 11 and newer:
163+
164+
Add the middleware to the `web` middleware group in `bootstrap/app.php`.
159165

160166
```php
161-
protected $middlewareGroups = [
162-
'web' => [
163-
//...
164-
\CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
165-
],
166-
];
167+
// bootstrap/app.php
168+
->withMiddleware(function (Middleware $middleware) {
169+
$middleware->web(remove: [
170+
\Illuminate\Routing\Middleware\SubstituteBindings::class,
171+
]);
172+
$middleware->web(append: [
173+
\CodeZero\Localizer\Middleware\SetLocale::class,
174+
\Illuminate\Routing\Middleware\SubstituteBindings::class,
175+
]);
176+
})
167177
```
168178

169-
You also need to add the middleware to the `$middlewarePriority` array in `app/Http/Kernel.php`.
170-
If you don't see the `$middlewarePriority` array, you can copy it from the parent class `Illuminate\Foundation\Http\Kernel`.
179+
### Laravel 10:
171180

172-
Make sure to add it after `StartSession` and before `SubstituteBindings` to trigger it in the correct order:
181+
Add the middleware to the `web` middleware group in `app/Http/Kernel.php`.
173182

174183
```php
175-
protected $middlewarePriority = [
176-
\Illuminate\Session\Middleware\StartSession::class, // <= after this
177-
//...
178-
\CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
179-
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
184+
// app/Http/Kernel.php
185+
protected $middlewareGroups = [
186+
'web' => [
187+
//...
188+
\Illuminate\Session\Middleware\StartSession::class, // <= after this
189+
//...
190+
\CodeZero\Localizer\Middleware\SetLocale::class,
191+
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
192+
],
180193
];
181194
```
182195

UPGRADE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# Upgrade Guide
22

3+
## Upgrading To 4.0 From 3.x
4+
5+
### ➡ Minimum Requirements Updated
6+
7+
Due to PHP and PHPUnit version constraints with Laravel 11, we dropped support for Laravel 7.x, 8.x and 9.x.
8+
9+
- The minimum PHP version required is now 8.1
10+
- The minimum Laravel version required is now 10
11+
12+
---
13+
14+
### ➡ Re-register Middleware
15+
16+
Laravel 11 no longer has a `app/Http/Kernel.php` to register middleware.
17+
This is now handled in `bootstrap/app.php`.
18+
19+
🔸 **Actions Required**
20+
21+
If you use Laravel 11, register the middleware in `bootstrap/app.php` as described in the README.
22+
323
## Upgrading To 3.0 From 2.x
424

525
This upgrade contains a number of small but breaking changes, as well as a huge internal makeover.

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
}
2121
],
2222
"require": {
23-
"php": "^7.2.5|^8.0",
23+
"php": "^8.1",
2424
"codezero/browser-locale": "^3.0",
2525
"codezero/composer-preload-files": "^1.0",
26-
"codezero/laravel-uri-translator": "^1.0",
26+
"codezero/laravel-uri-translator": "^2.0",
2727
"codezero/php-url-builder": "^1.0",
28-
"illuminate/support": "^7.0|^8.0|^9.0|^10.0"
28+
"illuminate/support": "^10.0|^11.0"
2929
},
3030
"require-dev": {
3131
"mockery/mockery": "^1.3.3",
32-
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0",
33-
"phpunit/phpunit": "^8.0|^9.0"
32+
"orchestra/testbench": "^8.0|^9.0",
33+
"phpunit/phpunit": "^10.5"
3434
},
3535
"scripts": {
3636
"test": "phpunit"

phpunit.xml.dist

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
backupGlobals="false"
44
bootstrap="vendor/autoload.php"
55
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
96
processIsolation="false"
10-
stopOnFailure="false">
7+
stopOnFailure="false"
8+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
9+
cacheDirectory=".phpunit.cache"
10+
backupStaticProperties="false">
1111
<testsuites>
1212
<testsuite name="Unit">
1313
<directory suffix="Test.php">./tests/Unit</directory>
@@ -16,15 +16,15 @@
1616
<directory suffix="Test.php">./tests/Feature</directory>
1717
</testsuite>
1818
</testsuites>
19-
<filter>
20-
<whitelist processUncoveredFilesFromWhitelist="true">
21-
<directory suffix=".php">./src</directory>
22-
</whitelist>
23-
</filter>
2419
<php>
2520
<env name="APP_ENV" value="testing"/>
2621
<env name="CACHE_DRIVER" value="array"/>
2722
<env name="SESSION_DRIVER" value="array"/>
2823
<env name="QUEUE_DRIVER" value="sync"/>
2924
</php>
25+
<source>
26+
<include>
27+
<directory suffix=".php">./src</directory>
28+
</include>
29+
</source>
3030
</phpunit>

tests/Feature/Macros/Route/IsLocalizedMacroTest.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace CodeZero\LocalizedRoutes\Tests\Feature\Macros\Route;
44

5+
use PHPUnit\Framework\Attributes\Test;
56
use CodeZero\LocalizedRoutes\Tests\TestCase;
67
use Illuminate\Support\Facades\Route;
78

8-
class IsLocalizedMacroTest extends TestCase
9+
final class IsLocalizedMacroTest extends TestCase
910
{
10-
/** @test */
11-
public function it_checks_if_the_current_route_is_localized()
11+
#[Test]
12+
public function it_checks_if_the_current_route_is_localized(): void
1213
{
1314
$this->withoutExceptionHandling();
1415
$this->setSupportedLocales(['en', 'nl']);
@@ -36,8 +37,8 @@ public function it_checks_if_the_current_route_is_localized()
3637
$this->assertEquals('false', $response->original);
3738
}
3839

39-
/** @test */
40-
public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix()
40+
#[Test]
41+
public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix(): void
4142
{
4243
$this->withoutExceptionHandling();
4344
$this->setSupportedLocales(['en', 'nl']);
@@ -69,8 +70,8 @@ public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix
6970
$this->assertEquals('false', $response->original);
7071
}
7172

72-
/** @test */
73-
public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale_prefix()
73+
#[Test]
74+
public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale_prefix(): void
7475
{
7576
$this->withoutExceptionHandling();
7677
$this->setSupportedLocales(['en', 'nl']);
@@ -90,8 +91,8 @@ public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale
9091
$this->assertEquals('false', $response->original);
9192
}
9293

93-
/** @test */
94-
public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_any_locale_prefix()
94+
#[Test]
95+
public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_any_locale_prefix(): void
9596
{
9697
$this->withoutExceptionHandling();
9798
$this->setSupportedLocales(['en', 'nl']);
@@ -135,8 +136,8 @@ public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of
135136
$this->assertEquals('false', $response->original);
136137
}
137138

138-
/** @test */
139-
public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_a_specific_locale_prefix()
139+
#[Test]
140+
public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_a_specific_locale_prefix(): void
140141
{
141142
$this->withoutExceptionHandling();
142143
$this->setSupportedLocales(['en', 'nl']);
@@ -180,8 +181,8 @@ public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of
180181
$this->assertEquals('false', $response->original);
181182
}
182183

183-
/** @test */
184-
public function it_checks_if_the_current_route_has_a_name_with_a_locale_prefix_in_an_array()
184+
#[Test]
185+
public function it_checks_if_the_current_route_has_a_name_with_a_locale_prefix_in_an_array(): void
185186
{
186187
$this->withoutExceptionHandling();
187188
$this->setSupportedLocales(['en', 'nl', 'fr']);

tests/Feature/Macros/Route/LocalizedMacroTest.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace CodeZero\LocalizedRoutes\Tests\Feature\Macros\Route;
44

5+
use PHPUnit\Framework\Attributes\Test;
56
use CodeZero\LocalizedRoutes\Tests\TestCase;
67
use Illuminate\Support\Facades\Config;
78
use Illuminate\Support\Facades\Route;
89

9-
class LocalizedMacroTest extends TestCase
10+
final class LocalizedMacroTest extends TestCase
1011
{
11-
/** @test */
12-
public function it_registers_a_route_for_each_locale()
12+
#[Test]
13+
public function it_registers_a_route_for_each_locale(): void
1314
{
1415
$this->setSupportedLocales(['en', 'nl']);
1516

@@ -35,8 +36,8 @@ public function it_registers_a_route_for_each_locale()
3536
$this->assertContains('nl/route', $uris);
3637
}
3738

38-
/** @test */
39-
public function it_registers_a_root_route_for_each_locale()
39+
#[Test]
40+
public function it_registers_a_root_route_for_each_locale(): void
4041
{
4142
$this->setSupportedLocales(['en', 'nl']);
4243

@@ -58,8 +59,8 @@ public function it_registers_a_root_route_for_each_locale()
5859
$this->assertContains('nl', $uris);
5960
}
6061

61-
/** @test */
62-
public function it_registers_a_url_without_prefix_for_a_configured_main_locale()
62+
#[Test]
63+
public function it_registers_a_url_without_prefix_for_a_configured_main_locale(): void
6364
{
6465
$this->setSupportedLocales(['en', 'nl']);
6566
$this->setOmittedLocale('en');
@@ -82,8 +83,8 @@ public function it_registers_a_url_without_prefix_for_a_configured_main_locale()
8283
$this->assertContains('nl/about', $uris);
8384
}
8485

85-
/** @test */
86-
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale()
86+
#[Test]
87+
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale(): void
8788
{
8889
$this->setSupportedLocales(['en', 'nl']);
8990
$this->setOmittedLocale('en');
@@ -99,8 +100,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co
99100
);
100101
}
101102

102-
/** @test */
103-
public function it_maps_a_custom_slug_to_each_locale()
103+
#[Test]
104+
public function it_maps_a_custom_slug_to_each_locale(): void
104105
{
105106
$this->setSupportedLocales([
106107
'en' => 'english',
@@ -123,8 +124,8 @@ public function it_maps_a_custom_slug_to_each_locale()
123124
$this->assertEquals('dutch', $route->uri);
124125
}
125126

126-
/** @test */
127-
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_custom_slugs()
127+
#[Test]
128+
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_custom_slugs(): void
128129
{
129130
$this->setSupportedLocales([
130131
'en' => 'english',
@@ -143,8 +144,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co
143144
);
144145
}
145146

146-
/** @test */
147-
public function it_maps_a_custom_domain_to_each_locale()
147+
#[Test]
148+
public function it_maps_a_custom_domain_to_each_locale(): void
148149
{
149150
$this->setSupportedLocales([
150151
'en' => 'english-domain.com',
@@ -169,8 +170,8 @@ public function it_maps_a_custom_domain_to_each_locale()
169170
$this->assertEquals('/', $route->uri);
170171
}
171172

172-
/** @test */
173-
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_domains()
173+
#[Test]
174+
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_domains(): void
174175
{
175176
$this->setSupportedLocales([
176177
'en' => 'english-domain.com',
@@ -208,8 +209,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co
208209
$this->assertEquals('{slug}', $route->uri);
209210
}
210211

211-
/** @test */
212-
public function it_uses_scoped_config_options()
212+
#[Test]
213+
public function it_uses_scoped_config_options(): void
213214
{
214215
$this->setSupportedLocales(['en']);
215216
$this->setOmittedLocale(null);

0 commit comments

Comments
 (0)