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 5d45790
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 122 deletions.
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
88 changes: 88 additions & 0 deletions tests/Controller/UserAccount/AbstractBaseFactories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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) {
$this->ensureKernelShutdown();
$this->client = $this->createClient();
$this->client->followRedirects();
$this->client->loginUser($user ?: $this->getTestUser());
}

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.

55 changes: 31 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,26 @@ 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'),
]);
'startAt' => new \DateTime('+1 days'),
'endAt' => new \DateTime('+1 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);
'startAt' => new \DateTime('+1 days'),
'endAt' => new \DateTime('+1 days'),
]));

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 +56,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(5, [
'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
}
}
18 changes: 8 additions & 10 deletions tests/Controller/UserAccount/ParticipationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\DomCrawler\Crawler;

class ParticipationControllerTest extends BaseFactories
class ParticipationControllerTest extends AbstractBaseFactories
{
/** @dataProvider provideRoutes */
public function testAllPagesLoad(string $route)
Expand All @@ -31,8 +31,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 +48,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 +58,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 +95,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 +104,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 +171,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 +185,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 5d45790

Please sign in to comment.