Skip to content

Commit cefc37f

Browse files
committed
Extract an interface for Fixtures and Connections.
Extract a basic interface for fixtures and a very tiny one for connections. The connection interface will be expanded once we understand it a bit better. For now it is a marker interface.
1 parent 53fbde2 commit cefc37f

File tree

5 files changed

+161
-43
lines changed

5 files changed

+161
-43
lines changed

src/Database/Connection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
namespace Cake\Database;
1616

17+
use Cake\Datasource\ConnectionInterface;
1718
use Cake\Database\Exception\MissingConnectionException;
1819
use Cake\Database\Exception\MissingDriverException;
1920
use Cake\Database\Exception\MissingExtensionException;
@@ -28,7 +29,7 @@
2829
/**
2930
* Represents a connection with a database server.
3031
*/
31-
class Connection
32+
class Connection implements ConnectionInterface
3233
{
3334

3435
use TypeConverterTrait;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 3.1.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace Cake\Datasource;
16+
17+
/**
18+
* This interface defines the methods you can depend on in
19+
* a connection.
20+
*/
21+
interface ConnectionInterface
22+
{
23+
24+
}

src/Datasource/FixtureInterface.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 3.1.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace Cake\Datasource;
16+
17+
use Cake\Datasource\ConnectionInterface;
18+
19+
/**
20+
* Defines the interface that testing fixtures use.
21+
*/
22+
interface FixtureInterface
23+
{
24+
/**
25+
* Create the fixture schema/mapping/definition
26+
*
27+
* @param Connection $db An instance of the connection the fixture should be created on.
28+
* @return bool True on success, false on failure.
29+
*/
30+
public function create(ConnectionInterface $db);
31+
32+
/**
33+
* Run after all tests executed, should remove the table/collection from the connection.
34+
*
35+
* @param Connection $db An instance of the connection the fixture should be removed from.
36+
* @return bool True on success, false on failure.
37+
*/
38+
public function drop(ConnectionInterface $db);
39+
40+
/**
41+
* Run before each test is executed.
42+
*
43+
* Should insert all the records into the test database.
44+
*
45+
* @param Connection $db An instance of the connection into which the records will be inserted.
46+
* @return bool on success or if there are no records to insert, or false on failure.
47+
*/
48+
public function insert(ConnectionInterface $db);
49+
50+
/**
51+
* Truncates the current fixture.
52+
*
53+
* @param Connection $db A reference to a db instance
54+
* @return bool
55+
*/
56+
public function truncate(ConnectionInterface $db);
57+
58+
/**
59+
* Get the connection name this fixture should be inserted into.
60+
*
61+
* @return string
62+
*/
63+
public function connection();
64+
65+
/**
66+
* Get the table/collection name for this fixture.
67+
*
68+
* @return string
69+
*/
70+
public function sourceName();
71+
}

src/TestSuite/Fixture/FixtureManager.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class FixtureManager
4949
*/
5050
protected $_fixtureMap = [];
5151

52+
/**
53+
* A map of connection names and the fixture currently in it.
54+
*
55+
* @var array
56+
*/
57+
protected $_insertionMap = [];
58+
5259
/**
5360
* Inspects the test to look for unloaded fixtures and loads them
5461
*
@@ -193,11 +200,12 @@ protected function _loadFixtures($test)
193200
*/
194201
protected function _setupTable($fixture, $db, array $sources, $drop = true)
195202
{
196-
if (!empty($fixture->created) && in_array($db->configName(), $fixture->created)) {
203+
$configName = $db->configName();
204+
if ($this->isFixtureSetup($configName, $fixture)) {
197205
return;
198206
}
199207

200-
$table = $fixture->table;
208+
$table = $fixture->sourceName();
201209
$exists = in_array($table, $sources);
202210

203211
if ($drop && $exists) {
@@ -206,7 +214,7 @@ protected function _setupTable($fixture, $db, array $sources, $drop = true)
206214
} elseif (!$exists) {
207215
$fixture->create($db);
208216
} else {
209-
$fixture->created[] = $db->configName();
217+
$this->_insertionMap[$configName][] = $fixture;
210218
$fixture->truncate($db);
211219
}
212220
}
@@ -232,8 +240,12 @@ public function load($test)
232240
try {
233241
$createTables = function ($db, $fixtures) use ($test) {
234242
$tables = $db->schemaCollection()->listTables();
243+
$configName = $db->configName();
244+
if (!isset($this->_insertionMap[$configName])) {
245+
$this->_insertionMap[$configName] = [];
246+
}
235247
foreach ($fixtures as $fixture) {
236-
if (!in_array($db->configName(), (array)$fixture->created)) {
248+
if (!in_array($fixture, $this->_insertionMap[$configName])) {
237249
$this->_setupTable($fixture, $db, $tables, $test->dropTables);
238250
} else {
239251
$fixture->truncate($db);
@@ -294,7 +306,7 @@ protected function _fixtureConnections($fixtures)
294306
foreach ($fixtures as $f) {
295307
if (!empty($this->_loaded[$f])) {
296308
$fixture = $this->_loaded[$f];
297-
$dbs[$fixture->connection][$f] = $fixture;
309+
$dbs[$fixture->connection()][$f] = $fixture;
298310
}
299311
}
300312
return $dbs;
@@ -312,9 +324,9 @@ public function unload($test)
312324
return;
313325
}
314326
$truncate = function ($db, $fixtures) {
315-
$connection = $db->configName();
327+
$configName = $db->configName();
316328
foreach ($fixtures as $fixture) {
317-
if (!empty($fixture->created) && in_array($connection, $fixture->created)) {
329+
if ($this->isFixtureSetup($configName, $fixture)) {
318330
$fixture->truncate($db);
319331
}
320332
}
@@ -336,10 +348,10 @@ public function loadSingle($name, $db = null, $dropTables = true)
336348
if (isset($this->_fixtureMap[$name])) {
337349
$fixture = $this->_fixtureMap[$name];
338350
if (!$db) {
339-
$db = ConnectionManager::get($fixture->connection);
351+
$db = ConnectionManager::get($fixture->connection());
340352
}
341353

342-
if (!in_array($db->configName(), (array)$fixture->created)) {
354+
if (!$this->isFixtureSetup($db->configName(), $fixture)) {
343355
$sources = $db->schemaCollection()->listTables();
344356
$this->_setupTable($fixture, $db, $sources, $dropTables);
345357
}
@@ -362,11 +374,26 @@ public function shutDown()
362374
$shutdown = function ($db, $fixtures) {
363375
$connection = $db->configName();
364376
foreach ($fixtures as $fixture) {
365-
if (!empty($fixture->created) && in_array($connection, $fixture->created)) {
377+
if ($this->isFixtureSetup($connection, $fixture)) {
366378
$fixture->drop($db);
379+
$index = array_search($fixture, $this->_insertionMap[$connection]);
380+
unset($this->_insertionMap[$connection][$index]);
367381
}
368382
}
369383
};
370384
$this->_runOperation(array_keys($this->_loaded), $shutdown);
371385
}
386+
387+
/**
388+
* Check whether or not a fixture has been inserted in a given connection name.
389+
*
390+
* @param string $connection The connection name.
391+
* @param \Cake\Datasource\FixtureInterface $fixture The fixture to check.
392+
* @return bool
393+
*/
394+
public function isFixtureSetup($connection, $fixture)
395+
{
396+
return isset($this->_insertionMap[$connection]) && in_array($fixture, $this->_insertionMap[$connection]);
397+
}
398+
372399
}

src/TestSuite/Fixture/TestFixture.php

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414
namespace Cake\TestSuite\Fixture;
1515

1616
use Cake\Core\Exception\Exception;
17-
use Cake\Database\Connection;
17+
use Cake\Datasource\ConnectionInterface;
1818
use Cake\Database\Schema\Table;
1919
use Cake\Datasource\ConnectionManager;
20+
use Cake\Datasource\FixtureInterface;
2021
use Cake\Log\Log;
2122
use Cake\Utility\Inflector;
2223

2324
/**
2425
* Cake TestFixture is responsible for building and destroying tables to be used
2526
* during testing.
2627
*/
27-
class TestFixture
28+
class TestFixture implements FixtureInterface
2829
{
2930

3031
/**
@@ -41,13 +42,6 @@ class TestFixture
4142
*/
4243
public $table = null;
4344

44-
/**
45-
* List of datasources where this fixture has been created
46-
*
47-
* @var array
48-
*/
49-
public $created = [];
50-
5145
/**
5246
* Fields / Schema for the fixture.
5347
*
@@ -105,6 +99,22 @@ public function __construct()
10599
$this->init();
106100
}
107101

102+
/**
103+
* {@inheritDoc}
104+
*/
105+
public function connection()
106+
{
107+
return $this->connection;
108+
}
109+
110+
/**
111+
* {@inheritDoc}
112+
*/
113+
public function sourceName()
114+
{
115+
return $this->table;
116+
}
117+
108118
/**
109119
* Initialize the fixture.
110120
*
@@ -205,12 +215,9 @@ public function schema(Table $schema = null)
205215
}
206216

207217
/**
208-
* Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
209-
*
210-
* @param Connection $db An instance of the database object used to create the fixture table
211-
* @return bool True on success, false on failure
218+
* {@inheritDoc}
212219
*/
213-
public function create(Connection $db)
220+
public function create(ConnectionInterface $db)
214221
{
215222
if (empty($this->_schema)) {
216223
return false;
@@ -236,12 +243,9 @@ public function create(Connection $db)
236243
}
237244

238245
/**
239-
* Run after all tests executed, should return SQL statement to drop table for this fixture.
240-
*
241-
* @param Connection $db An instance of the database object used to create the fixture table
242-
* @return bool True on success, false on failure
246+
* {@inheritDoc}
243247
*/
244-
public function drop(Connection $db)
248+
public function drop(ConnectionInterface $db)
245249
{
246250
if (empty($this->_schema)) {
247251
return false;
@@ -251,21 +255,16 @@ public function drop(Connection $db)
251255
foreach ($sql as $stmt) {
252256
$db->execute($stmt)->closeCursor();
253257
}
254-
$this->created = array_diff($this->created, [$db->configName()]);
255258
} catch (\Exception $e) {
256259
return false;
257260
}
258261
return true;
259262
}
260263

261264
/**
262-
* Run before each tests is executed, should return a set of SQL statements to insert records for the table
263-
* of this fixture could be executed successfully.
264-
*
265-
* @param Connection $db An instance of the database into which the records will be inserted
266-
* @return bool on success or if there are no records to insert, or false on failure
265+
* {@inheritDoc}
267266
*/
268-
public function insert(Connection $db)
267+
public function insert(ConnectionInterface $db)
269268
{
270269
if (isset($this->records) && !empty($this->records)) {
271270
list($fields, $values, $types) = $this->_getRecords();
@@ -308,13 +307,9 @@ protected function _getRecords()
308307
}
309308

310309
/**
311-
* Truncates the current fixture. Can be overwritten by classes extending
312-
* CakeFixture to trigger other events before / after truncate.
313-
*
314-
* @param Connection $db A reference to a db instance
315-
* @return bool
310+
* {@inheritDoc}
316311
*/
317-
public function truncate(Connection $db)
312+
public function truncate(ConnectionInterface $db)
318313
{
319314
$sql = $this->_schema->truncateSql($db);
320315
foreach ($sql as $stmt) {

0 commit comments

Comments
 (0)