diff --git a/Tests/Integration/Controller/AbstractControllerTest.php b/Tests/Integration/Controller/AbstractControllerTest.php index 180d760..3c5c307 100644 --- a/Tests/Integration/Controller/AbstractControllerTest.php +++ b/Tests/Integration/Controller/AbstractControllerTest.php @@ -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; @@ -23,9 +15,9 @@ * * @author Oliver Klee */ -abstract class AbstractControllerTest extends WebTestCase +abstract class AbstractControllerTest extends AbstractWebTest { - use TestCaseTrait; + use DatabaseTestTrait; /** * @var string @@ -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(); } /** diff --git a/Tests/Integration/Controller/Fixtures/TouchTable.csv b/Tests/Integration/Controller/Fixtures/TouchTable.csv deleted file mode 100644 index 8b13789..0000000 --- a/Tests/Integration/Controller/Fixtures/TouchTable.csv +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Tests/Integration/Controller/SessionControllerTest.php b/Tests/Integration/Controller/SessionControllerTest.php index 3227146..e06fc31 100644 --- a/Tests/Integration/Controller/SessionControllerTest.php +++ b/Tests/Integration/Controller/SessionControllerTest.php @@ -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); diff --git a/Tests/Integration/Controller/SubscriberControllerTest.php b/Tests/Integration/Controller/SubscriberControllerTest.php index 3a1b8d7..b809f3b 100644 --- a/Tests/Integration/Controller/SubscriberControllerTest.php +++ b/Tests/Integration/Controller/SubscriberControllerTest.php @@ -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); diff --git a/Tests/Integration/Routing/RoutingTest.php b/Tests/Integration/Routing/RoutingTest.php index 60b221a..ce197cd 100644 --- a/Tests/Integration/Routing/RoutingTest.php +++ b/Tests/Integration/Routing/RoutingTest.php @@ -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 */ -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 */