Last active
July 28, 2016 14:46
-
-
Save piotrplenik/849a542030d0e7f9b181924bee3f25c5 to your computer and use it in GitHub Desktop.
Sample test with login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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