Browse Source

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

Yves P 10 years ago
parent
commit
8dee7c23fa

+ 3 - 2
src/TestSuite/Fixture/FixtureManager.php

@@ -227,14 +227,15 @@ class FixtureManager
     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) {

+ 39 - 0
tests/TestCase/TestSuite/FixtureManagerTest.php

@@ -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;
@@ -93,6 +94,44 @@ class FixtureManagerTest extends TestCase
     }
 
     /**
+     * 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.
      *
      * @return void