Skip to content

Add support Laravel 11 and PHPUnit 10 #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,17 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ 8.0, 8.1 ]
laravel: [ 8.*, 9.* , 10.*]
php: [ 8.1, 8.2, 8.3 ]
laravel: [ 10.* , 11.*]
dependency-version: [ prefer-stable ]
exclude:
- laravel: 10.*
php: 8.0
- laravel: 11.*
php: 8.1
include:
- laravel: 7.*
php: 7.2
testbench: 5.*
- laravel: 7.*
php: 8.0
testbench: 5.*
- laravel: 8.*
php: 7.3
testbench: 6.*
- laravel: 8.*
testbench: 6.*
- laravel: 9.*
testbench: 7.*
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*

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

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
/phpunit.xml
composer.lock
/.phpunit.cache
/.phpunit.result.cache
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Laravel Localized Routes

[![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)
[![Laravel](https://img.shields.io/badge/laravel-10-red?style=flat-square&logo=laravel&logoColor=white)](https://laravel.com)
[![Laravel](https://img.shields.io/badge/laravel-11-red?style=flat-square&logo=laravel&logoColor=white)](https://laravel.com)
[![License](https://img.shields.io/packagist/l/codezero/laravel-localized-routes.svg?style=flat-square)](LICENSE.md)
[![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)
[![Code Coverage](https://img.shields.io/codacy/coverage/a5db8a1321664e67900c96eadc575ece/master?style=flat-square)](https://app.codacy.com/gh/codezero-be/laravel-localized-routes)
Expand Down Expand Up @@ -53,8 +53,8 @@ A convenient way to set up and use localized routes in a Laravel app.

## ✅ Requirements

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

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

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

Add the middleware to the `web` middleware group in `app/Http/Kernel.php`:
The order of the middleware is important if you are using localized route keys (translated slugs)!
The session needs to be active when setting the locale, and the locale needs to be set when substituting the route bindings.

### Laravel 11 and newer:

Add the middleware to the `web` middleware group in `bootstrap/app.php`.

```php
protected $middlewareGroups = [
'web' => [
//...
\CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
],
];
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->web(remove: [
\Illuminate\Routing\Middleware\SubstituteBindings::class,
]);
$middleware->web(append: [
\CodeZero\Localizer\Middleware\SetLocale::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
]);
})
```

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

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

```php
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class, // <= after this
//...
\CodeZero\LocalizedRoutes\Middleware\SetLocale::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
// app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
//...
\Illuminate\Session\Middleware\StartSession::class, // <= after this
//...
\CodeZero\Localizer\Middleware\SetLocale::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
],
];
```

Expand Down
20 changes: 20 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Upgrade Guide

## Upgrading To 4.0 From 3.x

### ➡ Minimum Requirements Updated

Due to PHP and PHPUnit version constraints with Laravel 11, we dropped support for Laravel 7.x, 8.x and 9.x.

- The minimum PHP version required is now 8.1
- The minimum Laravel version required is now 10

---

### ➡ Re-register Middleware

Laravel 11 no longer has a `app/Http/Kernel.php` to register middleware.
This is now handled in `bootstrap/app.php`.

🔸 **Actions Required**

If you use Laravel 11, register the middleware in `bootstrap/app.php` as described in the README.

## Upgrading To 3.0 From 2.x

This upgrade contains a number of small but breaking changes, as well as a huge internal makeover.
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
}
],
"require": {
"php": "^7.2.5|^8.0",
"php": "^8.1",
"codezero/browser-locale": "^3.0",
"codezero/composer-preload-files": "^1.0",
"codezero/laravel-uri-translator": "^1.0",
"codezero/laravel-uri-translator": "^2.0",
"codezero/php-url-builder": "^1.0",
"illuminate/support": "^7.0|^8.0|^9.0|^10.0"
"illuminate/support": "^10.0|^11.0"
},
"require-dev": {
"mockery/mockery": "^1.3.3",
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0",
"phpunit/phpunit": "^8.0|^9.0"
"orchestra/testbench": "^8.0|^9.0",
"phpunit/phpunit": "^10.5"
},
"scripts": {
"test": "phpunit"
Expand Down
22 changes: 11 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
Expand All @@ -16,15 +16,15 @@
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
27 changes: 14 additions & 13 deletions tests/Feature/Macros/Route/IsLocalizedMacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

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

use PHPUnit\Framework\Attributes\Test;
use CodeZero\LocalizedRoutes\Tests\TestCase;
use Illuminate\Support\Facades\Route;

class IsLocalizedMacroTest extends TestCase
final class IsLocalizedMacroTest extends TestCase
{
/** @test */
public function it_checks_if_the_current_route_is_localized()
#[Test]
public function it_checks_if_the_current_route_is_localized(): void
{
$this->withoutExceptionHandling();
$this->setSupportedLocales(['en', 'nl']);
Expand Down Expand Up @@ -36,8 +37,8 @@ public function it_checks_if_the_current_route_is_localized()
$this->assertEquals('false', $response->original);
}

/** @test */
public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix()
#[Test]
public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix(): void
{
$this->withoutExceptionHandling();
$this->setSupportedLocales(['en', 'nl']);
Expand Down Expand Up @@ -69,8 +70,8 @@ public function it_checks_if_the_current_route_has_a_name_with_any_locale_prefix
$this->assertEquals('false', $response->original);
}

/** @test */
public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale_prefix()
#[Test]
public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale_prefix(): void
{
$this->withoutExceptionHandling();
$this->setSupportedLocales(['en', 'nl']);
Expand All @@ -90,8 +91,8 @@ public function it_checks_if_the_current_route_has_a_name_with_a_specific_locale
$this->assertEquals('false', $response->original);
}

/** @test */
public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_any_locale_prefix()
#[Test]
public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_any_locale_prefix(): void
{
$this->withoutExceptionHandling();
$this->setSupportedLocales(['en', 'nl']);
Expand Down Expand Up @@ -135,8 +136,8 @@ public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of
$this->assertEquals('false', $response->original);
}

/** @test */
public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_a_specific_locale_prefix()
#[Test]
public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of_names_with_a_specific_locale_prefix(): void
{
$this->withoutExceptionHandling();
$this->setSupportedLocales(['en', 'nl']);
Expand Down Expand Up @@ -180,8 +181,8 @@ public function it_checks_if_the_current_route_has_a_name_that_is_in_an_array_of
$this->assertEquals('false', $response->original);
}

/** @test */
public function it_checks_if_the_current_route_has_a_name_with_a_locale_prefix_in_an_array()
#[Test]
public function it_checks_if_the_current_route_has_a_name_with_a_locale_prefix_in_an_array(): void
{
$this->withoutExceptionHandling();
$this->setSupportedLocales(['en', 'nl', 'fr']);
Expand Down
39 changes: 20 additions & 19 deletions tests/Feature/Macros/Route/LocalizedMacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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

use PHPUnit\Framework\Attributes\Test;
use CodeZero\LocalizedRoutes\Tests\TestCase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;

class LocalizedMacroTest extends TestCase
final class LocalizedMacroTest extends TestCase
{
/** @test */
public function it_registers_a_route_for_each_locale()
#[Test]
public function it_registers_a_route_for_each_locale(): void
{
$this->setSupportedLocales(['en', 'nl']);

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

/** @test */
public function it_registers_a_root_route_for_each_locale()
#[Test]
public function it_registers_a_root_route_for_each_locale(): void
{
$this->setSupportedLocales(['en', 'nl']);

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

/** @test */
public function it_registers_a_url_without_prefix_for_a_configured_main_locale()
#[Test]
public function it_registers_a_url_without_prefix_for_a_configured_main_locale(): void
{
$this->setSupportedLocales(['en', 'nl']);
$this->setOmittedLocale('en');
Expand All @@ -82,8 +83,8 @@ public function it_registers_a_url_without_prefix_for_a_configured_main_locale()
$this->assertContains('nl/about', $uris);
}

/** @test */
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale()
#[Test]
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale(): void
{
$this->setSupportedLocales(['en', 'nl']);
$this->setOmittedLocale('en');
Expand All @@ -99,8 +100,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co
);
}

/** @test */
public function it_maps_a_custom_slug_to_each_locale()
#[Test]
public function it_maps_a_custom_slug_to_each_locale(): void
{
$this->setSupportedLocales([
'en' => 'english',
Expand All @@ -123,8 +124,8 @@ public function it_maps_a_custom_slug_to_each_locale()
$this->assertEquals('dutch', $route->uri);
}

/** @test */
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_custom_slugs()
#[Test]
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_custom_slugs(): void
{
$this->setSupportedLocales([
'en' => 'english',
Expand All @@ -143,8 +144,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co
);
}

/** @test */
public function it_maps_a_custom_domain_to_each_locale()
#[Test]
public function it_maps_a_custom_domain_to_each_locale(): void
{
$this->setSupportedLocales([
'en' => 'english-domain.com',
Expand All @@ -169,8 +170,8 @@ public function it_maps_a_custom_domain_to_each_locale()
$this->assertEquals('/', $route->uri);
}

/** @test */
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_domains()
#[Test]
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_domains(): void
{
$this->setSupportedLocales([
'en' => 'english-domain.com',
Expand Down Expand Up @@ -208,8 +209,8 @@ public function it_registers_routes_in_the_correct_order_without_prefix_for_a_co
$this->assertEquals('{slug}', $route->uri);
}

/** @test */
public function it_uses_scoped_config_options()
#[Test]
public function it_uses_scoped_config_options(): void
{
$this->setSupportedLocales(['en']);
$this->setOmittedLocale(null);
Expand Down
Loading