Even more methods containing quoting specific to mysql out of DboSource

This commit is contained in:
José Lorenzo Rodríguez 2010-11-03 19:59:15 -04:30
parent 6028705c72
commit 3a8016b875
2 changed files with 713 additions and 710 deletions

View file

@ -1744,4 +1744,717 @@ class DboMysqlTest extends CakeTestCase {
$expected = array('DISTINCT `Vendor`.`id`', '`Vendor`.`name`');
$this->assertEqual($result, $expected);
}
/**
* testStringConditionsParsing method
*
* @access public
* @return void
*/
function testStringConditionsParsing() {
$result = $this->Dbo->conditions("ProjectBid.project_id = Project.id");
$expected = " WHERE `ProjectBid`.`project_id` = `Project`.`id`";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'");
$expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("HardCandy.name LIKE 'a' AND Candy.name LIKE 'c'");
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("Post.title = '1.1'");
$expected = " WHERE `Post`.`title` = '1.1'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("User.id != 0 AND User.user LIKE '%arr%'");
$expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("SUM(Post.comments_count) > 500");
$expected = " WHERE SUM(`Post`.`comments_count`) > 500";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("score BETWEEN 90.1 AND 95.7");
$expected = " WHERE score BETWEEN 90.1 AND 95.7";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('score' => array(2=>1, 2, 10)));
$expected = " WHERE score IN (1, 2, 10)";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("Aro.rght = Aro.lft + 1.1");
$expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2');
$expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2';
$this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result);
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"');
$expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('SUM(Post.comments_count) > 500 AND NOT Post.title IS NULL AND NOT Post.extended_title IS NULL');
$expected = ' WHERE SUM(`Post`.`comments_count`) > 500 AND NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('NOT Post.title IS NULL AND NOT Post.extended_title IS NULL AND SUM(Post.comments_count) > 500');
$expected = ' WHERE NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL AND SUM(`Post`.`comments_count`) > 500';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('NOT Post.extended_title IS NULL AND NOT Post.title IS NULL AND Post.title != "" AND SPOON(SUM(Post.comments_count) + 1.1) > 500');
$expected = ' WHERE NOT `Post`.`extended_title` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title` != "" AND SPOON(SUM(`Post`.`comments_count`) + 1.1) > 500';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('NOT Post.title_extended IS NULL AND NOT Post.title IS NULL AND Post.title_extended != Post.title');
$expected = ' WHERE NOT `Post`.`title_extended` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title_extended` != `Post`.`title`';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("Comment.id = 'a'");
$expected = " WHERE `Comment`.`id` = 'a'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("lower(Article.title) LIKE 'a%'");
$expected = " WHERE lower(`Article`.`title`) LIKE 'a%'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('((MATCH(Video.title) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(Video.description) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(Video.tags) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))');
$expected = ' WHERE ((MATCH(`Video`.`title`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(`Video`.`description`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(`Video`.`tags`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('DATEDIFF(NOW(),Article.published) < 1 && Article.live=1');
$expected = " WHERE DATEDIFF(NOW(),`Article`.`published`) < 1 && `Article`.`live`=1";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('file = "index.html"');
$expected = ' WHERE file = "index.html"';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions("file = 'index.html'");
$expected = " WHERE file = 'index.html'";
$this->assertEqual($result, $expected);
$letter = $letter = 'd.a';
$conditions = array('Company.name like ' => $letter . '%');
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE `Company`.`name` like 'd.a%'";
$this->assertEqual($result, $expected);
$conditions = array('Artist.name' => 'JUDY and MARY');
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE `Artist`.`name` = 'JUDY and MARY'";
$this->assertEqual($result, $expected);
$conditions = array('Artist.name' => 'JUDY AND MARY');
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE `Artist`.`name` = 'JUDY AND MARY'";
$this->assertEqual($result, $expected);
}
/**
* testQuotesInStringConditions method
*
* @access public
* @return void
*/
function testQuotesInStringConditions() {
$result = $this->Dbo->conditions('Member.email = \'mariano@cricava.com\'');
$expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\'';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('Member.email = "mariano@cricava.com"');
$expected = ' WHERE `Member`.`email` = "mariano@cricava.com"';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('Member.email = \'mariano@cricava.com\' AND Member.user LIKE \'mariano.iglesias%\'');
$expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\' AND `Member`.`user` LIKE \'mariano.iglesias%\'';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions('Member.email = "mariano@cricava.com" AND Member.user LIKE "mariano.iglesias%"');
$expected = ' WHERE `Member`.`email` = "mariano@cricava.com" AND `Member`.`user` LIKE "mariano.iglesias%"';
$this->assertEqual($result, $expected);
}
/**
* testParenthesisInStringConditions method
*
* @access public
* @return void
*/
function testParenthesisInStringConditions() {
$result = $this->Dbo->conditions('Member.name = \'(lu\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \')lu\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'va(lu\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'va)lu\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'va(lu)\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'va(lu)e\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'(mariano)\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'(mariano)iglesias\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'(mariano) iglesias\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'(mariano word) iglesias\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'(mariano.iglesias)\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias)\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias) CakePHP\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result);
$result = $this->Dbo->conditions('Member.name = \'(mariano.iglesias) CakePHP\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
}
/**
* testParenthesisInArrayConditions method
*
* @access public
* @return void
*/
function testParenthesisInArrayConditions() {
$result = $this->Dbo->conditions(array('Member.name' => '(lu'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => ')lu'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => 'va(lu'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => 'va)lu'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => 'va(lu)'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => 'va(lu)e'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => '(mariano)'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => '(mariano)iglesias'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => '(mariano) iglesias'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => '(mariano word) iglesias'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => '(mariano.iglesias)'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias)'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias) CakePHP'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result);
$result = $this->Dbo->conditions(array('Member.name' => '(mariano.iglesias) CakePHP'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
}
/**
* testArrayConditionsParsing method
*
* @access public
* @return void
*/
function testArrayConditionsParsing() {
$this->loadFixtures('Post', 'Author');
$result = $this->Dbo->conditions(array('Stereo.type' => 'in dash speakers'));
$this->assertPattern("/^\s+WHERE\s+`Stereo`.`type`\s+=\s+'in dash speakers'/", $result);
$result = $this->Dbo->conditions(array('Candy.name LIKE' => 'a', 'HardCandy.name LIKE' => 'c'));
$this->assertPattern("/^\s+WHERE\s+`Candy`.`name` LIKE\s+'a'\s+AND\s+`HardCandy`.`name`\s+LIKE\s+'c'/", $result);
$result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'a', 'Candy.name LIKE' => 'c'));
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'a%', 'Candy.name LIKE' => '%c%'));
$expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('HardCandy.name LIKE' => 'to be or%', 'Candy.name LIKE' => '%not to be%'));
$expected = " WHERE `HardCandy`.`name` LIKE 'to be or%' AND `Candy`.`name` LIKE '%not to be%'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('score BETWEEN ? AND ?' => array(90.1, 95.7)));
$expected = " WHERE `score` BETWEEN 90.100000 AND 95.700000";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Post.title' => 1.1));
$expected = " WHERE `Post`.`title` = 1.100000";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Post.title' => 1.1), true, true, new Post());
$expected = " WHERE `Post`.`title` = '1.1'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('SUM(Post.comments_count) >' => '500'));
$expected = " WHERE SUM(`Post`.`comments_count`) > '500'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('MAX(Post.rating) >' => '50'));
$expected = " WHERE MAX(`Post`.`rating`) > '50'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('title LIKE' => '%hello'));
$expected = " WHERE `title` LIKE '%hello'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Post.name' => 'mad(g)ik'));
$expected = " WHERE `Post`.`name` = 'mad(g)ik'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('score' => array(1, 2, 10)));
$expected = " WHERE score IN (1, 2, 10)";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('score' => array()));
$expected = " WHERE `score` IS NULL";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('score !=' => array()));
$expected = " WHERE `score` IS NOT NULL";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('score !=' => '20'));
$expected = " WHERE `score` != '20'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('score >' => '20'));
$expected = " WHERE `score` > '20'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('client_id >' => '20'), true, true, new TestModel());
$expected = " WHERE `client_id` > 20";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('OR' => array(
array('User.user' => 'mariano'),
array('User.user' => 'nate')
)));
$expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate'))";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('or' => array(
'score BETWEEN ? AND ?' => array('4', '5'), 'rating >' => '20'
)));
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > '20'))";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('or' => array(
'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
)));
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > '20'))";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('and' => array(
'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
)));
$expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > '20'))";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(
'published' => 1, 'or' => array('score >' => '2', array('score >' => '20'))
));
$expected = " WHERE `published` = 1 AND ((`score` > '2') OR (`score` > '20'))";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(array('Project.removed' => false)));
$expected = " WHERE `Project`.`removed` = '0'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(array('Project.removed' => true)));
$expected = " WHERE `Project`.`removed` = '1'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(array('Project.removed' => null)));
$expected = " WHERE `Project`.`removed` IS NULL";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(array('Project.removed !=' => null)));
$expected = " WHERE `Project`.`removed` IS NOT NULL";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('(Usergroup.permissions) & 4' => 4));
$expected = " WHERE (`Usergroup`.`permissions`) & 4 = 4";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('((Usergroup.permissions) & 4)' => 4));
$expected = " WHERE ((`Usergroup`.`permissions`) & 4) = 4";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Post.modified >=' => 'DATE_SUB(NOW(), INTERVAL 7 DAY)'));
$expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Post.modified >= DATE_SUB(NOW(), INTERVAL 7 DAY)'));
$expected = " WHERE `Post`.`modified` >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(
'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)),
'Enrollment.yearcompleted >' => '0')
);
$this->assertPattern('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(level_of_education_id IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result);
$result = $this->Dbo->conditions(array('id <>' => '8'));
$this->assertPattern('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result);
$result = $this->Dbo->conditions(array('TestModel.field =' => 'gribe$@()lu'));
$expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'";
$this->assertEqual($result, $expected);
$conditions['NOT'] = array('Listing.expiration BETWEEN ? AND ?' => array("1", "100"));
$conditions[0]['OR'] = array(
"Listing.title LIKE" => "%term%",
"Listing.description LIKE" => "%term%"
);
$conditions[1]['OR'] = array(
"Listing.title LIKE" => "%term_2%",
"Listing.description LIKE" => "%term_2%"
);
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE NOT (`Listing`.`expiration` BETWEEN '1' AND '100') AND" .
" ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND" .
" ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('MD5(CONCAT(Reg.email,Reg.id))' => 'blah'));
$expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(
'MD5(CONCAT(Reg.email,Reg.id))' => array('blah', 'blahblah')
));
$expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) IN ('blah', 'blahblah')";
$this->assertEqual($result, $expected);
$conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76));
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE id IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
$this->assertEqual($result, $expected);
$conditions = array('title' => 'user(s)');
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE `title` = 'user(s)'";
$this->assertEqual($result, $expected);
$conditions = array('title' => 'user(s) data');
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE `title` = 'user(s) data'";
$this->assertEqual($result, $expected);
$conditions = array('title' => 'user(s,arg) data');
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE `title` = 'user(s,arg) data'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array("Book.book_name" => 'Java(TM)'));
$expected = " WHERE `Book`.`book_name` = 'Java(TM)'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array("Book.book_name" => 'Java(TM) '));
$expected = " WHERE `Book`.`book_name` = 'Java(TM) '";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array("Book.id" => 0));
$expected = " WHERE `Book`.`id` = 0";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array("Book.id" => NULL));
$expected = " WHERE `Book`.`id` IS NULL";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Listing.beds >=' => 0));
$expected = " WHERE `Listing`.`beds` >= 0";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(
'ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN ? AND ?' => array(65, 90)
));
$expected = ' WHERE ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN 65 AND 90';
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('or' => array(
'? BETWEEN Model.field1 AND Model.field2' => '2009-03-04'
)));
$expected = " WHERE '2009-03-04' BETWEEN Model.field1 AND Model.field2";
$this->assertEqual($result, $expected);
}
/**
* testArrayConditionsParsingComplexKeys method
*
* @access public
* @return void
*/
function testArrayConditionsParsingComplexKeys() {
$result = $this->Dbo->conditions(array(
'CAST(Book.created AS DATE)' => '2008-08-02'
));
$expected = " WHERE CAST(`Book`.`created` AS DATE) = '2008-08-02'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(
'CAST(Book.created AS DATE) <=' => '2008-08-02'
));
$expected = " WHERE CAST(`Book`.`created` AS DATE) <= '2008-08-02'";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array(
'(Stats.clicks * 100) / Stats.views >' => 50
));
$expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50";
$this->assertEqual($result, $expected);
}
/**
* testMixedConditionsParsing method
*
* @access public
* @return void
*/
function testMixedConditionsParsing() {
$conditions[] = 'User.first_name = \'Firstname\'';
$conditions[] = array('User.last_name' => 'Lastname');
$result = $this->Dbo->conditions($conditions);
$expected = " WHERE `User`.`first_name` = 'Firstname' AND `User`.`last_name` = 'Lastname'";
$this->assertEqual($result, $expected);
$conditions = array(
'Thread.project_id' => 5,
'Thread.buyer_id' => 14,
'1=1 GROUP BY Thread.project_id'
);
$result = $this->Dbo->conditions($conditions);
$this->assertPattern('/^\s*WHERE\s+`Thread`.`project_id`\s*=\s*5\s+AND\s+`Thread`.`buyer_id`\s*=\s*14\s+AND\s+1\s*=\s*1\s+GROUP BY `Thread`.`project_id`$/', $result);
}
/**
* testConditionsOptionalArguments method
*
* @access public
* @return void
*/
function testConditionsOptionalArguments() {
$result = $this->Dbo->conditions( array('Member.name' => 'Mariano'), true, false);
$this->assertPattern('/^\s*`Member`.`name`\s*=\s*\'Mariano\'\s*$/', $result);
$result = $this->Dbo->conditions( array(), true, false);
$this->assertPattern('/^\s*1\s*=\s*1\s*$/', $result);
}
/**
* testConditionsWithModel
*
* @access public
* @return void
*/
function testConditionsWithModel() {
$this->Model = new Article2();
$result = $this->Dbo->conditions(array('Article2.viewed >=' => 0), true, true, $this->Model);
$expected = " WHERE `Article2`.`viewed` >= 0";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Article2.viewed >=' => '0'), true, true, $this->Model);
$expected = " WHERE `Article2`.`viewed` >= 0";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Article2.viewed >=' => '1'), true, true, $this->Model);
$expected = " WHERE `Article2`.`viewed` >= 1";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array(0, 10)), true, true, $this->Model);
$expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('0', '10')), true, true, $this->Model);
$expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10";
$this->assertEqual($result, $expected);
$result = $this->Dbo->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('1', '10')), true, true, $this->Model);
$expected = " WHERE `Article2`.`rate_sum` BETWEEN 1 AND 10";
$this->assertEqual($result, $expected);
}
/**
* testFieldParsing method
*
* @access public
* @return void
*/
function testFieldParsing() {
$this->Model = new TestModel();
$result = $this->Dbo->fields($this->Model, 'Vendor', "Vendor.id, COUNT(Model.vendor_id) AS `Vendor`.`count`");
$expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, 'Vendor', "`Vendor`.`id`, COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`");
$expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, 'Post', "CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name, Node.created");
$expected = array("CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", "`Node`.`created`");
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, 'round( (3.55441 * fooField), 3 ) AS test');
$this->assertEqual($result, array('round( (3.55441 * fooField), 3 ) AS test'));
$result = $this->Dbo->fields($this->Model, null, 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating');
$this->assertEqual($result, array('ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating'));
$result = $this->Dbo->fields($this->Model, null, 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating');
$this->assertEqual($result, array('ROUND(Rating.rate_total / Rating.rate_count,2) AS rating'));
$result = $this->Dbo->fields($this->Model, 'Post', "Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name");
$expected = array("`Node`.`created`", "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name");
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, 'Post', "2.2,COUNT(*), SUM(Something.else) as sum, Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name,Post.title,Post.1,1.1");
$expected = array(
'2.2', 'COUNT(*)', 'SUM(`Something`.`else`) as sum', '`Node`.`created`',
"CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", '`Post`.`title`', '`Post`.`1`', '1.1'
);
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, "(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
$expected = array("(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, 'Post');
$expected = array(
'`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`',
'`Post`.`passwd`', '`Post`.`addr_1`', '`Post`.`addr_2`', '`Post`.`zip_code`',
'`Post`.`city`', '`Post`.`country`', '`Post`.`phone`', '`Post`.`fax`',
'`Post`.`url`', '`Post`.`email`', '`Post`.`comments`', '`Post`.`last_login`',
'`Post`.`created`', '`Post`.`updated`'
);
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, 'Other');
$expected = array(
'`Other`.`id`', '`Other`.`client_id`', '`Other`.`name`', '`Other`.`login`',
'`Other`.`passwd`', '`Other`.`addr_1`', '`Other`.`addr_2`', '`Other`.`zip_code`',
'`Other`.`city`', '`Other`.`country`', '`Other`.`phone`', '`Other`.`fax`',
'`Other`.`url`', '`Other`.`email`', '`Other`.`comments`', '`Other`.`last_login`',
'`Other`.`created`', '`Other`.`updated`'
);
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, array(), false);
$expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, 'COUNT(*)');
$expected = array('COUNT(*)');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, 'SUM(Thread.unread_buyer) AS ' . $this->Dbo->name('sum_unread_buyer'));
$expected = array('SUM(`Thread`.`unread_buyer`) AS `sum_unread_buyer`');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, 'name, count(*)');
$expected = array('`TestModel`.`name`', 'count(*)');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, 'count(*), name');
$expected = array('count(*)', '`TestModel`.`name`');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields(
$this->Model, null, 'field1, field2, field3, count(*), name'
);
$expected = array(
'`TestModel`.`field1`', '`TestModel`.`field2`',
'`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`'
);
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, array('dayofyear(now())'));
$expected = array('dayofyear(now())');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, array('MAX(Model.field) As Max'));
$expected = array('MAX(`Model`.`field`) As Max');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, array('Model.field AS AnotherName'));
$expected = array('`Model`.`field` AS `AnotherName`');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, array('field AS AnotherName'));
$expected = array('`field` AS `AnotherName`');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, null, array(
'TestModel.field AS AnotherName'
));
$expected = array('`TestModel`.`field` AS `AnotherName`');
$this->assertEqual($result, $expected);
$result = $this->Dbo->fields($this->Model, 'Foo', array(
'id', 'title', '(user_count + discussion_count + post_count) AS score'
));
$expected = array(
'`Foo`.`id`',
'`Foo`.`title`',
'(user_count + discussion_count + post_count) AS score'
);
$this->assertEqual($result, $expected);
}
/**
* test that fields() will accept objects made from DboSource::expression
*
* @return void
*/
function testFieldsWithExpression() {
$this->Model = new TestModel;
$expression = $this->Dbo->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col");
$result = $this->Dbo->fields($this->Model, null, array("id", $expression));
$expected = array(
'`TestModel`.`id`',
"CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"
);
$this->assertEqual($result, $expected);
}
}

