Skip to content

Instantly share code, notes, and snippets.

@merk
Last active December 22, 2015 08:19
Show Gist options
  • Save merk/6444576 to your computer and use it in GitHub Desktop.
Save merk/6444576 to your computer and use it in GitHub Desktop.
User View Model
{% extends "ProjectUserBundle::base.html.twig" %}
{% do app.breadcrumbs.push('Edit ' ~ data.user.name) %}
{% block body %}
{% include "ProjectUserBundle:User:form.html.twig" with {
action: path('project_user_edit', { id: data.user.id }),
form: data.form,
} %}
{% endblock body %}
{{ form_start(form, { action: action, attr: { class: 'form-horizontal' } }) }}
{{ form_errors(form) }}
<div class="row">
<div class="span6">
{{ form_row(form.username, {
help: 'The username used to login to the system'
}) }}
{{ form_row(form.name, {
help: 'The users full name'
}) }}
{{ form_row(form.email, {
help: 'The users email address. Used for various account functions.'
}) }}
{{ form_row(form.enabled, {
attr: { 'data-toggle-visible': '#password' },
help: 'To disable access to the system, uncheck this box',
label: 'User is enabled',
}) }}
<div id="password">
{{ form_row(form.plainPassword, {
help: 'To set a password fill out this field. If you do not wish to change the
password, leave the field blank.',
label: 'Password',
}) }}
</div>
</div>
</div>
<div class="form-actions">
{{ form_row(form.save, { attr: { class: 'btn btn-primary btn-large' } }) }}
</div>
{{ form_end(form) }}
<?php
/**
* This file is part of the Project
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Project\UserBundle\Controller;
use Project\UserBundle\View\UserEditView;
use Project\UserBundle\View\UserListView;
use Entity\User;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\RouteRedirectView;
use Infinite\Traits\DoctrineORMTrait;
use Infinite\Traits\FormFactoryTrait;
use JMS\DiExtraBundle\Annotation as DI;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Feb;
use Symfony\Component\HttpFoundation\Request;
class UserController
{
use DoctrineORMTrait;
use FormFactoryTrait;
/**
* @DI\Inject("project.repository.user")
* @var \Entity\Repository\UserRepository
*/
public $userRepository;
/**
* Edits a user.
*
* @Feb\Route(
* name="project_user_add",
* pattern="/user/add"
* )
* @Rest\View
*
* @param Request $request
* @return UserEditView
*/
public function addAction(Request $request)
{
$user = new User;
return $this->editAction($request, $user);
}
/**
* Edits a user.
*
* @Feb\Route(
* name="project_user_edit",
* pattern="/user/{id}/edit",
* requirements={
* "id"="\d+"
* }
* )
* @Rest\View
* @Feb\ParamConverter("user")
*
* @param Request $request
* @param User $user
* @return UserEditView
*/
public function editAction(Request $request, User $user)
{
$view = new UserEditView;
$view->user = $user;
$form = $this->createForm('project_user_edit', $user);
$form->handleRequest($request);
$view->setForm($form);
if ($form->isValid()) {
$this->persist($user);
$this->flush();
return RouteRedirectView::create('project_user_list');
}
return $view;
}
/**
* Lists all users.
*
* @Feb\Route(name="project_user_list", pattern="/user")
* @Rest\View
*
* @return UserListView
*/
public function listAction()
{
$view = new UserListView;
$view->users = $this->userRepository->findAll();
return $view;
}
}
<?php
/**
* This file is part of the Project
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Test\ProjectUserBundle\Controller;
use Project\UserBundle\Controller\UserController;
use Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Mockery as m;
use Test\Traits\FormMock;
class UserControllerTest extends \PHPUnit_Framework_TestCase
{
use FormMock;
/**
* @var UserController
*/
private $controller;
/**
* @var \Doctrine\ORM\EntityManager|\Mockery\Mock
*/
private $entityManager;
/**
* @var \Symfony\Component\Form\FormFactoryInterface|\Mockery\Mock
*/
private $formFactory;
/**
* @var \Entity\Repository\UserRepository|\Mockery\Mock
*/
private $userRepository;
public function testAddView()
{
$request = new Request;
$form = $this->mockForm();
$form->shouldReceive('isValid')
->once()
->andReturn(false); // We're not posting yet
$this->formFactory->shouldReceive('create')
->with('argus_user_edit', m::type('Entity\\User'), m::type('array'))
->andReturn($form);
$view = $this->controller->addAction($request);
$this->assertInstanceOf('Project\\UserBundle\\View\\UserEditView', $view);
$this->assertInstanceOf('Symfony\\Component\\Form\\FormViewInterface', $view->form);
$this->assertInstanceOf('Entity\\User', $view->user);
}
public function testEditView()
{
$request = new Request;
$user = new User;
$form = $this->mockForm();
$form->shouldReceive('isValid')
->once()
->andReturn(false); // We're not posting yet
$this->formFactory->shouldReceive('create')
->with('argus_user_edit', $user, m::type('array'))
->andReturn($form);
$view = $this->controller->editAction($request, $user);
$this->assertInstanceOf('Project\\UserBundle\\View\\UserEditView', $view);
$this->assertInstanceOf('Symfony\\Component\\Form\\FormViewInterface', $view->form);
$this->assertSame($user, $view->user);
}
public function testEditSubmit()
{
$request = new Request();
$user = new User;
$form = $this->mockForm();
$form->shouldReceive('isValid')
->once()
->andReturn(true); // We're not posting yet
$this->formFactory->shouldReceive('create')
->with('argus_user_edit', $user, m::type('array'))
->andReturn($form);
$this->entityManager->shouldReceive('persist')
->once()
->with($user);
$this->entityManager->shouldReceive('flush')
->once();
$view = $this->controller->editAction($request, $user);
$this->assertInstanceOf('FOS\RestBundle\View\View', $view);
$this->assertEquals('argus_user_list', $view->getRoute());
}
public function testList()
{
$this->userRepository->shouldReceive('findAll')
->once()
->andReturn(array(new User()));
$view = $this->controller->listAction();
$this->assertInstanceOf('Project\\UserBundle\\View\\UserListView', $view);
$this->assertCount(1, $view->users);
}
protected function setUp()
{
$this->controller = new UserController();
$this->controller->formFactory = $this->formFactory = m::mock('Symfony\\Component\\Form\\FormFactoryInterface');
$this->controller->entityManager = $this->entityManager = m::mock('Doctrine\\ORM\\EntityManager');
$this->controller->userRepository = $this->userRepository = m::mock('Entity\\Repository\\UserRepository');
}
}
<?php
/**
* This file is part of the Project
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Project\UserBundle\View;
use Symfony\Component\Form\FormInterface;
class UserEditView
{
/**
* @var \Entity\User
*/
public $user;
/**
* @var \Symfony\Component\Form\FormInterface
*/
protected $form;
/**
* @return \Symfony\Component\Form\FormView
*/
public function getForm()
{
return $this->form->createView();
}
/**
* @param \Symfony\Component\Form\FormInterface $form
*/
public function setForm(FormInterface $form)
{
$this->form = $form;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment