From fd49e524c88f6e8ec313fb4e67698edb50e6c6a2 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 3 Sep 2018 15:17:53 -0700 Subject: [PATCH] Update middleware code --- app/Http/Middleware/LanguageMiddleware.php | 24 ++++--------------- .../Middleware/LanguageMiddlewareTest.php | 18 ++++---------- 2 files changed, 9 insertions(+), 33 deletions(-) diff --git a/app/Http/Middleware/LanguageMiddleware.php b/app/Http/Middleware/LanguageMiddleware.php index 04e5f3a98..65692d3e2 100644 --- a/app/Http/Middleware/LanguageMiddleware.php +++ b/app/Http/Middleware/LanguageMiddleware.php @@ -1,19 +1,10 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Http\Middleware; use Closure; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; use Illuminate\Foundation\Application; -use Illuminate\Contracts\Config\Repository; class LanguageMiddleware { @@ -22,25 +13,18 @@ class LanguageMiddleware */ private $app; - /** - * @var \Illuminate\Contracts\Config\Repository - */ - private $config; - /** * LanguageMiddleware constructor. * - * @param \Illuminate\Foundation\Application $app - * @param \Illuminate\Contracts\Config\Repository $config + * @param \Illuminate\Foundation\Application $app */ - public function __construct(Application $app, Repository $config) + public function __construct(Application $app) { $this->app = $app; - $this->config = $config; } /** - * Handle an incoming request. + * Handle an incoming request and set the user's preferred language. * * @param \Illuminate\Http\Request $request * @param \Closure $next @@ -48,7 +32,7 @@ class LanguageMiddleware */ public function handle(Request $request, Closure $next) { - $this->app->setLocale($request->user()->language ?? $this->config->get('app.locale', 'en')); + $this->app->setLocale($request->user()->language ?? config('app.locale', 'en')); return $next($request); } diff --git a/tests/Unit/Http/Middleware/LanguageMiddlewareTest.php b/tests/Unit/Http/Middleware/LanguageMiddlewareTest.php index 3e1662a19..337e47725 100644 --- a/tests/Unit/Http/Middleware/LanguageMiddlewareTest.php +++ b/tests/Unit/Http/Middleware/LanguageMiddlewareTest.php @@ -3,10 +3,9 @@ namespace Tests\Unit\Http\Middleware; use Mockery as m; -use Illuminate\Foundation\Application; -use Illuminate\Contracts\Config\Repository; -use Pterodactyl\Http\Middleware\LanguageMiddleware; use Pterodactyl\Models\User; +use Illuminate\Foundation\Application; +use Pterodactyl\Http\Middleware\LanguageMiddleware; class LanguageMiddlewareTest extends MiddlewareTestCase { @@ -15,11 +14,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase */ private $appMock; - /** - * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock - */ - private $config; - /** * Setup tests. */ @@ -28,7 +22,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase parent::setUp(); $this->appMock = m::mock(Application::class); - $this->config = m::mock(Repository::class); } /** @@ -37,8 +30,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase public function testLanguageIsSetForGuest() { $this->request->shouldReceive('user')->withNoArgs()->andReturnNull(); - - $this->config->shouldReceive('get')->with('app.locale', 'en')->once()->andReturn('en'); $this->appMock->shouldReceive('setLocale')->with('en')->once()->andReturnNull(); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); @@ -47,7 +38,8 @@ class LanguageMiddlewareTest extends MiddlewareTestCase /** * Test that a language is defined via the middleware for a user. */ - public function testLanguageIsSetWithAuthenticatedUser() { + public function testLanguageIsSetWithAuthenticatedUser() + { $user = factory(User::class)->make(['language' => 'de']); $this->request->shouldReceive('user')->withNoArgs()->andReturn($user); @@ -63,6 +55,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase */ private function getMiddleware(): LanguageMiddleware { - return new LanguageMiddleware($this->appMock, $this->config); + return new LanguageMiddleware($this->appMock); } }