Skip to content

Commit 539b907

Browse files
committed
Moving more methods out of DboSourceTest
1 parent 66779ee commit 539b907

File tree

2 files changed

+175
-128
lines changed

2 files changed

+175
-128
lines changed

cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2845,4 +2845,178 @@ function testHasAny() {
28452845
$this->Dbo->hasAny($this->Model, array());
28462846

28472847
}
2848-
}
2848+
2849+
2850+
/**
2851+
* testStatements method
2852+
*
2853+
* @access public
2854+
* @return void
2855+
*/
2856+
function testStatements() {
2857+
$this->skipIf(true, 'Fix me');
2858+
$this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'Attachment', 'ArticlesTag');
2859+
$Article = new Article();
2860+
//$this->testDb = $this->getMock('DboMysql', array('connect', 'execute', '_execute'));
2861+
2862+
$result = $this->testDb->update($Article, array('field1'), array('value1'));
2863+
$this->assertFalse($result);
2864+
$result = $this->testDb->getLastQuery();
2865+
$this->assertPattern('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . '\s+SET\s+`field1`\s*=\s*\'value1\'\s+WHERE\s+1 = 1\s*$/', $result);
2866+
2867+
$result = $this->testDb->update($Article, array('field1'), array('2'), '2=2');
2868+
$this->assertFalse($result);
2869+
$result = $this->testDb->getLastQuery();
2870+
$this->assertPattern('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . ' AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+SET\s+`Article`\.`field1`\s*=\s*2\s+WHERE\s+2\s*=\s*2\s*$/', $result);
2871+
2872+
$result = $this->testDb->delete($Article);
2873+
$this->assertTrue($result);
2874+
$result = $this->testDb->getLastQuery();
2875+
$this->assertPattern('/^\s*DELETE\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+WHERE\s+1 = 1\s*$/', $result);
2876+
2877+
$result = $this->testDb->delete($Article, true);
2878+
$this->assertTrue($result);
2879+
$result = $this->testDb->getLastQuery();
2880+
$this->assertPattern('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+1\s*=\s*1\s*$/', $result);
2881+
2882+
$result = $this->testDb->delete($Article, '2=2');
2883+
$this->assertTrue($result);
2884+
$result = $this->testDb->getLastQuery();
2885+
$this->assertPattern('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+2\s*=\s*2\s*$/', $result);
2886+
2887+
$result = $this->testDb->hasAny($Article, '1=2');
2888+
$this->assertFalse($result);
2889+
2890+
$result = $this->testDb->insertMulti('articles', array('field'), array('(1)', '(2)'));
2891+
$this->assertNull($result);
2892+
$result = $this->testDb->getLastQuery();
2893+
$this->assertPattern('/^\s*INSERT INTO\s+' . $this->testDb->fullTableName('articles') . '\s+\(`field`\)\s+VALUES\s+\(1\),\s*\(2\)\s*$/', $result);
2894+
}
2895+
2896+
/**
2897+
* test fields generating usable virtual fields to use in query
2898+
*
2899+
* @return void
2900+
*/
2901+
function testVirtualFields() {
2902+
$this->loadFixtures('Article', 'Comment');
2903+
$this->Dbo->virtualFieldSeparator = '__';
2904+
$Article = ClassRegistry::init('Article');
2905+
$Article->virtualFields = array(
2906+
'this_moment' => 'NOW()',
2907+
'two' => '1 + 1',
2908+
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') .
2909+
' WHERE Article.id = ' . $this->Dbo->fullTableName('comments') . '.article_id'
2910+
);
2911+
$result = $this->Dbo->fields($Article);
2912+
$expected = array(
2913+
'`Article`.`id`',
2914+
'`Article`.`user_id`',
2915+
'`Article`.`title`',
2916+
'`Article`.`body`',
2917+
'`Article`.`published`',
2918+
'`Article`.`created`',
2919+
'`Article`.`updated`',
2920+
'(NOW()) AS `Article__this_moment`',
2921+
'(1 + 1) AS `Article__two`',
2922+
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
2923+
);
2924+
$this->assertEqual($expected, $result);
2925+
2926+
$result = $this->Dbo->fields($Article, null, array('this_moment', 'title'));
2927+
$expected = array(
2928+
'`Article`.`title`',
2929+
'(NOW()) AS `Article__this_moment`',
2930+
);
2931+
$this->assertEqual($expected, $result);
2932+
2933+
$result = $this->Dbo->fields($Article, null, array('Article.title', 'Article.this_moment'));
2934+
$expected = array(
2935+
'`Article`.`title`',
2936+
'(NOW()) AS `Article__this_moment`',
2937+
);
2938+
$this->assertEqual($expected, $result);
2939+
2940+
$result = $this->Dbo->fields($Article, null, array('Article.this_moment', 'Article.title'));
2941+
$expected = array(
2942+
'`Article`.`title`',
2943+
'(NOW()) AS `Article__this_moment`',
2944+
);
2945+
$this->assertEqual($expected, $result);
2946+
2947+
$result = $this->Dbo->fields($Article, null, array('Article.*'));
2948+
$expected = array(
2949+
'`Article`.*',
2950+
'(NOW()) AS `Article__this_moment`',
2951+
'(1 + 1) AS `Article__two`',
2952+
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
2953+
);
2954+
$this->assertEqual($expected, $result);
2955+
2956+
$result = $this->Dbo->fields($Article, null, array('*'));
2957+
$expected = array(
2958+
'*',
2959+
'(NOW()) AS `Article__this_moment`',
2960+
'(1 + 1) AS `Article__two`',
2961+
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
2962+
);
2963+
$this->assertEqual($expected, $result);
2964+
}
2965+
2966+
/**
2967+
* test conditions to generate query conditions for virtual fields
2968+
*
2969+
* @return void
2970+
*/
2971+
function testVirtualFieldsInConditions() {
2972+
$Article = ClassRegistry::init('Article');
2973+
$Article->virtualFields = array(
2974+
'this_moment' => 'NOW()',
2975+
'two' => '1 + 1',
2976+
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') .
2977+
' WHERE Article.id = ' . $this->Dbo->fullTableName('comments') . '.article_id'
2978+
);
2979+
$conditions = array('two' => 2);
2980+
$result = $this->Dbo->conditions($conditions, true, false, $Article);
2981+
$expected = '(1 + 1) = 2';
2982+
$this->assertEqual($expected, $result);
2983+
2984+
$conditions = array('this_moment BETWEEN ? AND ?' => array(1,2));
2985+
$expected = 'NOW() BETWEEN 1 AND 2';
2986+
$result = $this->Dbo->conditions($conditions, true, false, $Article);
2987+
$this->assertEqual($expected, $result);
2988+
2989+
$conditions = array('comment_count >' => 5);
2990+
$expected = '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) > 5';
2991+
$result = $this->Dbo->conditions($conditions, true, false, $Article);
2992+
$this->assertEqual($expected, $result);
2993+
2994+
$conditions = array('NOT' => array('two' => 2));
2995+
$result = $this->Dbo->conditions($conditions, true, false, $Article);
2996+
$expected = 'NOT ((1 + 1) = 2)';
2997+
$this->assertEqual($expected, $result);
2998+
}
2999+
3000+
/**
3001+
* test that virtualFields with complex functions and aliases work.
3002+
*
3003+
* @return void
3004+
*/
3005+
function testConditionsWithComplexVirtualFields() {
3006+
$Article = ClassRegistry::init('Article');
3007+
$Article->virtualFields = array(
3008+
'distance' => 'ACOS(SIN(20 * PI() / 180)
3009+
* SIN(Article.latitude * PI() / 180)
3010+
+ COS(20 * PI() / 180)
3011+
* COS(Article.latitude * PI() / 180)
3012+
* COS((50 - Article.longitude) * PI() / 180)
3013+
) * 180 / PI() * 60 * 1.1515 * 1.609344'
3014+
);
3015+
$conditions = array('distance >=' => 20);
3016+
$result = $this->Dbo->conditions($conditions, true, true, $Article);
3017+
3018+
$this->assertPattern('/\) >= 20/', $result);
3019+
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result);
3020+
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result);
3021+
}
3022+
}

