Skip to content

Commit

Permalink
Allow --debug flag to enable fixture schema output.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed Oct 27, 2015
1 parent 9d7d63c commit 0558d5f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/TestSuite/Fixture/FixtureInjector.php
Expand Up @@ -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();
}
Expand Down
23 changes: 21 additions & 2 deletions src/TestSuite/Fixture/FixtureManager.php
Expand Up @@ -29,7 +29,7 @@ class FixtureManager
{

/**
* Was this class already initialized?
* Was this instance already initialized?
*
* @var bool
*/
Expand Down Expand Up @@ -63,6 +63,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
*
Expand Down Expand Up @@ -299,7 +318,7 @@ protected function _runOperation($fixtures, $operation)
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) {
Expand Down
4 changes: 3 additions & 1 deletion src/TestSuite/Fixture/TestFixture.php
Expand Up @@ -240,7 +240,9 @@ public function create(ConnectionInterface $db)
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(
Expand Down
33 changes: 33 additions & 0 deletions tests/TestCase/TestSuite/FixtureManagerTest.php
Expand Up @@ -16,8 +16,10 @@

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;

/**
Expand Down Expand Up @@ -53,6 +55,37 @@ public function testFixturizeCore()
$this->assertInstanceOf('Cake\Test\Fixture\ArticlesFixture', $fixtures['core.articles']);
}

/**
* 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.
*
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase/TestSuite/TestFixtureTest.php
Expand Up @@ -228,9 +228,11 @@ public function testCreate()
->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));
}
Expand Down

0 comments on commit 0558d5f

Please sign in to comment.