From 47132821345102a2c408ef5995b695b072cb10d1 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sat, 30 Nov 2024 21:57:36 +0800 Subject: [PATCH] test: refactor `ConfigCheckTest` --- phpstan-baseline.php | 6 - .../Commands/Utilities/ConfigCheckTest.php | 226 +++++------------- 2 files changed, 66 insertions(+), 166 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index e6f139f55012..e775d6b9f5ef 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -12265,12 +12265,6 @@ 'count' => 1, 'path' => __DIR__ . '/tests/system/Commands/Translation/LocalizationFinderTest.php', ]; -$ignoreErrors[] = [ - // identifier: missingType.return - 'message' => '#^Method CodeIgniter\\\\Commands\\\\Utilities\\\\ConfigCheckTest\\:\\:getBuffer\\(\\) has no return type specified\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/tests/system/Commands/Utilities/ConfigCheckTest.php', -]; $ignoreErrors[] = [ // identifier: missingType.return 'message' => '#^Method CodeIgniter\\\\Commands\\\\Utilities\\\\NamespacesTest\\:\\:getBuffer\\(\\) has no return type specified\\.$#', diff --git a/tests/system/Commands/Utilities/ConfigCheckTest.php b/tests/system/Commands/Utilities/ConfigCheckTest.php index c075ac3df422..c69cfa948635 100644 --- a/tests/system/Commands/Utilities/ConfigCheckTest.php +++ b/tests/system/Commands/Utilities/ConfigCheckTest.php @@ -13,9 +13,12 @@ namespace CodeIgniter\Commands\Utilities; +use Closure; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; use Config\App; +use Kint\Kint; +use Kint\Renderer\CliRenderer; use PHPUnit\Framework\Attributes\Group; /** @@ -26,6 +29,26 @@ final class ConfigCheckTest extends CIUnitTestCase { use StreamFilterTrait; + public static function setUpBeforeClass(): void + { + App::$override = false; + + putenv('NO_COLOR=1'); + CliRenderer::$cli_colors = false; + + parent::setUpBeforeClass(); + } + + public static function tearDownAfterClass(): void + { + App::$override = true; + + putenv('NO_COLOR'); + CliRenderer::$cli_colors = true; + + parent::tearDownAfterClass(); + } + protected function setUp(): void { $this->resetServices(); @@ -38,188 +61,71 @@ protected function tearDown(): void parent::tearDown(); } - protected function getBuffer() - { - return $this->getStreamFilterBuffer(); - } - - public function testCommandConfigCheckNoArg(): void + public function testCommandConfigCheckWithNoArgumentPassed(): void { command('config:check'); - $this->assertStringContainsString( - 'You must specify a Config classname.', - $this->getBuffer() - ); - } - - public function testCommandConfigCheckApp(): void - { - command('config:check App'); + $this->assertSame( + <<<'EOF' + You must specify a Config classname. + Usage: config:check + Example: config:check App + config:check 'CodeIgniter\Shield\Config\Auth' - $this->assertStringContainsString(App::class, $this->getBuffer()); - $this->assertStringContainsString("public 'baseURL", $this->getBuffer()); + EOF, + str_replace("\n\n", "\n", $this->getStreamFilterBuffer()) + ); } public function testCommandConfigCheckNonexistentClass(): void { command('config:check Nonexistent'); - $this->assertStringContainsString( - 'No such Config class: Nonexistent', - $this->getBuffer() + $this->assertSame( + "No such Config class: Nonexistent\n", + $this->getStreamFilterBuffer() ); } - public function testGetKintD(): void + public function testConfigCheckWithKintEnabledUsesKintD(): void { - $command = new ConfigCheck(service('logger'), service('commands')); - $getKintD = $this->getPrivateMethodInvoker($command, 'getKintD'); - - $output = $getKintD(new App()); - - $output = preg_replace( - '/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u', - '', - $output + /** @var Closure(object): string $command */ + $command = $this->getPrivateMethodInvoker( + new ConfigCheck(service('logger'), service('commands')), + 'getKintD' ); - $this->assertStringContainsString( - 'Config\App#', - $output - ); - $this->assertStringContainsString( - <<<'EOL' - ( - public 'baseURL' -> string (19) "http://example.com/" - public 'allowedHostnames' -> array (0) [] - public 'indexPage' -> string (9) "index.php" - public 'uriProtocol' -> string (11) "REQUEST_URI" - public 'permittedURIChars' -> string (14) "a-z 0-9~%.:_\-" - public 'defaultLocale' -> string (2) "en" - public 'negotiateLocale' -> boolean false - public 'supportedLocales' -> array (1) [ - 0 => string (2) "en" - ] - public 'appTimezone' -> string (3) "UTC" - public 'charset' -> string (5) "UTF-8" - public 'forceGlobalSecureRequests' -> boolean false - public 'proxyIPs' -> array (0) [] - public 'CSPEnabled' -> boolean false - EOL, - $output + command('config:check App'); + + $this->assertSame( + $command(config('App')) . "\n", + preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer()) ); } - public function testGetVarDump(): void + public function testConfigCheckWithKintDisabledUsesVarDump(): void { - $command = new ConfigCheck(service('logger'), service('commands')); - $getVarDump = $this->getPrivateMethodInvoker($command, 'getVarDump'); - - $output = $getVarDump(new App()); - - if ( - ini_get('xdebug.mode') - && in_array( - 'develop', - explode(',', ini_get('xdebug.mode')), - true - ) - ) { - // Xdebug force adds colors on xdebug.cli_color=2 - $output = preg_replace( - '/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u', - '', - $output - ); + /** @var Closure(object): string $command */ + $command = $this->getPrivateMethodInvoker( + new ConfigCheck(service('logger'), service('commands')), + 'getVarDump' + ); + $clean = static fn (string $input): string => trim(preg_replace( + '/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u', + '', + $input + )); - // Xdebug overloads var_dump(). - $this->assertStringContainsString( - 'class Config\App#', - $output - ); - $this->assertStringContainsString( - <<<'EOL' - { - public string $baseURL => - string(19) "http://example.com/" - public array $allowedHostnames => - array(0) { - } - public string $indexPage => - string(9) "index.php" - public string $uriProtocol => - string(11) "REQUEST_URI" - public string $permittedURIChars => - string(14) "a-z 0-9~%.:_\-" - public string $defaultLocale => - string(2) "en" - public bool $negotiateLocale => - bool(false) - public array $supportedLocales => - array(1) { - [0] => - string(2) "en" - } - public string $appTimezone => - string(3) "UTC" - public string $charset => - string(5) "UTF-8" - public bool $forceGlobalSecureRequests => - bool(false) - public array $proxyIPs => - array(0) { - } - public bool $CSPEnabled => - bool(false) - } - EOL, - $output - ); - } else { - // PHP's var_dump(). - $this->assertStringContainsString( - 'object(Config\App)#', - $output - ); - $this->assertStringContainsString( - <<<'EOL' - { - ["baseURL"]=> - string(19) "http://example.com/" - ["allowedHostnames"]=> - array(0) { - } - ["indexPage"]=> - string(9) "index.php" - ["uriProtocol"]=> - string(11) "REQUEST_URI" - ["permittedURIChars"]=> - string(14) "a-z 0-9~%.:_\-" - ["defaultLocale"]=> - string(2) "en" - ["negotiateLocale"]=> - bool(false) - ["supportedLocales"]=> - array(1) { - [0]=> - string(2) "en" - } - ["appTimezone"]=> - string(3) "UTC" - ["charset"]=> - string(5) "UTF-8" - ["forceGlobalSecureRequests"]=> - bool(false) - ["proxyIPs"]=> - array(0) { - } - ["CSPEnabled"]=> - bool(false) - } - EOL, - $output + try { + Kint::$enabled_mode = false; + command('config:check App'); + + $this->assertSame( + $clean($command(config('App'))), + $clean(preg_replace('/\s+Config Caching: \S+/', '', $this->getStreamFilterBuffer())) ); + } finally { + Kint::$enabled_mode = true; } } }