Skip to content
Permalink
Browse files

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
  • Loading branch information...
markstory committed Oct 27, 2015
1 parent 9d7d63c commit 0558d5fb853e758f31ee9c5c7db4b8fed69ea415
@@ -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();
}
@@ -29,7 +29,7 @@ class FixtureManager
{
/**
* Was this class already initialized?
* Was this instance already initialized?
*
* @var bool
*/
@@ -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
*
@@ -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) {
@@ -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(
@@ -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;
/**
@@ -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.
*
@@ -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));
}

0 comments on commit 0558d5f

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