5
5
namespace Redbitcz \DebugModeTests ;
6
6
7
7
use Redbitcz \DebugMode \Detector ;
8
+ use Redbitcz \DebugMode \Enabler ;
8
9
use Tester \Assert ;
9
10
use Tester \Helpers ;
10
11
use Tester \TestCase ;
11
12
12
13
require __DIR__ . '/bootstrap.php ' ;
13
14
14
- /**
15
- * @testCase
16
- */
15
+ /** @testCase */
17
16
class DetectorTest extends TestCase
18
17
{
19
- private const DEBUG_ENV_NAME = 'APP_DEBUG ' ;
20
- private const DEBUG_COOKIE_NAME = 'app-debug-mode ' ;
21
18
private const TEMP_DIR = __DIR__ . '/temp/enabler ' ;
22
19
23
20
protected function setUp (): void
@@ -47,9 +44,9 @@ public function getEnvDataProvider(): array
47
44
*/
48
45
public function testEnv ($ testValue , $ expected ): void
49
46
{
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 ));
51
48
52
- $ detector = new Detector (self :: TEMP_DIR );
49
+ $ detector = new Detector ();
53
50
Assert::equal ($ expected , $ detector ->isDebugModeByEnv ());
54
51
}
55
52
@@ -77,10 +74,10 @@ public function getCookieDataProvider(): array
77
74
public function testCookie ($ testValue , $ expected ): void
78
75
{
79
76
if ($ testValue !== null ) {
80
- $ _COOKIE [self ::DEBUG_COOKIE_NAME ] = $ testValue ;
77
+ $ _COOKIE [Detector ::DEBUG_COOKIE_NAME ] = $ testValue ;
81
78
}
82
79
83
- $ detector = new Detector (self :: TEMP_DIR );
80
+ $ detector = new Detector ();
84
81
Assert::equal ($ expected , $ detector ->isDebugModeByCookie ());
85
82
}
86
83
@@ -89,7 +86,7 @@ public function getIpDataProvider(): array
89
86
return [
90
87
// [null, null], // unable to test null, because then detector try load ip from `php_uname('n')`
91
88
['127.0.0.1 ' , true ],
92
- ['127.0.0.254 ' , true ],
89
+ ['127.0.0.254 ' , null ],
93
90
['127.0.1.0 ' , null ],
94
91
['192.168.1.1 ' , null ],
95
92
['::1 ' , true ],
@@ -103,12 +100,73 @@ public function getIpDataProvider(): array
103
100
* @param $expected
104
101
*/
105
102
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
106
163
{
107
164
unset($ _SERVER ['HTTP_X_FORWARDED_FOR ' ], $ _SERVER ['HTTP_FORWARDED ' ]);
108
165
$ _SERVER ['REMOTE_ADDR ' ] = $ testValue ;
109
166
110
167
111
- $ detector = new Detector (self ::TEMP_DIR );
168
+ $ detector = new Detector ();
169
+ $ detector ->addAllowedIp (...$ setIp );
112
170
Assert::equal ($ expected , $ detector ->isDebugModeByIp ());
113
171
}
114
172
@@ -127,7 +185,7 @@ public function getEnablerDataProvider(): array
127
185
*/
128
186
public function testEnabler ($ testValue ): void
129
187
{
130
- $ detector = new Detector (self ::TEMP_DIR );
188
+ $ detector = new Detector (Detector:: MODE_FULL , new Enabler ( self ::TEMP_DIR ) );
131
189
$ detector ->getEnabler ()->override ($ testValue );
132
190
Assert::equal ($ testValue , $ detector ->isDebugModeByEnabler ());
133
191
}
0 commit comments