Skip to content

Instantly share code, notes, and snippets.

@jmather
Created October 4, 2012 19:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmather/3836004 to your computer and use it in GitHub Desktop.
Save jmather/3836004 to your computer and use it in GitHub Desktop.
Authenticate a user login
<?php
namespace Application\Sonata\UserBundle\Tests\Functional;
use IC\Bundle\Base\TestBundle\Test\WebTestCase;
class LoginTest extends WebTestCase
{
/**
* {@inheritdoc}
*/
protected static function getFixtureList()
{
return array(
'Application\Sonata\UserBundle\DataFixtures\ORM\LoadUserData',
);
}
public function testLoadingLoginPage()
{
$client = $this->getClient();
$client->followRedirects(true);
$crawler = $client->request('GET', '/admin/dashboard');
$this->assertGreaterThan(0, $crawler->filter('html:contains("Username:")')->count());
}
public function testLogginIn()
{
$client = $this->getClient();
$client->followRedirects(true);
$crawler = $client->request('GET', '/admin/dashboard');
/** @var $super_admin \Application\Sonata\UserBundle\Entity\User */
$super_admin = $this->getReferenceRepository()->getReference('user.super_admin');
$data = array(
'_username' => $super_admin->getUsername(),
'_password' => 'test',
);
$form = $crawler->selectButton('Login')->form();
$crawler = $client->submit($form, $data);
$this->assertGreaterThan(0, $crawler->filter('html:contains("Users")')->count());
}
public function testNotLoggedInUser()
{
$client = $this->getClient();
$client->followRedirects(true);
$crawler = $client->request('GET', '/admin/dashboard');
$data = array(
'_username' => 'broken',
'_password' => 'test',
);
$form = $crawler->selectButton('Login')->form();
$crawler = $client->submit($form, $data);
$this->assertGreaterThan(0, $crawler->filter('html:contains("Bad credentials")')->count());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment