|
2 | 2 |
|
3 | 3 | namespace CodeIgniter; |
4 | 4 |
|
| 5 | +use CodeIgniter\Exceptions\CastException; |
5 | 6 | use CodeIgniter\I18n\Time; |
6 | 7 | use CodeIgniter\Test\ReflectionHelper; |
7 | 8 | use Tests\Support\SomeEntity; |
@@ -430,6 +431,93 @@ public function testCastAsJSONArray() |
430 | 431 | $this->assertEquals($data, $entity->eleventh); |
431 | 432 | } |
432 | 433 |
|
| 434 | + public function testCastAsJSONErrorDepth() |
| 435 | + { |
| 436 | + $entity = $this->getCastEntity(); |
| 437 | + |
| 438 | + // Create array with depth 513 to get depth error |
| 439 | + $array = []; |
| 440 | + $value = "test value"; |
| 441 | + $keys = rtrim(str_repeat('test.', 513), '.'); |
| 442 | + $keys = explode(".", $keys); |
| 443 | + $current = &$array; |
| 444 | + foreach ($keys as $key) |
| 445 | + { |
| 446 | + $current = &$current[$key]; |
| 447 | + } |
| 448 | + $current = $value; |
| 449 | + |
| 450 | + $this->expectException(CastException::class); |
| 451 | + $this->expectExceptionMessage('Maximum stack depth exceeded'); |
| 452 | + |
| 453 | + $entity->tenth = $array; |
| 454 | + $this->getPrivateProperty($entity, 'tenth'); |
| 455 | + } |
| 456 | + |
| 457 | + public function testCastAsJSONErrorUTF8() |
| 458 | + { |
| 459 | + $entity = $this->getCastEntity(); |
| 460 | + |
| 461 | + $this->expectException(CastException::class); |
| 462 | + $this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded'); |
| 463 | + |
| 464 | + $entity->tenth = "\xB1\x31"; |
| 465 | + $this->getPrivateProperty($entity, 'tenth'); |
| 466 | + } |
| 467 | + |
| 468 | + public function testCastAsJSONSyntaxError() |
| 469 | + { |
| 470 | + $entity = new Entity(); |
| 471 | + |
| 472 | + $method = $this->getPrivateMethodInvoker($entity,'castAsJson'); |
| 473 | + |
| 474 | + $this->expectException(CastException::class); |
| 475 | + $this->expectExceptionMessage('Syntax error, malformed JSON'); |
| 476 | + |
| 477 | + $method("{ this is bad string", true); |
| 478 | + } |
| 479 | + |
| 480 | + public function testCastAsJSONAnotherErrorDepth() |
| 481 | + { |
| 482 | + $entity = new Entity(); |
| 483 | + |
| 484 | + $method = $this->getPrivateMethodInvoker($entity,'castAsJson'); |
| 485 | + |
| 486 | + $this->expectException(CastException::class); |
| 487 | + $this->expectExceptionMessage('Maximum stack depth exceeded'); |
| 488 | + |
| 489 | + $string = '{'.str_repeat('"test":{', 513).'"test":"value"'.str_repeat('}', 513).'}'; |
| 490 | + |
| 491 | + $method($string, true); |
| 492 | + } |
| 493 | + |
| 494 | + public function testCastAsJSONControlCharCheck() |
| 495 | + { |
| 496 | + $entity = new Entity(); |
| 497 | + |
| 498 | + $method = $this->getPrivateMethodInvoker($entity,'castAsJson'); |
| 499 | + |
| 500 | + $this->expectException(CastException::class); |
| 501 | + $this->expectExceptionMessage('Unexpected control character found'); |
| 502 | + |
| 503 | + $string = "{\n\t\"property1\": \"The quick brown fox\njumps over the lazy dog\",\n\t\"property2\":\"value2\"\n}"; |
| 504 | + |
| 505 | + $method($string, true); |
| 506 | + } |
| 507 | + |
| 508 | + public function testCastAsJSONStateMismatch() |
| 509 | + { |
| 510 | + $entity = new Entity(); |
| 511 | + |
| 512 | + $method = $this->getPrivateMethodInvoker($entity,'castAsJson'); |
| 513 | + |
| 514 | + $this->expectException(CastException::class); |
| 515 | + $this->expectExceptionMessage('Underflow or the modes mismatch'); |
| 516 | + |
| 517 | + $string = '[{"name":"jack","product_id":"1234"]'; |
| 518 | + |
| 519 | + $method($string, true); |
| 520 | + } |
433 | 521 | //-------------------------------------------------------------------- |
434 | 522 |
|
435 | 523 | public function testAsArray() |
@@ -648,7 +736,7 @@ protected function getCastNullableEntity() |
648 | 736 | 'casts' => [ |
649 | 737 | 'string_null' => '?string', |
650 | 738 | 'string_empty' => 'string', |
651 | | - 'integner_null' => '?integer', |
| 739 | + 'integer_null' => '?integer', |
652 | 740 | 'integer_0' => 'integer', |
653 | 741 | ], |
654 | 742 | 'dates' => [], |
|
0 commit comments