Skip to content

[DoctrineBridge] Add a DoctrineTestCase class that rollbacks after each test #19401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Symfony/Bridge/Doctrine/Test/DoctrineTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Test;

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase;

/**
* @author Thomas Royer <thomas.royer.12@gmail.com>
*/
class DoctrineTestCase extends WebTestCase
{
/**
* @var EntityManager
*/
private $entityManager;

public function setEntityManager(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}

public function setUp()
{
parent::setUp();

$this->entityManager->beginTransaction();
}

public function tearDown()
{
parent::tearDown();

$this->entityManager->rollback();
}
}
65 changes: 65 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Test/DoctrineTestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Test;

use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bridge\Doctrine\Test\DoctrineTestCase;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Tests\Fixtures\User;

class DoctrineTestCaseTest extends DoctrineTestCase
{
private static $em;

public static function setUpBeforeClass()
{
parent::setUpBeforeClass();

self::$em = DoctrineTestHelper::createTestEntityManager();
self::createSchema();
}

public function setUp()
{
$this->setEntityManager(self::$em);

parent::setUp();
}

public function testItInsertsData()
{
$user = new User(1, 1, 'user');

self::$em->persist($user);
self::$em->flush();

$this->assertEquals(1, count(self::$em->getRepository(User::class)->findAll()));
}

public function testItHasRollbacked()
{
$user = new User(1, 2, 'user');

self::$em->persist($user);
self::$em->flush();

$this->assertEquals(1, count(self::$em->getRepository(User::class)->findAll()));
}

private static function createSchema()
{
$schemaTool = new SchemaTool(self::$em);
$schemaTool->createSchema(array(
self::$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\User'),
));
}
}