Skip to content

Commit 8f402d7

Browse files
committed
test: refactor ConfigCheckTest
1 parent 700ba29 commit 8f402d7

File tree

2 files changed

+61
-165
lines changed

2 files changed

+61
-165
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: 61 additions & 159 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,67 @@ 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-
}
68+
$this->assertSame(
69+
<<<'EOF'
70+
You must specify a Config classname.
5571
56-
public function testCommandConfigCheckApp(): void
57-
{
58-
command('config:check App');
72+
Usage: config:check <classname>
73+
Example: config:check App
74+
config:check 'CodeIgniter\Shield\Config\Auth'
5975

60-
$this->assertStringContainsString(App::class, $this->getBuffer());
61-
$this->assertStringContainsString("public 'baseURL", $this->getBuffer());
76+
EOF,
77+
$this->getStreamFilterBuffer()
78+
);
6279
}
6380

6481
public function testCommandConfigCheckNonexistentClass(): void
6582
{
6683
command('config:check Nonexistent');
6784

68-
$this->assertStringContainsString(
69-
'No such Config class: Nonexistent',
70-
$this->getBuffer()
85+
$this->assertSame(
86+
"No such Config class: Nonexistent\n",
87+
$this->getStreamFilterBuffer()
7188
);
7289
}
7390

74-
public function testGetKintD(): void
91+
public function testConfigCheckWithKintEnabledUsesKintD(): void
7592
{
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
93+
/** @var Closure(object): string $command */
94+
$command = $this->getPrivateMethodInvoker(
95+
new ConfigCheck(service('logger'), service('commands')),
96+
'getKintD'
8597
);
8698

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
99+
command('config:check App');
100+
101+
$this->assertSame(
102+
$command(config('App')) . "\n",
103+
preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer())
111104
);
112105
}
113106

114-
public function testGetVarDump(): void
107+
public function testConfigCheckWithKintDisabledUsesVarDump(): void
115108
{
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-
);
109+
/** @var Closure(object): string $command */
110+
$command = $this->getPrivateMethodInvoker(
111+
new ConfigCheck(service('logger'), service('commands')),
112+
'getVarDump'
113+
);
135114

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
115+
try {
116+
Kint::$enabled_mode = false;
117+
command('config:check App');
118+
119+
$this->assertSame(
120+
$command(config('App')),
121+
preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer())
222122
);
123+
} finally {
124+
Kint::$enabled_mode = true;
223125
}
224126
}
225127
}

0 commit comments

Comments
 (0)