|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Commands\Utilities; |
| 13 | + |
| 14 | +use CodeIgniter\Test\CIUnitTestCase; |
| 15 | +use CodeIgniter\Test\StreamFilterTrait; |
| 16 | +use Config\App; |
| 17 | +use Config\Services; |
| 18 | + |
| 19 | +/** |
| 20 | + * @internal |
| 21 | + * |
| 22 | + * @group Others |
| 23 | + */ |
| 24 | +final class ConfigCheckTest extends CIUnitTestCase |
| 25 | +{ |
| 26 | + use StreamFilterTrait; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->resetServices(); |
| 31 | + parent::setUp(); |
| 32 | + } |
| 33 | + |
| 34 | + protected function tearDown(): void |
| 35 | + { |
| 36 | + $this->resetServices(); |
| 37 | + parent::tearDown(); |
| 38 | + } |
| 39 | + |
| 40 | + protected function getBuffer() |
| 41 | + { |
| 42 | + return $this->getStreamFilterBuffer(); |
| 43 | + } |
| 44 | + |
| 45 | + public function testCommandConfigCheckNoArg(): void |
| 46 | + { |
| 47 | + command('config:check'); |
| 48 | + |
| 49 | + $this->assertStringContainsString( |
| 50 | + 'You must specify a Config classname.', |
| 51 | + $this->getBuffer() |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function testCommandConfigCheckApp(): void |
| 56 | + { |
| 57 | + command('config:check App'); |
| 58 | + |
| 59 | + $this->assertStringContainsString(App::class, $this->getBuffer()); |
| 60 | + $this->assertStringContainsString("public 'baseURL", $this->getBuffer()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testCommandConfigCheckNonexistentClass(): void |
| 64 | + { |
| 65 | + command('config:check Nonexistent'); |
| 66 | + |
| 67 | + $this->assertStringContainsString( |
| 68 | + 'No such Config class: Nonexistent', |
| 69 | + $this->getBuffer() |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function testGetKintD() |
| 74 | + { |
| 75 | + $command = new ConfigCheck(Services::logger(), Services::commands()); |
| 76 | + $getKintD = $this->getPrivateMethodInvoker($command, 'getKintD'); |
| 77 | + |
| 78 | + $output = $getKintD(new App()); |
| 79 | + |
| 80 | + $output = preg_replace( |
| 81 | + '/(\033\[[0-9;]+m)|(\035\[[0-9;]+m)/u', |
| 82 | + '', |
| 83 | + $output |
| 84 | + ); |
| 85 | + |
| 86 | + $this->assertStringContainsString( |
| 87 | + 'Config\App#', |
| 88 | + $output |
| 89 | + ); |
| 90 | + $this->assertStringContainsString( |
| 91 | + <<<'EOL' |
| 92 | + ( |
| 93 | + public 'baseURL' -> string (19) "http://example.com/" |
| 94 | + public 'allowedHostnames' -> array (0) [] |
| 95 | + public 'indexPage' -> string (9) "index.php" |
| 96 | + public 'uriProtocol' -> string (11) "REQUEST_URI" |
| 97 | + public 'defaultLocale' -> string (2) "en" |
| 98 | + public 'negotiateLocale' -> boolean false |
| 99 | + public 'supportedLocales' -> array (1) [ |
| 100 | + 0 => string (2) "en" |
| 101 | + ] |
| 102 | + public 'appTimezone' -> string (3) "UTC" |
| 103 | + public 'charset' -> string (5) "UTF-8" |
| 104 | + public 'forceGlobalSecureRequests' -> boolean false |
| 105 | + public 'proxyIPs' -> array (0) [] |
| 106 | + public 'CSPEnabled' -> boolean false |
| 107 | + EOL, |
| 108 | + $output |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + public function testGetVarDump() |
| 113 | + { |
| 114 | + $command = new ConfigCheck(Services::logger(), Services::commands()); |
| 115 | + $getVarDump = $this->getPrivateMethodInvoker($command, 'getVarDump'); |
| 116 | + |
| 117 | + $output = $getVarDump(new App()); |
| 118 | + |
| 119 | + if ( |
| 120 | + ini_get('xdebug.mode') |
| 121 | + && in_array( |
| 122 | + 'develop', |
| 123 | + explode(',', ini_get('xdebug.mode')), |
| 124 | + true |
| 125 | + ) |
| 126 | + ) { |
| 127 | + // Xdebug overloads var_dump(). |
| 128 | + $this->assertStringContainsString( |
| 129 | + 'class Config\App#', |
| 130 | + $output |
| 131 | + ); |
| 132 | + $this->assertStringContainsString( |
| 133 | + <<<'EOL' |
| 134 | + { |
| 135 | + public string $baseURL => |
| 136 | + string(19) "http://example.com/" |
| 137 | + public array $allowedHostnames => |
| 138 | + array(0) { |
| 139 | + } |
| 140 | + public string $indexPage => |
| 141 | + string(9) "index.php" |
| 142 | + public string $uriProtocol => |
| 143 | + string(11) "REQUEST_URI" |
| 144 | + public string $defaultLocale => |
| 145 | + string(2) "en" |
| 146 | + public bool $negotiateLocale => |
| 147 | + bool(false) |
| 148 | + public array $supportedLocales => |
| 149 | + array(1) { |
| 150 | + [0] => |
| 151 | + string(2) "en" |
| 152 | + } |
| 153 | + public string $appTimezone => |
| 154 | + string(3) "UTC" |
| 155 | + public string $charset => |
| 156 | + string(5) "UTF-8" |
| 157 | + public bool $forceGlobalSecureRequests => |
| 158 | + bool(false) |
| 159 | + public array $proxyIPs => |
| 160 | + array(0) { |
| 161 | + } |
| 162 | + public bool $CSPEnabled => |
| 163 | + bool(false) |
| 164 | + } |
| 165 | + EOL, |
| 166 | + $output |
| 167 | + ); |
| 168 | + } else { |
| 169 | + // PHP's var_dump(). |
| 170 | + $this->assertStringContainsString( |
| 171 | + 'object(Config\App)#', |
| 172 | + $output |
| 173 | + ); |
| 174 | + $this->assertStringContainsString( |
| 175 | + <<<'EOL' |
| 176 | + { |
| 177 | + ["baseURL"]=> |
| 178 | + string(19) "http://example.com/" |
| 179 | + ["allowedHostnames"]=> |
| 180 | + array(0) { |
| 181 | + } |
| 182 | + ["indexPage"]=> |
| 183 | + string(9) "index.php" |
| 184 | + ["uriProtocol"]=> |
| 185 | + string(11) "REQUEST_URI" |
| 186 | + ["defaultLocale"]=> |
| 187 | + string(2) "en" |
| 188 | + ["negotiateLocale"]=> |
| 189 | + bool(false) |
| 190 | + ["supportedLocales"]=> |
| 191 | + array(1) { |
| 192 | + [0]=> |
| 193 | + string(2) "en" |
| 194 | + } |
| 195 | + ["appTimezone"]=> |
| 196 | + string(3) "UTC" |
| 197 | + ["charset"]=> |
| 198 | + string(5) "UTF-8" |
| 199 | + ["forceGlobalSecureRequests"]=> |
| 200 | + bool(false) |
| 201 | + ["proxyIPs"]=> |
| 202 | + array(0) { |
| 203 | + } |
| 204 | + ["CSPEnabled"]=> |
| 205 | + bool(false) |
| 206 | + } |
| 207 | + EOL, |
| 208 | + $output |
| 209 | + ); |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments