Browse Source

Allow --debug flag to enable fixture schema output.

When PHPUnit's `--debug` flag is used and query logging is enabled,
output the table creation schema. This helps developers figure out
issues with their schema creation should it fail.

Refs #7606
Mark Story 10 years ago
parent
commit
0558d5fb85

+ 3 - 0
src/TestSuite/Fixture/FixtureInjector.php

@@ -50,6 +50,9 @@ class FixtureInjector implements PHPUnit_Framework_TestListener
      */
     public function __construct(FixtureManager $manager)
     {
+        if (isset($_SERVER['argv'])) {
+            $manager->setDebug(in_array('--debug', $_SERVER['argv']));
+        }
         $this->_fixtureManager = $manager;
         $this->_fixtureManager->shutdown();
     }

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

@@ -29,7 +29,7 @@ class FixtureManager
 {
 
     /**
-     * Was this class already initialized?
+     * Was this instance already initialized?
      *
      * @var bool
      */
@@ -64,6 +64,25 @@ class FixtureManager
     protected $_processed = [];
 
     /**
+     * Is the test runner being run with `--debug` enabled.
+     * When true, fixture SQL will also be logged.
+     *
+     * @var bool
+     */
+    protected $_debug = false;
+
+    /**
+     * Modify the debug mode.
+     *
+     * @param bool $debug Whether or not fixture debug mode is enabled.
+     * @retun void
+     */
+    public function setDebug($debug)
+    {
+        $this->_debug = $debug;
+    }
+
+    /**
      * Inspects the test to look for unloaded fixtures and loads them
      *
      * @param \Cake\TestSuite\TestCase $test The test case to inspect.
@@ -299,7 +318,7 @@ class FixtureManager
         foreach ($dbs as $connection => $fixtures) {
             $db = ConnectionManager::get($connection, false);
             $logQueries = $db->logQueries();
-            if ($logQueries) {
+            if ($logQueries && !$this->_debug) {
                 $db->logQueries(false);
             }
             $db->transactional(function ($db) use ($fixtures, $operation) {

+ 3 - 1
src/TestSuite/Fixture/TestFixture.php

@@ -240,7 +240,9 @@ class TestFixture implements FixtureInterface
         try {
             $queries = $this->_schema->createSql($db);
             foreach ($queries as $query) {
-                $db->execute($query)->closeCursor();
+                $stmt = $db->prepare($query);
+                $stmt->execute();
+                $stmt->closeCursor();
             }
         } catch (Exception $e) {
             $msg = sprintf(

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

@@ -16,8 +16,10 @@ namespace Cake\Test\TestSuite;
 
 use Cake\Core\Plugin;
 use Cake\Datasource\ConnectionManager;
+use Cake\Log\Log;
 use Cake\ORM\TableRegistry;
 use Cake\TestSuite\Fixture\FixtureManager;
+use Cake\TestSuite\Stub\ConsoleOutput;
 use Cake\TestSuite\TestCase;
 
 /**
@@ -54,6 +56,37 @@ class FixtureManagerTest extends TestCase
     }
 
     /**
+     * Test logging depends on fixture manager debug.
+     *
+     * @return void
+     */
+    public function testLogSchemaWithDebug()
+    {
+        $db = ConnectionManager::get('test');
+        $restore = $db->logQueries();
+        $db->logQueries(true);
+
+        $this->manager->setDebug(true);
+        $buffer = new ConsoleOutput();
+        Log::config('testQueryLogger', [
+            'className' => 'Console',
+            'stream' => $buffer
+        ]);
+
+        $test = $this->getMock('Cake\TestSuite\TestCase');
+        $test->fixtures = ['core.articles'];
+        $this->manager->fixturize($test);
+        // Need to load/shutdown twice to ensure fixture is created.
+        $this->manager->load($test);
+        $this->manager->shutdown();
+        $this->manager->load($test);
+        $this->manager->shutdown();
+
+        $db->logQueries($restore);
+        $this->assertContains('CREATE TABLE', implode('', $buffer->messages()));
+    }
+
+    /**
      * Test loading fixtures with constraints.
      *
      * @return void

+ 4 - 2
tests/TestCase/TestSuite/TestFixtureTest.php

@@ -228,9 +228,11 @@ class TestFixtureTest extends TestCase
             ->will($this->returnValue(['sql', 'sql']));
         $fixture->schema($table);
 
-        $statement = $this->getMock('\PDOStatement', ['closeCursor']);
+        $statement = $this->getMock('\PDOStatement', ['execute', 'closeCursor']);
         $statement->expects($this->atLeastOnce())->method('closeCursor');
-        $db->expects($this->exactly(2))->method('execute')
+        $statement->expects($this->atLeastOnce())->method('execute');
+        $db->expects($this->exactly(2))
+            ->method('prepare')
             ->will($this->returnValue($statement));
         $this->assertTrue($fixture->create($db));
     }