Permalink
Browse files

Making DboSource tests more portable

  • Loading branch information...
1 parent d06d5f8 commit 61ff8d6154fad212514162dbbb136da4d33957f1 @lorenzo lorenzo committed Sep 3, 2011
Showing with 33 additions and 51 deletions.
  1. +33 −51 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -24,7 +24,21 @@
require_once dirname(dirname(__FILE__)) . DS . 'models.php';
class MockDataSource extends DataSource {
+}
+
+class DboTestSource extends DboSource {
+
+ public function connect($config = array()) {
+ $this->connected = true;
+ }
+ public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
+ return parent::_mergeAssociation(&$data, &$merge, $association, $type, $selfJoin);
+ }
+
+ public function setConfig($config) {
+ $this->config = $config;
+ }
}
/**
@@ -67,39 +81,7 @@ public function setUp() {
parent::setUp();
$this->__config = $this->db->config;
- if (!class_exists('DboTest')) {
- $db = ConnectionManager::getDataSource('test');
- $class = get_class($db);
- eval("class DboTest extends $class {
- var \$simulated = array();
-
-/**
- * execute method
- *
- * @param \$sql
- * @return void
- */
- function _execute(\$sql) {
- \$this->simulated[] = \$sql;
- return null;
- }
-
-/**
- * getLastQuery method
- *
- * @return void
- */
- public function getLastQuery() {
- return \$this->simulated[count(\$this->simulated) - 1];
- }
-
- public function mergeAssociation(&\$data, &\$merge, \$association, \$type, \$selfJoin = false) {
- return parent::_mergeAssociation(\$data, \$merge, \$association, \$type, \$selfJoin);
- }
- }");
- }
-
- $this->testDb = new DboTest($this->__config);
+ $this->testDb = new DboTestSource();
$this->testDb->cacheSources = false;
$this->testDb->startQuote = '`';
$this->testDb->endQuote = '`';
@@ -435,69 +417,69 @@ public function testMergeAssociations() {
* @return void
*/
public function testMagicMethodQuerying() {
- $result = $this->testDb->query('findByFieldName', array('value'), $this->Model);
+ $result = $this->db->query('findByFieldName', array('value'), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.field_name' => 'value'),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($expected, $result);
- $result = $this->testDb->query('findByFindBy', array('value'), $this->Model);
+ $result = $this->db->query('findByFindBy', array('value'), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.find_by' => 'value'),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($expected, $result);
- $result = $this->testDb->query('findAllByFieldName', array('value'), $this->Model);
+ $result = $this->db->query('findAllByFieldName', array('value'), $this->Model);
$expected = array('all', array(
'conditions' => array('TestModel.field_name' => 'value'),
'fields' => null, 'order' => null, 'limit' => null,
'page' => null, 'recursive' => null
));
$this->assertEqual($expected, $result);
- $result = $this->testDb->query('findAllById', array('a'), $this->Model);
+ $result = $this->db->query('findAllById', array('a'), $this->Model);
$expected = array('all', array(
'conditions' => array('TestModel.id' => 'a'),
'fields' => null, 'order' => null, 'limit' => null,
'page' => null, 'recursive' => null
));
$this->assertEqual($expected, $result);
- $result = $this->testDb->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model);
+ $result = $this->db->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.field_name' => array('value1', 'value2', 'value3')),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($expected, $result);
- $result = $this->testDb->query('findByFieldName', array(null), $this->Model);
+ $result = $this->db->query('findByFieldName', array(null), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.field_name' => null),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($expected, $result);
- $result = $this->testDb->query('findByFieldName', array('= a'), $this->Model);
+ $result = $this->db->query('findByFieldName', array('= a'), $this->Model);
$expected = array('first', array(
'conditions' => array('TestModel.field_name' => '= a'),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($expected, $result);
- $result = $this->testDb->query('findByFieldName', array(), $this->Model);
+ $result = $this->db->query('findByFieldName', array(), $this->Model);
$expected = false;
$this->assertEqual($expected, $result);
+ }
- $result = $this->testDb->query('directCall', array(), $this->Model);
- $this->assertFalse($result);
-
- $result = $this->testDb->query('directCall', true, $this->Model);
- $this->assertFalse($result);
-
- $result = $this->testDb->query('directCall', false, $this->Model);
- $this->assertFalse($result);
+/**
+ *
+ * @expectedException PDOException
+ * @return void
+ */
+ public function testDirectCallThrowsException() {
+ $result = $this->db->query('directCall', array(), $this->Model);
}
@@ -507,10 +489,10 @@ public function testMagicMethodQuerying() {
* @return void
*/
public function testValue() {
- $result = $this->testDb->value('{$__cakeForeignKey__$}');
+ $result = $this->db->value('{$__cakeForeignKey__$}');
$this->assertEqual($result, '{$__cakeForeignKey__$}');
- $result = $this->testDb->value(array('first', 2, 'third'));
+ $result = $this->db->value(array('first', 2, 'third'));
$expected = array('\'first\'', 2, '\'third\'');
$this->assertEqual($expected, $result);
}

0 comments on commit 61ff8d6

Please sign in to comment.