View file

@ -142,716 +142,6 @@ class DboSourceTest extends CakeTestCase {
$result = $this->testDb->conditions(' ', '" " conditions failed %s');
$this->assertEqual($result, ' WHERE 1 = 1');
}
/**
* testStringConditionsParsing method
*
* @access public
* @return void
*/
function testStringConditionsParsing() {
$result = $this->testDb->conditions("ProjectBid.project_id = Project.id");
$expected = " WHERE `ProjectBid`.`project_id` = `Project`.`id`";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'");
$expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("HardCandy.name LIKE 'a' AND Candy.name LIKE 'c'");
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("Post.title = '1.1'");
$expected = " WHERE `Post`.`title` = '1.1'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("User.id != 0 AND User.user LIKE '%arr%'");
$expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("SUM(Post.comments_count) > 500");
$expected = " WHERE SUM(`Post`.`comments_count`) > 500";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("score BETWEEN 90.1 AND 95.7");
$expected = " WHERE score BETWEEN 90.1 AND 95.7";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('score' => array(2=>1, 2, 10)));
$expected = " WHERE score IN (1, 2, 10)";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("Aro.rght = Aro.lft + 1.1");
$expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("(Post.created < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(Post.created), MONTH(Post.created)");
$expected = " WHERE (`Post`.`created` < '" . date('Y-m-d H:i:s') . "') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`)";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2');
$expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2';
$this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result);
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"');
$expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('SUM(Post.comments_count) > 500 AND NOT Post.title IS NULL AND NOT Post.extended_title IS NULL');
$expected = ' WHERE SUM(`Post`.`comments_count`) > 500 AND NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('NOT Post.title IS NULL AND NOT Post.extended_title IS NULL AND SUM(Post.comments_count) > 500');
$expected = ' WHERE NOT `Post`.`title` IS NULL AND NOT `Post`.`extended_title` IS NULL AND SUM(`Post`.`comments_count`) > 500';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('NOT Post.extended_title IS NULL AND NOT Post.title IS NULL AND Post.title != "" AND SPOON(SUM(Post.comments_count) + 1.1) > 500');
$expected = ' WHERE NOT `Post`.`extended_title` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title` != "" AND SPOON(SUM(`Post`.`comments_count`) + 1.1) > 500';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('NOT Post.title_extended IS NULL AND NOT Post.title IS NULL AND Post.title_extended != Post.title');
$expected = ' WHERE NOT `Post`.`title_extended` IS NULL AND NOT `Post`.`title` IS NULL AND `Post`.`title_extended` != `Post`.`title`';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("Comment.id = 'a'");
$expected = " WHERE `Comment`.`id` = 'a'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("lower(Article.title) LIKE 'a%'");
$expected = " WHERE lower(`Article`.`title`) LIKE 'a%'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('((MATCH(Video.title) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(Video.description) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(Video.tags) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))');
$expected = ' WHERE ((MATCH(`Video`.`title`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 2) + (MATCH(`Video`.`description`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 0.4) + (MATCH(`Video`.`tags`) AGAINST(\'My Search*\' IN BOOLEAN MODE) * 1.5))';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('DATEDIFF(NOW(),Article.published) < 1 && Article.live=1');
$expected = " WHERE DATEDIFF(NOW(),`Article`.`published`) < 1 && `Article`.`live`=1";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('file = "index.html"');
$expected = ' WHERE file = "index.html"';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions("file = 'index.html'");
$expected = " WHERE file = 'index.html'";
$this->assertEqual($result, $expected);
$letter = $letter = 'd.a';
$conditions = array('Company.name like ' => $letter . '%');
$result = $this->testDb->conditions($conditions);
$expected = " WHERE `Company`.`name` like 'd.a%'";
$this->assertEqual($result, $expected);
$conditions = array('Artist.name' => 'JUDY and MARY');
$result = $this->testDb->conditions($conditions);
$expected = " WHERE `Artist`.`name` = 'JUDY and MARY'";
$this->assertEqual($result, $expected);
$conditions = array('Artist.name' => 'JUDY AND MARY');
$result = $this->testDb->conditions($conditions);
$expected = " WHERE `Artist`.`name` = 'JUDY AND MARY'";
$this->assertEqual($result, $expected);
}
/**
* testQuotesInStringConditions method
*
* @access public
* @return void
*/
function testQuotesInStringConditions() {
$result = $this->testDb->conditions('Member.email = \'mariano@cricava.com\'');
$expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\'';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('Member.email = "mariano@cricava.com"');
$expected = ' WHERE `Member`.`email` = "mariano@cricava.com"';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('Member.email = \'mariano@cricava.com\' AND Member.user LIKE \'mariano.iglesias%\'');
$expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\' AND `Member`.`user` LIKE \'mariano.iglesias%\'';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions('Member.email = "mariano@cricava.com" AND Member.user LIKE "mariano.iglesias%"');
$expected = ' WHERE `Member`.`email` = "mariano@cricava.com" AND `Member`.`user` LIKE "mariano.iglesias%"';
$this->assertEqual($result, $expected);
}
/**
* testParenthesisInStringConditions method
*
* @access public
* @return void
*/
function testParenthesisInStringConditions() {
$result = $this->testDb->conditions('Member.name = \'(lu\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
$result = $this->testDb->conditions('Member.name = \')lu\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'va(lu\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'va)lu\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'va(lu)\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'va(lu)e\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'(mariano)\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'(mariano)iglesias\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'(mariano) iglesias\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'(mariano word) iglesias\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'(mariano.iglesias)\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias)\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'Mariano Iglesias (mariano.iglesias) CakePHP\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result);
$result = $this->testDb->conditions('Member.name = \'(mariano.iglesias) CakePHP\'');
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
}
/**
* testParenthesisInArrayConditions method
*
* @access public
* @return void
*/
function testParenthesisInArrayConditions() {
$result = $this->testDb->conditions(array('Member.name' => '(lu'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => ')lu'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => 'va(lu'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => 'va)lu'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => 'va(lu)'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => 'va(lu)e'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => '(mariano)'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => '(mariano)iglesias'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => '(mariano) iglesias'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => '(mariano word) iglesias'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => '(mariano.iglesias)'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias)'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => 'Mariano Iglesias (mariano.iglesias) CakePHP'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/', $result);
$result = $this->testDb->conditions(array('Member.name' => '(mariano.iglesias) CakePHP'));
$this->assertPattern('/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/', $result);
}
/**
* testArrayConditionsParsing method
*
* @access public
* @return void
*/
function testArrayConditionsParsing() {
$this->loadFixtures('Post', 'Author');
$result = $this->testDb->conditions(array('Stereo.type' => 'in dash speakers'));
$this->assertPattern("/^\s+WHERE\s+`Stereo`.`type`\s+=\s+'in dash speakers'/", $result);
$result = $this->testDb->conditions(array('Candy.name LIKE' => 'a', 'HardCandy.name LIKE' => 'c'));
$this->assertPattern("/^\s+WHERE\s+`Candy`.`name` LIKE\s+'a'\s+AND\s+`HardCandy`.`name`\s+LIKE\s+'c'/", $result);
$result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'a', 'Candy.name LIKE' => 'c'));
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'a%', 'Candy.name LIKE' => '%c%'));
$expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('HardCandy.name LIKE' => 'to be or%', 'Candy.name LIKE' => '%not to be%'));
$expected = " WHERE `HardCandy`.`name` LIKE 'to be or%' AND `Candy`.`name` LIKE '%not to be%'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('score BETWEEN ? AND ?' => array(90.1, 95.7)));
$expected = " WHERE `score` BETWEEN 90.100000 AND 95.700000";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Post.title' => 1.1));
$expected = " WHERE `Post`.`title` = 1.100000";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Post.title' => 1.1), true, true, new Post());
$expected = " WHERE `Post`.`title` = '1.1'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('SUM(Post.comments_count) >' => '500'));
$expected = " WHERE SUM(`Post`.`comments_count`) > '500'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('MAX(Post.rating) >' => '50'));
$expected = " WHERE MAX(`Post`.`rating`) > '50'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('title LIKE' => '%hello'));
$expected = " WHERE `title` LIKE '%hello'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Post.name' => 'mad(g)ik'));
$expected = " WHERE `Post`.`name` = 'mad(g)ik'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('score' => array(1, 2, 10)));
$expected = " WHERE score IN (1, 2, 10)";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('score' => array()));
$expected = " WHERE `score` IS NULL";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('score !=' => array()));
$expected = " WHERE `score` IS NOT NULL";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('score !=' => '20'));
$expected = " WHERE `score` != '20'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('score >' => '20'));
$expected = " WHERE `score` > '20'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('client_id >' => '20'), true, true, new TestModel());
$expected = " WHERE `client_id` > 20";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('OR' => array(
array('User.user' => 'mariano'),
array('User.user' => 'nate')
)));
$expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate'))";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('or' => array(
'score BETWEEN ? AND ?' => array('4', '5'), 'rating >' => '20'
)));
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`rating` > '20'))";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('or' => array(
'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
)));
$expected = " WHERE ((`score` BETWEEN '4' AND '5') OR (`score` > '20'))";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('and' => array(
'score BETWEEN ? AND ?' => array('4', '5'), array('score >' => '20')
)));
$expected = " WHERE ((`score` BETWEEN '4' AND '5') AND (`score` > '20'))";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(
'published' => 1, 'or' => array('score >' => '2', array('score >' => '20'))
));
$expected = " WHERE `published` = 1 AND ((`score` > '2') OR (`score` > '20'))";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(array('Project.removed' => false)));
$expected = " WHERE `Project`.`removed` = 0";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(array('Project.removed' => true)));
$expected = " WHERE `Project`.`removed` = 1";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(array('Project.removed' => null)));
$expected = " WHERE `Project`.`removed` IS NULL";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(array('Project.removed !=' => null)));
$expected = " WHERE `Project`.`removed` IS NOT NULL";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('(Usergroup.permissions) & 4' => 4));
$expected = " WHERE (`Usergroup`.`permissions`) & 4 = 4";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('((Usergroup.permissions) & 4)' => 4));
$expected = " WHERE ((`Usergroup`.`permissions`) & 4) = 4";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Post.modified >=' => 'DATE_SUB(NOW(), INTERVAL 7 DAY)'));
$expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Post.modified >= DATE_SUB(NOW(), INTERVAL 7 DAY)'));
$expected = " WHERE `Post`.`modified` >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(
'NOT' => array('Course.id' => null, 'Course.vet' => 'N', 'level_of_education_id' => array(912,999)),
'Enrollment.yearcompleted >' => '0')
);
$this->assertPattern('/^\s*WHERE\s+\(NOT\s+\(`Course`\.`id` IS NULL\)\s+AND NOT\s+\(`Course`\.`vet`\s+=\s+\'N\'\)\s+AND NOT\s+\(level_of_education_id IN \(912, 999\)\)\)\s+AND\s+`Enrollment`\.`yearcompleted`\s+>\s+\'0\'\s*$/', $result);
$result = $this->testDb->conditions(array('id <>' => '8'));
$this->assertPattern('/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/', $result);
$result = $this->testDb->conditions(array('TestModel.field =' => 'gribe$@()lu'));
$expected = " WHERE `TestModel`.`field` = 'gribe$@()lu'";
$this->assertEqual($result, $expected);
$conditions['NOT'] = array('Listing.expiration BETWEEN ? AND ?' => array("1", "100"));
$conditions[0]['OR'] = array(
"Listing.title LIKE" => "%term%",
"Listing.description LIKE" => "%term%"
);
$conditions[1]['OR'] = array(
"Listing.title LIKE" => "%term_2%",
"Listing.description LIKE" => "%term_2%"
);
$result = $this->testDb->conditions($conditions);
$expected = " WHERE NOT (`Listing`.`expiration` BETWEEN '1' AND '100') AND" .
" ((`Listing`.`title` LIKE '%term%') OR (`Listing`.`description` LIKE '%term%')) AND" .
" ((`Listing`.`title` LIKE '%term_2%') OR (`Listing`.`description` LIKE '%term_2%'))";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('MD5(CONCAT(Reg.email,Reg.id))' => 'blah'));
$expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(
'MD5(CONCAT(Reg.email,Reg.id))' => array('blah', 'blahblah')
));
$expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) IN ('blah', 'blahblah')";
$this->assertEqual($result, $expected);
$conditions = array('id' => array(2, 5, 6, 9, 12, 45, 78, 43, 76));
$result = $this->testDb->conditions($conditions);
$expected = " WHERE id IN (2, 5, 6, 9, 12, 45, 78, 43, 76)";
$this->assertEqual($result, $expected);
$conditions = array('title' => 'user(s)');
$result = $this->testDb->conditions($conditions);
$expected = " WHERE `title` = 'user(s)'";
$this->assertEqual($result, $expected);
$conditions = array('title' => 'user(s) data');
$result = $this->testDb->conditions($conditions);
$expected = " WHERE `title` = 'user(s) data'";
$this->assertEqual($result, $expected);
$conditions = array('title' => 'user(s,arg) data');
$result = $this->testDb->conditions($conditions);
$expected = " WHERE `title` = 'user(s,arg) data'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM)'));
$expected = " WHERE `Book`.`book_name` = 'Java(TM)'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array("Book.book_name" => 'Java(TM) '));
$expected = " WHERE `Book`.`book_name` = 'Java(TM) '";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array("Book.id" => 0));
$expected = " WHERE `Book`.`id` = 0";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array("Book.id" => NULL));
$expected = " WHERE `Book`.`id` IS NULL";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Listing.beds >=' => 0));
$expected = " WHERE `Listing`.`beds` >= 0";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(
'ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN ? AND ?' => array(65, 90)
));
$expected = ' WHERE ASCII(SUBSTRING(keyword, 1, 1)) BETWEEN 65 AND 90';
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('or' => array(
'? BETWEEN Model.field1 AND Model.field2' => '2009-03-04'
)));
$expected = " WHERE '2009-03-04' BETWEEN Model.field1 AND Model.field2";
$this->assertEqual($result, $expected);
}
/**
* testArrayConditionsParsingComplexKeys method
*
* @access public
* @return void
*/
function testArrayConditionsParsingComplexKeys() {
$result = $this->testDb->conditions(array(
'CAST(Book.created AS DATE)' => '2008-08-02'
));
$expected = " WHERE CAST(`Book`.`created` AS DATE) = '2008-08-02'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(
'CAST(Book.created AS DATE) <=' => '2008-08-02'
));
$expected = " WHERE CAST(`Book`.`created` AS DATE) <= '2008-08-02'";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array(
'(Stats.clicks * 100) / Stats.views >' => 50
));
$expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50";
$this->assertEqual($result, $expected);
}
/**
* testMixedConditionsParsing method
*
* @access public
* @return void
*/
function testMixedConditionsParsing() {
$conditions[] = 'User.first_name = \'Firstname\'';
$conditions[] = array('User.last_name' => 'Lastname');
$result = $this->testDb->conditions($conditions);
$expected = " WHERE `User`.`first_name` = 'Firstname' AND `User`.`last_name` = 'Lastname'";
$this->assertEqual($result, $expected);
$conditions = array(
'Thread.project_id' => 5,
'Thread.buyer_id' => 14,
'1=1 GROUP BY Thread.project_id'
);
$result = $this->testDb->conditions($conditions);
$this->assertPattern('/^\s*WHERE\s+`Thread`.`project_id`\s*=\s*5\s+AND\s+`Thread`.`buyer_id`\s*=\s*14\s+AND\s+1\s*=\s*1\s+GROUP BY `Thread`.`project_id`$/', $result);
}
/**
* testConditionsOptionalArguments method
*
* @access public
* @return void
*/
function testConditionsOptionalArguments() {
$result = $this->testDb->conditions( array('Member.name' => 'Mariano'), true, false);
$this->assertPattern('/^\s*`Member`.`name`\s*=\s*\'Mariano\'\s*$/', $result);
$result = $this->testDb->conditions( array(), true, false);
$this->assertPattern('/^\s*1\s*=\s*1\s*$/', $result);
}
/**
* testConditionsWithModel
*
* @access public
* @return void
*/
function testConditionsWithModel() {
$this->Model = new Article2();
$result = $this->testDb->conditions(array('Article2.viewed >=' => 0), true, true, $this->Model);
$expected = " WHERE `Article2`.`viewed` >= 0";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Article2.viewed >=' => '0'), true, true, $this->Model);
$expected = " WHERE `Article2`.`viewed` >= 0";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Article2.viewed >=' => '1'), true, true, $this->Model);
$expected = " WHERE `Article2`.`viewed` >= 1";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array(0, 10)), true, true, $this->Model);
$expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('0', '10')), true, true, $this->Model);
$expected = " WHERE `Article2`.`rate_sum` BETWEEN 0 AND 10";
$this->assertEqual($result, $expected);
$result = $this->testDb->conditions(array('Article2.rate_sum BETWEEN ? AND ?' => array('1', '10')), true, true, $this->Model);
$expected = " WHERE `Article2`.`rate_sum` BETWEEN 1 AND 10";
$this->assertEqual($result, $expected);
}
/**
* testFieldParsing method
*
* @access public
* @return void
*/
function testFieldParsing() {
$result = $this->testDb->fields($this->Model, 'Vendor', "Vendor.id, COUNT(Model.vendor_id) AS `Vendor`.`count`");
$expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, 'Vendor', "`Vendor`.`id`, COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`");
$expected = array('`Vendor`.`id`', 'COUNT(`Model`.`vendor_id`) AS `Vendor`.`count`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, 'Post', "CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name, Node.created");
$expected = array("CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", "`Node`.`created`");
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, 'round( (3.55441 * fooField), 3 ) AS test');
$this->assertEqual($result, array('round( (3.55441 * fooField), 3 ) AS test'));
$result = $this->testDb->fields($this->Model, null, 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating');
$this->assertEqual($result, array('ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating'));
$result = $this->testDb->fields($this->Model, null, 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating');
$this->assertEqual($result, array('ROUND(Rating.rate_total / Rating.rate_count,2) AS rating'));
$result = $this->testDb->fields($this->Model, 'Post', "Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name");
$expected = array("`Node`.`created`", "CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name");
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, 'Post', "2.2,COUNT(*), SUM(Something.else) as sum, Node.created, CONCAT(REPEAT(' ', COUNT(Parent.name) - 1), Node.name) AS name,Post.title,Post.1,1.1");
$expected = array(
'2.2', 'COUNT(*)', 'SUM(`Something`.`else`) as sum', '`Node`.`created`',
"CONCAT(REPEAT(' ', COUNT(`Parent`.`name`) - 1), Node.name) AS name", '`Post`.`title`', '`Post`.`1`', '1.1'
);
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, "(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
$expected = array("(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, 'Post');
$expected = array(
'`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`',
'`Post`.`passwd`', '`Post`.`addr_1`', '`Post`.`addr_2`', '`Post`.`zip_code`',
'`Post`.`city`', '`Post`.`country`', '`Post`.`phone`', '`Post`.`fax`',
'`Post`.`url`', '`Post`.`email`', '`Post`.`comments`', '`Post`.`last_login`',
'`Post`.`created`', '`Post`.`updated`'
);
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, 'Other');
$expected = array(
'`Other`.`id`', '`Other`.`client_id`', '`Other`.`name`', '`Other`.`login`',
'`Other`.`passwd`', '`Other`.`addr_1`', '`Other`.`addr_2`', '`Other`.`zip_code`',
'`Other`.`city`', '`Other`.`country`', '`Other`.`phone`', '`Other`.`fax`',
'`Other`.`url`', '`Other`.`email`', '`Other`.`comments`', '`Other`.`last_login`',
'`Other`.`created`', '`Other`.`updated`'
);
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array(), false);
$expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, 'COUNT(*)');
$expected = array('COUNT(*)');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, 'SUM(Thread.unread_buyer) AS ' . $this->testDb->name('sum_unread_buyer'));
$expected = array('SUM(`Thread`.`unread_buyer`) AS `sum_unread_buyer`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, 'name, count(*)');
$expected = array('`TestModel`.`name`', 'count(*)');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, 'count(*), name');
$expected = array('count(*)', '`TestModel`.`name`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields(
$this->Model, null, 'field1, field2, field3, count(*), name'
);
$expected = array(
'`TestModel`.`field1`', '`TestModel`.`field2`',
'`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`'
);
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array('dayofyear(now())'));
$expected = array('dayofyear(now())');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array('MAX(Model.field) As Max'));
$expected = array('MAX(`Model`.`field`) As Max');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array('Model.field AS AnotherName'));
$expected = array('`Model`.`field` AS `AnotherName`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array('field AS AnotherName'));
$expected = array('`field` AS `AnotherName`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array(
'TestModel.field AS AnotherName'
));
$expected = array('`TestModel`.`field` AS `AnotherName`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, 'Foo', array(
'id', 'title', '(user_count + discussion_count + post_count) AS score'
));
$expected = array(
'`Foo`.`id`',
'`Foo`.`title`',
'(user_count + discussion_count + post_count) AS score'
);
$this->assertEqual($result, $expected);
}
/**
* test that fields() will accept objects made from DboSource::expression
*
* @return void
*/
function testFieldsWithExpression() {
$expression = $this->testDb->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col");
$result = $this->testDb->fields($this->Model, null, array("id", $expression));
$expected = array(
'`TestModel`.`id`',
"CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"
);
$this->assertEqual($result, $expected);
}
/**
* test that order() will accept objects made from DboSource::expression