-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BasicPHPUnitUsageExampleTest, AdvancedPHPUnitUsageExampleTest update …
…of readme.
- Loading branch information
1 parent
a436a21
commit ad38686
Showing
3 changed files
with
170 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters