Skip to content

Commit

Permalink
Make constraints, indexes and options _ prefixed.
Browse files Browse the repository at this point in the history
This further reduces the chances of colliding with column names.
  • Loading branch information
markstory committed May 29, 2013
1 parent 5990034 commit d9ae64c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
12 changes: 5 additions & 7 deletions lib/Cake/Test/Fixture/PostFixture.php
Expand Up @@ -41,16 +41,14 @@ class PostFixture extends TestFixture {
* @var array
*/
public $fields = array(
'id' => array('type' => 'integer'),
'author_id' => array('type' => 'integer', 'null' => false),
'title' => array('type' => 'string', 'null' => false),
'id' => ['type' => 'integer'],
'author_id' => ['type' => 'integer', 'null' => false],
'title' => ['type' => 'string', 'null' => false],
'body' => 'text',
'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
'created' => 'datetime',
'updated' => 'datetime',
'constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
);

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Test/TestCase/TestSuite/TestFixtureTest.php
Expand Up @@ -44,7 +44,7 @@ class ArticleFixture extends TestFixture {
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'length' => '255'],
'created' => ['type' => 'datetime'],
'constraints' => [
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
];
Expand Down Expand Up @@ -148,13 +148,13 @@ public function testInitStaticFixture() {
$this->assertInstanceOf('Cake\Database\Schema\Table', $schema);

$fields = $Fixture->fields;
unset($fields['constraints'], $fields['indexes']);
unset($fields['_constraints'], $fields['_indexes']);
$this->assertEquals(
array_keys($fields),
$schema->columns(),
'Fields do not match'
);
$this->assertEquals(array_keys($Fixture->fields['constraints']), $schema->constraints());
$this->assertEquals(array_keys($Fixture->fields['_constraints']), $schema->constraints());
$this->assertEmpty($schema->indexes());
}

Expand Down
17 changes: 10 additions & 7 deletions lib/Cake/TestSuite/Fixture/TestFixture.php
Expand Up @@ -56,8 +56,8 @@ class TestFixture {
* Fields / Schema for the fixture.
*
* This array should be compatible with Cake\Database\Schema\Table.
* The `constraints` and `indexes` keys are reserved for defining
* constraints and indexes respectively.
* The `_constraints`, `_options` and `_indexes` keys are reserved for defining
* constraints, options and indexes respectively.
*
* @var array
*/
Expand Down Expand Up @@ -144,7 +144,7 @@ public function init() {
protected function _schemaFromFields() {
$this->_schema = new Table($this->table);
foreach ($this->fields as $field => $data) {
if ($field === 'constraints' || $field === 'indexes') {
if ($field === '_constraints' || $field === '_indexes' || $field === '_options') {
continue;
}
// Trigger errors on deprecated usage.
Expand All @@ -154,21 +154,24 @@ protected function _schemaFromFields() {
}
$this->_schema->addColumn($field, $data);
}
if (!empty($this->fields['constraints'])) {
foreach ($this->fields['constraints'] as $name => $data) {
if (!empty($this->fields['_constraints'])) {
foreach ($this->fields['_constraints'] as $name => $data) {
$this->_schema->addConstraint($name, $data);
}
}
if (!empty($this->fields['indexes'])) {
if (!empty($this->fields['_indexes'])) {
// Trigger errors on deprecated usage.
if (empty($data['type'])) {
$msg = __d('cake_dev', 'Indexes must define a type. Try using the upgrade shell to migrate your fixtures.');
trigger_error($msg, E_USER_NOTICE);
}
foreach ($this->fields['indexes'] as $name => $data) {
foreach ($this->fields['_indexes'] as $name => $data) {
$this->_schema->addIndex($name, $data);
}
}
if (!empty($this->fields['_options'])) {
$this->_schema->options($this->fields['_options']);
}
}

/**
Expand Down

0 comments on commit d9ae64c

Please sign in to comment.