Skip to content

Instantly share code, notes, and snippets.

@piotrplenik
Last active July 28, 2016 14:46
Show Gist options
  • Save piotrplenik/849a542030d0e7f9b181924bee3f25c5 to your computer and use it in GitHub Desktop.
Save piotrplenik/849a542030d0e7f9b181924bee3f25c5 to your computer and use it in GitHub Desktop.
Sample test with login
<?php
/*
* This file is part of the MyJobCompany platform.
*
* © MyJobCompany https://myjob.company
*/
namespace tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class SampleControlerTest extends WebTestCase
{
/** @var Client */
private $client;
/** @var RouterInterface */
private $router;
public function setUp()
{
$this->client = static::createClient();
self::bootKernel();
$container = static::$kernel->getContainer();
$this->router = $container->get('router');
}
public function testLogin()
{
$profileRoute = $this->router->generate('enduser_profile_edit', ['_locale' => 'fr'], UrlGeneratorInterface::ABSOLUTE_URL);
$loginRoute = $this->router->generate('login', ['_locale' => 'fr'], UrlGeneratorInterface::ABSOLUTE_URL);
$dashboard = $this->router->generate('recruiter_dashboard_index', ['_locale' => 'fr'], UrlGeneratorInterface::ABSOLUTE_URL);
$crawler = $this->client->request(
'GET',
$profileRoute
);
$this->assertTrue($this->client->getResponse()->isRedirect($loginRoute));
$crawler = $this->logIn();
$this->assertEquals($crawler->getUri(), $dashboard);
}
private function logIn()
{
$this->client = static::createClient();
$this->client->followRedirects();
$crawler = $this->client->request('GET', $this->router->generate('login', ['_locale' => 'fr']));
$form = $crawler->selectButton('Se connecter')->form();
$crawler = $this->client->submit($form, ['_username' => '[email protected]', '_password' => 'lol2012MDR']);
return $crawler;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment