33
44namespace 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 ;
168use Symfony \Component \DomCrawler \Crawler ;
179use Symfony \Component \HttpFoundation \Response ;
1810
2315 *
2416 * @author Oliver Klee <oliver@phplist.com>
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 /**
0 commit comments