Skip to content

Instantly share code, notes, and snippets.

@ollo-ride-nico
Created January 18, 2019 21:20
Show Gist options
  • Save ollo-ride-nico/68a388f54a472599fd1d30242bea9574 to your computer and use it in GitHub Desktop.
Save ollo-ride-nico/68a388f54a472599fd1d30242bea9574 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by Nicolas Dirollo.
* Date: 13/01/2019
* Time: 18:57
* @package Symfony
* @author Nicolas Dirollo
* @copyright 2019 ND
* @license *
*/
namespace AppBundle\Controller;
use AppBundle\Entity\Task;
use AppBundle\Form\TaskType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* @Route("/tasks/create", name="task_create")
* Class CreateTaskController
* @package AppBundle\Controller
*/
class CreateTaskController extends Controller
{
public function __invoke(Request $request)
{
$task = new Task();
// On récupère l 'utilisateur de la session
$user = $this->getUser();
$form = $this->createForm(TaskType::class, $task);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// On attribut le 'username' de la session à la tache créée
$task->setAuthor($user->getUsername());
$em = $this->getDoctrine()->getManager();
$em->persist($task);
$em->flush();
$this->addFlash('success', 'La tâche a été bien été ajoutée.');
return $this->redirectToRoute('task_list');
}
return $this->render(
'task/create.html.twig',
['form' => $form->createView()]
);
}
}
<?php
/**
* Created by Nicolas Dirollo.
* Date: 15/01/2019
* Time: 17:39
* @package Symfony
* @author Nicolas Dirollo
* @copyright 2019 ND
* @license *
*/
namespace Tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
class CreateTaskControllerTest extends WebTestCase
{
private $client = null;
public function setUp()
{
$this->client = static::createClient();
}
public function testPageIsUp()
{
$this->logIn();
$crawler = $this->client->request('GET', '/tasks/create');
// Test
$this->assertEquals(
1,
$crawler->filter('form')->count()
);
$this->assertTrue($this->client->getResponse()->isSuccessful());
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
// Select the form
$form = $crawler->selectButton('Ajouter')->form();
// set some values
$form['task[title]'] = 'A test title';
$form['task[content]'] = 'A great content!';
// submit the form
$this->client->submit($form);
// Test
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
private function logIn()
{
$session = $this->client->getContainer()->get('session');
// the firewall context (defaults to the firewall name)
$firewall = 'main';
$token = new UsernamePasswordToken('Nico', null, $firewall, array('ROLE_ADMIN'));
$session->set('_security_'.$firewall, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
}
MBP-de-Dirollo:P8-Todo Dirollo$ vendor/bin/phpunit --filter=CreateTaskControllerTest
PHPUnit 7.5.1 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 589 ms, Memory: 20.00MB
There was 1 error:
1) Tests\AppBundle\Controller\CreateTaskControllerTest::testPageIsUp
Error: Call to a member function getUsername() on null
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/src/AppBundle/Controller/CreateTaskController.php:40
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:151
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:200
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Client.php:68
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:131
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:318
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:259
/Users/Dirollo/Qsync/Oc/P8 ToDo/Todo/P8-Todo/tests/AppBundle/Controller/CreateTaskControllerTest.php:51
ERRORS!
Tests: 1, Assertions: 3, Errors: 1.
security:
encoders:
AppBundle\Entity\User: bcrypt
providers:
doctrine:
entity:
class: AppBundle:User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
pattern: ^/
form_login:
login_path: login
check_path: login_check
always_use_default_target_path: true
default_target_path: /
logout: ~
logout_on_user_change: true
access_control:
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
#- { path: ^/users, roles: ROLE_USER }
#- { path: ^/tasks, roles: ROLE_USER }
- { path: ^/homepage, roles: ROLE_ADMIN }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
[2019-01-18 22:19:28] request.INFO: Matched route "task_create". {"route":"task_create","route_parameters":{"_controller":"AppBundle\\Controller\\CreateTaskController::__invoke","_route":"task_create"},"request_uri":"http://localhost/tasks/create","method":"GET"} []
[2019-01-18 22:19:28] security.DEBUG: Read existing security token from the session. {"key":"_security_main","token_class":"Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken"} []
[2019-01-18 22:19:28] security.DEBUG: Stored the security token in the session. {"key":"_security_main"} []
[2019-01-18 22:19:29] request.INFO: Matched route "task_create". {"route":"task_create","route_parameters":{"_controller":"AppBundle\\Controller\\CreateTaskController::__invoke","_route":"task_create"},"request_uri":"http://localhost/tasks/create","method":"POST"} []
[2019-01-18 22:19:29] security.DEBUG: Read existing security token from the session. {"key":"_security_main","token_class":"Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken"} []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment