Skip to content

Commit

Permalink
BasicPHPUnitUsageExampleTest, AdvancedPHPUnitUsageExampleTest update …
Browse files Browse the repository at this point in the history
…of readme.
  • Loading branch information
lzakrzewski committed Oct 14, 2015
1 parent a436a21 commit ad38686
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 80 deletions.
99 changes: 40 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ My target was to avoid wasting time for dropping/creating or purging database fo

This library puts contents of database to memory and share it between every tests.

**Notice** I don't recommend to use this library with large fixtures because it can cause huge memory usage.
I prefer to run tests with minimal database data because it is more readable for me and it have better performance.
**Notice:** I don't recommend to use this library with large fixtures because it can cause huge memory usage.
I prefer to run tests with minimal database setup because it is more readable for me and it have better performance.

Requirements
------------
Expand All @@ -25,7 +25,7 @@ Features
--------
- It supports **SqlitePlatform** and **MySqlPlatform**,
- It can create database backup per PHP process,
- It can clear database in fast way,
- It can purge database in fast way,
- It can restore database from backup before every test,
- It can restore clear database before every test.

Expand All @@ -40,78 +40,59 @@ composer require lucaszz/doctrine-database-backup "~1.0"
Basic usage (PHPUnit example)
--------
```php
//Incomplete...
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
/** {@inheritdoc} */
protected function setUp()
{
parent::setUp();

$this->entityManager = $this->createEntityManager();
$this->entityManager = $this->createEntityManager();

$backup = new DoctrineDatabaseBackup($this->entityManager);

if (!$backup->isCreated()) {
$backup->clearDatabase();
$backup->create();
}

$backup->restore();
}
$backup = new DoctrineDatabaseBackup($this->entityManager);
$backup->restoreClearDatabase();
}
```

This database setup prepares clear database before every test.


[Full working example](https://github.com/Lucaszz/DoctrineDatabaseBackup/blob/master/tests/Integration/BasicPHPUnitUsageExampleTest.php).

Advanced usage (PHPUnit example)
--------
```php
//Incomplete...
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();

$this->entityManager = $this->createEntityManager();

$backup = new DoctrineDatabaseBackup($this->entityManager);

if (!$backup->isCreated()) {
$backup->clearDatabase();
$backup->create();
}

$backup->restore();
/** {@inheritdoc} */
protected function setUp()
{
parent::setUp();

$this->entityManager = $this->createEntityManager();
$backup = new DoctrineDatabaseBackup($this->entityManager);

if (!$backup->getExecutor()->isBackupCreated()) {
$backup->getPurger()->purge();

//your fixtures
$this->entityManager->persist(new TestProduct('Iron', 99));
$this->entityManager->flush();

$backup->getExecutor()->create();
}

$backup->getExecutor()->restore();
}
```

This database setup database with your fixtures before every test.
[Full working example](https://github.com/Lucaszz/DoctrineDatabaseBackup/blob/master/tests/Integration/AdvancedPHPUnitUsageExampleTest.php).

[Full working examples](https://github.com/Lucaszz/DoctrineDatabaseBackup/blob/master/tests/Integration/ExampleTest.php)

**Notice that before first test of PHP process database should be created.**
**Notice:** that before first test of PHP process database should be created.

Behat example
--------
```php
//Incomplete...
/**
* @BeforeScenario
*/
public function restoreDatabase()
{
// "getEntityManager" is your own getter for EntityManager
$backup = new DoctrineDatabaseBackup($this->getEntityManager());

if (!$backup->isCreated()) {
$backup->clearDatabase();
$backup->create();
}

$backup->restore();
}
/** @BeforeScenario*/
public function restoreDatabase()
{
// "getEntityManager" is your own getter for EntityManager
$backup = new DoctrineDatabaseBackup($this->getEntityManager());
$backup->restoreClearDatabase();
}
```
118 changes: 118 additions & 0 deletions tests/Integration/AdvancedPHPUnitUsageExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Lucaszz\DoctrineDatabaseBackup\tests\Backup\Executor;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use Lucaszz\DoctrineDatabaseBackup\Backup\DoctrineDatabaseBackup;
use Lucaszz\DoctrineDatabaseBackup\Backup\Executor\SqliteExecutor;
use Lucaszz\DoctrineDatabaseBackup\tests\Integration\Entity\TestProduct;

class AdvancedPHPUnitUsageExampleTest extends \PHPUnit_Framework_TestCase
{
/** @var EntityManager */
private $entityManager;

public function testThatItAddsProduct()
{
$this->entityManager->persist(new TestProduct('Teapot', 25));
$this->entityManager->flush();

$this->assertCount(2, $this->entityManager->getRepository('\Lucaszz\DoctrineDatabaseBackup\tests\Integration\Entity\TestProduct')->findAll());
}

public function testThatDatabaseContainsFixtures()
{
$this->assertCount(1, $this->entityManager->getRepository('\Lucaszz\DoctrineDatabaseBackup\tests\Integration\Entity\TestProduct')->findAll());
}

/**
* Before first test of PHP process database should be created.
*/
public static function setUpBeforeClass()
{
$entityManager = self::createEntityManager();
self::setupDatabase($entityManager);

//Should be called only if another test in current PHP process created backup.
SqliteExecutor::clearMemory();
}

/** {@inheritdoc} */
protected function setUp()
{
parent::setUp();

$this->entityManager = $this->createEntityManager();
$backup = new DoctrineDatabaseBackup($this->entityManager);

if (!$backup->getExecutor()->isBackupCreated()) {
$backup->getPurger()->purge();

//your fixtures
$this->entityManager->persist(new TestProduct('Iron', 99));
$this->entityManager->flush();

$backup->getExecutor()->create();
}

$backup->getExecutor()->restore();
}

/** {@inheritdoc} */
protected function tearDown()
{
$this->entityManager = null;
}

/**
* Example of creating EntityManager.
*/
private static function createEntityManager()
{
$entityPath = array(__DIR__.'/Entity');

$config = Setup::createAnnotationMetadataConfiguration($entityPath, false);
$driver = new AnnotationDriver(new AnnotationReader(), $entityPath);
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);

return EntityManager::create(self::getParams(), $config);
}

/**
* Example of setup database before test.
*/
private static function setupDatabase(EntityManager $entityManager)
{
$params = self::getParams();

$tmpConnection = DriverManager::getConnection($params);
$tmpConnection->getSchemaManager()->createDatabase($params['path']);

$schemaTool = new SchemaTool($entityManager);
$schemaTool->dropDatabase();

$class = 'Lucaszz\DoctrineDatabaseBackup\tests\Integration\Entity\TestProduct';

$schemaTool->createSchema(array($entityManager->getClassMetadata($class)));
}

/**
* Example of Database connection parameters.
*/
private static function getParams()
{
return array(
'driver' => 'pdo_sqlite',
'user' => 'root',
'password' => '',
'path' => __DIR__.'/database/sqlite.db',
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,25 @@
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use Lucaszz\DoctrineDatabaseBackup\Backup\DoctrineDatabaseBackup;
use Lucaszz\DoctrineDatabaseBackup\Backup\Executor\SqliteExecutor;
use Lucaszz\DoctrineDatabaseBackup\tests\Integration\Entity\TestProduct;

class ExampleTest extends \PHPUnit_Framework_TestCase
class BasicPHPUnitUsageExampleTest extends \PHPUnit_Framework_TestCase
{
/** @var EntityManager */
private $entityManager;

public function testThatDatabaseContainsProduct()
public function testThatItAddsProduct()
{
$this->entityManager->persist(new TestProduct('Teapot', 25));
$this->entityManager->flush();

$this->assertCount(1, $this->entityManager->getRepository('\Lucaszz\DoctrineDatabaseBackup\tests\Integration\Entity\TestProduct')->findAll());
}

public function testThatDatabaseIsClearBeforeNextTest()
public function testThatDatabaseIsClear()
{
$this->entityManager->persist(new TestProduct('Iron', 99));
$this->entityManager->flush();

$this->assertCount(1, $this->entityManager->getRepository('\Lucaszz\DoctrineDatabaseBackup\tests\Integration\Entity\TestProduct')->findAll());
$this->assertCount(0, $this->entityManager->getRepository('\Lucaszz\DoctrineDatabaseBackup\tests\Integration\Entity\TestProduct')->findAll());
}

/**
Expand All @@ -40,30 +38,23 @@ public static function setUpBeforeClass()
{
$entityManager = self::createEntityManager();
self::setupDatabase($entityManager);

//Should be called only if another test in current PHP process created backup.
SqliteExecutor::clearMemory();
}

/**
* {@inheritdoc}
*/
/** {@inheritdoc} */
protected function setUp()
{
parent::setUp();

$this->entityManager = $this->createEntityManager();

$backup = new DoctrineDatabaseBackup($this->entityManager);

if (!$backup->isCreated()) {
$backup->clearDatabase();
$backup->create();
}

$backup->restore();
$backup->restoreClearDatabase();
}

/**
* {@inheritdoc}
*/
/** {@inheritdoc} */
protected function tearDown()
{
$this->entityManager = null;
Expand All @@ -87,7 +78,7 @@ private static function createEntityManager()
/**
* Example of setup database before test.
*/
private function setupDatabase(EntityManager $entityManager)
private static function setupDatabase(EntityManager $entityManager)
{
$params = self::getParams();

Expand Down

0 comments on commit ad38686

Please sign in to comment.