Skip to content

Commit c1ed0ec

Browse files
[11.x] json assertions on streamed content (#54565)
* json assertions on streamed content * Update TestResponse.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 16da841 commit c1ed0ec

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,12 @@ public function assertJsonIsObject($key = null)
10491049
*/
10501050
public function decodeResponseJson()
10511051
{
1052-
$testJson = new AssertableJsonString($this->getContent());
1052+
if ($this->baseResponse instanceof StreamedResponse ||
1053+
$this->baseResponse instanceof StreamedJsonResponse) {
1054+
$testJson = new AssertableJsonString($this->streamedContent());
1055+
} else {
1056+
$testJson = new AssertableJsonString($this->getContent());
1057+
}
10531058

10541059
$decodedResponse = $testJson->json();
10551060

tests/Testing/TestResponseTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,40 @@ public function testAssertStreamedJsonContent()
325325
}
326326
}
327327

328+
public function testJsonAssertionsOnStreamedJsonContent()
329+
{
330+
$response = TestResponse::fromBaseResponse(
331+
new StreamedJsonResponse([
332+
'data' => $this->yieldTestModels(),
333+
])
334+
);
335+
336+
$response->assertJsonPath('data', [
337+
['id' => 1],
338+
['id' => 2],
339+
['id' => 3],
340+
]);
341+
342+
try {
343+
$response->assertJsonPath('data', [
344+
['id' => 1],
345+
['id' => 2],
346+
]);
347+
$this->fail('xxxx');
348+
} catch (AssertionFailedError $e) {
349+
$this->assertSame('Failed asserting that two arrays are identical.', $e->getMessage());
350+
}
351+
352+
$response->assertJsonCount(3, 'data');
353+
354+
try {
355+
$response->assertJsonCount(2, 'data');
356+
$this->fail('xxxx');
357+
} catch (AssertionFailedError $e) {
358+
$this->assertSame("Failed to assert that the response count matched the expected 2\nFailed asserting that actual size 3 matches expected size 2.", $e->getMessage());
359+
}
360+
}
361+
328362
public function yieldTestModels()
329363
{
330364
yield new TestModel(['id' => 1]);

0 commit comments

Comments
 (0)