Skip to content

Commit

Permalink
Allows fixture schema to be generated without the $field or the $impo…
Browse files Browse the repository at this point in the history
…rt property set

The schema is generated directly from the datasource
  • Loading branch information
HavokInspiration committed Feb 3, 2016
1 parent 6a36f60 commit b81f889
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/TestSuite/Fixture/TestFixture.php
Expand Up @@ -150,6 +150,10 @@ public function init()
if (!empty($this->import)) {
$this->_schemaFromImport();
}

if (empty($this->import) && empty($this->fields)) {
$this->_schemaFromReflection();
}
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
64 changes: 64 additions & 0 deletions tests/TestCase/TestSuite/TestFixtureTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit b81f889

Please sign in to comment.