codifico / phpspec-rest-view-extension
REST View matcher
Installs: 767
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 3
Open Issues: 0
Type:phpspec-extension
Requires
- php: >=5.5
- coduo/php-matcher: ^1.1
- friendsofsymfony/rest-bundle: ~1.5
- jms/serializer: *
- phpspec/phpspec: ~2.0
Requires (Dev)
- behat/behat: ~3.0,>=3.0.4
- phpunit/phpunit: ~4.0.7
- symfony/process: ~2.1
This package is not auto-updated.
Last update: 2025-01-18 19:46:42 UTC
README
Provides an easy way to manage expectation about Rest View object, based on FOSRestBundle View object.
Instalation
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
Usage
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' => ["@string@.isDateTime()"], ] ]); } }
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
Copyright (c) 2015 Marcin Dryka (drymek). See LICENSE for details.
Contributors
- Marcin Dryka drymek [lead developer]
- Other awesome developers