codifico/phpspec-rest-view-extension

REST View matcher

dev-master 2015-10-07 05:51 UTC

This package is not auto-updated.

Last update: 2024-04-27 15:58:16 UTC


README

Build Status Scrutinizer Code Quality

Latest Stable Version Latest Unstable Version License Total Downloads

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