Skip to content

Commit

Permalink
Delete data from database before running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexislefebvre committed May 13, 2022
1 parent 497ed50 commit d49fca1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
1 change: 0 additions & 1 deletion tests/App/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class User implements UserInterface
* @var int
* @ORM\Id()
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

Expand Down
3 changes: 0 additions & 3 deletions tests/Test/WebTestCaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public function testIndexAuthenticatedClient(): void
*/
public function testIndexAuthenticationLoginAs(): void
{
$this->schemaUpdate();
$user = $this->loadTestFixtures();

$loginAs = $this->loginAs($user, 'secured_area');
Expand Down Expand Up @@ -151,7 +150,6 @@ public function testIndexAuthenticationLoginAs(): void
*/
public function testIndexAuthenticationLoginClient(): void
{
$this->schemaUpdate();
$user = $this->loadTestFixtures();

$this->client = static::makeClient();
Expand Down Expand Up @@ -189,7 +187,6 @@ public function testIndexAuthenticationLoginClient(): void
*/
public function testAllowedQueriesExceededException(): void
{
$this->schemaUpdate();
$user = $this->loadTestFixtures();

$this->assertInstanceOf(
Expand Down
43 changes: 26 additions & 17 deletions tests/Traits/LiipAcmeFixturesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,14 @@

trait LiipAcmeFixturesTrait
{
public function schemaUpdate(): void
{
// Create database
$kernel = $this->getContainer()->get('kernel');

$application = new Application($kernel);

$command = $application->find('doctrine:schema:update');
$commandTester = new CommandTester($command);
$return = $commandTester->execute([
'--force' => true,
]);

$this->assertSame(0, $return, $commandTester->getDisplay());
}

public function loadTestFixtures(): User
{
$this->resetSchema();

$user1 = new User();
$user1->setId(1);
$user1->setName('foo bar');
$user1->setEmail('foo@bar.com');
$user1->setEmail('foo@example');
$user1->setPassword('12341234');
$user1->setAlgorithm('plaintext');
$user1->setEnabled(true);
Expand All @@ -52,10 +38,33 @@ public function loadTestFixtures(): User
$user2 = clone $user1;

$user2->setId(2);
$user2->setName('alice bob');
$user2->setEmail('[email protected]');

$manager->persist($user2);
$manager->flush();

return $user1;
}

private function resetSchema(): void
{
// Create database
$kernel = $this->getContainer()->get('kernel');

$application = new Application($kernel);

$command = $application->find('doctrine:schema:update');
$commandTester = new CommandTester($command);
$return = $commandTester->execute([
'--force' => true,
]);

$this->assertSame(0, $return, $commandTester->getDisplay());

$manager = $this->getContainer()->get('doctrine')->getManager();

$connection = $manager->getConnection();
$connection->query('DELETE FROM liip_user');
}
}

0 comments on commit d49fca1

Please sign in to comment.