2008-05-30 11:40:08 +00:00
< ? php
/**
2009-03-18 17:55:58 +00:00
* DboSourceTest file
2008-05-30 11:40:08 +00:00
*
2010-10-03 16:31:21 +00:00
* PHP 5
2008-05-30 11:40:08 +00:00
*
2010-05-19 01:15:13 +00:00
* CakePHP ( tm ) Tests < http :// book . cakephp . org / view / 1196 / Testing >
2010-01-26 19:18:20 +00:00
* Copyright 2005 - 2010 , Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
2008-06-27 05:14:52 +00:00
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice .
2008-05-30 11:40:08 +00:00
*
2010-01-26 19:18:20 +00:00
* @ copyright Copyright 2005 - 2010 , Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2010-05-19 01:15:13 +00:00
* @ link http :// book . cakephp . org / view / 1196 / Testing CakePHP ( tm ) Tests
2009-03-18 17:55:58 +00:00
* @ package cake
2008-10-30 17:30:26 +00:00
* @ subpackage cake . tests . cases . libs . model . datasources
* @ since CakePHP ( tm ) v 1.2 . 0.4206
2010-10-03 16:27:27 +00:00
* @ license MIT License ( http :// www . opensource . org / licenses / mit - license . php )
2008-05-30 11:40:08 +00:00
*/
if ( ! defined ( 'CAKEPHP_UNIT_TEST_EXECUTION' )) {
define ( 'CAKEPHP_UNIT_TEST_EXECUTION' , 1 );
}
2009-07-29 08:51:08 +00:00
App :: import ( 'Model' , array ( 'Model' , 'DataSource' , 'DboSource' , 'DboMysql' , 'App' ));
2008-05-30 11:40:08 +00:00
require_once dirname ( dirname ( __FILE__ )) . DS . 'models.php' ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-03-18 17:55:58 +00:00
* DboSourceTest class
2008-05-30 11:40:08 +00:00
*
2009-03-18 17:55:58 +00:00
* @ package cake
2008-10-30 17:30:26 +00:00
* @ subpackage cake . tests . cases . libs . model . datasources
2008-05-30 11:40:08 +00:00
*/
class DboSourceTest extends CakeTestCase {
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* debug property
2008-06-05 14:48:54 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var mixed null
* @ access public
*/
2010-04-04 07:14:00 +00:00
public $debug = null ;
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* autoFixtures property
2008-06-05 14:48:54 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var bool false
* @ access public
*/
2010-04-04 07:14:00 +00:00
public $autoFixtures = false ;
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* fixtures property
2008-06-05 14:48:54 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var array
* @ access public
*/
2010-04-04 07:14:00 +00:00
public $fixtures = array (
2008-05-30 11:40:08 +00:00
'core.apple' , 'core.article' , 'core.articles_tag' , 'core.attachment' , 'core.comment' ,
2010-09-07 04:32:44 +00:00
'core.sample' , 'core.tag' , 'core.user' , 'core.post' , 'core.author' , 'core.data_test'
2008-05-30 11:40:08 +00:00
);
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
2010-09-26 01:36:49 +00:00
* setUp method
2008-06-05 14:48:54 +00:00
*
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2010-09-26 01:36:49 +00:00
function setUp () {
parent :: setUp ();
2008-05-30 11:40:08 +00:00
$this -> __config = $this -> db -> config ;
if ( ! class_exists ( 'DboTest' )) {
2010-09-20 02:58:30 +00:00
$db = ConnectionManager :: getDataSource ( 'test' );
2008-05-30 11:40:08 +00:00
$class = get_class ( $db );
eval ( " class DboTest extends $class {
var \ $simulated = array ();
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* execute method
2008-06-05 14:48:54 +00:00
*
* @ param \ $sql
2008-06-02 19:22:55 +00:00
* @ access protected
* @ return void
*/
2008-05-30 11:40:08 +00:00
function _execute ( \ $sql ) {
\ $this -> simulated [] = \ $sql ;
return null ;
}
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* getLastQuery method
2008-06-05 14:48:54 +00:00
*
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function getLastQuery () {
return \ $this -> simulated [ count ( \ $this -> simulated ) - 1 ];
}
} " );
}
2010-06-10 01:17:25 +00:00
$this -> testDb = new DboTest ( $this -> __config );
2008-08-07 15:36:26 +00:00
$this -> testDb -> cacheSources = false ;
2009-08-02 06:47:28 +00:00
$this -> testDb -> startQuote = '`' ;
$this -> testDb -> endQuote = '`' ;
2010-09-26 01:36:49 +00:00
2010-06-10 01:17:25 +00:00
$this -> Model = new TestModel ();
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
* endTest method
2008-06-05 14:48:54 +00:00
*
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2010-09-26 01:36:49 +00:00
function tearDown () {
parent :: tearDown ();
2008-05-30 11:40:08 +00:00
unset ( $this -> Model );
}
2009-07-24 19:18:37 +00:00
2010-01-22 15:30:22 +00:00
/**
* test that booleans and null make logical condition strings .
*
* @ return void
*/
function testBooleanNullConditionsParsing () {
$result = $this -> testDb -> conditions ( true );
$this -> assertEqual ( $result , ' WHERE 1 = 1' , 'true conditions failed %s' );
$result = $this -> testDb -> conditions ( false );
$this -> assertEqual ( $result , ' WHERE 0 = 1' , 'false conditions failed %s' );
$result = $this -> testDb -> conditions ( null );
$this -> assertEqual ( $result , ' WHERE 1 = 1' , 'null conditions failed %s' );
$result = $this -> testDb -> conditions ( array ());
$this -> assertEqual ( $result , ' WHERE 1 = 1' , 'array() conditions failed %s' );
$result = $this -> testDb -> conditions ( '' );
$this -> assertEqual ( $result , ' WHERE 1 = 1' , '"" conditions failed %s' );
$result = $this -> testDb -> conditions ( ' ' , '" " conditions failed %s' );
$this -> assertEqual ( $result , ' WHERE 1 = 1' );
}
2010-05-19 08:54:51 +00:00
/**
* test that order () will accept objects made from DboSource :: expression
*
* @ return void
*/
function testOrderWithExpression () {
$expression = $this -> testDb -> expression ( " CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col " );
$result = $this -> testDb -> order ( $expression );
$expected = " ORDER BY CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col " ;
$this -> assertEqual ( $result , $expected );
}
2008-06-05 15:20:45 +00:00
/**
* testMergeAssociations method
2008-06-14 19:45:26 +00:00
*
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-06-20 20:17:23 +00:00
function testMergeAssociations () {
2009-03-09 18:36:32 +00:00
$data = array ( 'Article2' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' ,
'body' => 'First Article Body' , 'published' => 'Y' ,
'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
));
$merge = array ( 'Topic' => array ( array (
'id' => '1' , 'topic' => 'Topic' , 'created' => '2007-03-17 01:16:23' ,
'updated' => '2007-03-17 01:18:31'
)));
2008-06-20 20:17:23 +00:00
$expected = array (
'Article2' => array (
2009-03-09 18:36:32 +00:00
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' ,
'body' => 'First Article Body' , 'published' => 'Y' ,
'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
2008-06-20 20:17:23 +00:00
),
'Topic' => array (
2009-03-09 18:36:32 +00:00
'id' => '1' , 'topic' => 'Topic' , 'created' => '2007-03-17 01:16:23' ,
'updated' => '2007-03-17 01:18:31'
2008-06-20 20:17:23 +00:00
)
);
$this -> testDb -> __mergeAssociation ( $data , $merge , 'Topic' , 'hasOne' );
$this -> assertEqual ( $data , $expected );
2009-03-09 18:36:32 +00:00
$data = array ( 'Article2' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' ,
'body' => 'First Article Body' , 'published' => 'Y' ,
'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
));
$merge = array ( 'User2' => array ( array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' ,
'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)));
2008-06-20 20:17:23 +00:00
$expected = array (
'Article2' => array (
2009-03-09 18:36:32 +00:00
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' ,
'body' => 'First Article Body' , 'published' => 'Y' ,
'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
2008-06-20 20:17:23 +00:00
),
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
);
$this -> testDb -> __mergeAssociation ( $data , $merge , 'User2' , 'belongsTo' );
$this -> assertEqual ( $data , $expected );
$data = array (
'Article2' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
)
);
$merge = array ( array ( 'Comment' => false ));
$expected = array (
'Article2' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
),
'Comment' => array ()
);
$this -> testDb -> __mergeAssociation ( $data , $merge , 'Comment' , 'hasMany' );
$this -> assertEqual ( $data , $expected );
$data = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
)
);
$merge = array (
array (
'Comment' => array (
'id' => '1' , 'comment' => 'Comment 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
),
array (
'Comment' => array (
'id' => '2' , 'comment' => 'Comment 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
)
);
$expected = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
),
'Comment' => array (
array (
'id' => '1' , 'comment' => 'Comment 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
array (
'id' => '2' , 'comment' => 'Comment 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
)
);
$this -> testDb -> __mergeAssociation ( $data , $merge , 'Comment' , 'hasMany' );
$this -> assertEqual ( $data , $expected );
$data = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
)
);
$merge = array (
array (
'Comment' => array (
'id' => '1' , 'comment' => 'Comment 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
),
array (
'Comment' => array (
'id' => '2' , 'comment' => 'Comment 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
)
);
$expected = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
),
'Comment' => array (
array (
'id' => '1' , 'comment' => 'Comment 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31' ,
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
),
array (
'id' => '2' , 'comment' => 'Comment 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31' ,
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
)
)
);
$this -> testDb -> __mergeAssociation ( $data , $merge , 'Comment' , 'hasMany' );
$this -> assertEqual ( $data , $expected );
$data = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
)
);
$merge = array (
array (
'Comment' => array (
'id' => '1' , 'comment' => 'Comment 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
'Tag' => array (
array ( 'id' => 1 , 'tag' => 'Tag 1' ),
array ( 'id' => 2 , 'tag' => 'Tag 2' )
)
),
array (
'Comment' => array (
'id' => '2' , 'comment' => 'Comment 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
'Tag' => array ()
)
);
$expected = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
),
'Comment' => array (
array (
'id' => '1' , 'comment' => 'Comment 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31' ,
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
'Tag' => array (
array ( 'id' => 1 , 'tag' => 'Tag 1' ),
array ( 'id' => 2 , 'tag' => 'Tag 2' )
)
),
array (
'id' => '2' , 'comment' => 'Comment 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31' ,
'User2' => array (
'id' => '1' , 'user' => 'mariano' , 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
'Tag' => array ()
)
)
);
$this -> testDb -> __mergeAssociation ( $data , $merge , 'Comment' , 'hasMany' );
$this -> assertEqual ( $data , $expected );
$data = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
)
);
$merge = array (
array (
'Tag' => array (
'id' => '1' , 'tag' => 'Tag 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
),
array (
'Tag' => array (
'id' => '2' , 'tag' => 'Tag 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
),
array (
'Tag' => array (
'id' => '3' , 'tag' => 'Tag 3' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
)
);
$expected = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
),
'Tag' => array (
array (
'id' => '1' , 'tag' => 'Tag 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
array (
'id' => '2' , 'tag' => 'Tag 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
),
array (
'id' => '3' , 'tag' => 'Tag 3' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
)
);
$this -> testDb -> __mergeAssociation ( $data , $merge , 'Tag' , 'hasAndBelongsToMany' );
$this -> assertEqual ( $data , $expected );
$data = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
)
);
$merge = array (
array (
'Tag' => array (
'id' => '1' , 'tag' => 'Tag 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
),
array (
'Tag' => array (
'id' => '2' , 'tag' => 'Tag 2' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
),
array (
'Tag' => array (
'id' => '3' , 'tag' => 'Tag 3' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31'
)
)
);
$expected = array (
'Article' => array (
'id' => '1' , 'user_id' => '1' , 'title' => 'First Article' , 'body' => 'First Article Body' , 'published' => 'Y' , 'created' => '2007-03-18 10:39:23' , 'updated' => '2007-03-18 10:41:31'
),
'Tag' => array ( 'id' => '1' , 'tag' => 'Tag 1' , 'created' => '2007-03-17 01:16:23' , 'updated' => '2007-03-17 01:18:31' )
);
$this -> testDb -> __mergeAssociation ( $data , $merge , 'Tag' , 'hasOne' );
$this -> assertEqual ( $data , $expected );
}
2009-07-24 19:18:37 +00:00
2008-06-05 15:20:45 +00:00
/**
* testMagicMethodQuerying method
2008-06-14 19:45:26 +00:00
*
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testMagicMethodQuerying () {
$result = $this -> testDb -> query ( 'findByFieldName' , array ( 'value' ), $this -> Model );
2008-08-07 16:38:15 +00:00
$expected = array ( 'first' , array (
'conditions' => array ( 'TestModel.field_name' => 'value' ),
'fields' => null , 'order' => null , 'recursive' => null
));
2008-05-30 11:40:08 +00:00
$this -> assertEqual ( $result , $expected );
2009-03-18 17:55:58 +00:00
2009-02-25 03:22:31 +00:00
$result = $this -> testDb -> query ( 'findByFindBy' , array ( 'value' ), $this -> Model );
$expected = array ( 'first' , array (
'conditions' => array ( 'TestModel.find_by' => 'value' ),
'fields' => null , 'order' => null , 'recursive' => null
));
$this -> assertEqual ( $result , $expected );
2008-05-30 11:40:08 +00:00
$result = $this -> testDb -> query ( 'findAllByFieldName' , array ( 'value' ), $this -> Model );
2008-08-07 16:38:15 +00:00
$expected = array ( 'all' , array (
'conditions' => array ( 'TestModel.field_name' => 'value' ),
'fields' => null , 'order' => null , 'limit' => null ,
'page' => null , 'recursive' => null
));
2008-05-30 11:40:08 +00:00
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> query ( 'findAllById' , array ( 'a' ), $this -> Model );
2008-08-07 16:38:15 +00:00
$expected = array ( 'all' , array (
'conditions' => array ( 'TestModel.id' => 'a' ),
'fields' => null , 'order' => null , 'limit' => null ,
'page' => null , 'recursive' => null
));
2008-05-30 11:40:08 +00:00
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> query ( 'findByFieldName' , array ( array ( 'value1' , 'value2' , 'value3' )), $this -> Model );
2008-08-07 16:38:15 +00:00
$expected = array ( 'first' , array (
'conditions' => array ( 'TestModel.field_name' => array ( 'value1' , 'value2' , 'value3' )),
'fields' => null , 'order' => null , 'recursive' => null
));
2008-05-30 11:40:08 +00:00
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> query ( 'findByFieldName' , array ( null ), $this -> Model );
2008-08-07 16:38:15 +00:00
$expected = array ( 'first' , array (
'conditions' => array ( 'TestModel.field_name' => null ),
'fields' => null , 'order' => null , 'recursive' => null
));
2008-05-30 11:40:08 +00:00
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> query ( 'findByFieldName' , array ( '= a' ), $this -> Model );
2008-08-07 16:38:15 +00:00
$expected = array ( 'first' , array (
'conditions' => array ( 'TestModel.field_name' => '= a' ),
'fields' => null , 'order' => null , 'recursive' => null
));
2008-05-30 11:40:08 +00:00
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> query ( 'findByFieldName' , array (), $this -> Model );
$expected = false ;
$this -> assertEqual ( $result , $expected );
$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 );
}
2009-07-24 19:18:37 +00:00
2008-06-05 15:20:45 +00:00
/**
* testIntrospectType method
2008-06-14 19:45:26 +00:00
*
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testIntrospectType () {
$this -> assertEqual ( $this -> testDb -> introspectType ( 0 ), 'integer' );
$this -> assertEqual ( $this -> testDb -> introspectType ( 2 ), 'integer' );
$this -> assertEqual ( $this -> testDb -> introspectType ( '2' ), 'string' );
$this -> assertEqual ( $this -> testDb -> introspectType ( '2.2' ), 'string' );
$this -> assertEqual ( $this -> testDb -> introspectType ( 2.2 ), 'float' );
$this -> assertEqual ( $this -> testDb -> introspectType ( 'stringme' ), 'string' );
$this -> assertEqual ( $this -> testDb -> introspectType ( '0stringme' ), 'string' );
$data = array ( 2.2 );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'float' );
$data = array ( '2.2' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'float' );
$data = array ( 2 );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'integer' );
$data = array ( '2' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'integer' );
$data = array ( 'string' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'string' );
$data = array ( 2.2 , '2.2' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'float' );
$data = array ( 2 , '2' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'integer' );
$data = array ( 'string one' , 'string two' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'string' );
$data = array ( '2.2' , 3 );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'integer' );
$data = array ( '2.2' , '0stringme' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'string' );
$data = array ( 2.2 , 3 );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'integer' );
$data = array ( 2.2 , '0stringme' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'string' );
$data = array ( 2 , 'stringme' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'string' );
$data = array ( 2 , '2.2' , 'stringgme' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'string' );
$data = array ( 2 , '2.2' );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'integer' );
$data = array ( 2 , 2.2 );
$this -> assertEqual ( $this -> testDb -> introspectType ( $data ), 'integer' );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
// NULL
$result = $this -> testDb -> value ( null , 'boolean' );
$this -> assertEqual ( $result , 'NULL' );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
// EMPTY STRING
$result = $this -> testDb -> value ( '' , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '0' " );
2008-07-27 02:49:02 +00:00
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
// BOOLEAN
$result = $this -> testDb -> value ( 'true' , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '1' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 'false' , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '1' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( true , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '1' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( false , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '0' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 1 , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '1' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 0 , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '0' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 'abc' , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '1' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 1.234 , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '1' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '1.234e05' , 'boolean' );
2010-11-09 05:55:05 +00:00
$this -> assertEqual ( $result , " '1' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
// NUMBERS
$result = $this -> testDb -> value ( 123 , 'integer' );
$this -> assertEqual ( $result , 123 );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '123' , 'integer' );
$this -> assertEqual ( $result , '123' );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '0123' , 'integer' );
$this -> assertEqual ( $result , " '0123' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '0x123ABC' , 'integer' );
$this -> assertEqual ( $result , " '0x123ABC' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '0x123' , 'integer' );
$this -> assertEqual ( $result , " '0x123' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 1.234 , 'float' );
$this -> assertEqual ( $result , 1.234 );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '1.234' , 'float' );
$this -> assertEqual ( $result , '1.234' );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( ' 1.234 ' , 'float' );
$this -> assertEqual ( $result , " ' 1.234 ' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '1.234e05' , 'float' );
$this -> assertEqual ( $result , " '1.234e05' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '1.234e+5' , 'float' );
$this -> assertEqual ( $result , " '1.234e+5' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( '1,234' , 'float' );
$this -> assertEqual ( $result , " '1,234' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 'FFF' , 'integer' );
$this -> assertEqual ( $result , " 'FFF' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 'abc' , 'integer' );
$this -> assertEqual ( $result , " 'abc' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
// STRINGS
$result = $this -> testDb -> value ( '123' , 'string' );
$this -> assertEqual ( $result , " '123' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 123 , 'string' );
$this -> assertEqual ( $result , " '123' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 1.234 , 'string' );
$this -> assertEqual ( $result , " '1.234' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 'abc' , 'string' );
$this -> assertEqual ( $result , " 'abc' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( ' abc ' , 'string' );
$this -> assertEqual ( $result , " ' abc ' " );
2008-06-05 14:48:54 +00:00
2008-06-03 04:43:44 +00:00
$result = $this -> testDb -> value ( 'a bc' , 'string' );
$this -> assertEqual ( $result , " 'a bc' " );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-05 15:20:45 +00:00
/**
* testValue method
2008-06-14 19:45:26 +00:00
*
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testValue () {
$result = $this -> testDb -> value ( '{$__cakeForeignKey__$}' );
$this -> assertEqual ( $result , '{$__cakeForeignKey__$}' );
$result = $this -> testDb -> value ( array ( 'first' , 2 , 'third' ));
$expected = array ( '\'first\'' , 2 , '\'third\'' );
2008-08-02 14:26:50 +00:00
$this -> assertEqual ( $result , $expected );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-05 15:20:45 +00:00
/**
* testReconnect method
2008-06-14 19:45:26 +00:00
*
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testReconnect () {
2008-08-07 16:38:15 +00:00
$this -> testDb -> reconnect ( array ( 'prefix' => 'foo' ));
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( $this -> testDb -> connected );
2008-08-07 16:38:15 +00:00
$this -> assertEqual ( $this -> testDb -> config [ 'prefix' ], 'foo' );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-06-05 15:20:45 +00:00
/**
* testRealQueries method
2008-06-14 19:45:26 +00:00
*
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testRealQueries () {
2010-06-10 01:17:25 +00:00
$this -> loadFixtures ( 'Apple' , 'Article' , 'User' , 'Comment' , 'Tag' , 'Sample' );
2008-05-30 11:40:08 +00:00
2010-06-10 01:17:25 +00:00
$Apple = ClassRegistry :: init ( 'Apple' );
$Article = ClassRegistry :: init ( 'Article' );
2008-05-30 11:40:08 +00:00
2008-08-07 18:53:05 +00:00
$result = $this -> db -> rawQuery ( 'SELECT color, name FROM ' . $this -> db -> fullTableName ( 'apples' ));
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( ! empty ( $result ));
2008-08-07 18:53:05 +00:00
$result = $this -> db -> fetchRow ( $result );
$expected = array ( $this -> db -> fullTableName ( 'apples' , false ) => array (
2008-05-30 11:40:08 +00:00
'color' => 'Red 1' ,
'name' => 'Red Apple 1'
));
$this -> assertEqual ( $result , $expected );
2008-08-07 18:53:05 +00:00
$result = $this -> db -> fetchAll ( 'SELECT name FROM ' . $this -> testDb -> fullTableName ( 'apples' ) . ' ORDER BY id' );
2008-05-30 11:40:08 +00:00
$expected = array (
2008-08-07 18:53:05 +00:00
array ( $this -> db -> fullTableName ( 'apples' , false ) => array ( 'name' => 'Red Apple 1' )),
array ( $this -> db -> fullTableName ( 'apples' , false ) => array ( 'name' => 'Bright Red Apple' )),
array ( $this -> db -> fullTableName ( 'apples' , false ) => array ( 'name' => 'green blue' )),
array ( $this -> db -> fullTableName ( 'apples' , false ) => array ( 'name' => 'Test Name' )),
array ( $this -> db -> fullTableName ( 'apples' , false ) => array ( 'name' => 'Blue Green' )),
array ( $this -> db -> fullTableName ( 'apples' , false ) => array ( 'name' => 'My new apple' )),
array ( $this -> db -> fullTableName ( 'apples' , false ) => array ( 'name' => 'Some odd color' ))
2008-05-30 11:40:08 +00:00
);
$this -> assertEqual ( $result , $expected );
2008-08-07 18:53:05 +00:00
$result = $this -> db -> field ( $this -> testDb -> fullTableName ( 'apples' , false ), 'SELECT color, name FROM ' . $this -> testDb -> fullTableName ( 'apples' ) . ' ORDER BY id' );
2008-05-30 11:40:08 +00:00
$expected = array (
'color' => 'Red 1' ,
'name' => 'Red Apple 1'
);
$this -> assertEqual ( $result , $expected );
$Apple -> unbindModel ( array (), false );
2008-08-07 18:53:05 +00:00
$result = $this -> db -> read ( $Apple , array (
2008-05-30 11:40:08 +00:00
'fields' => array ( $Apple -> escapeField ( 'name' )),
'conditions' => null ,
'recursive' => - 1
));
$expected = array (
array ( 'Apple' => array ( 'name' => 'Red Apple 1' )),
array ( 'Apple' => array ( 'name' => 'Bright Red Apple' )),
array ( 'Apple' => array ( 'name' => 'green blue' )),
array ( 'Apple' => array ( 'name' => 'Test Name' )),
array ( 'Apple' => array ( 'name' => 'Blue Green' )),
array ( 'Apple' => array ( 'name' => 'My new apple' )),
array ( 'Apple' => array ( 'name' => 'Some odd color' ))
);
$this -> assertEqual ( $result , $expected );
2008-08-07 18:53:05 +00:00
$result = $this -> db -> read ( $Article , array (
2008-05-30 11:40:08 +00:00
'fields' => array ( 'id' , 'user_id' , 'title' ),
'conditions' => null ,
'recursive' => 1
));
2008-08-07 18:53:05 +00:00
2008-05-30 11:40:08 +00:00
$this -> assertTrue ( Set :: matches ( '/Article[id=1]' , $result ));
$this -> assertTrue ( Set :: matches ( '/Comment[id=1]' , $result ));
$this -> assertTrue ( Set :: matches ( '/Comment[id=2]' , $result ));
$this -> assertFalse ( Set :: matches ( '/Comment[id=10]' , $result ));
}
2009-07-24 19:18:37 +00:00
2008-06-05 15:20:45 +00:00
/**
* testName method
2008-06-14 19:45:26 +00:00
*
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testName () {
$result = $this -> testDb -> name ( 'name' );
$expected = '`name`' ;
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> name ( array ( 'name' , 'Model.*' ));
$expected = array ( '`name`' , '`Model`.*' );
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> name ( 'MTD()' );
2008-08-22 04:06:29 +00:00
$expected = 'MTD()' ;
2008-05-30 11:40:08 +00:00
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> name ( '(sm)' );
2008-08-22 04:06:29 +00:00
$expected = '(sm)' ;
$this -> assertEqual ( $result , $expected );
2010-01-17 04:14:19 +00:00
$result = $this -> testDb -> name ( 'name AS x' );
$expected = '`name` AS `x`' ;
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> name ( 'Model.name AS x' );
$expected = '`Model`.`name` AS `x`' ;
$this -> assertEqual ( $result , $expected );
2008-08-22 04:06:29 +00:00
$result = $this -> testDb -> name ( 'Function(Something.foo)' );
$expected = 'Function(`Something`.`foo`)' ;
2008-05-30 11:40:08 +00:00
$this -> assertEqual ( $result , $expected );
2010-01-17 04:14:19 +00:00
$result = $this -> testDb -> name ( 'Function(SubFunction(Something.foo))' );
$expected = 'Function(SubFunction(`Something`.`foo`))' ;
$this -> assertEqual ( $result , $expected );
$result = $this -> testDb -> name ( 'Function(Something.foo) AS x' );
$expected = 'Function(`Something`.`foo`) AS `x`' ;
$this -> assertEqual ( $result , $expected );
2010-02-19 15:41:52 +00:00
$result = $this -> testDb -> name ( 'name-with-minus' );
$expected = '`name-with-minus`' ;
$this -> assertEqual ( $result , $expected );
2010-02-20 16:42:17 +00:00
$result = $this -> testDb -> name ( array ( 'my-name' , 'Foo-Model.*' ));
$expected = array ( '`my-name`' , '`Foo-Model`.*' );
$this -> assertEqual ( $result , $expected );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2010-03-14 03:55:18 +00:00
/**
* test that cacheMethod works as exepected
*
* @ return void
*/
function testCacheMethod () {
$this -> testDb -> cacheMethods = true ;
$result = $this -> testDb -> cacheMethod ( 'name' , 'some-key' , 'stuff' );
$this -> assertEqual ( $result , 'stuff' );
$result = $this -> testDb -> cacheMethod ( 'name' , 'some-key' );
$this -> assertEqual ( $result , 'stuff' );
$result = $this -> testDb -> cacheMethod ( 'conditions' , 'some-key' );
$this -> assertNull ( $result );
$result = $this -> testDb -> cacheMethod ( 'name' , 'other-key' );
$this -> assertNull ( $result );
$this -> testDb -> cacheMethods = false ;
$result = $this -> testDb -> cacheMethod ( 'name' , 'some-key' , 'stuff' );
$this -> assertEqual ( $result , 'stuff' );
$result = $this -> testDb -> cacheMethod ( 'name' , 'some-key' );
$this -> assertNull ( $result );
}
2008-06-05 15:20:45 +00:00
/**
* testLog method
2008-06-14 19:45:26 +00:00
*
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testLog () {
$this -> testDb -> logQuery ( 'Query 1' );
$this -> testDb -> logQuery ( 'Query 2' );
2009-12-20 00:54:49 +00:00
$log = $this -> testDb -> getLog ( false , false );
$result = Set :: extract ( $log [ 'log' ], '/query' );
2008-05-30 11:40:08 +00:00
$expected = array ( 'Query 1' , 'Query 2' );
$this -> assertEqual ( $result , $expected );
$oldError = $this -> testDb -> error ;
$this -> testDb -> error = true ;
$result = $this -> testDb -> logQuery ( 'Error 1' );
$this -> assertFalse ( $result );
$this -> testDb -> error = $oldError ;
2009-12-20 00:54:49 +00:00
$log = $this -> testDb -> getLog ( false , false );
$result = Set :: combine ( $log [ 'log' ], '/query' , '/error' );
2008-05-30 11:40:08 +00:00
$expected = array ( 'Query 1' => false , 'Query 2' => false , 'Error 1' => true );
$this -> assertEqual ( $result , $expected );
2009-12-20 00:54:49 +00:00
Configure :: write ( 'debug' , 2 );
2008-05-30 11:40:08 +00:00
ob_start ();
$this -> testDb -> showLog ();
2008-06-14 19:45:26 +00:00
$contents = ob_get_clean ();
2008-05-30 11:40:08 +00:00
$this -> assertPattern ( '/Query 1/s' , $contents );
$this -> assertPattern ( '/Query 2/s' , $contents );
$this -> assertPattern ( '/Error 1/s' , $contents );
ob_start ();
$this -> testDb -> showLog ( true );
2008-06-14 19:45:26 +00:00
$contents = ob_get_clean ();
2008-05-30 11:40:08 +00:00
$this -> assertPattern ( '/Query 1/s' , $contents );
$this -> assertPattern ( '/Query 2/s' , $contents );
$this -> assertPattern ( '/Error 1/s' , $contents );
$oldError = $this -> testDb -> error ;
$oldDebug = Configure :: read ( 'debug' );
Configure :: write ( 'debug' , 2 );
$this -> testDb -> error = $oldError ;
Configure :: write ( 'debug' , $oldDebug );
}
2009-07-24 19:18:37 +00:00
2010-09-25 05:13:01 +00:00
function testShowQueryError () {
$this -> testDb -> error = true ;
try {
$this -> testDb -> showQuery ( 'Error 2' );
$this -> fail ( 'No exception' );
} catch ( Exception $e ) {
$this -> assertPattern ( '/SQL Error/' , $e -> getMessage ());
$this -> assertTrue ( true , 'Exception thrown' );
}
}
2009-09-02 03:23:47 +00:00
/**
* test getting the query log as an array .
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2009-09-02 03:23:47 +00:00
function testGetLog () {
$this -> testDb -> logQuery ( 'Query 1' );
$this -> testDb -> logQuery ( 'Query 2' );
$oldError = $this -> testDb -> error ;
$this -> testDb -> error = true ;
$result = $this -> testDb -> logQuery ( 'Error 1' );
$this -> assertFalse ( $result );
$this -> testDb -> error = $oldError ;
$log = $this -> testDb -> getLog ();
$expected = array ( 'query' => 'Query 1' , 'error' => '' , 'affected' => '' , 'numRows' => '' , 'took' => '' );
2009-12-04 04:29:28 +00:00
$this -> assertEqual ( $log [ 'log' ][ 0 ], $expected );
2009-09-02 03:23:47 +00:00
$expected = array ( 'query' => 'Query 2' , 'error' => '' , 'affected' => '' , 'numRows' => '' , 'took' => '' );
2009-12-04 04:29:28 +00:00
$this -> assertEqual ( $log [ 'log' ][ 1 ], $expected );
2009-09-02 03:23:47 +00:00
$expected = array ( 'query' => 'Error 1' , 'error' => true , 'affected' => '' , 'numRows' => '' , 'took' => '' );
2009-12-04 04:29:28 +00:00
$this -> assertEqual ( $log [ 'log' ][ 2 ], $expected );
2009-09-02 03:23:47 +00:00
}
2009-09-30 04:37:06 +00:00
/**
* test that execute runs queries .
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2009-09-30 04:37:06 +00:00
function testExecute () {
$query = 'SELECT * FROM ' . $this -> testDb -> fullTableName ( 'articles' ) . ' WHERE 1 = 1' ;
$this -> db -> took = null ;
$this -> db -> affected = null ;
2010-11-11 03:40:13 +00:00
$result = $this -> db -> execute ( $query , array ( 'log' => false ));
2009-09-30 04:37:06 +00:00
$this -> assertNotNull ( $result , 'No query performed! %s' );
$this -> assertNull ( $this -> db -> took , 'Stats were set %s' );
$this -> assertNull ( $this -> db -> affected , 'Stats were set %s' );
$result = $this -> db -> execute ( $query );
$this -> assertNotNull ( $result , 'No query performed! %s' );
$this -> assertNotNull ( $this -> db -> took , 'Stats were not set %s' );
$this -> assertNotNull ( $this -> db -> affected , 'Stats were not set %s' );
}
2009-10-30 00:31:29 +00:00
/**
* test that query () returns boolean values from operations like CREATE TABLE
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2009-10-30 00:31:29 +00:00
function testFetchAllBooleanReturns () {
$name = $this -> db -> fullTableName ( 'test_query' );
$query = " CREATE TABLE { $name } (name varchar(10)); " ;
$result = $this -> db -> query ( $query );
2010-10-17 00:58:07 +00:00
$this -> assertTrue ( $result , 'Query did not return a boolean' );
2009-10-30 00:31:29 +00:00
$query = " DROP TABLE { $name } ; " ;
2010-10-17 00:58:07 +00:00
$result = $this -> db -> query ( $query );
$this -> assertTrue ( $result , 'Query did not return a boolean' );
2009-10-30 00:31:29 +00:00
}
2009-01-14 04:12:46 +00:00
/**
* test ShowQuery generation of regular and error messages
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2009-01-14 04:12:46 +00:00
function testShowQuery () {
$this -> testDb -> error = false ;
ob_start ();
$this -> testDb -> showQuery ( 'Some Query' );
$contents = ob_get_clean ();
$this -> assertPattern ( '/Some Query/s' , $contents );
$this -> assertPattern ( '/Aff:/s' , $contents );
$this -> assertPattern ( '/Num:/s' , $contents );
$this -> assertPattern ( '/Took:/s' , $contents );
}
2009-12-17 19:12:46 +00:00
2009-12-17 19:20:36 +00:00
/**
* test order to generate query order clause for virtual fields
*
* @ return void
*/
2009-12-17 19:12:46 +00:00
function testVirtualFieldsInOrder () {
2010-10-17 14:48:46 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2009-12-17 19:12:46 +00:00
$Article -> virtualFields = array (
'this_moment' => 'NOW()' ,
'two' => '1 + 1' ,
);
2009-12-19 00:16:10 +00:00
$order = array ( 'two' , 'this_moment' );
$result = $this -> db -> order ( $order , 'ASC' , $Article );
2009-12-17 19:12:46 +00:00
$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC' ;
2009-12-19 00:16:10 +00:00
$this -> assertEqual ( $expected , $result );
2010-05-31 00:30:58 +00:00
$order = array ( 'Article.two' , 'Article.this_moment' );
$result = $this -> db -> order ( $order , 'ASC' , $Article );
$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC' ;
$this -> assertEqual ( $expected , $result );
2009-12-17 19:12:46 +00:00
}
2009-12-17 19:20:36 +00:00
/**
* test a full example of using virtual fields
*
* @ return void
*/
2009-12-17 19:12:46 +00:00
function testVirtualFieldsFetch () {
2009-12-19 00:18:23 +00:00
$this -> loadFixtures ( 'Article' , 'Comment' );
2009-12-17 19:12:46 +00:00
2010-06-10 01:17:25 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2009-12-17 19:12:46 +00:00
$Article -> virtualFields = array (
'comment_count' => 'SELECT COUNT(*) FROM ' . $this -> db -> fullTableName ( 'comments' ) .
2009-12-19 00:18:23 +00:00
' WHERE Article.id = ' . $this -> db -> fullTableName ( 'comments' ) . '.article_id'
2009-12-17 19:12:46 +00:00
);
$conditions = array ( 'comment_count >' => 2 );
2009-12-19 00:18:23 +00:00
$query = 'SELECT ' . join ( ',' , $this -> db -> fields ( $Article , null , array ( 'id' , 'comment_count' ))) .
' FROM ' . $this -> db -> fullTableName ( $Article ) . ' Article ' . $this -> db -> conditions ( $conditions , true , true , $Article );
2009-12-17 19:12:46 +00:00
$result = $this -> db -> fetchAll ( $query );
$expected = array ( array (
'Article' => array ( 'id' => 1 , 'comment_count' => 4 )
));
2009-12-19 00:18:23 +00:00
$this -> assertEqual ( $expected , $result );
2009-12-17 19:12:46 +00:00
}
2010-01-11 16:38:46 +00:00
2010-09-07 04:32:44 +00:00
/**
* test reading complex virtualFields with subqueries .
*
* @ return void
*/
function testVirtualFieldsComplexRead () {
$this -> loadFixtures ( 'DataTest' , 'Article' , 'Comment' );
2010-10-17 14:48:46 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2010-09-07 04:32:44 +00:00
$commentTable = $this -> db -> fullTableName ( 'comments' );
2010-10-17 14:48:46 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2010-09-07 04:32:44 +00:00
$Article -> virtualFields = array (
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable .
' AS Comment WHERE Article.id = Comment.article_id'
);
$result = $Article -> find ( 'all' );
$this -> assertTrue ( count ( $result ) > 0 );
$this -> assertTrue ( $result [ 0 ][ 'Article' ][ 'comment_count' ] > 0 );
2010-10-17 14:48:46 +00:00
$DataTest = ClassRegistry :: init ( 'DataTest' );
2010-09-07 04:32:44 +00:00
$DataTest -> virtualFields = array (
'complicated' => ' ACOS ( SIN ( 20 * PI () / 180 )
* SIN ( DataTest . float * PI () / 180 )
+ COS ( 20 * PI () / 180 )
* COS ( DataTest . count * PI () / 180 )
* COS (( 50 - DataTest . float ) * PI () / 180 )
) * 180 / PI () * 60 * 1.1515 * 1.609344 '
);
$result = $DataTest -> find ( 'all' );
$this -> assertTrue ( count ( $result ) > 0 );
$this -> assertTrue ( $result [ 0 ][ 'DataTest' ][ 'complicated' ] > 0 );
}
2010-01-11 16:38:46 +00:00
2010-06-09 03:29:40 +00:00
/**
* test the permutations of fullTableName ()
*
* @ return void
*/
function testFullTablePermutations () {
2010-10-17 14:48:46 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2010-06-09 03:29:40 +00:00
$result = $this -> testDb -> fullTableName ( $Article , false );
$this -> assertEqual ( $result , 'articles' );
$Article -> tablePrefix = 'tbl_' ;
$result = $this -> testDb -> fullTableName ( $Article , false );
$this -> assertEqual ( $result , 'tbl_articles' );
2010-09-22 00:34:27 +00:00
$Article -> useTable = $Article -> table = 'with spaces' ;
$Article -> tablePrefix = '' ;
$result = $this -> testDb -> fullTableName ( $Article );
$this -> assertEqual ( $result , '`with spaces`' );
2010-06-09 03:29:40 +00:00
}
2010-07-11 17:06:33 +00:00
/**
* test that read () only calls queryAssociation on db objects when the method is defined .
*
* @ return void
*/
function testReadOnlyCallingQueryAssociationWhenDefined () {
ConnectionManager :: create ( 'test_no_queryAssociation' , array (
'datasource' => 'data'
));
2010-10-17 00:58:07 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2010-07-11 17:06:33 +00:00
$Article -> Comment -> useDbConfig = 'test_no_queryAssociation' ;
$result = $Article -> find ( 'all' );
$this -> assertTrue ( is_array ( $result ));
}
2010-10-21 02:28:31 +00:00
/**
* test that fields () is using methodCache ()
*
* @ return void
*/
function testFieldsUsingMethodCache () {
$this -> testDb -> cacheMethods = false ;
$this -> assertTrue ( empty ( $this -> testDb -> methodCache [ 'fields' ]), 'Cache not empty' );
2010-11-09 06:13:23 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2010-10-21 02:28:31 +00:00
$this -> testDb -> fields ( $Article , null , array ( 'title' , 'body' , 'published' ));
$this -> assertTrue ( empty ( $this -> testDb -> methodCache [ 'fields' ]), 'Cache not empty' );
}
2008-08-07 15:36:26 +00:00
}