-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from tfe2012/refactoring_test_base
refactoring test base class
- Loading branch information
Showing
11 changed files
with
285 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ConnectionsBundle\Tests\Functional; | ||
|
||
use Doctrine\Common\Annotations\AnnotationRegistry; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Driver\DriverException; | ||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\ORM\EntityManager; | ||
use Symfony\Bridge\Doctrine\RegistryInterface; | ||
use Symfony\Bundle\FrameworkBundle\Client; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Base function test class. | ||
*/ | ||
abstract class AbstractTestCase extends WebTestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $setUpDbFile = null; | ||
|
||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* Sets up required info before each test. | ||
*/ | ||
protected function setUp() | ||
{ | ||
AnnotationRegistry::registerFile( | ||
'vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' | ||
); | ||
|
||
$container = static::createClient()->getContainer(); | ||
|
||
$name = $container->getParameter('database_name'); | ||
|
||
$connection = DriverManager::getConnection( | ||
[ | ||
'driver' => $container->getParameter('database_driver'), | ||
'host' => $container->getParameter('database_host'), | ||
'port' => $container->getParameter('database_port'), | ||
'user' => $container->getParameter('database_user'), | ||
'password' => $container->getParameter('database_password'), | ||
'charset' => 'UTF8', | ||
] | ||
); | ||
$name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name); | ||
|
||
$connection->getSchemaManager()->dropAndCreateDatabase($name); | ||
$connection->close(); | ||
|
||
if ($this->getSetUpDbFile() !== null) { | ||
$this->executeLargeSqlFile(static::getRootDir($container) . $this->getSetUpDbFile()); | ||
} | ||
} | ||
|
||
/** | ||
* Deletes the database. | ||
*/ | ||
public static function tearDownAfterClass() | ||
{ | ||
$container = static::createClient()->getContainer(); | ||
/** @var EntityManager $entityManager */ | ||
$entityManager = $container->get('doctrine')->getManager(); | ||
|
||
$connection = $entityManager->getConnection(); | ||
$name = $container->getParameter('database_name'); | ||
$name = $connection->getSchemaManager()->getDatabasePlatform()->quoteSingleIdentifier($name); | ||
|
||
$connection->getSchemaManager()->dropDatabase($name); | ||
} | ||
|
||
/** | ||
* Gets entity manager. | ||
* | ||
* @return EntityManager | ||
*/ | ||
public function getEntityManager() | ||
{ | ||
return $this->getServiceContainer()->get('doctrine')->getManager(); | ||
} | ||
|
||
/** | ||
* Imports sql file for testing. | ||
* | ||
* @param string $file | ||
*/ | ||
public function importData($file) | ||
{ | ||
$this->executeSqlFile($this->getConnection(), 'Tests/Functional/Fixtures/' . $file); | ||
} | ||
|
||
/** | ||
* Set full route to db file. | ||
* | ||
* @param string $dbFile | ||
*/ | ||
public function setSetUpDbFile($dbFile) | ||
{ | ||
$this->setUpDbFile = $dbFile; | ||
} | ||
|
||
/** | ||
* Return full route to db file. | ||
* | ||
* @return string | ||
*/ | ||
public function getSetUpDbFile() | ||
{ | ||
return $this->setUpDbFile; | ||
} | ||
|
||
/** | ||
* Returns service container, creates new if it does not exist. | ||
* | ||
* @return ContainerInterface | ||
*/ | ||
protected function getServiceContainer() | ||
{ | ||
if ($this->container === null) { | ||
$this->container = self::createClient()->getContainer(); | ||
} | ||
|
||
return $this->container; | ||
} | ||
|
||
/** | ||
* Gets Connection from container. | ||
* | ||
* @return Connection | ||
*/ | ||
protected function getConnection() | ||
{ | ||
/** @var $doctrine RegistryInterface */ | ||
$doctrine = $this->getServiceContainer()->get('doctrine'); | ||
|
||
return $doctrine->getConnection(); | ||
} | ||
|
||
/** | ||
* Compares two sets of records (suited for sync jobs data comparison). | ||
* | ||
* @param array $expectedRecords | ||
* @param array $actualRecords | ||
* @param bool $checkAllFields | ||
*/ | ||
protected function compareRecords($expectedRecords, $actualRecords, $checkAllFields = true) | ||
{ | ||
$ignoredFields = ['timestamp']; | ||
|
||
if (!$checkAllFields && isset($expectedRecords[0]) && isset($actualRecords[0])) { | ||
$ignoredFields = array_merge( | ||
$ignoredFields, | ||
array_diff(array_keys($actualRecords[0]), array_keys($expectedRecords[0])) | ||
); | ||
} | ||
|
||
// Remove ignored values. | ||
foreach ($actualRecords as &$actualRecord) { | ||
foreach ($ignoredFields as $field) { | ||
unset($actualRecord[$field]); | ||
} | ||
} | ||
|
||
$this->assertEquals($expectedRecords, $actualRecords); | ||
} | ||
|
||
/** | ||
* Executes an SQL file. | ||
* | ||
* @param Connection $conn | ||
* @param string $file | ||
*/ | ||
protected function executeSqlFile(Connection $conn, $file) | ||
{ | ||
$sql = file_get_contents($file); | ||
$stmt = $conn->prepare($sql); | ||
$stmt->execute(); | ||
} | ||
|
||
/** | ||
* Executes large SQL file. | ||
* | ||
* @param string $filename | ||
*/ | ||
protected function executeLargeSqlFile($filename) | ||
{ | ||
$container = static::createClient()->getContainer(); | ||
/** @var EntityManager $manager */ | ||
$manager = $container->get('doctrine')->getManager(); | ||
$connection = $manager->getConnection(); | ||
$tempLine = ''; | ||
$lines = file($filename); | ||
|
||
foreach ($lines as $line) { | ||
// Skip it if it's a comment. | ||
if (substr($line, 0, 2) == '--' || $line == '') { | ||
continue; | ||
} | ||
|
||
// Add this line to the current segment. | ||
$tempLine .= $line; | ||
|
||
// If it has a semicolon at the end, it's the end of the query. | ||
if (substr(trim($line), -1, 1) == ';') { | ||
$connection->exec($tempLine); | ||
// Reset temp variable to empty. | ||
$tempLine = ''; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Return full path to kernel root dir. | ||
* | ||
* @param ContainerInterface $container | ||
* | ||
* @return string | ||
*/ | ||
protected static function getRootDir($container) | ||
{ | ||
return $container->get('kernel')->getRootDir(); | ||
} | ||
|
||
/** | ||
* Return an array of elements required for testing. | ||
* | ||
* @param array $ids | ||
* @param string $repository | ||
* | ||
* @return Object[] | ||
*/ | ||
protected function getTestElements(array $ids, $repository) | ||
{ | ||
$items = []; | ||
$entityManager = $this->getEntityManager(); | ||
$rep = $entityManager->getRepository($repository); | ||
foreach ($ids as $id) { | ||
if (is_array($id)) { | ||
$element = $rep->findBy($id); | ||
} else { | ||
$element = $rep->find($id); | ||
} | ||
|
||
if ($element !== null) { | ||
$items[] = $element; | ||
} | ||
} | ||
|
||
return $items; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.