diff --git a/src/TestSuite/Fixture/TestFixture.php b/src/TestSuite/Fixture/TestFixture.php index 28a70d5c1f6..086dc281e80 100644 --- a/src/TestSuite/Fixture/TestFixture.php +++ b/src/TestSuite/Fixture/TestFixture.php @@ -150,6 +150,10 @@ public function init() if (!empty($this->import)) { $this->_schemaFromImport(); } + + if (empty($this->import) && empty($this->fields)) { + $this->_schemaFromReflection(); + } } /** @@ -218,6 +222,31 @@ protected function _schemaFromImport() $this->_schema = $table; } + /** + * Build fixture schema directly from the datasource + * + * @return void + * @throws \Cake\Core\Exception\Exception when trying to reflect a table that does not exist + */ + protected function _schemaFromReflection() + { + $db = ConnectionManager::get($this->connection()); + $schemaCollection = $db->schemaCollection(); + $tables = $schemaCollection->listTables(); + + if (!in_array($this->table, $tables)) { + throw new CakeException( + sprintf( + 'Cannot describe schema for table `%s` for fixture `%s` : the table does not exist.', + $this->table, + get_class($this) + ) + ); + } + + $this->_schema = $schema = $db->schemaCollection()->describe($this->table); + } + /** * Get/Set the Cake\Database\Schema\Table instance used by this fixture. * diff --git a/tests/TestCase/TestSuite/TestFixtureTest.php b/tests/TestCase/TestSuite/TestFixtureTest.php index 87f9c81adbb..0ebc3ebed1b 100644 --- a/tests/TestCase/TestSuite/TestFixtureTest.php +++ b/tests/TestCase/TestSuite/TestFixtureTest.php @@ -14,6 +14,8 @@ */ namespace Cake\Test\TestCase\TestSuite; +use Cake\Database\Schema\Table; +use Cake\Datasource\ConnectionManager; use Cake\Log\Log; use Cake\TestSuite\Fixture\TestFixture; use Cake\TestSuite\TestCase; @@ -122,6 +124,26 @@ class ImportsFixture extends TestFixture ]; } +/** + * This class allows testing the fixture data insertion when the properties + * $fields and $import are not set + * + */ +class LettersFixture extends TestFixture +{ + + /** + * records property + * + * @var array + */ + public $records = [ + ['letter' => 'a'], + ['letter' => 'b'], + ['letter' => 'c'] + ]; +} + /** * Test case for TestFixture * @@ -237,6 +259,48 @@ public function testInitImportModel() $this->assertEquals($expected, $fixture->schema()->columns()); } + /** + * test schema reflection without $import or $fields and without the table existing + * it will throw an exception + * + * @expectedException \Cake\Core\Exception\Exception + * @expectedExceptionMessage Cannot describe schema for table `letters` for fixture `Cake\Test\TestCase\TestSuite\LettersFixture` : the table does not exist. + * @return void + */ + public function testInitNoImportNoFieldsException() + { + $fixture = new LettersFixture(); + $fixture->init(); + } + + /** + * test schema reflection without $import or $fields will reflect the schema + *. + * @return void + */ + public function testInitNoImportNoFields() + { + $db = ConnectionManager::get('test'); + $collection = $db->schemaCollection(); + if (!in_array('letters', $collection->listTables())) { + $table = new Table('letters', [ + 'id' => ['type' => 'integer'], + 'letters' => ['type' => 'integer', 'null' => true] + ]); + $table->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); + $sql = $table->createSql($db); + + foreach ($sql as $stmt) { + $db->execute($stmt); + } + } + + $fixture = new LettersFixture(); + $fixture->init(); + + $this->assertEquals(['id', 'letters'], $fixture->schema()->columns()); + } + /** * test create method *