cake/tests/cases/libs/model/datasources/dbo_source.test.php

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -977,133 +977,6 @@ function testShowQuery() {
977977
$this->assertPattern('/Took:/s', $contents);
978978
}
979979

980-
/**
981-
* test fields generating usable virtual fields to use in query
982-
*
983-
* @return void
984-
*/
985-
function testVirtualFields() {
986-
$this->loadFixtures('Article');
987-
988-
$Article = ClassRegistry::init('Article');
989-
$Article->virtualFields = array(
990-
'this_moment' => 'NOW()',
991-
'two' => '1 + 1',
992-
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') .
993-
' WHERE Article.id = ' . $this->db->fullTableName('comments') . '.article_id'
994-
);
995-
$result = $this->db->fields($Article);
996-
$expected = array(
997-
'`Article`.`id`',
998-
'`Article`.`user_id`',
999-
'`Article`.`title`',
1000-
'`Article`.`body`',
1001-
'`Article`.`published`',
1002-
'`Article`.`created`',
1003-
'`Article`.`updated`',
1004-
'(NOW()) AS `Article__this_moment`',
1005-
'(1 + 1) AS `Article__two`',
1006-
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
1007-
);
1008-
$this->assertEqual($expected, $result);
1009-
1010-
$result = $this->db->fields($Article, null, array('this_moment', 'title'));
1011-
$expected = array(
1012-
'`Article`.`title`',
1013-
'(NOW()) AS `Article__this_moment`',
1014-
);
1015-
$this->assertEqual($expected, $result);
1016-
1017-
$result = $this->db->fields($Article, null, array('Article.title', 'Article.this_moment'));
1018-
$expected = array(
1019-
'`Article`.`title`',
1020-
'(NOW()) AS `Article__this_moment`',
1021-
);
1022-
$this->assertEqual($expected, $result);
1023-
1024-
$result = $this->db->fields($Article, null, array('Article.this_moment', 'Article.title'));
1025-
$expected = array(
1026-
'`Article`.`title`',
1027-
'(NOW()) AS `Article__this_moment`',
1028-
);
1029-
$this->assertEqual($expected, $result);
1030-
1031-
$result = $this->db->fields($Article, null, array('Article.*'));
1032-
$expected = array(
1033-
'`Article`.*',
1034-
'(NOW()) AS `Article__this_moment`',
1035-
'(1 + 1) AS `Article__two`',
1036-
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
1037-
);
1038-
$this->assertEqual($expected, $result);
1039-
1040-
$result = $this->db->fields($Article, null, array('*'));
1041-
$expected = array(
1042-
'*',
1043-
'(NOW()) AS `Article__this_moment`',
1044-
'(1 + 1) AS `Article__two`',
1045-
'(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `Article__comment_count`'
1046-
);
1047-
$this->assertEqual($expected, $result);
1048-
}
1049-
1050-
/**
1051-
* test conditions to generate query conditions for virtual fields
1052-
*
1053-
* @return void
1054-
*/
1055-
function testVirtualFieldsInConditions() {
1056-
$Article = ClassRegistry::init('Article');
1057-
$Article->virtualFields = array(
1058-
'this_moment' => 'NOW()',
1059-
'two' => '1 + 1',
1060-
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') .
1061-
' WHERE Article.id = ' . $this->db->fullTableName('comments') . '.article_id'
1062-
);
1063-
$conditions = array('two' => 2);
1064-
$result = $this->db->conditions($conditions, true, false, $Article);
1065-
$expected = '(1 + 1) = 2';
1066-
$this->assertEqual($expected, $result);
1067-
1068-
$conditions = array('this_moment BETWEEN ? AND ?' => array(1,2));
1069-
$expected = 'NOW() BETWEEN 1 AND 2';
1070-
$result = $this->db->conditions($conditions, true, false, $Article);
1071-
$this->assertEqual($expected, $result);
1072-
1073-
$conditions = array('comment_count >' => 5);
1074-
$expected = '(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) > 5';
1075-
$result = $this->db->conditions($conditions, true, false, $Article);
1076-
$this->assertEqual($expected, $result);
1077-
1078-
$conditions = array('NOT' => array('two' => 2));
1079-
$result = $this->db->conditions($conditions, true, false, $Article);
1080-
$expected = 'NOT ((1 + 1) = 2)';
1081-
$this->assertEqual($expected, $result);
1082-
}
1083-
1084-
/**
1085-
* test that virtualFields with complex functions and aliases work.
1086-
*
1087-
* @return void
1088-
*/
1089-
function testConditionsWithComplexVirtualFields() {
1090-
$Article = ClassRegistry::init('Article');
1091-
$Article->virtualFields = array(
1092-
'distance' => 'ACOS(SIN(20 * PI() / 180)
1093-
* SIN(Article.latitude * PI() / 180)
1094-
+ COS(20 * PI() / 180)
1095-
* COS(Article.latitude * PI() / 180)
1096-
* COS((50 - Article.longitude) * PI() / 180)
1097-
) * 180 / PI() * 60 * 1.1515 * 1.609344'
1098-
);
1099-
$conditions = array('distance >=' => 20);
1100-
$result = $this->db->conditions($conditions, true, true, $Article);
1101-
1102-
$this->assertPattern('/\) >= 20/', $result);
1103-
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result);
1104-
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result);
1105-
}
1106-
1107980
/**
1108981
* test order to generate query order clause for virtual fields
1109982
*

0 commit comments

Comments
 (0)