Skip to content

Commit

Permalink
fix: middleware and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jul 1, 2022
1 parent 03403fb commit f506ea4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Middlewares/LocalizationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class LocalizationMiddleware extends Middleware
{
public function handle(Request $request, Closure $next)
{
if (!config('vendor-localization.localization_enabled')) {
return $next($request);
}

// find and validate the lang on that request
$lang = $this->validateLanguage($this->findLanguage($request));

Expand Down
22 changes: 8 additions & 14 deletions Providers/MainServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,18 @@
*/
class MainServiceProvider extends ParentMainServiceProvider
{
/**
* Container Service Providers.
*/
public array $serviceProviders = [
LocalizationServiceProvider::class,
MiddlewareServiceProvider::class,
];

/**
* Container Aliases
*/
public array $aliases = [

];

public function __construct(
$app,
/**
* Container Service Providers.
*/
public array $serviceProviders = [
LocalizationServiceProvider::class,
]) {
if (config('vendor-localization.localization_enabled')) {
array_push($serviceProviders, MiddlewareServiceProvider::class);
}
parent::__construct($app);
}
}
24 changes: 19 additions & 5 deletions UI/API/Tests/Functional/CheckLocalizationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ public function testIfMiddlewareSetsDefaultAppLanguage(): void

$response = $this->makeCall($data, $requestHeaders);

$response->assertStatus(200)
->assertHeader('content-language', $defaultLanguage);
if (config('vendor-localization.localization_enabled')) {
$response->assertStatus(200)
->assertHeader('content-language', $defaultLanguage);
} else {
$response->assertStatus(200)
->assertHeaderMissing('content-language', $defaultLanguage);
}
}

public function testIfMiddlewareSetsCustomLanguage(): void
Expand All @@ -41,8 +46,13 @@ public function testIfMiddlewareSetsCustomLanguage(): void

$response = $this->makeCall($data, $requestHeaders);

$response->assertStatus(200)
->assertHeader('content-language', $language);
if (config('vendor-localization.localization_enabled')) {
$response->assertStatus(200)
->assertHeader('content-language', $language);
} else {
$response->assertStatus(200)
->assertHeaderMissing('content-language', $language);
}
}

public function testIfMiddlewareThrowsErrorOnWrongLanguage(): void
Expand All @@ -55,6 +65,10 @@ public function testIfMiddlewareThrowsErrorOnWrongLanguage(): void

$response = $this->makeCall($data, $requestHeaders);

$response->assertStatus(412);
if (config('vendor-localization.localization_enabled')) {
$response->assertStatus(412);
} else {
$response->assertStatus(200);
}
}
}

0 comments on commit f506ea4

Please sign in to comment.