|
22 | 22 | use Config\Exceptions as ExceptionsConfig; |
23 | 23 | use Config\Services; |
24 | 24 | use PHPUnit\Framework\Attributes\Group; |
| 25 | +use stdClass; |
25 | 26 |
|
26 | 27 | /** |
27 | 28 | * @internal |
@@ -262,4 +263,127 @@ public function testHighlightFile(): void |
262 | 263 |
|
263 | 264 | $this->restoreIniValues(); |
264 | 265 | } |
| 266 | + |
| 267 | + public function testSanitizeDataWithResource(): void |
| 268 | + { |
| 269 | + $sanitizeData = self::getPrivateMethodInvoker($this->handler, 'sanitizeData'); |
| 270 | + |
| 271 | + // Create a resource (file handle) |
| 272 | + $resource = fopen('php://memory', 'rb'); |
| 273 | + $result = $sanitizeData($resource); |
| 274 | + |
| 275 | + $this->assertIsString($result); |
| 276 | + $this->assertStringStartsWith('[Resource #', $result); |
| 277 | + $this->assertStringEndsWith(']', $result); |
| 278 | + |
| 279 | + fclose($resource); |
| 280 | + } |
| 281 | + |
| 282 | + public function testSanitizeDataWithClosure(): void |
| 283 | + { |
| 284 | + $sanitizeData = self::getPrivateMethodInvoker($this->handler, 'sanitizeData'); |
| 285 | + |
| 286 | + $closure = static fn (): string => 'test'; |
| 287 | + $result = $sanitizeData($closure); |
| 288 | + |
| 289 | + $this->assertSame('[Closure]', $result); |
| 290 | + } |
| 291 | + |
| 292 | + public function testSanitizeDataWithCircularReference(): void |
| 293 | + { |
| 294 | + $sanitizeData = self::getPrivateMethodInvoker($this->handler, 'sanitizeData'); |
| 295 | + |
| 296 | + // Create an object with circular reference |
| 297 | + $obj = new stdClass(); |
| 298 | + $obj->self = $obj; |
| 299 | + |
| 300 | + $result = $sanitizeData($obj); |
| 301 | + |
| 302 | + $this->assertIsArray($result); |
| 303 | + $this->assertArrayHasKey('self', $result); |
| 304 | + $this->assertStringContainsString('*RECURSION*', (string) $result['self']); |
| 305 | + $this->assertStringContainsString('stdClass', (string) $result['self']); |
| 306 | + } |
| 307 | + |
| 308 | + public function testSanitizeDataWithArrayContainingResource(): void |
| 309 | + { |
| 310 | + $sanitizeData = self::getPrivateMethodInvoker($this->handler, 'sanitizeData'); |
| 311 | + |
| 312 | + $resource = fopen('php://memory', 'rb'); |
| 313 | + $data = [ |
| 314 | + 'string' => 'test', |
| 315 | + 'number' => 123, |
| 316 | + 'resource' => $resource, |
| 317 | + ]; |
| 318 | + |
| 319 | + $result = $sanitizeData($data); |
| 320 | + |
| 321 | + $this->assertIsArray($result); |
| 322 | + $this->assertSame('test', $result['string']); |
| 323 | + $this->assertSame(123, $result['number']); |
| 324 | + $this->assertIsString($result['resource']); |
| 325 | + $this->assertStringStartsWith('[Resource #', $result['resource']); |
| 326 | + |
| 327 | + fclose($resource); |
| 328 | + } |
| 329 | + |
| 330 | + public function testSanitizeDataWithObjectContainingResource(): void |
| 331 | + { |
| 332 | + $sanitizeData = self::getPrivateMethodInvoker($this->handler, 'sanitizeData'); |
| 333 | + |
| 334 | + $resource = fopen('php://memory', 'rb'); |
| 335 | + |
| 336 | + $obj = new stdClass(); |
| 337 | + $obj->name = 'test'; |
| 338 | + $obj->connID = $resource; |
| 339 | + $obj->database = 'mydb'; |
| 340 | + |
| 341 | + $result = $sanitizeData($obj); |
| 342 | + |
| 343 | + $this->assertIsArray($result); |
| 344 | + $this->assertSame('test', $result['name']); |
| 345 | + $this->assertSame('mydb', $result['database']); |
| 346 | + $this->assertIsString($result['connID']); |
| 347 | + $this->assertStringStartsWith('[Resource #', $result['connID']); |
| 348 | + |
| 349 | + fclose($resource); |
| 350 | + } |
| 351 | + |
| 352 | + public function testSanitizeDataWithNestedObjects(): void |
| 353 | + { |
| 354 | + $sanitizeData = self::getPrivateMethodInvoker($this->handler, 'sanitizeData'); |
| 355 | + |
| 356 | + $resource = fopen('php://memory', 'rb'); |
| 357 | + |
| 358 | + $inner = new stdClass(); |
| 359 | + $inner->connID = $resource; |
| 360 | + $inner->host = 'localhost'; |
| 361 | + |
| 362 | + $outer = new stdClass(); |
| 363 | + $outer->db = $inner; |
| 364 | + $outer->cache = 'file'; |
| 365 | + |
| 366 | + $result = $sanitizeData($outer); |
| 367 | + |
| 368 | + $this->assertIsArray($result); |
| 369 | + $this->assertSame('file', $result['cache']); |
| 370 | + $this->assertIsArray($result['db']); |
| 371 | + $this->assertSame('localhost', $result['db']['host']); |
| 372 | + $this->assertIsString($result['db']['connID']); |
| 373 | + $this->assertStringStartsWith('[Resource #', $result['db']['connID']); |
| 374 | + |
| 375 | + fclose($resource); |
| 376 | + } |
| 377 | + |
| 378 | + public function testSanitizeDataWithScalars(): void |
| 379 | + { |
| 380 | + $sanitizeData = self::getPrivateMethodInvoker($this->handler, 'sanitizeData'); |
| 381 | + |
| 382 | + $this->assertSame('string', $sanitizeData('string')); |
| 383 | + $this->assertSame(123, $sanitizeData(123)); |
| 384 | + $this->assertEqualsWithDelta(45.67, $sanitizeData(45.67), PHP_FLOAT_EPSILON); |
| 385 | + $this->assertTrue($sanitizeData(true)); |
| 386 | + $this->assertFalse($sanitizeData(false)); |
| 387 | + $this->assertNull($sanitizeData(null)); |
| 388 | + } |
265 | 389 | } |
0 commit comments