Skip to content

Commit 69e73f2

Browse files
committed
Detector: Full refactoring - fix tests
1 parent 0aa74d9 commit 69e73f2

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed

.phpstorm.meta.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
expectedArguments(
66
\Redbitcz\DebugMode\Detector::__construct(),
7-
1,
8-
\Redbitcz\DebugMode\Detector::MODE_ALL,
7+
0,
8+
\Redbitcz\DebugMode\Detector::MODE_FULL,
99
\Redbitcz\DebugMode\Detector::MODE_SIMPLE,
1010
\Redbitcz\DebugMode\Detector::MODE_ENABLER,
1111
\Redbitcz\DebugMode\Detector::MODE_COOKIE,
@@ -15,8 +15,8 @@
1515

1616
expectedArguments(
1717
\Redbitcz\DebugMode\Detector::detect(),
18-
1,
19-
\Redbitcz\DebugMode\Detector::MODE_ALL,
18+
0,
19+
\Redbitcz\DebugMode\Detector::MODE_FULL,
2020
\Redbitcz\DebugMode\Detector::MODE_SIMPLE,
2121
\Redbitcz\DebugMode\Detector::MODE_ENABLER,
2222
\Redbitcz\DebugMode\Detector::MODE_COOKIE,
@@ -26,8 +26,8 @@
2626

2727
expectedArguments(
2828
\Redbitcz\DebugMode\Detector::detectProductionMode(),
29-
1,
30-
\Redbitcz\DebugMode\Detector::MODE_ALL,
29+
0,
30+
\Redbitcz\DebugMode\Detector::MODE_FULL,
3131
\Redbitcz\DebugMode\Detector::MODE_SIMPLE,
3232
\Redbitcz\DebugMode\Detector::MODE_ENABLER,
3333
\Redbitcz\DebugMode\Detector::MODE_COOKIE,

src/Detector.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class Detector
1919
/** Name of Cookie used to detect Debug mode */
2020
public const DEBUG_COOKIE_NAME = 'app-debug-mode';
2121

22-
2322
/** Enables Debug mode detection by client IP */
2423
public const MODE_IP = 0b0001;
2524

@@ -36,7 +35,7 @@ class Detector
3635
public const MODE_SIMPLE = self::MODE_COOKIE | self::MODE_ENV | self::MODE_IP;
3736

3837
/** Full mode with Enabler */
39-
public const MODE_ALL = self::MODE_ENABLER | self::MODE_SIMPLE;
38+
public const MODE_FULL = self::MODE_ENABLER | self::MODE_SIMPLE;
4039

4140

4241
/** @var Enabler|null */
@@ -145,18 +144,19 @@ public function isDebugModeByIp(): ?bool
145144
$addr = $_SERVER['REMOTE_ADDR'] ?? php_uname('n');
146145

147146
// Security check: Prevent false-positive match behind reverse proxy
148-
$result = isset($_SERVER['HTTP_X_FORWARDED_FOR']) === false
149-
&& isset($_SERVER['HTTP_FORWARDED']) === false
150-
&& isset($_SERVER['HTTP_X_REAL_IP']) === false
151-
&& in_array($addr, $this->ips, true);
147+
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])
148+
|| isset($_SERVER['HTTP_FORWARDED'])
149+
|| isset($_SERVER['HTTP_X_REAL_IP'])) {
150+
return false;
151+
}
152152

153-
return $result ?: null;
153+
return in_array($addr, $this->ips, true) ?: null;
154154
}
155155

156156
/**
157157
* Set client IP address with allowed Debug mode
158158
*/
159-
public function allowIp(string ...$ips): self
159+
public function setAllowedIp(string ...$ips): self
160160
{
161161
$this->ips = $ips;
162162
return $this;
@@ -167,8 +167,7 @@ public function allowIp(string ...$ips): self
167167
*/
168168
public function addAllowedIp(string ...$ips): self
169169
{
170-
/** @noinspection AdditionOperationOnArraysInspection */
171-
$this->ips += $ips;
170+
$this->ips = array_merge($this->ips, $ips);
172171
return $this;
173172
}
174173

tests/DetectorTest.php

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
namespace Redbitcz\DebugModeTests;
66

77
use Redbitcz\DebugMode\Detector;
8+
use Redbitcz\DebugMode\Enabler;
89
use Tester\Assert;
910
use Tester\Helpers;
1011
use Tester\TestCase;
1112

1213
require __DIR__ . '/bootstrap.php';
1314

14-
/**
15-
* @testCase
16-
*/
15+
/** @testCase */
1716
class DetectorTest extends TestCase
1817
{
19-
private const DEBUG_ENV_NAME = 'APP_DEBUG';
20-
private const DEBUG_COOKIE_NAME = 'app-debug-mode';
2118
private const TEMP_DIR = __DIR__ . '/temp/enabler';
2219

2320
protected function setUp(): void
@@ -47,9 +44,9 @@ public function getEnvDataProvider(): array
4744
*/
4845
public function testEnv($testValue, $expected): void
4946
{
50-
putenv(sprintf('%s%s%s', self::DEBUG_ENV_NAME, $testValue === null ? '' : '=', $testValue));
47+
putenv(sprintf('%s%s%s', Detector::DEBUG_ENV_NAME, $testValue === null ? '' : '=', $testValue));
5148

52-
$detector = new Detector(self::TEMP_DIR);
49+
$detector = new Detector();
5350
Assert::equal($expected, $detector->isDebugModeByEnv());
5451
}
5552

@@ -77,10 +74,10 @@ public function getCookieDataProvider(): array
7774
public function testCookie($testValue, $expected): void
7875
{
7976
if ($testValue !== null) {
80-
$_COOKIE[self::DEBUG_COOKIE_NAME] = $testValue;
77+
$_COOKIE[Detector::DEBUG_COOKIE_NAME] = $testValue;
8178
}
8279

83-
$detector = new Detector(self::TEMP_DIR);
80+
$detector = new Detector();
8481
Assert::equal($expected, $detector->isDebugModeByCookie());
8582
}
8683

@@ -89,7 +86,7 @@ public function getIpDataProvider(): array
8986
return [
9087
// [null, null], // unable to test null, because then detector try load ip from `php_uname('n')`
9188
['127.0.0.1', true],
92-
['127.0.0.254', true],
89+
['127.0.0.254', null],
9390
['127.0.1.0', null],
9491
['192.168.1.1', null],
9592
['::1', true],
@@ -103,12 +100,73 @@ public function getIpDataProvider(): array
103100
* @param $expected
104101
*/
105102
public function testIp($testValue, $expected): void
103+
{
104+
unset($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['HTTP_FORWARDED'], $_SERVER['HTTP_X_REAL_IP']);
105+
$_SERVER['REMOTE_ADDR'] = $testValue;
106+
107+
108+
$detector = new Detector();
109+
Assert::equal($expected, $detector->isDebugModeByIp());
110+
}
111+
112+
public function getSettedIpDataProvider(): array
113+
{
114+
return [
115+
[['127.0.0.1'], '127.0.0.1', true],
116+
[['127.0.0.2'], '127.0.0.1', null],
117+
[['127.0.0.1'], '127.0.0.254', null],
118+
[['127.0.0.1', '127.0.1.0'], '127.0.1.0', true],
119+
[['127.0.0.1'], '127.0.1.0', null],
120+
[['127.0.0.1'], '192.168.1.1', null],
121+
[['127.0.0.1'], '::1', null],
122+
[['127.0.0.1', '2600:1005:b062:61e4:74d7:f292:802c:fbfd'], '2600:1005:b062:61e4:74d7:f292:802c:fbfd', true],
123+
[['127.0.0.1'], '2600:1005:b062:61e4:74d7:f292:802c:fbfd', null],
124+
];
125+
}
126+
127+
/**
128+
* @dataProvider getSettedIpDataProvider
129+
* @param $testValue
130+
* @param $expected
131+
*/
132+
public function testSettedIp(array $setIp, $testValue, $expected): void
133+
{
134+
unset($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['HTTP_FORWARDED']);
135+
$_SERVER['REMOTE_ADDR'] = $testValue;
136+
137+
138+
$detector = new Detector();
139+
$detector->setAllowedIp(...$setIp);
140+
Assert::equal($expected, $detector->isDebugModeByIp());
141+
}
142+
143+
public function getAddedIpDataProvider(): array
144+
{
145+
return [
146+
[['10.0.0.1'], '127.0.0.1', true],
147+
[['10.0.0.1'], '127.0.0.254', null],
148+
[['10.0.0.1'], '127.0.1.0', null],
149+
[['10.0.0.1'], '192.168.1.1', null],
150+
[['10.0.0.1'], '::1', true],
151+
[['10.0.0.1'], '2600:1005:b062:61e4:74d7:f292:802c:fbfd', null],
152+
[['10.0.0.1'], '10.0.0.1', true],
153+
[['10.0.0.1', '10.0.0.2'], '10.0.0.2', true],
154+
];
155+
}
156+
157+
/**
158+
* @dataProvider getAddedIpDataProvider
159+
* @param $testValue
160+
* @param $expected
161+
*/
162+
public function testAddedIp(array $setIp, $testValue, $expected): void
106163
{
107164
unset($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['HTTP_FORWARDED']);
108165
$_SERVER['REMOTE_ADDR'] = $testValue;
109166

110167

111-
$detector = new Detector(self::TEMP_DIR);
168+
$detector = new Detector();
169+
$detector->addAllowedIp(...$setIp);
112170
Assert::equal($expected, $detector->isDebugModeByIp());
113171
}
114172

@@ -127,7 +185,7 @@ public function getEnablerDataProvider(): array
127185
*/
128186
public function testEnabler($testValue): void
129187
{
130-
$detector = new Detector(self::TEMP_DIR);
188+
$detector = new Detector(Detector::MODE_FULL, new Enabler(self::TEMP_DIR));
131189
$detector->getEnabler()->override($testValue);
132190
Assert::equal($testValue, $detector->isDebugModeByEnabler());
133191
}

0 commit comments

Comments
 (0)