Skip to content

Commit

Permalink
Update usage of Query() class and fix insertion errors.
Browse files Browse the repository at this point in the history
Fix errors inserting records (there are still remaining issues with
SQLite).
  • Loading branch information
markstory committed May 26, 2013
1 parent 69f35d1 commit 9d985a8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
36 changes: 26 additions & 10 deletions lib/Cake/Test/TestCase/TestSuite/TestFixtureTest.php
Expand Up @@ -386,13 +386,21 @@ public function testInsert() {
->with('articles', ['name', 'created'], ['string', 'datetime'])
->will($this->returnSelf());
$expected = [
['Gandalf', '2009-04-28 19:20:00'],
['Captain Picard', '2009-04-28 19:20:00'],
['Chewbacca', '2009-04-28 19:20:00']
['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'],
['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'],
['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00']
];
$query->expects($this->once())
$query->expects($this->at(1))
->method('values')
->with($expected[0])
->will($this->returnSelf());
$query->expects($this->at(2))
->method('values')
->with($expected[1])
->will($this->returnSelf());
$query->expects($this->at(3))
->method('values')
->with($expected)
->with($expected[2])
->will($this->returnSelf());

$query->expects($this->once())
Expand Down Expand Up @@ -422,13 +430,21 @@ public function testInsertStrings() {
->will($this->returnSelf());

$expected = [
['Mark Doe', 'mark.doe@email.com', null],
['John Doe', 'john.doe@email.com', 20],
['Jane Doe', 'jane.doe@email.com', 30],
['name' => 'Mark Doe', 'email' => 'mark.doe@email.com', 'age' => null],
['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20],
['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30],
];
$query->expects($this->once())
$query->expects($this->at(1))
->method('values')
->with($expected[0])
->will($this->returnSelf());
$query->expects($this->at(2))
->method('values')
->with($expected[1])
->will($this->returnSelf());
$query->expects($this->at(3))
->method('values')
->with($expected)
->with($expected[2])
->will($this->returnSelf());

$query->expects($this->once())
Expand Down
11 changes: 7 additions & 4 deletions lib/Cake/TestSuite/Fixture/TestFixture.php
Expand Up @@ -156,6 +156,7 @@ protected function _schemaFromFields() {
if ($field === 'constraints' || $field === 'indexes') {
continue;
}
// TODO issue E_USER_NOTICE if a column defines 'key'
$this->_schema->addColumn($field, $data);
}
if (!empty($this->fields['constraints'])) {
Expand Down Expand Up @@ -256,7 +257,6 @@ public function drop(Connection $db) {
}
$this->created = array_diff($this->created, [$db->configKeyName]);
} catch (\Exception $e) {
var_dump($e);
return false;
}
return true;
Expand All @@ -273,8 +273,11 @@ public function insert(Connection $db) {
if (isset($this->records) && !empty($this->records)) {
list($fields, $values, $types) = $this->_getRecords();
$query = $db->newQuery()
->insert($this->table, $fields, $types)
->values($values);
->insert($this->table, $fields, $types);

foreach ($values as $row) {
$query->values($row);
}

$result = $query->execute();

Expand Down Expand Up @@ -306,7 +309,7 @@ protected function _getRecords() {
}
$default = array_fill_keys($fields, null);
foreach ($this->records as $record) {
$values[] = array_values(array_merge($default, $record));
$values[] = array_merge($default, $record);
}
return [$fields, $values, $types];
}
Expand Down

0 comments on commit 9d985a8

Please sign in to comment.