Skip to content

Commit

Permalink
It table exists but the fixture was not setup, we should reset the table
Browse files Browse the repository at this point in the history
  • Loading branch information
HavokInspiration committed Feb 10, 2016
1 parent bc507b2 commit 8dee7c2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/TestSuite/Fixture/FixtureManager.php
Expand Up @@ -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) {
Expand Down
39 changes: 39 additions & 0 deletions tests/TestCase/TestSuite/FixtureManagerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 8dee7c2

Please sign in to comment.