Skip to content
Permalink
Browse files

Allows fixture schema to be generated without the $field or the $impo…

…rt property set

The schema is generated directly from the datasource
  • Loading branch information...
HavokInspiration committed Feb 3, 2016
1 parent 6a36f60 commit b81f889cd7fadd74761dccbec8c974e1d6aaa650
Showing with 93 additions and 0 deletions.
  1. +29 −0 src/TestSuite/Fixture/TestFixture.php
  2. +64 −0 tests/TestCase/TestSuite/TestFixtureTest.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.
*
@@ -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
*

0 comments on commit b81f889

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