Test custom json response

This commit is contained in:
Ruben 2016-09-04 12:36:41 +01:00
parent 87e29f23a0
commit c1c45b9172
4 changed files with 103 additions and 3 deletions

View File

@ -0,0 +1,94 @@
<?php
namespace EmotionHero\Api;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* Extend JsonResponse to allow custom serialiser
*/
class CustomJsonResponse extends JsonResponse {
protected $serializer_callback;
/**
* Constructor.
*
* @param mixed $data The response data
* @param int $status The response status code
* @param array $headers An array of response headers
*/
public function __construct($data = null, $serializer_callback = null, $status = 200, $headers = array())
{
Response::__construct('', $status, $headers);
$this->serializer_callback = $serializer_callback == null ? [$this, 'serializeDefault'] : $serializer_callback;
if(!is_callable($this->serializer_callback)) {
throw new \RuntimeException("Invalid serializer callback provided", 1);
}
if (null === $data) {
$data = new \ArrayObject();
}
$this->setData($data);
}
/**
* {@inheritdoc}
*/
public static function create($data = null, $status = 200, $headers = array())
{
return new static($data, null, $status, $headers);
}
/**
* Sets the data to be sent as JSON.
*
* @param mixed $data
*
* @return JsonResponse
*
* @throws \InvalidArgumentException
*/
public function setData($data = array())
{
$data = call_user_func($this->serializer_callback, $data);
$this->data = $data;
return $this->update();
}
/**
* Set
* @param [type] $data [description]
* @return [type] [description]
*/
public static function serializeDefault($data) {
if (defined('HHVM_VERSION')) {
// HHVM does not trigger any warnings and let exceptions
// thrown from a JsonSerializable object pass through.
// If only PHP did the same...
$data = json_encode($data, $this->encodingOptions);
} else {
try {
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
// objects in a new exception that needs to be removed.
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
$data = json_encode($data, $this->encodingOptions);
} catch (\Exception $e) {
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
throw $e->getPrevious() ?: $e;
}
throw $e;
}
}
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(json_last_error_msg());
}
return $data;
}
}

View File

@ -47,8 +47,11 @@ class ScoreControllerProvider implements ControllerProviderInterface
$game = $request->attributes->get('game');
return $app['serializer']->serialize($game, 'json');
if(empty($game)) {
return new CustomJsonResponse(['message' => 'Game not found'], $app['serializer.json'], 404);
}
return new CustomJsonResponse($game, $app['serializer.json']);
})
->bind('game')
->convert('game', function($game, Request $request) use ($app){ return $app['entity.manager']->getRepository(Models\Game::class)->find($request->attributes->get('gameId'));});

View File

@ -95,7 +95,7 @@ class Game
/**
* Gets the value of level.
*
*
* @return mixed
*/
public function getLevel()

View File

@ -42,6 +42,9 @@ $app['users'] = function () use ($eh) {
$app['serializer'] = function () use ($eh) {
return JMS\Serializer\SerializerBuilder::create()->build();
};
$app['serializer.json'] = function ($data) use ($app) {
return $app['serializer']->serialize($data, 'json');
};
$app['security.firewalls'] = array(
'login' => [