Skip to content

Commit 9a84423

Browse files
committed
test: refactor ConfigCheckTest
1 parent d81f4c5 commit 9a84423

File tree

2 files changed

+66
-166
lines changed

2 files changed

+66
-166
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12265,12 +12265,6 @@
1226512265
'count' => 1,
1226612266
'path' => __DIR__ . '/tests/system/Commands/Translation/LocalizationFinderTest.php',
1226712267
];
12268-
$ignoreErrors[] = [
12269-
// identifier: missingType.return
12270-
'message' => '#^Method CodeIgniter\\\\Commands\\\\Utilities\\\\ConfigCheckTest\\:\\:getBuffer\\(\\) has no return type specified\\.$#',
12271-
'count' => 1,
12272-
'path' => __DIR__ . '/tests/system/Commands/Utilities/ConfigCheckTest.php',
12273-
];
1227412268
$ignoreErrors[] = [
1227512269
// identifier: missingType.return
1227612270
'message' => '#^Method CodeIgniter\\\\Commands\\\\Utilities\\\\NamespacesTest\\:\\:getBuffer\\(\\) has no return type specified\\.$#',

tests/system/Commands/Utilities/ConfigCheckTest.php

Lines changed: 66 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
namespace CodeIgniter\Commands\Utilities;
1515

16+
use Closure;
1617
use CodeIgniter\Test\CIUnitTestCase;
1718
use CodeIgniter\Test\StreamFilterTrait;
1819
use Config\App;
20+
use Kint\Kint;
21+
use Kint\Renderer\CliRenderer;
1922
use PHPUnit\Framework\Attributes\Group;
2023

2124
/**
@@ -26,6 +29,26 @@ final class ConfigCheckTest extends CIUnitTestCase
2629
{
2730
use StreamFilterTrait;
2831

32+
public static function setUpBeforeClass(): void
33+
{
34+
App::$override = false;
35+
36+
putenv('NO_COLOR=1');
37+
CliRenderer::$cli_colors = false;
38+
39+
parent::setUpBeforeClass();
40+
}
41+
42+
public static function tearDownAfterClass(): void
43+
{
44+
App::$override = true;
45+
46+
putenv('NO_COLOR');
47+
CliRenderer::$cli_colors = true;
48+
49+
parent::tearDownAfterClass();
50+
}
51+
2952
protected function setUp(): void
3053
{
3154
$this->resetServices();
@@ -38,188 +61,71 @@ protected function tearDown(): void
3861
parent::tearDown();
3962
}
4063

41-
protected function getBuffer()
42-
{
43-
return $this->getStreamFilterBuffer();
44-
}
45-
46-
public function testCommandConfigCheckNoArg(): void
64+
public function testCommandConfigCheckWithNoArgumentPassed(): void
4765
{
4866
command('config:check');
4967

50-
$this->assertStringContainsString(
51-
'You must specify a Config classname.',
52-
$this->getBuffer()
53-
);
54-
}
55-
56-
public function testCommandConfigCheckApp(): void
57-
{
58-
command('config:check App');
68+
$this->assertSame(
69+
<<<'EOF'
70+
You must specify a Config classname.
71+
Usage: config:check <classname>
72+
Example: config:check App
73+
config:check 'CodeIgniter\Shield\Config\Auth'
5974

60-
$this->assertStringContainsString(App::class, $this->getBuffer());
61-
$this->assertStringContainsString("public 'baseURL", $this->getBuffer());
75+
EOF,
76+
str_replace("\n\n", "\n", $this->getStreamFilterBuffer())
77+
);
6278
}
6379

6480
public function testCommandConfigCheckNonexistentClass(): void
6581
{
6682
command('config:check Nonexistent');
6783

68-
$this->assertStringContainsString(
69-
'No such Config class: Nonexistent',
70-
$this->getBuffer()
84+
$this->assertSame(
85+
"No such Config class: Nonexistent\n",
86+
$this->getStreamFilterBuffer()
7187
);
7288
}
7389

74-
public function testGetKintD(): void
90+
public function testConfigCheckWithKintEnabledUsesKintD(): void
7591
{
76-
$command = new ConfigCheck(service('logger'), service('commands'));
77-
$getKintD = $this->getPrivateMethodInvoker($command, 'getKintD');
78-
79-
$output = $getKintD(new App());
80-
81-
$output = preg_replace(
82-
'/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u',
83-
'',
84-
$output
92+
/** @var Closure(object): string $command */
93+
$command = $this->getPrivateMethodInvoker(
94+
new ConfigCheck(service('logger'), service('commands')),
95+
'getKintD'
8596
);
8697

87-
$this->assertStringContainsString(
88-
'Config\App#',
89-
$output
90-
);
91-
$this->assertStringContainsString(
92-
<<<'EOL'
93-
(
94-
public 'baseURL' -> string (19) "http://example.com/"
95-
public 'allowedHostnames' -> array (0) []
96-
public 'indexPage' -> string (9) "index.php"
97-
public 'uriProtocol' -> string (11) "REQUEST_URI"
98-
public 'permittedURIChars' -> string (14) "a-z 0-9~%.:_\-"
99-
public 'defaultLocale' -> string (2) "en"
100-
public 'negotiateLocale' -> boolean false
101-
public 'supportedLocales' -> array (1) [
102-
0 => string (2) "en"
103-
]
104-
public 'appTimezone' -> string (3) "UTC"
105-
public 'charset' -> string (5) "UTF-8"
106-
public 'forceGlobalSecureRequests' -> boolean false
107-
public 'proxyIPs' -> array (0) []
108-
public 'CSPEnabled' -> boolean false
109-
EOL,
110-
$output
98+
command('config:check App');
99+
100+
$this->assertSame(
101+
$command(config('App')) . "\n",
102+
preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer())
111103
);
112104
}
113105

114-
public function testGetVarDump(): void
106+
public function testConfigCheckWithKintDisabledUsesVarDump(): void
115107
{
116-
$command = new ConfigCheck(service('logger'), service('commands'));
117-
$getVarDump = $this->getPrivateMethodInvoker($command, 'getVarDump');
118-
119-
$output = $getVarDump(new App());
120-
121-
if (
122-
ini_get('xdebug.mode')
123-
&& in_array(
124-
'develop',
125-
explode(',', ini_get('xdebug.mode')),
126-
true
127-
)
128-
) {
129-
// Xdebug force adds colors on xdebug.cli_color=2
130-
$output = preg_replace(
131-
'/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u',
132-
'',
133-
$output
134-
);
108+
/** @var Closure(object): string $command */
109+
$command = $this->getPrivateMethodInvoker(
110+
new ConfigCheck(service('logger'), service('commands')),
111+
'getVarDump'
112+
);
113+
$clean = static fn (string $input): string => trim(preg_replace(
114+
'/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u',
115+
'',
116+
$input
117+
));
135118

136-
// Xdebug overloads var_dump().
137-
$this->assertStringContainsString(
138-
'class Config\App#',
139-
$output
140-
);
141-
$this->assertStringContainsString(
142-
<<<'EOL'
143-
{
144-
public string $baseURL =>
145-
string(19) "http://example.com/"
146-
public array $allowedHostnames =>
147-
array(0) {
148-
}
149-
public string $indexPage =>
150-
string(9) "index.php"
151-
public string $uriProtocol =>
152-
string(11) "REQUEST_URI"
153-
public string $permittedURIChars =>
154-
string(14) "a-z 0-9~%.:_\-"
155-
public string $defaultLocale =>
156-
string(2) "en"
157-
public bool $negotiateLocale =>
158-
bool(false)
159-
public array $supportedLocales =>
160-
array(1) {
161-
[0] =>
162-
string(2) "en"
163-
}
164-
public string $appTimezone =>
165-
string(3) "UTC"
166-
public string $charset =>
167-
string(5) "UTF-8"
168-
public bool $forceGlobalSecureRequests =>
169-
bool(false)
170-
public array $proxyIPs =>
171-
array(0) {
172-
}
173-
public bool $CSPEnabled =>
174-
bool(false)
175-
}
176-
EOL,
177-
$output
178-
);
179-
} else {
180-
// PHP's var_dump().
181-
$this->assertStringContainsString(
182-
'object(Config\App)#',
183-
$output
184-
);
185-
$this->assertStringContainsString(
186-
<<<'EOL'
187-
{
188-
["baseURL"]=>
189-
string(19) "http://example.com/"
190-
["allowedHostnames"]=>
191-
array(0) {
192-
}
193-
["indexPage"]=>
194-
string(9) "index.php"
195-
["uriProtocol"]=>
196-
string(11) "REQUEST_URI"
197-
["permittedURIChars"]=>
198-
string(14) "a-z 0-9~%.:_\-"
199-
["defaultLocale"]=>
200-
string(2) "en"
201-
["negotiateLocale"]=>
202-
bool(false)
203-
["supportedLocales"]=>
204-
array(1) {
205-
[0]=>
206-
string(2) "en"
207-
}
208-
["appTimezone"]=>
209-
string(3) "UTC"
210-
["charset"]=>
211-
string(5) "UTF-8"
212-
["forceGlobalSecureRequests"]=>
213-
bool(false)
214-
["proxyIPs"]=>
215-
array(0) {
216-
}
217-
["CSPEnabled"]=>
218-
bool(false)
219-
}
220-
EOL,
221-
$output
119+
try {
120+
Kint::$enabled_mode = false;
121+
command('config:check App');
122+
123+
$this->assertSame(
124+
$clean($command(config('App'))),
125+
$clean(preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer()))
222126
);
127+
} finally {
128+
Kint::$enabled_mode = true;
223129
}
224130
}
225131
}

0 commit comments

Comments
 (0)