Navigation Menu

Skip to content

Commit

Permalink
Speed up tests for CI
Browse files Browse the repository at this point in the history
- Store MySQL data in memory
- Authenticate user without going to /login
  • Loading branch information
B-Galati committed Jun 18, 2018
1 parent 5fcc192 commit 5fd0315
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 69 deletions.
6 changes: 3 additions & 3 deletions docker-compose.ci.yml
Expand Up @@ -6,14 +6,14 @@ services:
environment:
- SYMFONY_ENV=test
- APP_ENV=test
volumes:
- ./var/cache:/app/var/cache
- ./var/logs:/app/var/logs
build:
cache_from:
- ${APP_DOCKER_IMAGE_NAME}:${APP_DOCKER_IMAGE_MD5}
- ${APP_DOCKER_IMAGE_NAME}:dev

db:
tmpfs: /var/lib/mysql

selenium:
ports:
- 5900:5900
2 changes: 2 additions & 0 deletions docker-compose.yml
Expand Up @@ -19,6 +19,8 @@ services:

db:
image: mysql:5.7
volumes:
- ./docker/dev/my.cnf:/etc/mysql/conf.d/my.cnf
environment:
MYSQL_ROOT_PASSWORD: root

Expand Down
2 changes: 2 additions & 0 deletions docker/dev/my.cnf
@@ -0,0 +1,2 @@
# https://stackoverflow.com/questions/10386037/how-can-i-speed-up-my-phpunit-dbunit-test-suite-execution
innodb_flush_log_at_trx_commit = 2
67 changes: 26 additions & 41 deletions tests/Controller/ControllerTestTrait.php
Expand Up @@ -2,17 +2,18 @@

namespace Tests\AppBundle\Controller;

use AppBundle\DataFixtures\ORM\LoadAdherentData;
use AppBundle\Entity\Adherent;
use AppBundle\Entity\EventCategory;
use AppBundle\Entity\ReferentTag;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
use Tests\AppBundle\TestHelperTrait;

/**
Expand Down Expand Up @@ -50,57 +51,42 @@ public function assertClientIsRedirectedTo(string $path, Client $client, bool $w
);
}

public function logout(Client $client): Crawler
public function logout(Client $client): void
{
$client->request(Request::METHOD_GET, '/deconnexion');
$session = $client->getContainer()->get('session');

return $client->followRedirect();
$client->getCookieJar()->clear();
$session->set('_security_main_context', null);
$session->save();
}

public function authenticateAsAdherent(Client $client, string $emailAddress, string $password = LoadAdherentData::DEFAULT_PASSWORD): Crawler
public function authenticateAsAdherent(Client $client, string $emailAddress): void
{
$crawler = $client->request(Request::METHOD_GET, '/connexion');

$this->assertResponseStatusCode(Response::HTTP_OK, $client->getResponse());

$client->submit($crawler->selectButton('Connexion')->form([
'_login_email' => $emailAddress,
'_login_password' => $password,
]));

$shouldBeRedirectedTo = 'http://'.$this->hosts['app'].'/evenements';

if ($shouldBeRedirectedTo !== $client->getResponse()->headers->get('location')) {
$this->fail(
'Authentication as '.$emailAddress.' failed: check the credentials used in authenticateAsAdherent() '.
'and ensure you are properly loading adherents fixtures.'
);
if (!$user = $this->getAdherentRepository()->findOneBy(['emailAddress' => $emailAddress])) {
throw new \Exception(sprintf('Adherent %s not found', $emailAddress));
}

return $client->followRedirect();
$this->authenticate($client, $user);
}

public function authenticateAsAdmin(Client $client, string $emailAddress = 'admin@en-marche-dev.fr', string $password = 'admin'): Crawler
public function authenticateAsAdmin(Client $client, string $email = 'admin@en-marche-dev.fr'): void
{
$crawler = $client->request(Request::METHOD_GET, '/admin/login');

$this->assertResponseStatusCode(Response::HTTP_OK, $client->getResponse());
if (!$user = $this->getAdministratorRepository()->loadUserByUsername($email)) {
throw new \Exception(sprintf('Admin %s not found', $email));
}

$client->submit($crawler->selectButton('Connexion')->form([
'_login_email' => $emailAddress,
'_login_password' => $password,
]));
$this->authenticate($client, $user);
}

$shouldBeRedirectedTo = 'http://'.$this->hosts['app'].'/admin/dashboard';
private function authenticate(Client $client, UserInterface $user): void
{
$session = $client->getContainer()->get('session');

if ($shouldBeRedirectedTo !== $client->getResponse()->headers->get('location')) {
$this->fail(
'Authentication as '.$emailAddress.' failed: check the credentials used in authenticateAsAdmin() '.
'and ensure you are properly loading adherents fixtures.'
);
}
$token = new UsernamePasswordToken($user, null, 'main_context', $user->getRoles());
$session->set('_security_main_context', serialize($token));
$session->save();

return $client->followRedirect();
$client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
}

protected function getFirstPrefixForm(Form $form): ?string
Expand Down Expand Up @@ -186,10 +172,9 @@ protected function init(array $fixtures = [], string $host = 'app')

protected function kill()
{
$this->loadFixtures([]);
$this->client = null;
$this->container->get('doctrine')->getConnection()->close();
$this->container = null;
$this->manager->getConnection()->close();
$this->manager = null;
$this->hosts = [];

Expand Down
17 changes: 12 additions & 5 deletions tests/Controller/EnMarche/AdherentControllerTest.php
Expand Up @@ -53,7 +53,8 @@ public function testMyEventsPageIsProtected(): void

public function testAuthenticatedAdherentCanSeeHisUpcomingAndPastEvents(): void
{
$crawler = $this->authenticateAsAdherent($this->client, 'jacques.picard@en-marche.fr');
$this->authenticateAsAdherent($this->client, 'jacques.picard@en-marche.fr');
$crawler = $this->client->request(Request::METHOD_GET, '/');

$this->assertResponseStatusCode(Response::HTTP_OK, $this->client->getResponse());
$crawler = $this->client->click($crawler->selectLink('Mes activités')->link());
Expand Down Expand Up @@ -358,6 +359,7 @@ public function testEditAdherentInterests(): void
$this->assertClientIsRedirectedTo('/espace-adherent/mon-compte/centres-d-interet', $this->client);

/* @var Adherent $adherent */
$this->container->get('doctrine.orm.entity_manager')->clear();
$adherent = $this->getAdherentRepository()->findOneByEmail('carl999@example.fr');

