|
11 | 11 | use Gt\Input\MissingInputParameterException;
|
12 | 12 | use Gt\Input\Test\Helper\Helper;
|
13 | 13 | use Gt\Input\Trigger\Trigger;
|
| 14 | +use Gt\Json\JsonObject; |
| 15 | +use Gt\Json\JsonPrimitive\JsonArrayPrimitive; |
| 16 | +use Gt\Json\JsonPrimitive\JsonStringPrimitive; |
14 | 17 | use PHPUnit\Framework\TestCase;
|
15 | 18 |
|
16 | 19 | class InputTest extends TestCase {
|
@@ -664,6 +667,49 @@ public function testGetMultipleFile():void {
|
664 | 667 | }
|
665 | 668 | }
|
666 | 669 |
|
| 670 | + public function testGetBodyJson_notJson():void { |
| 671 | + $get = []; |
| 672 | + $post = ["this" => "is not JSON!"]; |
| 673 | + $sut = new Input($get, $post); |
| 674 | + $json = $sut->getBodyJson(); |
| 675 | + self::assertNull($json); |
| 676 | + } |
| 677 | + |
| 678 | + public function testGetBodyJson_string():void { |
| 679 | + $inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-"); |
| 680 | + file_put_contents($inputPath, "\"Hello, PHP.Gt!\""); |
| 681 | + $sut = new Input(bodyPath: $inputPath); |
| 682 | + $json = $sut->getBodyJson(); |
| 683 | + self::assertInstanceOf(JsonStringPrimitive::class, $json); |
| 684 | + self::assertSame("Hello, PHP.Gt!", $json->getPrimitiveValue()); |
| 685 | + } |
| 686 | + |
| 687 | + public function testGetBodyJson_arrayWithObject():void { |
| 688 | + $inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-"); |
| 689 | + file_put_contents($inputPath, "[1, 2, 3, {\"name\": \"Cody\"}]"); |
| 690 | + $sut = new Input(bodyPath: $inputPath); |
| 691 | + $json = $sut->getBodyJson(); |
| 692 | + self::assertInstanceOf(JsonArrayPrimitive::class, $json); |
| 693 | + $array = $json->getPrimitiveValue(); |
| 694 | + self::assertSame(1, $array[0]); |
| 695 | + self::assertSame(2, $array[1]); |
| 696 | + self::assertSame(3, $array[2]); |
| 697 | + /** @var JsonObject $thirdArrayElement */ |
| 698 | + $thirdArrayElement = $array[3]; |
| 699 | + self::assertInstanceOf(JsonObject::class, $thirdArrayElement); |
| 700 | + self::assertSame("Cody", $thirdArrayElement->getString("name")); |
| 701 | + } |
| 702 | + |
| 703 | + public function testGetBodyJson_withQueryString():void { |
| 704 | + $inputPath = tempnam(sys_get_temp_dir(), "phpgt-input-test-"); |
| 705 | + file_put_contents($inputPath, "\"Hello, PHP.Gt!\""); |
| 706 | + $sut = new Input(["id" => 123], bodyPath: $inputPath); |
| 707 | + $json = $sut->getBodyJson(); |
| 708 | + self::assertInstanceOf(JsonStringPrimitive::class, $json); |
| 709 | + self::assertSame("Hello, PHP.Gt!", $json->getPrimitiveValue()); |
| 710 | + self::assertSame(123, $sut->getInt("id")); |
| 711 | + } |
| 712 | + |
667 | 713 | public static function dataRandomGetPost():array {
|
668 | 714 | $data = [];
|
669 | 715 |
|
|
0 commit comments