Skip to content

Commit

Permalink
Remove resetDatabase trait from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HedicGuibert committed Sep 30, 2021
1 parent fcf1555 commit 97d256b
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 122 deletions.
3 changes: 3 additions & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ security:
password_hashers:
App\Entity\User:
algorithm: native
encoders:
App\Entity\User:
algorithm: native
providers:
user:
entity:
Expand Down
6 changes: 6 additions & 0 deletions config/packages/test/security.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
security:
encoders:
App\Entity\User:
algorithm: native
cost: 4
time_cost: 3
memory_cost: 10
password_hashers:
App\Entity\User:
algorithm: native
Expand Down
1 change: 1 addition & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def tests(c):
Launch unit and functional tests
"""
with Builder(c):
reset(c)
docker_compose_run(c, 'php ./vendor/bin/simple-phpunit')


Expand Down
89 changes: 89 additions & 0 deletions tests/Controller/UserAccount/AbstractBaseFactories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the Starfleet Project.
*
* (c) Starfleet <[email protected]>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace App\Tests\Controller\UserAccount;

use App\Entity\User;
use App\Factory\UserFactory;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Zenstruck\Foundry\Test\Factories;

abstract class AbstractBaseFactories extends WebTestCase
{
use Factories;

private ?KernelBrowser $client = null;

protected function setUp(): void
{
$purger = new ORMPurger($this->getEntityManager());
$purger->purge();
$this->generateData();
}

protected function getClient(?User $user = null)
{
if (!$this->client) {
$user = $user ?: $this->getTestUser();
$this->ensureKernelShutdown();
$this->client = $this->createClient();
$this->client->followRedirects();
$this->client->loginUser($user);
}

return $this->client;
}

protected function getTestUser()
{
$user = UserFactory::repository()->findOneBy([
'email' => '[email protected]',
]);

if (!$user) {
$user = UserFactory::createOne([
'name' => 'Starfleet User',
'email' => '[email protected]',
'password' => 'password',
]);
}

$user = $user->object();

return $user;
}

protected function getAdminUser()
{
$user = $this->getTestUser()
->addRole('ROLE_ADMIN')
->setName('Starfleet Admin')
->setEmail('[email protected]')
->setPassword('password')
;

$this->getEntityManager()->flush();

return $user;
}

/**
* This method must create all the entities your test will need.
*/
abstract protected function generateData();

private function getEntityManager()
{
return $this->getContainer()->get('doctrine')->getManager();
}
}
69 changes: 0 additions & 69 deletions tests/Controller/UserAccount/BaseFactories.php

This file was deleted.

51 changes: 27 additions & 24 deletions tests/Controller/UserAccount/FutureConferencesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

use App\Factory\ConferenceFactory;

class FutureConferencesControllerTest extends BaseFactories
/**
* @group user_account
*/
class FutureConferencesControllerTest extends AbstractBaseFactories
{
private string $conferencesUrl = '/user/future-conferences';

Expand All @@ -25,31 +28,22 @@ public function testFutureConferencesPageLoad()

public function testConferencesAreDisplayed()
{
ConferenceFactory::createMany(3, [
$crawler = $this->getClient()->request('GET', $this->conferencesUrl);

$featuredConferencesCount = \count(ConferenceFactory::findBy([
'featured' => true,
'startAt' => new \DateTime('+2 days'),
'endAt' => new \DateTime('+5 days'),
]);
]));

ConferenceFactory::createMany(5, [
$regularConferencesCount = \count(ConferenceFactory::findBy([
'featured' => false,
'startAt' => new \DateTime('+2 days'),
'endAt' => new \DateTime('+5 days'),
]);
]));

$crawler = $this->getClient()->request('GET', $this->conferencesUrl);

self::assertCount(3, $crawler->filter('div#featured-conferences-block div.conference-card'));
self::assertCount(5, $crawler->filter('div#regular-conferences-block div.conference-card'));
self::assertCount($featuredConferencesCount, $crawler->filter('div#featured-conferences-block div.conference-card'));
self::assertCount($regularConferencesCount, $crawler->filter('div#regular-conferences-block div.conference-card'));
}

public function testAskParticipationWork()
{
ConferenceFactory::createOne([
'startAt' => new \DateTime('+2 days'),
'endAt' => new \DateTime('+5 days'),
]);

$this->getClient()->request('GET', $this->conferencesUrl);
$this->getClient()->submitForm('Ask Participation');

Expand All @@ -58,15 +52,24 @@ public function testAskParticipationWork()

public function testSubmitTalkWork()
{
ConferenceFactory::createOne([
'startAt' => new \DateTime('+2 days'),
'endAt' => new \DateTime('+5 days'),
'cfpEndAt' => new \DateTime('+5 days'),
]);

$this->getClient()->request('GET', $this->conferencesUrl);
$this->getClient()->submitForm('Submit a Talk');

self::assertResponseIsSuccessful();
}

protected function generateData()
{
ConferenceFactory::createMany(3, [
'featured' => true,
'startAt' => new \DateTime('+1 days'),
'endAt' => new \DateTime('+1 days'),
]);

ConferenceFactory::createMany(3, [
'featured' => false,
'startAt' => new \DateTime('+1 days'),
'endAt' => new \DateTime('+1 days'),
]);
}
}
12 changes: 10 additions & 2 deletions tests/Controller/UserAccount/HomepageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace App\Tests\Controller\UserAccount;

class HomepageControllerTest extends BaseFactories
/**
* @group user_account
*/
class HomepageControllerTest extends AbstractBaseFactories
{
public function testUserPageForUsers()
{
Expand All @@ -25,10 +28,15 @@ public function testUserPageForUsers()
public function testUserPageForAdmins()
{
$user = $this->getAdminUser();
$crawler = $this->getClient()->request('GET', '/user/account');
$crawler = $this->getClient($user)->request('GET', '/user/account');

self::assertResponseIsSuccessful();
self::assertSelectorTextContains('.username', $user->getName());
self::assertCount(1, $crawler->filter('#admin-button'));
}

protected function generateData()
{
// No specific data needed for these tests
}
}
21 changes: 11 additions & 10 deletions tests/Controller/UserAccount/ParticipationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\DomCrawler\Crawler;

class ParticipationControllerTest extends BaseFactories
/**
* @group user_account
*/
class ParticipationControllerTest extends AbstractBaseFactories
{
/** @dataProvider provideRoutes */
public function testAllPagesLoad(string $route)
Expand All @@ -31,8 +34,6 @@ public function testAllPagesLoad(string $route)

public function testParticipationsPageWork()
{
$this->createTestData();

$crawler = $this->getClient()->request('GET', '/user/participations');

self::assertCount(2, $crawler->filter('#pending-participations-block .card'));
Expand All @@ -50,6 +51,7 @@ public function testParticipationsFormWork()
'startAt' => new \DateTime('+10 days'),
'endAt' => new \DateTime('+12 days'),
]);
$preSubmitCount = \count(ParticipationFactory::all());

$this->getClient()->request('GET', '/user/participations');
$this->getClient()->submitForm('submit_participation', [
Expand All @@ -59,8 +61,10 @@ public function testParticipationsFormWork()
'participation[conferenceTicketStatus]' => Participation::STATUS_NOT_NEEDED,
]);

self::assertCount(1, ParticipationFactory::all());
self::assertSame($this->getTestUser(), ParticipationFactory::find(1)->getParticipant());
$allParticipations = ParticipationFactory::all();

self::assertCount($preSubmitCount + 1, $allParticipations);
self::assertSame($this->getTestUser(), $allParticipations[$preSubmitCount]->getParticipant());
}

public function testEditParticipationPageWork()
Expand Down Expand Up @@ -94,8 +98,6 @@ public function testEditParticipationPageWork()
/** @dataProvider provideActionRoutes */
public function testAllEditParticipationLinksWork(string $route)
{
$this->createTestData();

$this->getClient()->request('GET', $route);
$this->getClient()->submitForm('Edit Participation');

Expand All @@ -105,7 +107,6 @@ public function testAllEditParticipationLinksWork(string $route)
/** @dataProvider provideActionRoutes */
public function testAllCancelParticipationLinksWork(string $route)
{
$this->createTestData();
$crawler = $this->getClient()->request('GET', $route);

if ('/user/participations' === $route) {
Expand Down Expand Up @@ -173,7 +174,7 @@ public function testParticipationCancelCsrfProtection()

private function mainPageCancelLink(KernelBrowser $client, Crawler $crawler)
{
$participationRepository = static::$container->get(ParticipationRepository::class);
$participationRepository = $this->getContainer()->get(ParticipationRepository::class);
$participations = $participationRepository->findPendingParticipationsByUser($this->getTestUser());
$preCancelCount = \count($participations);

Expand All @@ -187,7 +188,7 @@ private function mainPageCancelLink(KernelBrowser $client, Crawler $crawler)
self::assertCount(--$preCancelCount, $participationRepository->findPendingParticipationsByUser($this->getTestUser()));
}

private function createTestData()
protected function generateData()
{
ParticipationFactory::createMany(2, [
'participant' => $this->getTestUser(),
Expand Down
Loading

0 comments on commit 97d256b

Please sign in to comment.