Skip to content

Commit

Permalink
Add tests after refctor trait, change typo in dir
Browse files Browse the repository at this point in the history
  • Loading branch information
HamzaHassanM committed Nov 26, 2024
1 parent 6a79e1e commit 2535017
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 56 deletions.
2 changes: 2 additions & 0 deletions src/LexiTranslateFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

class LexiTranslateFacade extends Facade
{

/**
* Get the registered name of the component.
*
* @return string
* @see \Omaralalwi\LexiTranslate\LexiTranslate
*/
protected static function getFacadeAccessor()
{
Expand Down
10 changes: 4 additions & 6 deletions src/Middleware/WebLocalized.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@

class WebLocalized
{

use HasLocale;

/**
* Handle an incoming request to switch the app locale.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle(Request $request, Closure $next): Response
{
$requestLocale = $request->route('locale') ?: $request->get('locale') ?: session('locale') ?: cookie('locale');
$locale = $this->getValidatedLocale($requestLocale);

if(!$locale) {
$locale = app()->getLocale();
}
$locale = $this->getValidatedLocale($requestLocale) ?: app()->getLocale();

if ($locale && in_array($locale, Config::get('lexi-translate.supported_locales'))) {
App::setLocale($locale);
session(['locale' => $locale]);
cookie()->queue(cookie('locale', $locale, 60 * 24 * 365));
cookie()->queue(cookie('locale', $locale, 60 * 24 * 365, null, null, true, true, false, 'Lax'));
}

return $next($request);
Expand Down
47 changes: 0 additions & 47 deletions src/Models/Scones/TranslationsScopes.php

This file was deleted.

66 changes: 66 additions & 0 deletions src/Models/Scopes/TranslationsScopes.php
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();
}
}
6 changes: 3 additions & 3 deletions src/Traits/LexiTranslatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Omaralalwi\LexiTranslate\Models\Translation;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Facades\Cache;
use Omaralalwi\LexiTranslate\Models\Scones\TranslationsScopes;
use Omaralalwi\LexiTranslate\Models\Scopes\TranslationsScopes;
use Omaralalwi\LexiTranslate\Traits\HasLocale;

trait LexiTranslatable
Expand Down Expand Up @@ -59,7 +59,7 @@ public function translate(string $column, ?string $locale = null): string
? Cache::remember($cacheKey, now()->addHours(self::cacheTtl()), fn() => $this->getTranslation($column, $locale))
: $this->getTranslation($column, $locale);

if(array_key_exists($column, $this->attributes)) {
if (array_key_exists($column, $this->attributes)) {
$originalText = $this->attributes[$column];
}

Expand Down Expand Up @@ -130,7 +130,7 @@ public function setTranslations(array $translations): void
*
* @param string $column The name of the column to translate.
* @param string $locale The locale for the translation.
* @param string $text The translation text.
* @param string $text The translation text.
* @return void
*/
public function setTranslation(string $column, string $locale, string $text): void
Expand Down
85 changes: 85 additions & 0 deletions tests/Unit/ApiLocalizedTest.php
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());
}
}
Loading

0 comments on commit 2535017

Please sign in to comment.