Skip to content

Commit

Permalink
When setting options as a non-associative array, Column fails with "C…
Browse files Browse the repository at this point in the history
…all to undefined method Phinx\Db\Table\Column::set0()". This check makes sure the developer gets an appropriate message.

Using strict in_array() to check for non-assoc arrays
  • Loading branch information
f3ath committed Jul 25, 2016
1 parent dd2b94c commit a6afbd9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Phinx/Db/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public function setOptions($options)
$option = $aliasOptions[$option];
}

if (!in_array($option, $validOptions)) {
if (!in_array($option, $validOptions, true)) {
throw new \RuntimeException(sprintf('"%s" is not a valid column option.', $option));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Phinx/Db/Table/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ public function setOptions($options)
// Valid Options
$validOptions = array('delete', 'update', 'constraint');
foreach ($options as $option => $value) {
if (!in_array($option, $validOptions)) {
throw new \RuntimeException('\'' . $option . '\' is not a valid foreign key option.');
if (!in_array($option, $validOptions, true)) {
throw new \RuntimeException(sprintf('"%s" is not a valid foreign key option.', $option));
}

// handle $options['delete'] as $options['update']
Expand Down
4 changes: 2 additions & 2 deletions src/Phinx/Db/Table/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public function setOptions($options)
// Valid Options
$validOptions = array('type', 'unique', 'name', 'limit');
foreach ($options as $option => $value) {
if (!in_array($option, $validOptions)) {
throw new \RuntimeException('\'' . $option . '\' is not a valid index option.');
if (!in_array($option, $validOptions, true)) {
throw new \RuntimeException(sprintf('"%s" is not a valid index option.', $option));
}

// handle $options['unique']
Expand Down
18 changes: 18 additions & 0 deletions tests/Phinx/Db/Table/ColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Test\Phinx\Db\Table;

use Phinx\Db\Table\Column;

class ColumnTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage "0" is not a valid column option.
*/
public function testSetOptionThrowsExceptionIfOptionIsNotString()
{
$column = new Column();
$column->setOptions(['identity']);
}
}
9 changes: 9 additions & 0 deletions tests/Phinx/Db/Table/ForeignKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,13 @@ public function actionsProvider()
array('Set_nuLL', ForeignKey::SET_NULL),
);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage "0" is not a valid foreign key option.
*/
public function testSetOptionThrowsExceptionIfOptionIsNotString()
{
$this->fk->setOptions(['update']);
}
}
18 changes: 18 additions & 0 deletions tests/Phinx/Db/Table/IndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Test\Phinx\Db\Table;

use Phinx\Db\Table\Index;

class IndexTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage "0" is not a valid index option.
*/
public function testSetOptionThrowsExceptionIfOptionIsNotString()
{
$column = new Index();
$column->setOptions(['type']);
}
}

0 comments on commit a6afbd9

Please sign in to comment.