From e48bd1f9c6f2117828390000665851e37f3c44af Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Wed, 4 Sep 2019 21:28:21 +0800 Subject: [PATCH] Tweak TestCase --- app/helpers.php | 25 +++++++++---------------- tests/BrowserKitTestCase.php | 10 ---------- tests/TestCase.php | 10 ---------- 3 files changed, 9 insertions(+), 36 deletions(-) diff --git a/app/helpers.php b/app/helpers.php index 5d10f72d..b509a215 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -299,21 +299,13 @@ if (! function_exists('get_client_ip')) { */ function get_client_ip(): string { + $request = request(); if (option('ip_get_method') == '0') { - // Use `HTTP_X_FORWARDED_FOR` if available first - $ip = Arr::get( - $_SERVER, - 'HTTP_X_FORWARDED_FOR', - // Fallback to `HTTP_CLIENT_IP` - Arr::get( - $_SERVER, - 'HTTP_CLIENT_IP', - // Fallback to `REMOTE_ADDR` - Arr::get($_SERVER, 'REMOTE_ADDR') - ) - ); + $ip = $request->server('HTTP_X_FORWARDED_FOR') + ?? $request->server('HTTP_CLIENT_IP') + ?? $request->server('REMOTE_ADDR'); } else { - $ip = Arr::get($_SERVER, 'REMOTE_ADDR'); + $ip = $request->server('REMOTE_ADDR'); } return $ip; @@ -350,15 +342,16 @@ if (! function_exists('is_request_secure')) { */ function is_request_secure(): bool { - if (Arr::get($_SERVER, 'HTTPS') == 'on') { + $request = request(); + if ($request->server('HTTPS') == 'on') { return true; } - if (Arr::get($_SERVER, 'HTTP_X_FORWARDED_PROTO') == 'https') { + if ($request->server('HTTP_X_FORWARDED_PROTO') == 'https') { return true; } - if (Arr::get($_SERVER, 'HTTP_X_FORWARDED_SSL') == 'on') { + if ($request->server('HTTP_X_FORWARDED_SSL') == 'on') { return true; } diff --git a/tests/BrowserKitTestCase.php b/tests/BrowserKitTestCase.php index 112ef44f..7d3db0df 100644 --- a/tests/BrowserKitTestCase.php +++ b/tests/BrowserKitTestCase.php @@ -28,8 +28,6 @@ class BrowserKitTestCase extends TestCase Artisan::call('migrate:refresh'); - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; - return $app; } @@ -49,12 +47,4 @@ class BrowserKitTestCase extends TestCase return $this->actingAs($role); } - - protected function tearDown(): void - { - $this->beforeApplicationDestroyed(function () { - DB::disconnect(); - }); - parent::tearDown(); - } } diff --git a/tests/TestCase.php b/tests/TestCase.php index b2a256a9..45e5dd14 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -27,8 +27,6 @@ class TestCase extends \Illuminate\Foundation\Testing\TestCase Artisan::call('migrate:refresh'); - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; - return $app; } @@ -48,12 +46,4 @@ class TestCase extends \Illuminate\Foundation\Testing\TestCase return $this->actingAs($role); } - - protected function tearDown(): void - { - $this->beforeApplicationDestroyed(function () { - DB::disconnect(); - }); - parent::tearDown(); - } }