Skip to content

[CLEANUP] Use the database test trait and web test base class #91

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

Merged
merged 1 commit into from
Feb 15, 2018
Merged
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
157 changes: 6 additions & 151 deletions Tests/Integration/Controller/AbstractControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@

namespace PhpList\RestBundle\Tests\Integration\Controller;

use Doctrine\ORM\EntityManagerInterface;
use PhpList\PhpList4\Core\Bootstrap;
use PhpList\PhpList4\Core\Environment;
use PHPUnit\DbUnit\Database\Connection;
use PHPUnit\DbUnit\DataSet\CsvDataSet;
use PHPUnit\DbUnit\Operation\Factory;
use PHPUnit\DbUnit\Operation\Operation;
use PHPUnit\DbUnit\TestCaseTrait;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use PhpList\PhpList4\TestingSupport\AbstractWebTest;
use PhpList\PhpList4\TestingSupport\Traits\DatabaseTestTrait;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -23,9 +15,9 @@
*
* @author Oliver Klee <[email protected]>
*/
abstract class AbstractControllerTest extends WebTestCase
abstract class AbstractControllerTest extends AbstractWebTest
{
use TestCaseTrait;
use DatabaseTestTrait;

/**
* @var string
Expand All @@ -37,147 +29,10 @@ abstract class AbstractControllerTest extends WebTestCase
*/
const TOKEN_TABLE_NAME = 'phplist_admintoken';

/**
* @var Connection
*/
private $databaseConnection = null;

/**
* @var \PDO
*/
private static $pdo = null;

/**
* @var CsvDataSet
*/
private $dataSet = null;

/**
* @var Bootstrap
*/
protected $bootstrap = null;

/**
* @var EntityManagerInterface
*/
protected $entityManager = null;

/**
* @var Client
*/
protected $client = null;

protected function setUp()
{
// This makes sure that all DateTime instances use the same time zone, thus making the dates in the
// JSON provided by the REST API easier to test.
date_default_timezone_set('UTC');

$this->initializeDatabaseTester();
$this->bootstrap = Bootstrap::getInstance()->setEnvironment(Environment::TESTING)->configure();
$this->entityManager = $this->bootstrap->getEntityManager();
static::assertTrue($this->entityManager->isOpen());

$this->client = static::createClient(['environment' => Environment::TESTING]);
}

/**
* Initializes the CSV data set and the database tester.
*
* @return void
*/
protected function initializeDatabaseTester()
{
$this->dataSet = new CsvDataSet();

$this->databaseTester = null;
$this->getDatabaseTester()->setSetUpOperation($this->getSetUpOperation());
}

protected function tearDown()
{
$this->entityManager->close();

$this->getDatabaseTester()->setTearDownOperation($this->getTearDownOperation());
$this->getDatabaseTester()->setDataSet($this->getDataSet());
$this->getDatabaseTester()->onTearDown();

// Destroy the tester after the test is run to keep DB connections
// from piling up.
$this->databaseTester = null;

Bootstrap::purgeInstance();
}

/**
* Returns the database operation executed in test cleanup.
*
* @return Operation
*/
protected function getTearDownOperation(): Operation
{
return Factory::TRUNCATE();
}

/**
* Returns the test database connection.
*
* @return Connection
*/
protected function getConnection(): Connection
{
if ($this->databaseConnection === null) {
if (self::$pdo === null) {
self::$pdo = new \PDO(
'mysql:dbname=' . getenv('PHPLIST_DATABASE_NAME'),
getenv('PHPLIST_DATABASE_USER'),
getenv('PHPLIST_DATABASE_PASSWORD')
);
}
$this->databaseConnection = $this->createDefaultDBConnection(self::$pdo);
}

return $this->databaseConnection;
}

/**
* Returns the test data set.
*
* Add data to in the individual test by calling $this->getDataSet()->addTable.
*
* @return CsvDataSet
*/
protected function getDataSet(): CsvDataSet
{
return $this->dataSet;
}

/**
* Applies all database changes on $this->dataSet.
*
* This methods needs to be called after the last addTable call in each test.
*
* @return void
*/
protected function applyDatabaseChanges()
{
$this->getDatabaseTester()->setDataSet($this->getDataSet());
$this->getDatabaseTester()->onSetUp();
}

/**
* Marks the table with the given name as "touched", i.e., it will be truncated in the tearDown method.
*
* This is useful if the table gets populated only by the tested code instead of by using the addTable
* and applyDatabaseChanges method.
*
* @param string $tableName
*
* @return void
*/
protected function touchDatabaseTable(string $tableName)
{
$this->getDataSet()->addTable($tableName, __DIR__ . '/Fixtures/TouchTable.csv');
$this->setUpDatabaseTest();
$this->setUpWebTest();
}

/**
Expand Down
1 change: 0 additions & 1 deletion Tests/Integration/Controller/Fixtures/TouchTable.csv

This file was deleted.

3 changes: 2 additions & 1 deletion Tests/Integration/Controller/SessionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class SessionControllerTest extends AbstractControllerTest

protected function setUp()
{
parent::setUp();
$this->setUpDatabaseTest();
$this->setUpWebTest();

$this->administratorTokenRepository = $this->bootstrap->getContainer()
->get(AdministratorTokenRepository::class);
Expand Down
3 changes: 2 additions & 1 deletion Tests/Integration/Controller/SubscriberControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class SubscriberControllerTest extends AbstractControllerTest

protected function setUp()
{
parent::setUp();
$this->setUpDatabaseTest();
$this->setUpWebTest();

$this->subscriberRepository = $this->bootstrap->getContainer()
->get(SubscriberRepository::class);
Expand Down
24 changes: 2 additions & 22 deletions Tests/Integration/Routing/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,15 @@

namespace PhpList\RestBundle\Tests\Integration\Routing;

use PhpList\PhpList4\Core\Bootstrap;
use PhpList\PhpList4\Core\Environment;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use PhpList\PhpList4\TestingSupport\AbstractWebTest;

/**
* Testcase.
*
* @author Oliver Klee <[email protected]>
*/
class RoutingTest extends WebTestCase
class RoutingTest extends AbstractWebTest
{
/**
* @var Client
*/
private $client = null;

protected function setUp()
{
Bootstrap::getInstance()->setEnvironment(Environment::TESTING)->configure();

$this->client = static::createClient(['environment' => Environment::TESTING]);
}

protected function tearDown()
{
Bootstrap::purgeInstance();
}

/**
* @test
*/
Expand Down