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 Oct 1, 2021
1 parent d182205 commit 77315fc
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 269 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
91 changes: 91 additions & 0 deletions tests/AbstractStarfleetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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;

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 AbstractStarfleetTest 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',
]);
}

return $user->object();
}

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

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

return $user->object();
}

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

private function getEntityManager()
{
return $this->getContainer()->get('doctrine')->getManager();
}
}
33 changes: 16 additions & 17 deletions tests/Controller/Front/FrontControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,29 @@
use App\Factory\ConferenceFactory;
use App\Factory\ParticipationFactory;
use App\Factory\UserFactory;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Tests\AbstractStarfleetTest;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

class FrontControllerTest extends WebTestCase
class FrontControllerTest extends AbstractStarfleetTest
{
use Factories;
use ResetDatabase;

public function testConferencesWithAcceptedParticipationsAreDisplayed()
{
UserFactory::createMany(2);
$crawler = $this->getClient()->request('GET', '/');

$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('#conferencesTabs', 'Incoming conferences');
$this->assertSelectorTextContains('#conferencesTabs', '🔴 Live conferences! Find us there!');
$this->assertSelectorTextContains('#conferencesTabs', 'Past conferences');
$this->assertCount(1, $crawler->filter('div#future div.card'));
$this->assertCount(1, $crawler->filter('div#live div.card'));
$this->assertCount(1, $crawler->filter('div#past div.card'));
}

protected function generateData()
{
UserFactory::createOne();
$pastConferenceProxy = ConferenceFactory::createOne([
'name' => 'Past Conference',
'excluded' => false,
Expand Down Expand Up @@ -57,17 +68,5 @@ public function testConferencesWithAcceptedParticipationsAreDisplayed()
'marking' => Participation::ACCEPTED,
'conference' => ConferenceFactory::find($futureConferenceProxy),
]);

$this->ensureKernelShutdown();
$client = static::createClient();
$crawler = $client->request('GET', '/');

$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('#conferencesTabs', 'Incoming conferences');
$this->assertSelectorTextContains('#conferencesTabs', '🔴 Live conferences! Find us there!');
$this->assertSelectorTextContains('#conferencesTabs', 'Past conferences');
$this->assertCount(1, $crawler->filter('div#future div.card'));
$this->assertCount(1, $crawler->filter('div#live div.card'));
$this->assertCount(1, $crawler->filter('div#past div.card'));
}
}
39 changes: 11 additions & 28 deletions tests/Controller/Front/SecurityControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,32 @@

namespace App\Tests\Controller\Front;

use App\Factory\UserFactory;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Tests\AbstractStarfleetTest;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

class SecurityControllerTest extends WebTestCase
class SecurityControllerTest extends AbstractStarfleetTest
{
use Factories;
use ResetDatabase;

public function testAdminCanAccessToAdmin()
{
$userProxy = UserFactory::createOne([
'email' => '[email protected]',
'roles' => ['ROLE_ADMIN'],
'name' => 'Admin',
'password' => 'password',
]);

$this->ensureKernelShutdown();
$client = $this->createClient();
$client->loginUser($userProxy->object());
$client->followRedirects();

$client = $this->getClient($this->getAdminUser());
$client->request('GET', '/admin/');

$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('span.user-name', 'Admin');
$this->assertSelectorTextContains('span.user-name', 'Starfleet Admin');
}

public function testUserCannotAccessToAdmin()
{
$userProxy = UserFactory::createOne([
'roles' => ['ROLE_USER'],
]);

$this->ensureKernelShutdown();
$client = $this->createClient();
$client->followRedirects();
$client->loginUser($userProxy->object());

$client->request('GET', '/admin/');
$this->getClient()->request('GET', '/admin/');

$this->assertResponseStatusCodeSame(403);
}

public function testNotAuthenticatedUserCanNotAccessToAdmin()
{
$this->ensureKernelShutdown();
$client = $this->createClient();
$client->followRedirects();

Expand All @@ -67,4 +45,9 @@ public function testNotAuthenticatedUserCanNotAccessToAdmin()
$this->assertResponseIsSuccessful();
$this->assertRouteSame('login');
}

protected function generateData()
{
// No specific data needed for these tests.
}
}
69 changes: 0 additions & 69 deletions tests/Controller/UserAccount/BaseFactories.php

This file was deleted.

52 changes: 28 additions & 24 deletions tests/Controller/UserAccount/FutureConferencesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
namespace App\Tests\Controller\UserAccount;

use App\Factory\ConferenceFactory;
use App\Tests\AbstractStarfleetTest;

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

Expand All @@ -25,31 +29,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 +53,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'),
]);
}
}
Loading

0 comments on commit 77315fc

Please sign in to comment.