Skip to content

Commit b0a3f4c

Browse files
authored
[CLEANUP] Use the database test trait and web test base class (#91)
1 parent bdb384a commit b0a3f4c

File tree

5 files changed

+12
-176
lines changed

5 files changed

+12
-176
lines changed

Tests/Integration/Controller/AbstractControllerTest.php

Lines changed: 6 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,8 @@
33

44
namespace PhpList\RestBundle\Tests\Integration\Controller;
55

6-
use Doctrine\ORM\EntityManagerInterface;
7-
use PhpList\PhpList4\Core\Bootstrap;
8-
use PhpList\PhpList4\Core\Environment;
9-
use PHPUnit\DbUnit\Database\Connection;
10-
use PHPUnit\DbUnit\DataSet\CsvDataSet;
11-
use PHPUnit\DbUnit\Operation\Factory;
12-
use PHPUnit\DbUnit\Operation\Operation;
13-
use PHPUnit\DbUnit\TestCaseTrait;
14-
use Symfony\Bundle\FrameworkBundle\Client;
15-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
use PhpList\PhpList4\TestingSupport\AbstractWebTest;
7+
use PhpList\PhpList4\TestingSupport\Traits\DatabaseTestTrait;
168
use Symfony\Component\DomCrawler\Crawler;
179
use Symfony\Component\HttpFoundation\Response;
1810

@@ -23,9 +15,9 @@
2315
*
2416
* @author Oliver Klee <[email protected]>
2517
*/
26-
abstract class AbstractControllerTest extends WebTestCase
18+
abstract class AbstractControllerTest extends AbstractWebTest
2719
{
28-
use TestCaseTrait;
20+
use DatabaseTestTrait;
2921

3022
/**
3123
* @var string
@@ -37,147 +29,10 @@ abstract class AbstractControllerTest extends WebTestCase
3729
*/
3830
const TOKEN_TABLE_NAME = 'phplist_admintoken';
3931

40-
/**
41-
* @var Connection
42-
*/
43-
private $databaseConnection = null;
44-
45-
/**
46-
* @var \PDO
47-
*/
48-
private static $pdo = null;
49-
50-
/**
51-
* @var CsvDataSet
52-
*/
53-
private $dataSet = null;
54-
55-
/**
56-
* @var Bootstrap
57-
*/
58-
protected $bootstrap = null;
59-
60-
/**
61-
* @var EntityManagerInterface
62-
*/
63-
protected $entityManager = null;
64-
65-
/**
66-
* @var Client
67-
*/
68-
protected $client = null;
69-
7032
protected function setUp()
7133
{
72-
// This makes sure that all DateTime instances use the same time zone, thus making the dates in the
73-
// JSON provided by the REST API easier to test.
74-
date_default_timezone_set('UTC');
75-
76-
$this->initializeDatabaseTester();
77-
$this->bootstrap = Bootstrap::getInstance()->setEnvironment(Environment::TESTING)->configure();
78-
$this->entityManager = $this->bootstrap->getEntityManager();
79-
static::assertTrue($this->entityManager->isOpen());
80-
81-
$this->client = static::createClient(['environment' => Environment::TESTING]);
82-
}
83-
84-
/**
85-
* Initializes the CSV data set and the database tester.
86-
*
87-
* @return void
88-
*/
89-
protected function initializeDatabaseTester()
90-
{
91-
$this->dataSet = new CsvDataSet();
92-
93-
$this->databaseTester = null;
94-
$this->getDatabaseTester()->setSetUpOperation($this->getSetUpOperation());
95-
}
96-
97-
protected function tearDown()
98-
{
99-
$this->entityManager->close();
100-
101-
$this->getDatabaseTester()->setTearDownOperation($this->getTearDownOperation());
102-
$this->getDatabaseTester()->setDataSet($this->getDataSet());
103-
$this->getDatabaseTester()->onTearDown();
104-
105-
// Destroy the tester after the test is run to keep DB connections
106-
// from piling up.
107-
$this->databaseTester = null;
108-
109-
Bootstrap::purgeInstance();
110-
}
111-
112-
/**
113-
* Returns the database operation executed in test cleanup.
114-
*
115-
* @return Operation
116-
*/
117-
protected function getTearDownOperation(): Operation
118-
{
119-
return Factory::TRUNCATE();
120-
}
121-
122-
/**
123-
* Returns the test database connection.
124-
*
125-
* @return Connection
126-
*/
127-
protected function getConnection(): Connection
128-
{
129-
if ($this->databaseConnection === null) {
130-
if (self::$pdo === null) {
131-
self::$pdo = new \PDO(
132-
'mysql:dbname=' . getenv('PHPLIST_DATABASE_NAME'),
133-
getenv('PHPLIST_DATABASE_USER'),
134-
getenv('PHPLIST_DATABASE_PASSWORD')
135-
);
136-
}
137-
$this->databaseConnection = $this->createDefaultDBConnection(self::$pdo);
138-
}
139-
140-
return $this->databaseConnection;
141-
}
142-
143-
/**
144-
* Returns the test data set.
145-
*
146-
* Add data to in the individual test by calling $this->getDataSet()->addTable.
147-
*
148-
* @return CsvDataSet
149-
*/
150-
protected function getDataSet(): CsvDataSet
151-
{
152-
return $this->dataSet;
153-
}
154-
155-
/**
156-
* Applies all database changes on $this->dataSet.
157-
*
158-
* This methods needs to be called after the last addTable call in each test.
159-
*
160-
* @return void
161-
*/
162-
protected function applyDatabaseChanges()
163-
{
164-
$this->getDatabaseTester()->setDataSet($this->getDataSet());
165-
$this->getDatabaseTester()->onSetUp();
166-
}
167-
168-
/**
169-
* Marks the table with the given name as "touched", i.e., it will be truncated in the tearDown method.
170-
*
171-
* This is useful if the table gets populated only by the tested code instead of by using the addTable
172-
* and applyDatabaseChanges method.
173-
*
174-
* @param string $tableName
175-
*
176-
* @return void
177-
*/
178-
protected function touchDatabaseTable(string $tableName)
179-
{
180-
$this->getDataSet()->addTable($tableName, __DIR__ . '/Fixtures/TouchTable.csv');
34+
$this->setUpDatabaseTest();
35+
$this->setUpWebTest();
18136
}
18237

18338
/**

Tests/Integration/Controller/Fixtures/TouchTable.csv

Lines changed: 0 additions & 1 deletion
This file was deleted.

Tests/Integration/Controller/SessionControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class SessionControllerTest extends AbstractControllerTest
2323

2424
protected function setUp()
2525
{
26-
parent::setUp();
26+
$this->setUpDatabaseTest();
27+
$this->setUpWebTest();
2728

2829
$this->administratorTokenRepository = $this->bootstrap->getContainer()
2930
->get(AdministratorTokenRepository::class);

Tests/Integration/Controller/SubscriberControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class SubscriberControllerTest extends AbstractControllerTest
2626

2727
protected function setUp()
2828
{
29-
parent::setUp();
29+
$this->setUpDatabaseTest();
30+
$this->setUpWebTest();
3031

3132
$this->subscriberRepository = $this->bootstrap->getContainer()
3233
->get(SubscriberRepository::class);

Tests/Integration/Routing/RoutingTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,15 @@
33

44
namespace PhpList\RestBundle\Tests\Integration\Routing;
55

6-
use PhpList\PhpList4\Core\Bootstrap;
7-
use PhpList\PhpList4\Core\Environment;
8-
use Symfony\Bundle\FrameworkBundle\Client;
9-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
use PhpList\PhpList4\TestingSupport\AbstractWebTest;
107

118
/**
129
* Testcase.
1310
*
1411
* @author Oliver Klee <[email protected]>
1512
*/
16-
class RoutingTest extends WebTestCase
13+
class RoutingTest extends AbstractWebTest
1714
{
18-
/**
19-
* @var Client
20-
*/
21-
private $client = null;
22-
23-
protected function setUp()
24-
{
25-
Bootstrap::getInstance()->setEnvironment(Environment::TESTING)->configure();
26-
27-
$this->client = static::createClient(['environment' => Environment::TESTING]);
28-
}
29-
30-
protected function tearDown()
31-
{
32-
Bootstrap::purgeInstance();
33-
}
34-
3515
/**
3616
* @test
3717
*/

0 commit comments

Comments
 (0)