diff --git a/src/TestSuite/Fixture/TestFixture.php b/src/TestSuite/Fixture/TestFixture.php index 1c0b38a9097..eb34ef90226 100644 --- a/src/TestSuite/Fixture/TestFixture.php +++ b/src/TestSuite/Fixture/TestFixture.php @@ -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); @@ -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) { diff --git a/tests/TestCase/TestSuite/TestFixtureTest.php b/tests/TestCase/TestSuite/TestFixtureTest.php index 529a0279faa..b62bb86f771 100644 --- a/tests/TestCase/TestSuite/TestFixtureTest.php +++ b/tests/TestCase/TestSuite/TestFixtureTest.php @@ -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!') + ); } /** @@ -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 *