-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Laravel-Lang/1.x
Added new `LocalizationByModel` middleware
- Loading branch information
Showing
13 changed files
with
293 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\Routes\Middlewares; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\Request; | ||
|
||
class LocalizationByModel extends Middleware | ||
{ | ||
public function __construct( | ||
protected ?string $guard = null, | ||
) {} | ||
|
||
protected function detect(Request $request): bool|float|int|string|null | ||
{ | ||
if ($this->has($user = $this->user($request))) { | ||
return $user->getAttribute($this->attribute()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function has(?Model $user): bool | ||
{ | ||
return $user && $this->hasAttribute($user); | ||
} | ||
|
||
protected function user(Request $request): ?Model | ||
{ | ||
return $request->user($this->guard); | ||
} | ||
|
||
protected function attribute(): string | ||
{ | ||
return $this->names()->column; | ||
} | ||
|
||
protected function hasAttribute(Model $user): bool | ||
{ | ||
if (method_exists($user, 'hasAttribute')) { | ||
return $user->hasAttribute($this->attribute()); | ||
} | ||
|
||
return array_key_exists($this->attribute(), $user->getAttributes()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tests\Constants\LocaleValue; | ||
|
||
use function Pest\Laravel\actingAs; | ||
use function Pest\Laravel\getJson; | ||
|
||
test('main locale', function () { | ||
actingAs(fakeUser(), 'foo'); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.guard', compact('foo'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventNotDispatched(); | ||
}); | ||
|
||
test('aliased locale', function (string $locale) { | ||
actingAs(fakeUser($locale)); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.guard', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationGerman); | ||
|
||
assertEventDispatched(); | ||
})->with('aliased-locales'); | ||
|
||
test('empty locale', function (int|string|null $locale) { | ||
actingAs(fakeUser($locale)); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.guard', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventNotDispatched(); | ||
})->with('empty-locales'); | ||
|
||
test('uninstalled locale', function (string $locale) { | ||
actingAs(fakeUser($locale)); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.guard', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventDispatched(); | ||
})->with('uninstalled-locales'); | ||
|
||
test('unknown locale', function (int|string $locale) { | ||
actingAs(fakeUser($locale)); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.guard', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventDispatched(); | ||
})->with('unknown-locales'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tests\Constants\LocaleValue; | ||
|
||
use function Pest\Laravel\actingAs; | ||
use function Pest\Laravel\getJson; | ||
|
||
test('main locale', function () { | ||
actingAs(fakeUser()); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.default', compact('foo'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventNotDispatched(); | ||
}); | ||
|
||
test('aliased locale', function (string $locale) { | ||
actingAs(fakeUser($locale)); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.default', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationGerman); | ||
|
||
assertEventDispatched(); | ||
})->with('aliased-locales'); | ||
|
||
test('empty locale', function (int|string|null $locale) { | ||
actingAs(fakeUser($locale)); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.default', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventNotDispatched(); | ||
})->with('empty-locales'); | ||
|
||
test('uninstalled locale', function (string $locale) { | ||
actingAs(fakeUser($locale)); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.default', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventDispatched(); | ||
})->with('uninstalled-locales'); | ||
|
||
test('unknown locale', function (int|string $locale) { | ||
actingAs(fakeUser($locale)); | ||
|
||
$foo = 'test'; | ||
|
||
getJson(route('via.model.default', compact('foo', 'locale'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventDispatched(); | ||
})->with('unknown-locales'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tests\Constants\LocaleValue; | ||
|
||
use function Pest\Laravel\getJson; | ||
|
||
test('with guard', function () { | ||
$foo = 'test'; | ||
|
||
getJson(route('via.model.guard', compact('foo'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventNotDispatched(); | ||
}); | ||
|
||
test('without guard', function () { | ||
$foo = 'test'; | ||
|
||
getJson(route('via.model.default', compact('foo'))) | ||
->assertSuccessful() | ||
->assertJsonPath($foo, LocaleValue::TranslationFrench); | ||
|
||
assertEventNotDispatched(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Fixtures\Models; | ||
|
||
use Illuminate\Foundation\Auth\User as Authenticatable; | ||
|
||
class User extends Authenticatable | ||
{ | ||
protected $fillable = [ | ||
'locale', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tests\Fixtures\Models\User; | ||
|
||
function fakeUser(int|string|null $locale = null): User | ||
{ | ||
return User::create([ | ||
'locale' => $locale, | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<?php | ||
|
||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use Illuminate\Support\Facades\Event; | ||
|
||
uses(Tests\TestCase::class) | ||
uses(Tests\TestCase::class, DatabaseTransactions::class) | ||
->beforeEach(fn () => Event::fake()) | ||
->in('Feature', 'Unit'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/database/migrations/2024_06_20_000000_create_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->id(); | ||
|
||
$table->string('locale')->nullable(); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('users'); | ||
} | ||
}; |