Provides an easy way to manage expectation about Rest View object, based on FOSRestBundle View object.
php composer.phar require codifico/phpspec-rest-view-extension:dev-master --dev
Activate extension by specifying its class in your phpspec.yml:
# phpspec.yml
extensions:
- Codifico\PhpSpec\RestViewExtension\Extension
The spec file usage:
# UserControllerSpec.php
<?php
namespace spec\AppBundle\Controller;
use AppBundle\Controller\UserController;
use FOS\RestBundle\Util\Codes;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @mixin UserController
*
* @method beConstructedWith(UserInterface $user)
*/
class UserControllerSpec extends ObjectBehavior
{
function let(UserInterface $user)
{
$this->beConstructedWith($user);
}
function it_show_the_current_user(UserInterface $user)
{
$this->getUser()->shouldBeRestViewWith([
'data' => $user,
'statusCode' => Codes::HTTP_OK,
'serializationGroups' => ['user_profile'],
'headers' => [
'cache-control' => ['no-cache'],
'date' => ["@[email protected]()"],
]
]);
}
}
and coresponding controller file:
# UserController.php
<?php
namespace AppBundle\Controller;
use FOS\RestBundle\Util\Codes;
use Symfony\Component\Security\Core\User\UserInterface;
use FOS\RestBundle\View\View;
use JMS\Serializer\SerializationContext;
class UserController
{
/**
* @var UserInterface
*/
private $user;
/**
* @param UserInterface $user
*/
public function __construct(UserInterface $user)
{
$this->user = $user;
}
/**
* @return \FOS\RestBundle\View\View|null
*/
public function getUser()
{
$context = SerializationContext::create();
$context->setGroups('user_profile');
$view = View::create($this->user, Codes::HTTP_OK, []);
$view->setSerializationContext($context);
return $view;
}
}
Copyright (c) 2015 Marcin Dryka (drymek). See LICENSE for details.
- Marcin Dryka drymek [lead developer]
- Other awesome developers