|
7 | 7 | use Flow\ETL\Row\Entry\StructureEntry; |
8 | 8 | use Flow\ETL\Rows; |
9 | 9 | use Flow\ETL\Tests\FlowTestCase; |
| 10 | +use Flow\Types\Exception\CastingException; |
10 | 11 | use Http\Mock\Client; |
11 | 12 | use Nyholm\Psr7\Factory\Psr17Factory; |
12 | 13 | use Nyholm\Psr7\Response; |
@@ -93,6 +94,48 @@ public function test_http_extractor(): void |
93 | 94 | static::assertSame('tomaszhanc', $tomekResponseBody['login']); |
94 | 95 | } |
95 | 96 |
|
| 97 | + public function test_schema_typed_response_body_with_empty_body(): void |
| 98 | + { |
| 99 | + $factory = new Psr17Factory(); |
| 100 | + $client = new Client($factory); |
| 101 | + $client->addResponse(new Response(200, ['Content-Type' => 'application/json'], '{}')); |
| 102 | + |
| 103 | + $this->expectException(CastingException::class); |
| 104 | + |
| 105 | + from_static_http_requests( |
| 106 | + $client, |
| 107 | + [$factory->createRequest('GET', 'https://api.github.com/users/norberttech')], |
| 108 | + schema(structure_schema('response_body', type_structure([ |
| 109 | + 'login' => type_string(), |
| 110 | + 'id' => type_integer(), |
| 111 | + ]))), |
| 112 | + ) |
| 113 | + ->extract(flow_context(config())) |
| 114 | + ->current(); |
| 115 | + } |
| 116 | + |
| 117 | + public function test_schema_typed_response_body_with_missing_field(): void |
| 118 | + { |
| 119 | + $factory = new Psr17Factory(); |
| 120 | + $client = new Client($factory); |
| 121 | + $client->addResponse(new Response(200, ['Content-Type' => 'application/json'], json_encode([ |
| 122 | + 'login' => 'norberttech', |
| 123 | + ], JSON_THROW_ON_ERROR))); |
| 124 | + |
| 125 | + $this->expectException(CastingException::class); |
| 126 | + |
| 127 | + from_static_http_requests( |
| 128 | + $client, |
| 129 | + [$factory->createRequest('GET', 'https://api.github.com/users/norberttech')], |
| 130 | + schema(structure_schema('response_body', type_structure([ |
| 131 | + 'login' => type_string(), |
| 132 | + 'id' => type_integer(), |
| 133 | + ]))), |
| 134 | + ) |
| 135 | + ->extract(flow_context(config())) |
| 136 | + ->current(); |
| 137 | + } |
| 138 | + |
96 | 139 | public function test_schema_typed_response_body(): void |
97 | 140 | { |
98 | 141 | $factory = new Psr17Factory(); |
|
0 commit comments