Skip to content

Commit

Permalink
Fix for #3568
Browse files Browse the repository at this point in the history
  • Loading branch information
joerx committed May 26, 2014
1 parent f6c5571 commit 816e779
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/TestSuite/Fixture/TestFixture.php
Expand Up @@ -191,6 +191,8 @@ protected function _schemaFromImport() {

if (empty($import['table'])) {
throw new Error\Exception('Cannot import from undefined table.');
} else {
$this->table = $import['table'];
}

$db = ConnectionManager::get($import['connection'], false);
Expand Down Expand Up @@ -297,8 +299,9 @@ public function insert(Connection $db) {
*/
protected function _getRecords() {
$fields = $values = $types = [];
$columns = $this->_schema->columns();
foreach ($this->records as $record) {
$fields = array_merge($fields, array_keys(array_intersect_key($record, $this->fields)));
$fields = array_merge($fields, array_intersect(array_keys($record), $columns));
}
$fields = array_values(array_unique($fields));
foreach ($fields as $field) {
Expand Down
51 changes: 51 additions & 0 deletions tests/TestCase/TestSuite/TestFixtureTest.php
Expand Up @@ -110,6 +110,16 @@ class ImportFixture extends TestFixture {
* @var mixed
*/
public $import = ['table' => 'posts', 'connection' => 'test'];


/**
* Records property
*
* @var array
*/
public $records = array(
array('title' => 'Hello!', 'body' => 'Hello world!')
);
}

/**
Expand Down Expand Up @@ -269,6 +279,47 @@ public function testInsert() {
$this->assertSame($statement, $fixture->insert($db));
}

/**
* test the insert method
*
* @return void
*/
public function testInsertImport() {
$fixture = new ImportFixture();

$db = $this->getMock('Cake\Database\Connection', [], [], '', false);
$query = $this->getMock('Cake\Database\Query', [], [$db]);
$db->expects($this->once())
->method('newQuery')
->will($this->returnValue($query));

$query->expects($this->once())
->method('insert')
->with(['title', 'body'], ['title' => 'string', 'body' => 'text'])
->will($this->returnSelf());

$query->expects($this->once())
->method('into')
->with('posts')
->will($this->returnSelf());

$expected = [
['title' => 'Hello!', 'body' => 'Hello world!'],
];
$query->expects($this->at(2))
->method('values')
->with($expected[0])
->will($this->returnSelf());

$statement = $this->getMock('\PDOStatement', ['closeCursor']);
$statement->expects($this->once())->method('closeCursor');
$query->expects($this->once())
->method('execute')
->will($this->returnValue($statement));

$this->assertSame($statement, $fixture->insert($db));
}

/**
* test the insert method
*
Expand Down

0 comments on commit 816e779

Please sign in to comment.