$this->assertSame(array_values($chosenInterests), $adherent->getInterests());
Expand Down Expand Up @@ -619,7 +621,9 @@ public function testAnonymousUserCannotCreateCitizenProject(): void

public function testAdherentCanCreateNewCitizenProject(): void
{
$crawler = $this->authenticateAsAdherent($this->client, 'carl999@example.fr');
$this->authenticateAsAdherent($this->client, 'carl999@example.fr');

$crawler = $this->client->request(Request::METHOD_GET, '/');
$this->assertSame(3, $crawler->selectLink('Lancer mon projet')->count());

$this->client->request(Request::METHOD_GET, '/espace-adherent/creer-mon-projet-citoyen');
Expand All @@ -628,7 +632,8 @@ public function testAdherentCanCreateNewCitizenProject(): void

public function testCitizenProjectAdministratorCannotCreateAnotherCitizenProject(): void
{
$crawler = $this->authenticateAsAdherent($this->client, 'jacques.picard@en-marche.fr');
$this->authenticateAsAdherent($this->client, 'jacques.picard@en-marche.fr');
$crawler = $this->client->request(Request::METHOD_GET, '/');
$this->assertSame(0, $crawler->selectLink('Lancer mon projet')->count());

$this->client->request(Request::METHOD_GET, '/espace-adherent/creer-mon-projet-citoyen');
Expand Down Expand Up @@ -731,7 +736,8 @@ public function testCreateCitizenProjectSuccessful(): void
*/
public function testCommitteesAdherentsHostsAreNotAllowedToCreateNewCommittees(string $emailAddress): void
{
$crawler = $this->authenticateAsAdherent($this->client, $emailAddress);
$this->authenticateAsAdherent($this->client, $emailAddress);
$crawler = $this->client->request(Request::METHOD_GET, '/');
$this->assertSame(0, $crawler->selectLink('Créer un comité')->count());

// Try to cheat the system with a direct URL access.
Expand All @@ -758,7 +764,8 @@ public function provideCommitteesHostsAdherentsCredentials(): array
*/
public function testRegularAdherentCanCreateOneNewCommittee(string $emaiLAddress, string $phone): void
{
$crawler = $this->authenticateAsAdherent($this->client, $emaiLAddress);
$this->authenticateAsAdherent($this->client, $emaiLAddress);
$crawler = $this->client->request(Request::METHOD_GET, '/');
$crawler = $this->client->click($crawler->selectLink('Créer un comité')->link());

$this->assertResponseStatusCode(Response::HTTP_OK, $this->client->getResponse());
Expand Down
3 changes: 2 additions & 1 deletion tests/Controller/EnMarche/CitizenProjectControllerTest.php
Expand Up @@ -287,7 +287,8 @@ public function testCommitteeSupportCitizenProject()
public function testCitizenProjectContactActors()
{
// Authenticate as the administrator (host)
$crawler = $this->authenticateAsAdherent($this->client, 'lolodie.dutemps@hotnix.tld');
$this->authenticateAsAdherent($this->client, 'lolodie.dutemps@hotnix.tld');
$crawler = $this->client->request(Request::METHOD_GET, '/evenements');
$crawler = $this->client->click($crawler->selectLink('En Marche - Projet citoyen')->link());
$crawler = $this->client->click($crawler->selectLink('Voir')->link());

Expand Down
15 changes: 10 additions & 5 deletions tests/Controller/EnMarche/CommitteeControllerTest.php
Expand Up @@ -38,7 +38,8 @@ public function testRedirectionComiteFromOldUrl()
public function testAuthenticatedCommitteeSupervisorCannotUnfollowCommittee()
{
// Login as supervisor
$crawler = $this->authenticateAsAdherent($this->client, 'jacques.picard@en-marche.fr');
$this->authenticateAsAdherent($this->client, 'jacques.picard@en-marche.fr');
$crawler = $this->client->request(Request::METHOD_GET, '/evenements');

$crawler = $this->client->click($crawler->selectLink('En Marche Paris 8')->link());
$this->assertResponseStatusCode(Response::HTTP_OK, $this->client->getResponse());
Expand All @@ -53,7 +54,8 @@ public function testAuthenticatedCommitteeSupervisorCannotUnfollowCommittee()
public function testAuthenticatedCommitteeHostCanUnfollowCommittee()
{
// Login as host
$crawler = $this->authenticateAsAdherent($this->client, 'gisele-berthoux@caramail.com');
$this->authenticateAsAdherent($this->client, 'gisele-berthoux@caramail.com');
$crawler = $this->client->request(Request::METHOD_GET, '/evenements');
$crawler = $this->client->click($crawler->selectLink('En Marche Paris 8')->link());

$this->assertResponseStatusCode(Response::HTTP_OK, $this->client->getResponse());
Expand All @@ -80,7 +82,8 @@ public function testAuthenticatedCommitteeHostCanUnfollowCommittee()
$this->client->getCookieJar()->clear();

// Login again as supervisor
$crawler = $this->authenticateAsAdherent($this->client, 'jacques.picard@en-marche.fr');
$this->authenticateAsAdherent($this->client, 'jacques.picard@en-marche.fr');
$crawler = $this->client->request(Request::METHOD_GET, '/evenements');

$crawler = $this->client->click($crawler->selectLink('En Marche Paris 8')->link());

Expand Down Expand Up @@ -252,7 +255,8 @@ public function testAuthenticatedAdherentCanShowCommitteePage()

public function testAuthenticatedCommitteeFollowerCanShowCommitteePage()
{
$crawler = $this->authenticateAsAdherent($this->client, 'carl999@example.fr');
$this->authenticateAsAdherent($this->client, 'carl999@example.fr');
$crawler = $this->client->request(Request::METHOD_GET, '/evenements');
$crawler = $this->client->click($crawler->selectLink('En Marche Paris 8')->link());

$this->assertResponseStatusCode(Response::HTTP_OK, $this->client->getResponse());
Expand All @@ -269,7 +273,8 @@ public function testAuthenticatedCommitteeFollowerCanShowCommitteePage()

public function testAuthenticatedCommitteeHostCanShowCommitteePage()
{
$crawler = $this->authenticateAsAdherent($this->client, 'gisele-berthoux@caramail.com');
$this->authenticateAsAdherent($this->client, 'gisele-berthoux@caramail.com');
$crawler = $this->client->request(Request::METHOD_GET, '/evenements');
$crawler = $this->client->click($crawler->selectLink('En Marche Paris 8')->link());

$this->assertResponseStatusCode(Response::HTTP_OK, $this->client->getResponse());
Expand Down

0 comments on commit 5fd0315

Please sign in to comment.