Merge pull request #2612 from dereuromark/master-fixture

Clearer error message when fixture insertion fails.
This commit is contained in:
Mark Story 2014-01-06 18:13:37 -08:00
commit 53c8b4ff05
2 changed files with 62 additions and 2 deletions

View file

@ -65,7 +65,7 @@ class CakeTestFixtureTestFixture extends CakeTestFixture {
}
/**
* StringFieldsTestFixture class
* StringTestFixture class
*
* @package Cake.Test.Case.TestSuite
*/
@ -109,6 +109,50 @@ class StringsTestFixture extends CakeTestFixture {
);
}
/**
* InvalidTestFixture class
*
* @package Cake.Test.Case.TestSuite
*/
class InvalidTestFixture extends CakeTestFixture {
/**
* Name property
*
* @var string
*/
public $name = 'Invalid';
/**
* Table property
*
* @var string
*/
public $table = 'invalid';
/**
* Fields array - missing "email" row
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'name' => array('type' => 'string', 'length' => '255'),
'age' => array('type' => 'integer', 'default' => 10)
);
/**
* Records property
*
* @var array
*/
public $records = array(
array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com'),
array('name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20),
array('email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30)
);
}
/**
* CakeTestFixtureImportFixture class
*
@ -482,6 +526,17 @@ class CakeTestFixtureTest extends CakeTestCase {
$this->assertEquals($expected, $this->insertMulti['fields_values']);
}
/**
* test the insert method with invalid fixture
*
* @expectedException CakeException
* @return void
*/
public function testInsertInvalid() {
$Fixture = new InvalidTestFixture();
$return = $Fixture->insert($this->criticDb);
}
/**
* Test the drop method
*

View file

@ -265,6 +265,7 @@ class CakeTestFixture {
*
* @param DboSource $db An instance of the database into which the records will be inserted
* @return boolean on success or if there are no records to insert, or false on failure
* @throws CakeException if counts of values and fields do not match.
*/
public function insert($db) {
if (!isset($this->_insert)) {
@ -277,7 +278,11 @@ class CakeTestFixture {
$fields = array_unique($fields);
$default = array_fill_keys($fields, null);
foreach ($this->records as $record) {
$values[] = array_values(array_merge($default, $record));
$merge = array_values(array_merge($default, $record));
if (count($fields) !== count($merge)) {
throw new CakeException('Fixture invalid: Count of fields does not match count of values');
}
$values[] = $merge;
}
$nested = $db->useNestedTransactions;
$db->useNestedTransactions = false;