/** * Test the limit function. * * @return void */ public function testLimit() { $db = new DboTestSource(); $result = $db->limit('0'); $this->assertNull($result); $result = $db->limit('10'); $this->assertEquals(' LIMIT 10', $result); $result = $db->limit('FARTS', 'BOOGERS'); $this->assertEquals(' LIMIT 0, 0', $result); $result = $db->limit(20, 10); $this->assertEquals(' LIMIT 10, 20', $result); $result = $db->limit(10, 3.0E+29); $scientificNotation = sprintf('%.1E', 3.0E+29); $this->assertNotContains($scientificNotation, $result); }
/** * Tests that transaction commands are logged * * @return void **/ public function testTransactionLogging() { $conn = $this->getMock('MockPDO'); $db = new DboTestSource(); $db->setConnection($conn); $conn->expects($this->exactly(2))->method('beginTransaction')->will($this->returnValue(true)); $conn->expects($this->once())->method('commit')->will($this->returnValue(true)); $conn->expects($this->once())->method('rollback')->will($this->returnValue(true)); $db->begin(); $log = $db->getLog(); $expected = array('query' => 'BEGIN', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => ''); $this->assertEquals($expected, $log['log'][0]); $db->commit(); $expected = array('query' => 'COMMIT', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => ''); $log = $db->getLog(); $this->assertEquals($expected, $log['log'][0]); $db->begin(); $expected = array('query' => 'BEGIN', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => ''); $log = $db->getLog(); $this->assertEquals($expected, $log['log'][0]); $db->rollback(); $expected = array('query' => 'ROLLBACK', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => ''); $log = $db->getLog(); $this->assertEquals($expected, $log['log'][0]); }
/** * Test conditionKeysToString() with virtual field * * @return void */ public function testConditionKeysToStringVirtualField() { $Article = ClassRegistry::init('Article'); $Article->virtualFields = array('extra' => 'something virtual'); $conn = $this->getMock('MockPDO', array('quote')); $db = new DboTestSource(); $db->setConnection($conn); $conn->expects($this->at(0))->method('quote')->will($this->returnValue('just text')); $conditions = array('Article.extra' => 'just text'); $result = $db->conditionKeysToString($conditions, true, $Article); $expected = "(" . $Article->virtualFields['extra'] . ") = just text"; $this->assertEquals($expected, $result[0]); $conn->expects($this->at(0))->method('quote')->will($this->returnValue('just text')); $conn->expects($this->at(1))->method('quote')->will($this->returnValue('other text')); $conditions = array('Article.extra' => array('just text', 'other text')); $result = $db->conditionKeysToString($conditions, true, $Article); $expected = "(" . $Article->virtualFields['extra'] . ") IN (just text, other text)"; $this->assertEquals($expected, $result[0]); }
/** * test that fields() is using methodCache() * * @return void */ public function testFieldsUsingMethodCache() { $this->testDb->cacheMethods = false; DboTestSource::$methodCache = array(); $Article = ClassRegistry::init('Article'); $this->testDb->fields($Article, null, array('title', 'body', 'published')); $this->assertTrue(empty(DboTestSource::$methodCache['fields']), 'Cache not empty'); }
/** * Test build statement with some fields missing * * @return void */ public function testBuildStatementDefaults() { $conn = $this->getMock('MockPDO'); $db = new DboTestSource(); $db->setConnection($conn); $subQuery = $db->buildStatement(array('fields' => array('DISTINCT(AssetsTag.asset_id)'), 'table' => "assets_tags", 'alias' => "AssetsTag", 'conditions' => array("Tag.name" => 'foo bar'), 'limit' => null, 'group' => "AssetsTag.asset_id"), $this->Model); }
/** * Test the limit function. * * @return void */ public function testLimit() { $db = new DboTestSource; $result = $db->limit('0'); $this->assertNull($result); $result = $db->limit('10'); $this->assertEquals(' LIMIT 10', $result); $result = $db->limit('FARTS', 'BOOGERS'); $this->assertEquals(' LIMIT 0, 0', $result); $result = $db->limit(20, 10); $this->assertEquals(' LIMIT 10, 20', $result); $result = $db->limit(10, 300000000000000000000000000000); $this->assertEquals(' LIMIT 0, 10', $result); }