Skip to content

Commit

Permalink
Refactor relevant tests to be more comprehensive
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Stenvall committed Mar 6, 2018
1 parent ea5bf58 commit 1930a19
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions tests/Http/HttpJsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
}

Expand Down

0 comments on commit 1930a19

Please sign in to comment.