Skip to content

Commit bc5f614

Browse files
committed
Add tests
1 parent b654f07 commit bc5f614

File tree

5 files changed

+161
-3
lines changed

5 files changed

+161
-3
lines changed

.github/workflows/code_analysis.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ jobs:
2020
- name: Easy Coding Standard
2121
run: composer ecs
2222

23+
- name: Nette Tester
24+
run: composer tester
25+
2326

2427
name: ${{ matrix.actions.name }}
2528
runs-on: ubuntu-latest

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/vendor
22
/composer.lock
3+
/tests/temp
4+
/tests/lock

composer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
"Redbitcz\\DebugMode\\": "src/"
2424
}
2525
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Redbitcz\\DebugModeTests\\": "tests/"
29+
}
30+
},
2631
"replace": {
2732
"jakubboucek/nette-debug-enabler": "*"
2833
},
@@ -33,11 +38,13 @@
3338
},
3439
"require-dev": {
3540
"phpstan/phpstan": "^0.12.50",
36-
"symplify/easy-coding-standard": "^8.3"
41+
"symplify/easy-coding-standard": "^8.3",
42+
"nette/tester": "^2.3"
3743
},
3844
"scripts": {
3945
"phpstan": "phpstan analyze src --level 8",
40-
"ecs": "ecs check src --set psr12",
41-
"ecs-fix": "ecs check src --set psr12 --fix"
46+
"ecs": "ecs check src tests --set psr12",
47+
"ecs-fix": "ecs check src tests --set psr12 --fix",
48+
"tester": "tester tests"
4249
}
4350
}

tests/DetectorTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Redbitcz\DebugModeTests;
6+
7+
use Redbitcz\DebugMode\Detector;
8+
use Tester\Assert;
9+
use Tester\Helpers;
10+
use Tester\TestCase;
11+
12+
require __DIR__ . '/bootstrap.php';
13+
14+
/**
15+
* @testCase
16+
*/
17+
class DetectorTest extends TestCase
18+
{
19+
private const DEBUG_ENV_NAME = 'APP_DEBUG';
20+
private const DEBUG_COOKIE_NAME = 'app-debug-mode';
21+
private const TEMP_DIR = __DIR__ . '/temp/enabler';
22+
23+
protected function setUp(): void
24+
{
25+
@mkdir(self::TEMP_DIR, 0777, true);
26+
Helpers::purge(self::TEMP_DIR);
27+
}
28+
29+
public function getEnvDataProvider(): array
30+
{
31+
return [
32+
[null, null],
33+
['', null],
34+
['0', false],
35+
['1', true],
36+
['2', null],
37+
['-1', null],
38+
['foo', null],
39+
['bar', null],
40+
];
41+
}
42+
43+
/**
44+
* @dataProvider getEnvDataProvider
45+
* @param $testValue
46+
* @param $expected
47+
*/
48+
public function testEnv($testValue, $expected): void
49+
{
50+
putenv(sprintf('%s%s%s', self::DEBUG_ENV_NAME, $testValue === null ? '' : '=', $testValue));
51+
52+
$detector = new Detector(self::TEMP_DIR);
53+
Assert::equal($expected, $detector->isDebugModeByEnv());
54+
}
55+
56+
public function getCookieDataProvider(): array
57+
{
58+
return [
59+
[null, null],
60+
['', null],
61+
[0, false],
62+
['0', false],
63+
['1', null],
64+
['1', null],
65+
['2', null],
66+
['-1', null],
67+
['foo', null],
68+
['bar', null],
69+
];
70+
}
71+
72+
/**
73+
* @dataProvider getCookieDataProvider
74+
* @param $testValue
75+
* @param $expected
76+
*/
77+
public function testCookie($testValue, $expected): void
78+
{
79+
if ($testValue !== null) {
80+
$_COOKIE[self::DEBUG_COOKIE_NAME] = $testValue;
81+
}
82+
83+
$detector = new Detector(self::TEMP_DIR);
84+
Assert::equal($expected, $detector->isDebugModeByCookie());
85+
}
86+
87+
public function getIpDataProvider(): array
88+
{
89+
return [
90+
// [null, null], // unable to test null, because then detector try load ip from `php_uname('n')`
91+
['127.0.0.1', true],
92+
['127.0.0.254', true],
93+
['127.0.1.0', null],
94+
['192.168.1.1', null],
95+
['::1', true],
96+
['2600:1005:b062:61e4:74d7:f292:802c:fbfd', null],
97+
];
98+
}
99+
100+
/**
101+
* @dataProvider getIpDataProvider
102+
* @param $testValue
103+
* @param $expected
104+
*/
105+
public function testIp($testValue, $expected): void
106+
{
107+
unset($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['HTTP_FORWARDED']);
108+
$_SERVER['REMOTE_ADDR'] = $testValue;
109+
110+
111+
$detector = new Detector(self::TEMP_DIR);
112+
Assert::equal($expected, $detector->isDebugModeByIp());
113+
}
114+
115+
public function getEnablerDataProvider(): array
116+
{
117+
return [
118+
// [null], // unable to test null, because then detector fetch cookie from globals
119+
[true],
120+
[false],
121+
];
122+
}
123+
124+
/**
125+
* @dataProvider getEnablerDataProvider
126+
* @param $testValue
127+
*/
128+
public function testEnabler($testValue): void
129+
{
130+
$detector = new Detector(self::TEMP_DIR);
131+
$detector->getEnabler()->override($testValue);
132+
Assert::equal($testValue, $detector->isDebugModeByEnabler());
133+
}
134+
}
135+
136+
(new DetectorTest())->run();

tests/bootstrap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
if (@!include __DIR__ . '/../vendor/autoload.php') {
6+
echo 'Install Nette Tester using `composer install`';
7+
exit(1);
8+
}
9+
10+
Tester\Environment::setup();

0 commit comments

Comments
 (0)