Skip to content

test: refactor ConfigCheckTest #9295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\\.$#',
Expand Down
226 changes: 66 additions & 160 deletions tests/system/Commands/Utilities/ConfigCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
Expand All @@ -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 <classname>
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;
}
}
}
Loading