Skip to content

Commit

Permalink
Merge pull request #157 from tfe2012/refactoring_test_base
Browse files Browse the repository at this point in the history
refactoring test base class
  • Loading branch information
tautrimas committed Feb 19, 2015
2 parents fea26fd + 9d01151 commit 5e228f2
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 175 deletions.
267 changes: 267 additions & 0 deletions Tests/Functional/AbstractTestCase.php
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;
}
}
4 changes: 2 additions & 2 deletions Tests/Functional/Command/SyncProvideCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use ONGR\ConnectionsBundle\Command\SyncProvideCommand;
use ONGR\ConnectionsBundle\Sync\ActionTypes;
use ONGR\ConnectionsBundle\Sync\StorageManager\MysqlStorageManager;
use ONGR\ConnectionsBundle\Tests\Functional\TestBase;
use ONGR\ConnectionsBundle\Tests\Functional\AbstractTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
Expand All @@ -23,7 +23,7 @@
use ONGR\ConnectionsBundle\Sync\DiffProvider\Binlog\BinlogDiffProvider;
use ONGR\ConnectionsBundle\Sync\DiffProvider\Binlog\BinlogParser;

class SyncProvideCommandTest extends TestBase
class SyncProvideCommandTest extends AbstractTestCase
{
/**
* @var MysqlStorageManager
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Command/SyncStorageCreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
use Doctrine\DBAL\Schema\Table;
use ONGR\ConnectionsBundle\Command\SyncStorageCreateCommand;
use ONGR\ConnectionsBundle\Sync\SyncStorage\SyncStorage;
use ONGR\ConnectionsBundle\Tests\Functional\TestBase;
use ONGR\ConnectionsBundle\Tests\Functional\AbstractTestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Integration test for ongr:sync:storage:create command.
*/
class SyncStorageCreateCommandTest extends TestBase
class SyncStorageCreateCommandTest extends AbstractTestCase
{
/**
* @var \Symfony\Component\Console\Command\Command
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Command/UrlInvalidatorServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

use ONGR\ConnectionsBundle\Service\UrlInvalidatorService;
use ONGR\ConnectionsBundle\Tests\Model\ProductModel;
use ONGR\ConnectionsBundle\Tests\Functional\TestBase;
use ONGR\ConnectionsBundle\Tests\Functional\AbstractTestCase;

/**
* Functional test for url invalidator service.
*/
class UrlInvalidatorServiceTest extends TestBase
class UrlInvalidatorServiceTest extends AbstractTestCase
{
/**
* @return array
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Sync/Binlog/BinlogParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

use ONGR\ConnectionsBundle\Sync\ActionTypes;
use ONGR\ConnectionsBundle\Sync\DiffProvider\Binlog\BinlogParser;
use ONGR\ConnectionsBundle\Tests\Functional\TestBase;
use ONGR\ConnectionsBundle\Tests\Functional\AbstractTestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Functional test for binary log parser.
*/
class BinlogParserTest extends TestBase
class BinlogParserTest extends AbstractTestCase
{
/**
* Clear logs before each test.
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Sync/Extractor/DoctrineExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use ONGR\ConnectionsBundle\Sync\DiffProvider\Item\CreateDiffItem;
use ONGR\ConnectionsBundle\Sync\DiffProvider\Item\UpdateDiffItem;
use ONGR\ConnectionsBundle\Sync\SyncStorage\SyncStorageInterface;
use ONGR\ConnectionsBundle\Tests\Functional\TestBase;
use ONGR\ConnectionsBundle\Tests\Functional\AbstractTestCase;

class DoctrineExtractorTest extends TestBase
class DoctrineExtractorTest extends AbstractTestCase
{
/**
* Test extraction service to insert updates to SyncStorage.
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use ONGR\ConnectionsBundle\Sync\Extractor\PassthroughExtractor;
use ONGR\ConnectionsBundle\Sync\StorageManager\MysqlStorageManager;
use ONGR\ConnectionsBundle\Sync\SyncStorage\SyncStorage;
use ONGR\ConnectionsBundle\Tests\Functional\TestBase;
use ONGR\ConnectionsBundle\Tests\Functional\AbstractTestCase;

class PassthroughExtractorTest extends TestBase
class PassthroughExtractorTest extends AbstractTestCase
{
const TABLE_NAME = 'data_sync_test_storage';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use DateTime;
use ONGR\ConnectionsBundle\Sync\ActionTypes;
use ONGR\ConnectionsBundle\Sync\StorageManager\MysqlStorageManager;
use ONGR\ConnectionsBundle\Tests\Functional\TestBase;
use ONGR\ConnectionsBundle\Tests\Functional\AbstractTestCase;

class MysqlStorageManagerTest extends TestBase
class MysqlStorageManagerTest extends AbstractTestCase
{
const TABLE_NAME = 'sync_storage_test_storage';

Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Sync/SyncStorage/SyncStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
use ONGR\ConnectionsBundle\Sync\ActionTypes;
use ONGR\ConnectionsBundle\Sync\StorageManager\StorageManagerInterface;
use ONGR\ConnectionsBundle\Sync\SyncStorage\SyncStorage;
use ONGR\ConnectionsBundle\Tests\Functional\TestBase;
use ONGR\ConnectionsBundle\Tests\Functional\AbstractTestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Functional test for SyncStorage.
*/
class SyncStorageTest extends TestBase
class SyncStorageTest extends AbstractTestCase
{
/**
* @var StorageManagerInterface|MockObject
Expand Down
Loading

0 comments on commit 5e228f2

Please sign in to comment.