forked from omaralalwi/lexi-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests after refctor trait, change typo in dir
- Loading branch information
1 parent
6a79e1e
commit 2535017
Showing
7 changed files
with
311 additions
and
56 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 was deleted.
Oops, something went wrong.
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,66 @@ | ||
<?php | ||
|
||
namespace Omaralalwi\LexiTranslate\Models\Scopes; | ||
|
||
trait TranslationsScopes | ||
{ | ||
/** | ||
* Search the model by translated fields. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $field The translatable field to search. | ||
* @param string $keyword The keyword to search for. | ||
* @param string|null $locale The locale to search in. Defaults to app locale. | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function scopeSearchByTranslation($query, string $field, string $keyword, ?string $locale = null) | ||
{ | ||
return $this->applyTranslationFilter($query, $field, "%{$keyword}%", $locale, 'LIKE'); | ||
} | ||
|
||
/** | ||
* Filter the model by translated field values. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $field The translatable field to filter. | ||
* @param mixed $value The exact value to filter for. | ||
* @param string|null $locale The locale to filter in. Defaults to app locale. | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function scopeFilterByTranslation($query, string $field, $value, ?string $locale = null) | ||
{ | ||
return $this->applyTranslationFilter($query, $field, $value, $locale); | ||
} | ||
|
||
/** | ||
* Apply a translation filter to the query. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $field The translatable field to filter. | ||
* @param mixed $value The value or pattern to match. | ||
* @param string|null $locale The locale to filter in. Defaults to app locale. | ||
* @param string $operator The operator for the comparison. Defaults to '='. | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
protected function applyTranslationFilter($query, string $field, $value, ?string $locale, string $operator = '=') | ||
{ | ||
$locale = $this->resolveLocale($locale); | ||
|
||
return $query->whereHas('translations', function ($q) use ($field, $value, $locale, $operator) { | ||
$q->where('column', $field) | ||
->where('locale', $locale) | ||
->where('text', $operator, $value); | ||
}); | ||
} | ||
|
||
/** | ||
* Resolve the locale to use for the query. | ||
* | ||
* @param string|null $locale The locale provided by the user. | ||
* @return string The resolved locale. | ||
*/ | ||
protected function resolveLocale(?string $locale): string | ||
{ | ||
return $locale && $this->isSupportedLocal($locale) ? $locale : app()->getLocale(); | ||
} | ||
} |
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,85 @@ | ||
<?php | ||
|
||
namespace Omaralalwi\LexiTranslate\Tests\Unit; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\App; | ||
use Omaralalwi\LexiTranslate\Middleware\ApiLocalized; | ||
use Omaralalwi\LexiTranslate\Tests\TestCase; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
|
||
class ApiLocalizedTest extends TestCase | ||
{ | ||
|
||
public function testLocaleFromHeader() | ||
{ | ||
// Define a test locale header | ||
$localeHeader = 'ar'; | ||
|
||
// Create a mock request | ||
$request = Request::create('/api/test', 'GET', [], [], [], ['HTTP_Accept-Language' => $localeHeader]); | ||
|
||
// Instantiate the middleware | ||
$middleware = new ApiLocalized(); | ||
|
||
// Process the request through the middleware | ||
$response = $middleware->handle($request, function ($req) { | ||
// This is the closure after the middleware processes the request | ||
return new Response('OK', 200); | ||
}); | ||
|
||
// Assert that the locale was set to the header value | ||
$this->assertEquals('ar', App::getLocale()); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
} | ||
|
||
|
||
/** | ||
* Test that the middleware defaults to the app's default locale if no locale is provided. | ||
* | ||
* @return void | ||
*/ | ||
public function testDefaultLocale() | ||
{ | ||
// Create a mock request with no locale specified | ||
$request = Request::create('/api/test', 'GET'); | ||
|
||
// Instantiate the middleware | ||
$middleware = new ApiLocalized(); | ||
|
||
// Process the request through the middleware | ||
$response = $middleware->handle($request, function ($req) { | ||
return new Response('OK', 200); | ||
}); | ||
|
||
// Assert that the default locale is used | ||
$this->assertEquals(config('app.locale'), App::getLocale()); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* Test that an invalid locale falls back to the default locale. | ||
* | ||
* @return void | ||
*/ | ||
public function testInvalidLocale() | ||
{ | ||
$invalidLocale = 'xyz'; // Invalid locale | ||
|
||
// Create a mock request with an invalid locale header | ||
$request = Request::create('/api/test', 'GET', [], [], [], ['HTTP_Accept-Language' => $invalidLocale]); | ||
|
||
// Instantiate the middleware | ||
$middleware = new ApiLocalized(); | ||
|
||
// Process the request through the middleware | ||
$response = $middleware->handle($request, function ($req) { | ||
return new Response('OK', 200); | ||
}); | ||
|
||
// Assert that the locale falls back to the default locale | ||
$this->assertEquals(config('app.locale'), App::getLocale()); | ||
$this->assertEquals(200, $response->getStatusCode()); | ||
} | ||
} |
Oops, something went wrong.