From 1930a19594b7c246d06c2142e0fcb56357a420b7 Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Tue, 6 Mar 2018 17:43:37 +0200 Subject: [PATCH] Refactor relevant tests to be more comprehensive --- tests/Http/HttpJsonResponseTest.php | 58 +++++++++++++++-------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/tests/Http/HttpJsonResponseTest.php b/tests/Http/HttpJsonResponseTest.php index def5310322d4..858f1d08bfe4 100644 --- a/tests/Http/HttpJsonResponseTest.php +++ b/tests/Http/HttpJsonResponseTest.php @@ -75,45 +75,49 @@ public function testSetAndRetrieveStatusCode() } /** + * @param mixed $data + * * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Type is not supported + * + * @dataProvider jsonErrorDataProvider */ - public function testJsonErrorResource() + public function testInvalidArgumentExceptionOnJsonError($data) { - $resource = tmpfile(); - $response = new \Illuminate\Http\JsonResponse(['resource' => $resource]); + new \Illuminate\Http\JsonResponse(['data' => $data]); } - public function testJsonErrorResourceWithPartialOutputOnError() + /** + * @param mixed $data + * + * @dataProvider jsonErrorDataProvider + */ + public function testGracefullyHandledSomeJsonErrorsWithPartialOutputOnError($data) { - $resource = tmpfile(); - $response = new \Illuminate\Http\JsonResponse(['resource' => $resource], 200, [], JSON_PARTIAL_OUTPUT_ON_ERROR); - $data = $response->getData(); - $this->assertInstanceOf('stdClass', $data); - $this->assertNull($data->resource); + new \Illuminate\Http\JsonResponse(['data' => $data], 200, [], JSON_PARTIAL_OUTPUT_ON_ERROR); } - public function testJsonErrorRecursionDetectedWithPartialOutputOnError() + /** + * @return array + */ + public function jsonErrorDataProvider() { - $objectA = new \stdClass(); - $objectB = new \stdClass(); - $objectA->b = $objectB; - $objectB->a = $objectA; - - $response = new \Illuminate\Http\JsonResponse($objectA, 200, [], JSON_PARTIAL_OUTPUT_ON_ERROR); - $data = $response->getData(); - - $this->assertNotNull($data); - } + // Resources can't be encoded + $resource = tmpfile(); - public function testJsonErrorInfOrNanWithPartialOutputOnError() - { - $data = ['product' => NAN]; + // Recursion can't be encoded + $recursiveObject = new \stdClass(); + $objectB = new \stdClass(); + $recursiveObject->b = $objectB; + $objectB->a = $recursiveObject; - $response = new \Illuminate\Http\JsonResponse($data, 200, [], JSON_PARTIAL_OUTPUT_ON_ERROR); - $data = $response->getData(); + // NAN or INF can't be encoded + $nan = NAN; - $this->assertNotNull($data); + return [ + [$resource], + [$recursiveObject], + [$nan], + ]; } }