Skip to content
Permalink
Browse files

It table exists but the fixture was not setup, we should reset the table

  • Loading branch information...
HavokInspiration committed Feb 9, 2016
1 parent bc507b2 commit 8dee7c23fac366d87739f8b74102ec0fcf9b8227
Showing with 42 additions and 2 deletions.
  1. +3 −2 src/TestSuite/Fixture/FixtureManager.php
  2. +39 −0 tests/TestCase/TestSuite/FixtureManagerTest.php
@@ -227,14 +227,15 @@ protected function _loadFixtures($test)
protected function _setupTable($fixture, $db, array $sources, $drop = true)
{
$configName = $db->configName();
if ($this->isFixtureSetup($configName, $fixture)) {
$isFixtureSetup = $this->isFixtureSetup($configName, $fixture);
if ($isFixtureSetup) {
return;
}
$table = $fixture->sourceName();
$exists = in_array($table, $sources);
if ($drop && $exists) {
if (($drop && $exists) || ($exists && !$isFixtureSetup)) {
$fixture->drop($db);
$fixture->create($db);
} elseif (!$exists) {
@@ -15,6 +15,7 @@
namespace Cake\Test\TestSuite;
use Cake\Core\Plugin;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;
@@ -92,6 +93,44 @@ public function testLogSchemaWithDebug()
$this->assertContains('CREATE TABLE', implode('', $buffer->messages()));
}
/**
* Test that if a table already exists in the test database, it will dropped
* before being recreated
*
* @return void
*/
public function testResetDbIfTableExists()
{
$db = ConnectionManager::get('test');
$restore = $db->logQueries();
$db->logQueries(true);
$this->manager->setDebug(true);
$buffer = new ConsoleOutput();
Log::config('testQueryLogger', [
'className' => 'Console',
'stream' => $buffer
]);
$table = new Table('articles', [
'id' => ['type' => 'integer', 'unsigned' => true],
'title' => ['type' => 'string', 'length' => 255],
]);
$table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]);
$sql = $table->createSql($db);
foreach ($sql as $stmt) {
$db->execute($stmt);
}
$test = $this->getMock('Cake\TestSuite\TestCase');
$test->fixtures = ['core.articles'];
$this->manager->fixturize($test);
$this->manager->load($test);
$db->logQueries($restore);
$this->assertContains('DROP TABLE', implode('', $buffer->messages()));
}
/**
* Test loading fixtures with constraints.
*

0 comments on commit 8dee7c2

Please sign in to comment.
You can’t perform that action at this time.