2008-05-30 11:40:08 +00:00
< ? php
/**
2009-03-18 17:55:58 +00:00
* DboMysqlTest file
2008-05-30 11:40:08 +00:00
*
2009-11-06 06:46:59 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// cakephp . org )
2013-02-08 11:59:49 +00:00
* Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
2013-02-08 12:22:51 +00:00
* For full copyright and license information , please see the LICENSE . txt
2008-05-30 11:40:08 +00:00
* Redistributions of files must retain the above copyright notice .
*
2013-02-08 11:59:49 +00:00
* @ copyright Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2009-11-06 06:00:11 +00:00
* @ link http :// cakephp . org CakePHP ( tm ) Project
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Model . Datasource . Database
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0
2013-05-30 22:11:14 +00:00
* @ license http :// www . opensource . org / licenses / mit - license . php MIT License
2008-05-30 11:40:08 +00:00
*/
2013-05-30 22:11:14 +00:00
2010-12-09 05:55:24 +00:00
App :: uses ( 'Model' , 'Model' );
App :: uses ( 'AppModel' , 'Model' );
App :: uses ( 'Mysql' , 'Model/Datasource/Database' );
App :: uses ( 'CakeSchema' , 'Model' );
2010-11-03 23:44:22 +00:00
require_once dirname ( dirname ( dirname ( __FILE__ ))) . DS . 'models.php' ;
2008-07-27 03:36:30 +00:00
2008-05-30 11:40:08 +00:00
/**
2010-11-03 23:44:22 +00:00
* DboMysqlTest class
2008-05-30 11:40:08 +00:00
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Model . Datasource . Database
2008-06-02 19:22:55 +00:00
*/
2011-09-13 03:00:35 +00:00
class MysqlTest extends CakeTestCase {
2012-03-18 17:08:27 +00:00
2008-06-02 19:22:55 +00:00
/**
2010-11-03 23:44:22 +00:00
* autoFixtures property
2008-11-05 13:44:05 +00:00
*
2014-07-03 13:36:42 +00:00
* @ var bool
2008-06-02 19:22:55 +00:00
*/
2010-11-03 23:44:22 +00:00
public $autoFixtures = false ;
2009-07-24 19:18:37 +00:00
2008-06-02 19:22:55 +00:00
/**
2010-11-03 23:44:22 +00:00
* fixtures property
2008-11-05 13:44:05 +00:00
*
2010-11-03 23:44:22 +00:00
* @ var array
2008-06-02 19:22:55 +00:00
*/
2010-11-03 23:44:22 +00:00
public $fixtures = array (
'core.apple' , 'core.article' , 'core.articles_tag' , 'core.attachment' , 'core.comment' ,
'core.sample' , 'core.tag' , 'core.user' , 'core.post' , 'core.author' , 'core.data_test' ,
2013-11-17 20:37:01 +00:00
'core.binary_test' , 'core.inno' , 'core.unsigned'
2010-11-03 23:44:22 +00:00
);
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The Dbo instance to be tested
*
2009-03-17 21:10:28 +00:00
* @ var DboSource
2008-05-30 11:40:08 +00:00
*/
2010-06-10 01:33:46 +00:00
public $Dbo = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Sets up a Dbo class instance for testing
*
2014-04-02 01:02:37 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-06-10 01:49:25 +00:00
public function setUp () {
2013-07-28 23:22:52 +00:00
parent :: setUp ();
2010-09-20 02:58:30 +00:00
$this -> Dbo = ConnectionManager :: getDataSource ( 'test' );
2011-01-06 03:44:33 +00:00
if ( ! ( $this -> Dbo instanceof Mysql )) {
2010-06-10 01:49:25 +00:00
$this -> markTestSkipped ( 'The MySQL extension is not available.' );
}
$this -> _debug = Configure :: read ( 'debug' );
Configure :: write ( 'debug' , 1 );
2011-10-01 18:36:41 +00:00
$this -> model = ClassRegistry :: init ( 'MysqlTestModel' );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Sets up a Dbo class instance for testing
*
2014-04-02 01:02:37 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function tearDown () {
2013-07-28 23:22:52 +00:00
parent :: tearDown ();
2009-10-24 16:53:22 +00:00
unset ( $this -> model );
ClassRegistry :: flush ();
2008-12-09 04:54:58 +00:00
Configure :: write ( 'debug' , $this -> _debug );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Test Dbo value method
*
2010-10-15 21:32:37 +00:00
* @ group quoting
2014-04-02 01:02:37 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function testQuoting () {
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> fields ( $this -> model );
2008-05-30 11:40:08 +00:00
$expected = array (
'`MysqlTestModel`.`id`' ,
'`MysqlTestModel`.`client_id`' ,
'`MysqlTestModel`.`name`' ,
'`MysqlTestModel`.`login`' ,
'`MysqlTestModel`.`passwd`' ,
'`MysqlTestModel`.`addr_1`' ,
'`MysqlTestModel`.`addr_2`' ,
'`MysqlTestModel`.`zip_code`' ,
'`MysqlTestModel`.`city`' ,
'`MysqlTestModel`.`country`' ,
'`MysqlTestModel`.`phone`' ,
'`MysqlTestModel`.`fax`' ,
'`MysqlTestModel`.`url`' ,
'`MysqlTestModel`.`email`' ,
'`MysqlTestModel`.`comments`' ,
'`MysqlTestModel`.`last_login`' ,
'`MysqlTestModel`.`created`' ,
'`MysqlTestModel`.`updated`'
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$expected = 1.2 ;
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> value ( 1.2 , 'float' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$expected = " '1,2' " ;
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> value ( '1,2' , 'float' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$expected = " '4713e29446' " ;
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> value ( '4713e29446' );
2008-07-27 02:49:02 +00:00
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-07-27 02:49:02 +00:00
2012-03-18 16:29:39 +00:00
$expected = 'NULL' ;
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> value ( '' , 'integer' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-05 13:44:05 +00:00
2010-11-03 23:44:22 +00:00
$expected = " '0' " ;
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> value ( '' , 'boolean' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$expected = 10010001 ;
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> value ( 10010001 );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
$expected = " '00010010001' " ;
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> value ( '00010010001' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2010-08-21 04:19:30 +00:00
/**
* test that localized floats don ' t cause trouble .
*
2010-10-15 21:32:37 +00:00
* @ group quoting
2010-08-21 04:19:30 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testLocalizedFloats () {
2011-04-25 03:31:44 +00:00
$this -> skipIf ( DS === '\\' , 'The locale is not supported in Windows and affect the others tests.' );
2012-06-19 16:33:51 +00:00
$restore = setlocale ( LC_NUMERIC , 0 );
2013-08-14 20:32:04 +00:00
$this -> skipIf ( setlocale ( LC_NUMERIC , 'de_DE' ) === false , " The German locale isn't available. " );
2010-08-21 04:19:30 +00:00
2010-10-15 21:32:37 +00:00
$result = $this -> Dbo -> value ( 3.141593 );
2011-10-07 01:23:35 +00:00
$this -> assertEquals ( '3.141593' , $result );
2011-09-18 16:09:04 +00:00
2011-10-07 01:23:35 +00:00
$result = $this -> db -> value ( 3.141593 , 'float' );
$this -> assertEquals ( '3.141593' , $result );
2011-10-07 01:06:40 +00:00
$result = $this -> db -> value ( 1234567.11 , 'float' );
2011-10-07 01:23:35 +00:00
$this -> assertEquals ( '1234567.11' , $result );
2011-10-07 01:06:40 +00:00
$result = $this -> db -> value ( 123456.45464748 , 'float' );
2011-10-07 01:58:11 +00:00
$this -> assertContains ( '123456.454647' , $result );
2011-10-07 01:06:40 +00:00
$result = $this -> db -> value ( 0.987654321 , 'float' );
2011-10-07 01:23:35 +00:00
$this -> assertEquals ( '0.987654321' , ( string ) $result );
2011-10-07 01:06:40 +00:00
2011-09-18 16:09:04 +00:00
$result = $this -> db -> value ( 2.2E-54 , 'float' );
2011-10-07 01:23:35 +00:00
$this -> assertEquals ( '2.2E-54' , ( string ) $result );
2011-09-18 16:09:04 +00:00
$result = $this -> db -> value ( 2.2E-54 );
2011-10-07 01:23:35 +00:00
$this -> assertEquals ( '2.2E-54' , ( string ) $result );
2010-08-21 04:19:30 +00:00
2012-06-19 16:33:51 +00:00
setlocale ( LC_NUMERIC , $restore );
2010-08-21 04:19:30 +00:00
}
2011-09-18 16:09:04 +00:00
/**
* test that scientific notations are working correctly
*
* @ return void
*/
2012-02-17 07:13:12 +00:00
public function testScientificNotation () {
2011-09-18 16:09:04 +00:00
$result = $this -> db -> value ( 2.2E-54 , 'float' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( '2.2E-54' , ( string ) $result );
2011-09-18 16:09:04 +00:00
$result = $this -> db -> value ( 2.2E-54 );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( '2.2E-54' , ( string ) $result );
2011-09-18 16:09:04 +00:00
}
2008-06-05 15:20:45 +00:00
/**
* testTinyintCasting method
2008-11-05 13:44:05 +00:00
*
2008-06-05 15:20:45 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testTinyintCasting () {
2010-06-10 01:33:46 +00:00
$this -> Dbo -> cacheSources = false ;
$tableName = 'tinyint_' . uniqid ();
2010-10-16 18:31:01 +00:00
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $this -> Dbo -> fullTableName ( $tableName ) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));' );
2008-05-30 11:40:08 +00:00
$this -> model = new CakeTestModel ( array (
2010-09-20 02:58:30 +00:00
'name' => 'Tinyint' , 'table' => $tableName , 'ds' => 'test'
2008-05-30 11:40:08 +00:00
));
2008-08-27 08:01:00 +00:00
2008-05-30 11:40:08 +00:00
$result = $this -> model -> schema ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'boolean' , $result [ 'bool' ][ 'type' ]);
$this -> assertEquals ( 'integer' , $result [ 'small_int' ][ 'type' ]);
2008-05-30 11:40:08 +00:00
2010-06-10 01:33:46 +00:00
$this -> assertTrue (( bool ) $this -> model -> save ( array ( 'bool' => 5 , 'small_int' => 5 )));
2008-05-30 11:40:08 +00:00
$result = $this -> model -> find ( 'first' );
2013-09-18 22:17:21 +00:00
$this -> assertTrue ( $result [ 'Tinyint' ][ 'bool' ]);
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $result [ 'Tinyint' ][ 'small_int' ], '5' );
2008-05-30 11:40:08 +00:00
$this -> model -> deleteAll ( true );
2010-06-10 01:33:46 +00:00
$this -> assertTrue (( bool ) $this -> model -> save ( array ( 'bool' => 0 , 'small_int' => 100 )));
2008-05-30 11:40:08 +00:00
$result = $this -> model -> find ( 'first' );
2013-09-18 22:17:21 +00:00
$this -> assertFalse ( $result [ 'Tinyint' ][ 'bool' ]);
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $result [ 'Tinyint' ][ 'small_int' ], '100' );
2008-05-30 11:40:08 +00:00
$this -> model -> deleteAll ( true );
2010-06-10 01:33:46 +00:00
$this -> assertTrue (( bool ) $this -> model -> save ( array ( 'bool' => true , 'small_int' => 0 )));
2008-05-30 11:40:08 +00:00
$result = $this -> model -> find ( 'first' );
2013-09-18 22:17:21 +00:00
$this -> assertTrue ( $result [ 'Tinyint' ][ 'bool' ]);
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $result [ 'Tinyint' ][ 'small_int' ], '0' );
2008-05-30 11:40:08 +00:00
$this -> model -> deleteAll ( true );
2011-10-20 12:25:45 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $this -> Dbo -> fullTableName ( $tableName ));
}
2011-12-06 20:52:48 +00:00
2011-10-20 12:25:45 +00:00
/**
* testLastAffected method
*
* @ return void
*/
public function testLastAffected () {
$this -> Dbo -> cacheSources = false ;
$tableName = 'tinyint_' . uniqid ();
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $this -> Dbo -> fullTableName ( $tableName ) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));' );
$this -> model = new CakeTestModel ( array (
'name' => 'Tinyint' , 'table' => $tableName , 'ds' => 'test'
));
$this -> assertTrue (( bool ) $this -> model -> save ( array ( 'bool' => 5 , 'small_int' => 5 )));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( 1 , $this -> model -> find ( 'count' ));
2011-10-20 12:25:45 +00:00
$this -> model -> deleteAll ( true );
$result = $this -> Dbo -> lastAffected ();
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( 1 , $result );
$this -> assertEquals ( 0 , $this -> model -> find ( 'count' ));
2011-10-20 12:25:45 +00:00
2010-10-16 18:31:01 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $this -> Dbo -> fullTableName ( $tableName ));
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-11-05 13:44:05 +00:00
/**
* testIndexDetection method
*
2010-10-15 21:57:36 +00:00
* @ group indices
2008-11-05 13:44:05 +00:00
* @ return void
*/
2010-04-05 03:19:38 +00:00
public function testIndexDetection () {
2010-06-10 01:33:46 +00:00
$this -> Dbo -> cacheSources = false ;
2008-11-05 13:44:05 +00:00
2010-06-10 01:33:46 +00:00
$name = $this -> Dbo -> fullTableName ( 'simple' );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));' );
2008-11-05 13:44:05 +00:00
$expected = array ( 'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ));
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> index ( 'simple' , false );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-10-15 23:11:17 +00:00
2012-08-28 00:34:02 +00:00
$name = $this -> Dbo -> fullTableName ( 'bigint' );
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id bigint(20) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));' );
$expected = array ( 'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ));
$result = $this -> Dbo -> index ( 'bigint' , false );
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
$this -> assertEquals ( $expected , $result );
2010-06-10 01:33:46 +00:00
$name = $this -> Dbo -> fullTableName ( 'with_a_key' );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));' );
2008-11-05 13:44:05 +00:00
$expected = array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'pointless_bool' => array ( 'column' => 'bool' , 'unique' => 0 ),
);
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> index ( 'with_a_key' , false );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-05 13:44:05 +00:00
2010-06-10 01:33:46 +00:00
$name = $this -> Dbo -> fullTableName ( 'with_two_keys' );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));' );
2008-11-05 13:44:05 +00:00
$expected = array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'pointless_bool' => array ( 'column' => 'bool' , 'unique' => 0 ),
'pointless_small_int' => array ( 'column' => 'small_int' , 'unique' => 0 ),
);
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> index ( 'with_two_keys' , false );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-05 13:44:05 +00:00
2010-06-10 01:33:46 +00:00
$name = $this -> Dbo -> fullTableName ( 'with_compound_keys' );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));' );
2008-11-05 13:44:05 +00:00
$expected = array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'pointless_bool' => array ( 'column' => 'bool' , 'unique' => 0 ),
'pointless_small_int' => array ( 'column' => 'small_int' , 'unique' => 0 ),
'one_way' => array ( 'column' => array ( 'bool' , 'small_int' ), 'unique' => 0 ),
);
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> index ( 'with_compound_keys' , false );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-05 13:44:05 +00:00
2010-06-10 01:33:46 +00:00
$name = $this -> Dbo -> fullTableName ( 'with_multiple_compound_keys' );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));' );
2008-11-05 13:44:05 +00:00
$expected = array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'pointless_bool' => array ( 'column' => 'bool' , 'unique' => 0 ),
'pointless_small_int' => array ( 'column' => 'small_int' , 'unique' => 0 ),
'one_way' => array ( 'column' => array ( 'bool' , 'small_int' ), 'unique' => 0 ),
'other_way' => array ( 'column' => array ( 'small_int' , 'bool' ), 'unique' => 0 ),
);
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> index ( 'with_multiple_compound_keys' , false );
2010-10-15 21:32:37 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2012-09-20 03:21:26 +00:00
$name = $this -> Dbo -> fullTableName ( 'with_fulltext' );
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, name varchar(255), description text, primary key(id), FULLTEXT KEY `MyFtIndex` ( `name`, `description` )) ENGINE=MyISAM;' );
$expected = array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'MyFtIndex' => array ( 'column' => array ( 'name' , 'description' ), 'type' => 'fulltext' )
);
$result = $this -> Dbo -> index ( 'with_fulltext' , false );
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
$this -> assertEquals ( $expected , $result );
2012-12-14 01:15:36 +00:00
2012-12-03 03:58:57 +00:00
$name = $this -> Dbo -> fullTableName ( 'with_text_index' );
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, text_field text, primary key(id), KEY `text_index` ( `text_field`(20) ));' );
$expected = array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'text_index' => array ( 'column' => 'text_field' , 'unique' => 0 , 'length' => array ( 'text_field' => 20 )),
);
$result = $this -> Dbo -> index ( 'with_text_index' , false );
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
$this -> assertEquals ( $expected , $result );
$name = $this -> Dbo -> fullTableName ( 'with_compound_text_index' );
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, text_field1 text, text_field2 text, primary key(id), KEY `text_index` ( `text_field1`(20), `text_field2`(20) ));' );
$expected = array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'text_index' => array ( 'column' => array ( 'text_field1' , 'text_field2' ), 'unique' => 0 , 'length' => array ( 'text_field1' => 20 , 'text_field2' => 20 )),
);
$result = $this -> Dbo -> index ( 'with_compound_text_index' , false );
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $name );
$this -> assertEquals ( $expected , $result );
2008-11-05 13:44:05 +00:00
}
2009-07-24 19:18:37 +00:00
2009-09-29 15:25:42 +00:00
/**
* testBuildColumn method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testBuildColumn () {
2010-06-10 01:33:46 +00:00
$restore = $this -> Dbo -> columns ;
$this -> Dbo -> columns = array ( 'varchar(255)' => 1 );
2009-09-29 15:25:42 +00:00
$data = array (
'name' => 'testName' ,
'type' => 'varchar(255)' ,
'default' ,
'null' => true ,
'key' ,
'comment' => 'test'
);
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`testName` DEFAULT NULL COMMENT \'test\'' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-09-29 15:25:42 +00:00
$data = array (
'name' => 'testName' ,
'type' => 'varchar(255)' ,
'default' ,
'null' => true ,
'key' ,
'charset' => 'utf8' ,
'collate' => 'utf8_unicode_ci'
);
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`testName` CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-06-10 01:33:46 +00:00
$this -> Dbo -> columns = $restore ;
2009-09-29 15:25:42 +00:00
}
2009-02-08 23:09:08 +00:00
/**
* MySQL 4. x returns index data in a different format ,
* Using a mock ensure that MySQL 4. x output is properly parsed .
*
2010-10-15 21:57:36 +00:00
* @ group indices
2009-02-08 23:09:08 +00:00
* @ return void
2009-11-14 12:19:25 +00:00
*/
2011-05-30 20:02:32 +00:00
public function testIndexOnMySQL4Output () {
2010-06-10 01:33:46 +00:00
$name = $this -> Dbo -> fullTableName ( 'simple' );
2009-02-08 23:09:08 +00:00
2011-01-06 03:44:33 +00:00
$mockDbo = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' , 'getVersion' ));
2009-03-17 21:10:28 +00:00
$columnData = array (
2009-02-08 23:09:08 +00:00
array ( '0' => array (
2009-03-17 21:10:28 +00:00
'Table' => 'with_compound_keys' ,
'Non_unique' => '0' ,
'Key_name' => 'PRIMARY' ,
'Seq_in_index' => '1' ,
'Column_name' => 'id' ,
'Collation' => 'A' ,
'Cardinality' => '0' ,
2012-03-18 17:08:27 +00:00
'Sub_part' => null ,
'Packed' => null ,
2009-03-17 21:10:28 +00:00
'Null' => '' ,
'Index_type' => 'BTREE' ,
2009-02-08 23:09:08 +00:00
'Comment' => ''
2009-03-17 21:10:28 +00:00
)),
2009-02-08 23:09:08 +00:00
array ( '0' => array (
2009-03-17 21:10:28 +00:00
'Table' => 'with_compound_keys' ,
'Non_unique' => '1' ,
'Key_name' => 'pointless_bool' ,
'Seq_in_index' => '1' ,
'Column_name' => 'bool' ,
'Collation' => 'A' ,
2012-03-18 17:08:27 +00:00
'Cardinality' => null ,
'Sub_part' => null ,
'Packed' => null ,
2009-03-17 21:10:28 +00:00
'Null' => 'YES' ,
'Index_type' => 'BTREE' ,
2009-02-08 23:09:08 +00:00
'Comment' => ''
)),
array ( '0' => array (
2009-03-17 21:10:28 +00:00
'Table' => 'with_compound_keys' ,
'Non_unique' => '1' ,
'Key_name' => 'pointless_small_int' ,
'Seq_in_index' => '1' ,
'Column_name' => 'small_int' ,
'Collation' => 'A' ,
2012-03-18 17:08:27 +00:00
'Cardinality' => null ,
'Sub_part' => null ,
'Packed' => null ,
2009-03-17 21:10:28 +00:00
'Null' => 'YES' ,
'Index_type' => 'BTREE' ,
2009-02-08 23:09:08 +00:00
'Comment' => ''
2009-03-17 21:10:28 +00:00
)),
2009-02-08 23:09:08 +00:00
array ( '0' => array (
2009-03-17 21:10:28 +00:00
'Table' => 'with_compound_keys' ,
'Non_unique' => '1' ,
'Key_name' => 'one_way' ,
'Seq_in_index' => '1' ,
'Column_name' => 'bool' ,
'Collation' => 'A' ,
2012-03-18 17:08:27 +00:00
'Cardinality' => null ,
'Sub_part' => null ,
'Packed' => null ,
2009-03-17 21:10:28 +00:00
'Null' => 'YES' ,
'Index_type' => 'BTREE' ,
2009-02-08 23:09:08 +00:00
'Comment' => ''
2009-03-17 21:10:28 +00:00
)),
2009-02-08 23:09:08 +00:00
array ( '0' => array (
2009-03-17 21:10:28 +00:00
'Table' => 'with_compound_keys' ,
'Non_unique' => '1' ,
'Key_name' => 'one_way' ,
'Seq_in_index' => '2' ,
'Column_name' => 'small_int' ,
'Collation' => 'A' ,
2012-03-18 17:08:27 +00:00
'Cardinality' => null ,
'Sub_part' => null ,
'Packed' => null ,
2009-03-17 21:10:28 +00:00
'Null' => 'YES' ,
'Index_type' => 'BTREE' ,
'Comment' => ''
2009-02-08 23:09:08 +00:00
))
);
2010-10-15 21:57:36 +00:00
$mockDbo -> expects ( $this -> once ()) -> method ( 'getVersion' ) -> will ( $this -> returnValue ( '4.1' ));
$resultMock = $this -> getMock ( 'PDOStatement' , array ( 'fetch' ));
2010-06-10 01:33:46 +00:00
$mockDbo -> expects ( $this -> once ())
2010-10-15 21:57:36 +00:00
-> method ( '_execute' )
2010-06-10 01:33:46 +00:00
-> with ( 'SHOW INDEX FROM ' . $name )
2010-10-15 21:57:36 +00:00
-> will ( $this -> returnValue ( $resultMock ));
2010-10-15 23:11:17 +00:00
2010-10-15 21:57:36 +00:00
foreach ( $columnData as $i => $data ) {
2012-03-18 17:08:27 +00:00
$resultMock -> expects ( $this -> at ( $i )) -> method ( 'fetch' ) -> will ( $this -> returnValue (( object ) $data ));
2010-10-15 21:57:36 +00:00
}
2009-02-08 23:09:08 +00:00
$result = $mockDbo -> index ( $name , false );
$expected = array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'pointless_bool' => array ( 'column' => 'bool' , 'unique' => 0 ),
'pointless_small_int' => array ( 'column' => 'small_int' , 'unique' => 0 ),
'one_way' => array ( 'column' => array ( 'bool' , 'small_int' ), 'unique' => 0 ),
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-02-08 23:09:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-11-06 02:15:12 +00:00
/**
* testColumn method
*
* @ return void
*/
2010-04-05 03:19:38 +00:00
public function testColumn () {
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'varchar(50)' );
2008-11-06 02:15:12 +00:00
$expected = 'string' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'text' );
2008-11-06 02:15:12 +00:00
$expected = 'text' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'int(11)' );
2008-11-06 02:15:12 +00:00
$expected = 'integer' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'int(11) unsigned' );
2008-11-06 02:15:12 +00:00
$expected = 'integer' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2012-08-28 00:34:02 +00:00
$result = $this -> Dbo -> column ( 'bigint(20)' );
$expected = 'biginteger' ;
$this -> assertEquals ( $expected , $result );
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'tinyint(1)' );
2008-11-06 02:15:12 +00:00
$expected = 'boolean' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'boolean' );
2008-11-06 02:15:12 +00:00
$expected = 'boolean' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'float' );
2008-11-06 02:15:12 +00:00
$expected = 'float' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'float unsigned' );
2008-11-06 02:15:12 +00:00
$expected = 'float' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'double unsigned' );
2008-11-06 02:15:12 +00:00
$expected = 'float' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
2013-09-28 01:23:46 +00:00
$result = $this -> Dbo -> column ( 'decimal' );
$expected = 'decimal' ;
$this -> assertEquals ( $expected , $result );
2013-09-28 15:05:53 +00:00
$result = $this -> Dbo -> column ( 'numeric' );
$expected = 'decimal' ;
$this -> assertEquals ( $expected , $result );
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> column ( 'decimal(14,7) unsigned' );
2013-09-28 01:23:46 +00:00
$expected = 'decimal' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2008-11-06 02:15:12 +00:00
}
2009-07-24 19:18:37 +00:00
2008-12-09 04:54:58 +00:00
/**
* testAlterSchemaIndexes method
2009-03-17 21:10:28 +00:00
*
2010-10-15 21:57:36 +00:00
* @ group indices
2008-12-09 04:54:58 +00:00
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAlterSchemaIndexes () {
2010-06-10 01:33:46 +00:00
$this -> Dbo -> cacheSources = $this -> Dbo -> testing = false ;
2011-02-24 04:14:48 +00:00
$table = $this -> Dbo -> fullTableName ( 'altertest' );
2008-12-09 04:54:58 +00:00
2012-03-18 17:08:27 +00:00
$schemaA = new CakeSchema ( array (
2008-12-09 04:54:58 +00:00
'name' => 'AlterTest1' ,
2010-09-20 02:58:30 +00:00
'connection' => 'test' ,
2008-12-09 04:54:58 +00:00
'altertest' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'name' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
'group1' => array ( 'type' => 'integer' , 'null' => true ),
'group2' => array ( 'type' => 'integer' , 'null' => true )
)));
2012-03-18 17:08:27 +00:00
$result = $this -> Dbo -> createSchema ( $schemaA );
2012-03-18 16:29:39 +00:00
$this -> assertContains ( '`id` int(11) DEFAULT 0 NOT NULL,' , $result );
$this -> assertContains ( '`name` varchar(50) NOT NULL,' , $result );
$this -> assertContains ( '`group1` int(11) DEFAULT NULL' , $result );
$this -> assertContains ( '`group2` int(11) DEFAULT NULL' , $result );
2010-10-15 23:11:17 +00:00
//Test that the string is syntactically correct
$query = $this -> Dbo -> getConnection () -> prepare ( $result );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $query -> queryString , $result );
2008-12-09 04:54:58 +00:00
2012-03-18 17:08:27 +00:00
$schemaB = new CakeSchema ( array (
2008-12-09 04:54:58 +00:00
'name' => 'AlterTest2' ,
2010-09-20 02:58:30 +00:00
'connection' => 'test' ,
2008-12-09 04:54:58 +00:00
'altertest' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'name' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
'group1' => array ( 'type' => 'integer' , 'null' => true ),
'group2' => array ( 'type' => 'integer' , 'null' => true ),
'indexes' => array (
'name_idx' => array ( 'column' => 'name' , 'unique' => 0 ),
'group_idx' => array ( 'column' => 'group1' , 'unique' => 0 ),
'compound_idx' => array ( 'column' => array ( 'group1' , 'group2' ), 'unique' => 0 ),
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ))
)));
2009-03-17 21:10:28 +00:00
2012-03-18 17:08:27 +00:00
$result = $this -> Dbo -> alterSchema ( $schemaB -> compare ( $schemaA ));
2011-02-24 04:14:48 +00:00
$this -> assertContains ( " ALTER TABLE $table " , $result );
2012-09-20 03:21:26 +00:00
$this -> assertContains ( 'ADD KEY `name_idx` (`name`),' , $result );
$this -> assertContains ( 'ADD KEY `group_idx` (`group1`),' , $result );
$this -> assertContains ( 'ADD KEY `compound_idx` (`group1`, `group2`),' , $result );
2010-10-15 23:11:17 +00:00
$this -> assertContains ( 'ADD PRIMARY KEY (`id`);' , $result );
//Test that the string is syntactically correct
$query = $this -> Dbo -> getConnection () -> prepare ( $result );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $query -> queryString , $result );
2009-03-17 21:10:28 +00:00
2008-12-09 04:54:58 +00:00
// Change three indexes, delete one and add another one
2012-03-18 17:08:27 +00:00
$schemaC = new CakeSchema ( array (
2008-12-09 04:54:58 +00:00
'name' => 'AlterTest3' ,
2010-09-20 02:58:30 +00:00
'connection' => 'test' ,
2008-12-09 04:54:58 +00:00
'altertest' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'name' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
'group1' => array ( 'type' => 'integer' , 'null' => true ),
'group2' => array ( 'type' => 'integer' , 'null' => true ),
'indexes' => array (
2009-03-17 21:10:28 +00:00
'name_idx' => array ( 'column' => 'name' , 'unique' => 1 ),
2008-12-09 04:54:58 +00:00
'group_idx' => array ( 'column' => 'group2' , 'unique' => 0 ),
'compound_idx' => array ( 'column' => array ( 'group2' , 'group1' ), 'unique' => 0 ),
'id_name_idx' => array ( 'column' => array ( 'id' , 'name' ), 'unique' => 0 ))
)));
2012-03-18 17:08:27 +00:00
$result = $this -> Dbo -> alterSchema ( $schemaC -> compare ( $schemaB ));
2011-02-24 04:14:48 +00:00
$this -> assertContains ( " ALTER TABLE $table " , $result );
2010-10-15 23:11:17 +00:00
$this -> assertContains ( 'DROP PRIMARY KEY,' , $result );
2012-09-20 03:21:26 +00:00
$this -> assertContains ( 'DROP KEY `name_idx`,' , $result );
$this -> assertContains ( 'DROP KEY `group_idx`,' , $result );
$this -> assertContains ( 'DROP KEY `compound_idx`,' , $result );
$this -> assertContains ( 'ADD KEY `id_name_idx` (`id`, `name`),' , $result );
$this -> assertContains ( 'ADD UNIQUE KEY `name_idx` (`name`),' , $result );
$this -> assertContains ( 'ADD KEY `group_idx` (`group2`),' , $result );
$this -> assertContains ( 'ADD KEY `compound_idx` (`group2`, `group1`);' , $result );
2008-12-09 04:54:58 +00:00
2010-10-15 23:11:17 +00:00
$query = $this -> Dbo -> getConnection () -> prepare ( $result );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $query -> queryString , $result );
2008-12-09 04:54:58 +00:00
// Compare us to ourself.
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( array (), $schemaC -> compare ( $schemaC ));
2008-12-09 04:54:58 +00:00
// Drop the indexes
2012-03-18 17:08:27 +00:00
$result = $this -> Dbo -> alterSchema ( $schemaA -> compare ( $schemaC ));
2008-12-09 04:54:58 +00:00
2011-02-24 04:14:48 +00:00
$this -> assertContains ( " ALTER TABLE $table " , $result );
2012-09-20 03:21:26 +00:00
$this -> assertContains ( 'DROP KEY `name_idx`,' , $result );
$this -> assertContains ( 'DROP KEY `group_idx`,' , $result );
$this -> assertContains ( 'DROP KEY `compound_idx`,' , $result );
$this -> assertContains ( 'DROP KEY `id_name_idx`;' , $result );
2008-12-09 04:54:58 +00:00
2010-10-15 23:11:17 +00:00
$query = $this -> Dbo -> getConnection () -> prepare ( $result );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $query -> queryString , $result );
2009-10-05 01:48:20 +00:00
}
2010-10-15 23:11:17 +00:00
2009-10-24 16:53:22 +00:00
/**
* test saving and retrieval of blobs
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2011-05-30 20:02:32 +00:00
public function testBlobSaving () {
2010-10-16 14:53:13 +00:00
$this -> loadFixtures ( 'BinaryTest' );
2010-06-10 01:33:46 +00:00
$this -> Dbo -> cacheSources = false ;
2011-10-15 16:55:42 +00:00
$data = file_get_contents ( CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif' );
2009-10-24 16:53:22 +00:00
2010-10-16 14:53:13 +00:00
$model = new CakeTestModel ( array ( 'name' => 'BinaryTest' , 'ds' => 'test' ));
2009-10-24 16:53:22 +00:00
$model -> save ( compact ( 'data' ));
$result = $model -> find ( 'first' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $data , $result [ 'BinaryTest' ][ 'data' ]);
2009-10-24 16:53:22 +00:00
}
2009-10-05 01:48:20 +00:00
/**
* test altering the table settings with schema .
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2011-05-30 20:02:32 +00:00
public function testAlteringTableParameters () {
2010-06-10 01:33:46 +00:00
$this -> Dbo -> cacheSources = $this -> Dbo -> testing = false ;
2009-10-05 01:48:20 +00:00
2012-03-18 17:08:27 +00:00
$schemaA = new CakeSchema ( array (
2009-10-05 01:48:20 +00:00
'name' => 'AlterTest1' ,
2010-09-20 02:58:30 +00:00
'connection' => 'test' ,
2009-10-05 01:48:20 +00:00
'altertest' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'name' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
'tableParameters' => array (
'charset' => 'latin1' ,
'collate' => 'latin1_general_ci' ,
'engine' => 'MyISAM'
)
)
));
2012-03-18 17:08:27 +00:00
$this -> Dbo -> rawQuery ( $this -> Dbo -> createSchema ( $schemaA ));
$schemaB = new CakeSchema ( array (
2009-10-05 01:48:20 +00:00
'name' => 'AlterTest1' ,
2010-09-20 02:58:30 +00:00
'connection' => 'test' ,
2009-10-05 01:48:20 +00:00
'altertest' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'name' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
'tableParameters' => array (
'charset' => 'utf8' ,
'collate' => 'utf8_general_ci' ,
'engine' => 'InnoDB'
)
)
));
2012-03-18 17:08:27 +00:00
$result = $this -> Dbo -> alterSchema ( $schemaB -> compare ( $schemaA ));
2010-10-16 17:58:18 +00:00
$this -> assertContains ( 'DEFAULT CHARSET=utf8' , $result );
$this -> assertContains ( 'ENGINE=InnoDB' , $result );
$this -> assertContains ( 'COLLATE=utf8_general_ci' , $result );
2009-10-05 01:48:20 +00:00
2010-10-16 17:58:18 +00:00
$this -> Dbo -> rawQuery ( $result );
2011-11-08 05:38:36 +00:00
$result = $this -> Dbo -> listDetailedSources ( $this -> Dbo -> fullTableName ( 'altertest' , false , false ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'utf8_general_ci' , $result [ 'Collation' ]);
$this -> assertEquals ( 'InnoDB' , $result [ 'Engine' ]);
$this -> assertEquals ( 'utf8' , $result [ 'charset' ]);
2011-05-16 22:49:00 +00:00
2012-03-18 17:08:27 +00:00
$this -> Dbo -> rawQuery ( $this -> Dbo -> dropSchema ( $schemaA ));
2008-12-09 04:54:58 +00:00
}
2009-09-29 18:06:31 +00:00
2010-08-16 02:17:02 +00:00
/**
* test alterSchema on two tables .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAlteringTwoTables () {
2010-10-16 17:58:18 +00:00
$schema1 = new CakeSchema ( array (
2010-08-16 02:17:02 +00:00
'name' => 'AlterTest1' ,
2010-09-20 02:58:30 +00:00
'connection' => 'test' ,
2010-08-16 02:17:02 +00:00
'altertest' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'name' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
),
'other_table' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'name' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
)
));
2010-10-16 17:58:18 +00:00
$schema2 = new CakeSchema ( array (
2010-08-16 02:17:02 +00:00
'name' => 'AlterTest1' ,
2010-09-20 02:58:30 +00:00
'connection' => 'test' ,
2010-08-16 02:17:02 +00:00
'altertest' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'field_two' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
),
'other_table' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'default' => 0 ),
'field_two' => array ( 'type' => 'string' , 'null' => false , 'length' => 50 ),
)
));
2010-11-09 05:55:05 +00:00
$result = $this -> Dbo -> alterSchema ( $schema2 -> compare ( $schema1 ));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( 2 , substr_count ( $result , 'field_two' ), 'Too many fields' );
2010-08-16 02:17:02 +00:00
}
2009-09-29 18:06:31 +00:00
/**
* testReadTableParameters method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testReadTableParameters () {
2010-06-10 01:33:46 +00:00
$this -> Dbo -> cacheSources = $this -> Dbo -> testing = false ;
$tableName = 'tinyint_' . uniqid ();
2011-02-24 04:14:48 +00:00
$table = $this -> Dbo -> fullTableName ( $tableName );
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $table . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;' );
2011-11-08 05:38:36 +00:00
$result = $this -> Dbo -> readTableParameters ( $this -> Dbo -> fullTableName ( $tableName , false , false ));
2011-02-24 04:14:48 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $table );
2009-09-29 18:06:31 +00:00
$expected = array (
'charset' => 'utf8' ,
'collate' => 'utf8_unicode_ci' ,
'engine' => 'InnoDB' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-09-30 04:39:32 +00:00
2011-02-24 04:14:48 +00:00
$table = $this -> Dbo -> fullTableName ( $tableName );
$this -> Dbo -> rawQuery ( 'CREATE TABLE ' . $table . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;' );
2011-11-08 05:38:36 +00:00
$result = $this -> Dbo -> readTableParameters ( $this -> Dbo -> fullTableName ( $tableName , false , false ));
2011-02-24 04:14:48 +00:00
$this -> Dbo -> rawQuery ( 'DROP TABLE ' . $table );
2009-09-29 18:06:31 +00:00
$expected = array (
'charset' => 'cp1250' ,
'collate' => 'cp1250_general_ci' ,
'engine' => 'MyISAM' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-09-29 18:06:31 +00:00
}
/**
* testBuildTableParameters method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testBuildTableParameters () {
2010-06-10 01:33:46 +00:00
$this -> Dbo -> cacheSources = $this -> Dbo -> testing = false ;
2009-09-29 18:06:31 +00:00
$data = array (
'charset' => 'utf8' ,
'collate' => 'utf8_unicode_ci' ,
'engine' => 'InnoDB' );
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> buildTableParameters ( $data );
2009-09-29 18:06:31 +00:00
$expected = array (
'DEFAULT CHARSET=utf8' ,
'COLLATE=utf8_unicode_ci' ,
'ENGINE=InnoDB' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2009-09-29 18:06:31 +00:00
}
/**
2012-10-07 14:47:31 +00:00
* testGetCharsetName method
2009-09-29 18:06:31 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGetCharsetName () {
2010-06-10 01:33:46 +00:00
$this -> Dbo -> cacheSources = $this -> Dbo -> testing = false ;
$result = $this -> Dbo -> getCharsetName ( 'utf8_unicode_ci' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'utf8' , $result );
2010-06-10 01:33:46 +00:00
$result = $this -> Dbo -> getCharsetName ( 'cp1250_general_ci' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'cp1250' , $result );
2009-09-29 18:06:31 +00:00
}
2012-10-07 14:47:31 +00:00
/**
* testGetCharsetNameCaching method
*
* @ return void
*/
public function testGetCharsetNameCaching () {
$db = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' , 'getVersion' ));
$queryResult = $this -> getMock ( 'PDOStatement' );
$db -> expects ( $this -> exactly ( 2 )) -> method ( 'getVersion' ) -> will ( $this -> returnValue ( '5.1' ));
$db -> expects ( $this -> exactly ( 1 ))
-> method ( '_execute' )
-> with ( 'SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?' , array ( 'utf8_unicode_ci' ))
-> will ( $this -> returnValue ( $queryResult ));
$queryResult -> expects ( $this -> once ())
-> method ( 'fetch' )
-> with ( PDO :: FETCH_ASSOC )
-> will ( $this -> returnValue ( array ( 'CHARACTER_SET_NAME' => 'utf8' )));
$result = $db -> getCharsetName ( 'utf8_unicode_ci' );
$this -> assertEquals ( 'utf8' , $result );
$result = $db -> getCharsetName ( 'utf8_unicode_ci' );
$this -> assertEquals ( 'utf8' , $result );
}
2010-05-22 04:58:54 +00:00
/**
* test that changing the virtualFieldSeparator allows for __ fields .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testVirtualFieldSeparators () {
2010-10-16 18:14:16 +00:00
$this -> loadFixtures ( 'BinaryTest' );
$model = new CakeTestModel ( array ( 'table' => 'binary_tests' , 'ds' => 'test' , 'name' => 'BinaryTest' ));
2010-05-22 04:58:54 +00:00
$model -> virtualFields = array (
'other__field' => 'SUM(id)'
);
2010-11-27 00:56:41 +00:00
2010-11-09 05:55:05 +00:00
$this -> Dbo -> virtualFieldSeparator = '_$_' ;
$result = $this -> Dbo -> fields ( $model , null , array ( 'data' , 'other__field' ));
2010-11-27 00:56:41 +00:00
2010-10-17 20:23:44 +00:00
$expected = array ( '`BinaryTest`.`data`' , '(SUM(id)) AS `BinaryTest_$_other__field`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-08-10 03:25:32 +00:00
}
2011-10-15 01:25:14 +00:00
/**
* Test describe () on a fixture .
*
* @ return void
*/
public function testDescribe () {
$this -> loadFixtures ( 'Apple' );
$model = new Apple ();
$result = $this -> Dbo -> describe ( $model );
$this -> assertTrue ( isset ( $result [ 'id' ]));
$this -> assertTrue ( isset ( $result [ 'color' ]));
$result = $this -> Dbo -> describe ( $model -> useTable );
$this -> assertTrue ( isset ( $result [ 'id' ]));
$this -> assertTrue ( isset ( $result [ 'color' ]));
}
2014-08-25 09:14:39 +00:00
/**
* Test that describe () ignores `default current_timestamp` in timestamp columns .
*
* @ return void
*/
public function testDescribeHandleCurrentTimestamp () {
$name = $this -> Dbo -> fullTableName ( 'timestamp_default_values' );
$sql = <<< SQL
CREATE TABLE $name (
id INT ( 11 ) NOT NULL AUTO_INCREMENT ,
phone VARCHAR ( 10 ),
limit_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY ( id )
);
SQL ;
2014-08-25 14:54:12 +00:00
$this -> Dbo -> execute ( $sql );
2014-08-25 09:14:39 +00:00
$model = new Model ( array (
'table' => 'timestamp_default_values' ,
'ds' => 'test' ,
'alias' => 'TimestampDefaultValue'
));
$result = $this -> Dbo -> describe ( $model );
$this -> assertEquals ( '' , $result [ 'limit_date' ][ 'default' ]);
$this -> Dbo -> execute ( 'DROP TABLE ' . $name );
}
2010-08-10 03:25:32 +00:00
/**
* test that a describe () gets additional fieldParameters
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testDescribeGettingFieldParameters () {
2010-10-16 18:02:59 +00:00
$schema = new CakeSchema ( array (
2010-09-20 02:58:30 +00:00
'connection' => 'test' ,
2010-08-10 03:25:32 +00:00
'testdescribes' => array (
'id' => array ( 'type' => 'integer' , 'key' => 'primary' ),
'stringy' => array (
'type' => 'string' ,
'null' => true ,
'charset' => 'cp1250' ,
'collate' => 'cp1250_general_ci' ,
),
'other_col' => array (
'type' => 'string' ,
'null' => false ,
'charset' => 'latin1' ,
'comment' => 'Test Comment'
)
)
));
2010-11-09 05:55:05 +00:00
$this -> Dbo -> execute ( $this -> Dbo -> createSchema ( $schema ));
2010-10-16 18:02:59 +00:00
$model = new CakeTestModel ( array ( 'table' => 'testdescribes' , 'name' => 'Testdescribes' ));
2011-02-24 04:14:48 +00:00
$result = $model -> getDataSource () -> describe ( $model );
2010-11-09 05:55:05 +00:00
$this -> Dbo -> execute ( $this -> Dbo -> dropSchema ( $schema ));
2010-10-16 18:14:16 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'cp1250_general_ci' , $result [ 'stringy' ][ 'collate' ]);
$this -> assertEquals ( 'cp1250' , $result [ 'stringy' ][ 'charset' ]);
$this -> assertEquals ( 'Test Comment' , $result [ 'other_col' ][ 'comment' ]);
2010-05-22 04:58:54 +00:00
}
2012-07-28 02:31:49 +00:00
/**
* Test that two columns with key => primary doesn ' t create invalid sql .
*
* @ return void
*/
public function testTwoColumnsWithPrimaryKey () {
$schema = new CakeSchema ( array (
'connection' => 'test' ,
'roles_users' => array (
'role_id' => array (
'type' => 'integer' ,
'null' => false ,
'default' => null ,
'key' => 'primary'
),
'user_id' => array (
'type' => 'integer' ,
'null' => false ,
'default' => null ,
'key' => 'primary'
),
'indexes' => array (
'user_role_index' => array (
'column' => array ( 'role_id' , 'user_id' ),
'unique' => 1
),
'user_index' => array (
'column' => 'user_id' ,
'unique' => 0
)
),
)
));
$result = $this -> Dbo -> createSchema ( $schema );
$this -> assertContains ( '`role_id` int(11) NOT NULL,' , $result );
$this -> assertContains ( '`user_id` int(11) NOT NULL,' , $result );
}
2012-10-23 00:33:43 +00:00
/**
* Test that the primary flag is handled correctly .
*
* @ return void
*/
public function testCreateSchemaAutoPrimaryKey () {
$schema = new CakeSchema ();
$schema -> tables = array (
'no_indexes' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'key' => 'primary' ),
'data' => array ( 'type' => 'integer' , 'null' => false ),
'indexes' => array (),
)
);
$result = $this -> Dbo -> createSchema ( $schema , 'no_indexes' );
$this -> assertContains ( 'PRIMARY KEY (`id`)' , $result );
$this -> assertNotContains ( 'UNIQUE KEY' , $result );
$schema -> tables = array (
'primary_index' => array (
'id' => array ( 'type' => 'integer' , 'null' => false ),
'data' => array ( 'type' => 'integer' , 'null' => false ),
'indexes' => array (
'PRIMARY' => array ( 'column' => 'id' , 'unique' => 1 ),
'some_index' => array ( 'column' => 'data' , 'unique' => 1 )
),
)
);
$result = $this -> Dbo -> createSchema ( $schema , 'primary_index' );
$this -> assertContains ( 'PRIMARY KEY (`id`)' , $result );
$this -> assertContains ( 'UNIQUE KEY `some_index` (`data`)' , $result );
$schema -> tables = array (
'primary_flag_has_index' => array (
'id' => array ( 'type' => 'integer' , 'null' => false , 'key' => 'primary' ),
'data' => array ( 'type' => 'integer' , 'null' => false ),
2013-08-03 01:02:52 +00:00
'indexes' => array (
2012-10-23 00:33:43 +00:00
'some_index' => array ( 'column' => 'data' , 'unique' => 1 )
),
)
);
$result = $this -> Dbo -> createSchema ( $schema , 'primary_flag_has_index' );
$this -> assertContains ( 'PRIMARY KEY (`id`)' , $result );
$this -> assertContains ( 'UNIQUE KEY `some_index` (`data`)' , $result );
}
2010-10-14 05:40:51 +00:00
/**
* Tests that listSources method sends the correct query and parses the result accordingly
* @ return void
*/
public function testListSources () {
2011-01-06 03:44:33 +00:00
$db = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' ));
2010-10-14 05:40:51 +00:00
$queryResult = $this -> getMock ( 'PDOStatement' );
$db -> expects ( $this -> once ())
-> method ( '_execute' )
2011-02-23 16:36:41 +00:00
-> with ( 'SHOW TABLES FROM `cake`' )
2010-10-14 05:40:51 +00:00
-> will ( $this -> returnValue ( $queryResult ));
$queryResult -> expects ( $this -> at ( 0 ))
-> method ( 'fetch' )
-> will ( $this -> returnValue ( array ( 'cake_table' )));
$queryResult -> expects ( $this -> at ( 1 ))
-> method ( 'fetch' )
-> will ( $this -> returnValue ( array ( 'another_table' )));
$queryResult -> expects ( $this -> at ( 2 ))
-> method ( 'fetch' )
-> will ( $this -> returnValue ( null ));
$tables = $db -> listSources ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( array ( 'cake_table' , 'another_table' ), $tables );
2010-10-14 05:40:51 +00:00
}
2010-10-15 03:45:17 +00:00
2011-02-12 03:37:56 +00:00
/**
* test that listDetailedSources with a named table that doesn ' t exist .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testListDetailedSourcesNamed () {
2011-02-12 03:37:56 +00:00
$this -> loadFixtures ( 'Apple' );
$result = $this -> Dbo -> listDetailedSources ( 'imaginary' );
$this -> assertEquals ( array (), $result , 'Should be empty when table does not exist.' );
$result = $this -> Dbo -> listDetailedSources ();
2011-11-08 05:38:36 +00:00
$tableName = $this -> Dbo -> fullTableName ( 'apples' , false , false );
2011-02-24 04:14:48 +00:00
$this -> assertTrue ( isset ( $result [ $tableName ]), 'Key should exist' );
2011-02-12 03:37:56 +00:00
}
2010-10-15 03:45:17 +00:00
/**
* Tests that getVersion method sends the correct query for getting the mysql version
* @ return void
*/
public function testGetVersion () {
2010-10-15 21:33:59 +00:00
$version = $this -> Dbo -> getVersion ();
$this -> assertTrue ( is_string ( $version ));
2010-10-15 03:45:17 +00:00
}
2010-10-15 03:48:07 +00:00
/**
* Tests that getVersion method sends the correct query for getting the client encoding
* @ return void
*/
public function testGetEncoding () {
2011-01-06 03:44:33 +00:00
$db = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' ));
2010-10-15 03:48:07 +00:00
$queryResult = $this -> getMock ( 'PDOStatement' );
$db -> expects ( $this -> once ())
-> method ( '_execute' )
-> with ( 'SHOW VARIABLES LIKE ?' , array ( 'character_set_client' ))
-> will ( $this -> returnValue ( $queryResult ));
$result = new StdClass ;
$result -> Value = 'utf-8' ;
$queryResult -> expects ( $this -> once ())
-> method ( 'fetchObject' )
-> will ( $this -> returnValue ( $result ));
$encoding = $db -> getEncoding ();
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( 'utf-8' , $encoding );
2010-10-15 03:48:07 +00:00
}
2010-11-03 23:44:22 +00:00
/**
* testFieldDoubleEscaping method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testFieldDoubleEscaping () {
2011-12-12 04:07:01 +00:00
$db = $this -> Dbo -> config [ 'database' ];
2011-01-06 03:44:33 +00:00
$test = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' , 'execute' ));
2011-12-12 04:07:01 +00:00
$test -> config [ 'database' ] = $db ;
2010-11-03 23:44:22 +00:00
$this -> Model = $this -> getMock ( 'Article2' , array ( 'getDataSource' ));
$this -> Model -> alias = 'Article' ;
$this -> Model -> expects ( $this -> any ())
-> method ( 'getDataSource' )
-> will ( $this -> returnValue ( $test ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '`Article`.`id`' , $this -> Model -> escapeField ());
2010-11-03 23:44:22 +00:00
$result = $test -> fields ( $this -> Model , null , $this -> Model -> escapeField ());
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( array ( '`Article`.`id`' ), $result );
2010-11-03 23:44:22 +00:00
$test -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
2012-03-18 17:08:27 +00:00
-> with ( 'SELECT `Article`.`id` FROM ' . $test -> fullTableName ( 'articles' ) . ' AS `Article` WHERE 1 = 1' );
2010-11-03 23:44:22 +00:00
$result = $test -> read ( $this -> Model , array (
'fields' => $this -> Model -> escapeField (),
'conditions' => null ,
'recursive' => - 1
));
$test -> startQuote = '[' ;
$test -> endQuote = ']' ;
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '[Article].[id]' , $this -> Model -> escapeField ());
2010-11-03 23:44:22 +00:00
$result = $test -> fields ( $this -> Model , null , $this -> Model -> escapeField ());
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( array ( '[Article].[id]' ), $result );
2010-11-03 23:44:22 +00:00
$test -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
2011-11-08 05:38:36 +00:00
-> with ( 'SELECT [Article].[id] FROM ' . $test -> fullTableName ( 'articles' ) . ' AS [Article] WHERE 1 = 1' );
2010-11-03 23:44:22 +00:00
$result = $test -> read ( $this -> Model , array (
'fields' => $this -> Model -> escapeField (),
'conditions' => null ,
'recursive' => - 1
));
}
2010-11-03 23:48:51 +00:00
/**
* testGenerateAssociationQuerySelfJoin method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQuerySelfJoin () {
2011-01-06 03:44:33 +00:00
$this -> Dbo = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' , 'execute' ));
2010-11-03 23:48:51 +00:00
$this -> startTime = microtime ( true );
$this -> Model = new Article2 ();
$this -> _buildRelatedModels ( $this -> Model );
$this -> _buildRelatedModels ( $this -> Model -> Category2 );
$this -> Model -> Category2 -> ChildCat = new Category2 ();
$this -> Model -> Category2 -> ParentCat = new Category2 ();
$queryData = array ();
foreach ( $this -> Model -> Category2 -> associations () as $type ) {
foreach ( $this -> Model -> Category2 -> { $type } as $assoc => $assocData ) {
$linkModel = $this -> Model -> Category2 -> { $assoc };
$external = isset ( $assocData [ 'external' ]);
2014-05-04 07:58:53 +00:00
if ( $this -> Model -> Category2 -> alias === $linkModel -> alias &&
$type !== 'hasAndBelongsToMany' &&
$type !== 'hasMany'
) {
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model -> Category2 , $linkModel , $type , $assoc , $assocData , $queryData , $external );
2010-11-03 23:48:51 +00:00
$this -> assertFalse ( empty ( $result ));
} else {
2014-04-29 12:19:33 +00:00
if ( $this -> Model -> Category2 -> useDbConfig === $linkModel -> useDbConfig ) {
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model -> Category2 , $linkModel , $type , $assoc , $assocData , $queryData , $external );
2010-11-03 23:48:51 +00:00
$this -> assertFalse ( empty ( $result ));
}
}
}
}
2013-11-09 08:50:06 +00:00
$query = $this -> Dbo -> buildAssociationQuery ( $this -> Model -> Category2 , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+(.+)FROM(.+)`Category2`\.`group_id`\s+=\s+`Group`\.`id`\)\s+LEFT JOIN(.+)WHERE\s+1 = 1\s*$/' , $query );
2010-11-03 23:48:51 +00:00
$this -> Model = new TestModel4 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'belongsTo' , 'model' => 'TestModel4Parent' );
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
$_queryData = $queryData ;
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-03 23:48:51 +00:00
$this -> assertTrue ( $result );
$expected = array (
'conditions' => array (),
'fields' => array (
'`TestModel4`.`id`' ,
'`TestModel4`.`name`' ,
'`TestModel4`.`created`' ,
'`TestModel4`.`updated`' ,
'`TestModel4Parent`.`id`' ,
'`TestModel4Parent`.`name`' ,
'`TestModel4Parent`.`created`' ,
'`TestModel4Parent`.`updated`'
),
'joins' => array (
array (
2011-11-08 05:38:36 +00:00
'table' => $this -> Dbo -> fullTableName ( $this -> Model ),
2010-11-03 23:48:51 +00:00
'alias' => 'TestModel4Parent' ,
'type' => 'LEFT' ,
'conditions' => '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`'
)
),
'order' => array (),
'limit' => array (),
'offset' => array (),
2011-10-23 19:03:29 +00:00
'group' => array (),
'callbacks' => null
2010-11-03 23:48:51 +00:00
);
2011-10-02 02:38:20 +00:00
$queryData [ 'joins' ][ 0 ][ 'table' ] = $this -> Dbo -> fullTableName ( $queryData [ 'joins' ][ 0 ][ 'table' ]);
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $expected , $queryData );
2010-11-03 23:48:51 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4Parent`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/' , $result );
2013-10-22 12:29:36 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+1 = 1$/' , $result );
2010-11-03 23:48:51 +00:00
$params [ 'assocData' ][ 'type' ] = 'INNER' ;
$this -> Model -> belongsTo [ 'TestModel4Parent' ][ 'type' ] = 'INNER' ;
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $_queryData , $params [ 'external' ]);
2010-11-03 23:48:51 +00:00
$this -> assertTrue ( $result );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'INNER' , $_queryData [ 'joins' ][ 0 ][ 'type' ]);
2010-11-03 23:48:51 +00:00
}
/**
* buildRelatedModels method
*
2012-05-13 00:43:31 +00:00
* @ param Model $model
2010-11-03 23:48:51 +00:00
* @ return void
*/
2012-02-23 14:10:29 +00:00
protected function _buildRelatedModels ( Model $model ) {
2010-11-03 23:48:51 +00:00
foreach ( $model -> associations () as $type ) {
2013-01-23 12:45:50 +00:00
foreach ( $model -> { $type } as $assocData ) {
2010-11-03 23:48:51 +00:00
if ( is_string ( $assocData )) {
$className = $assocData ;
} elseif ( isset ( $assocData [ 'className' ])) {
$className = $assocData [ 'className' ];
}
$model -> $className = new $className ();
$model -> $className -> schema ();
}
}
}
/**
* & _prepareAssociationQuery method
*
2012-05-13 00:43:31 +00:00
* @ param Model $model
* @ param array $queryData
* @ param array $binding
2010-11-03 23:48:51 +00:00
* @ return void
*/
2012-02-23 14:10:29 +00:00
protected function & _prepareAssociationQuery ( Model $model , & $queryData , $binding ) {
2010-11-03 23:48:51 +00:00
$type = $binding [ 'type' ];
$assoc = $binding [ 'model' ];
$assocData = $model -> { $type }[ $assoc ];
$className = $assocData [ 'className' ];
$linkModel = $model -> { $className };
$external = isset ( $assocData [ 'external' ]);
2012-03-02 14:55:58 +00:00
$queryData = $this -> _scrubQueryData ( $queryData );
2010-11-03 23:48:51 +00:00
$result = array_merge ( array ( 'linkModel' => & $linkModel ), compact ( 'type' , 'assoc' , 'assocData' , 'external' ));
return $result ;
}
2010-11-04 00:05:15 +00:00
2012-03-02 14:55:58 +00:00
/**
* Helper method copied from DboSource :: _scrubQueryData ()
*
* @ param array $data
* @ return array
*/
2012-03-18 17:08:27 +00:00
protected function _scrubQueryData ( $data ) {
2012-03-02 14:55:58 +00:00
static $base = null ;
if ( $base === null ) {
$base = array_fill_keys ( array ( 'conditions' , 'fields' , 'joins' , 'order' , 'limit' , 'offset' , 'group' ), array ());
$base [ 'callbacks' ] = null ;
}
return ( array ) $data + $base ;
}
2014-03-24 01:09:08 +00:00
/**
* test that read () places provided joins after the generated ones .
*
* @ return void
*/
public function testReadCustomJoinsAfterGeneratedJoins () {
$db = $this -> Dbo -> config [ 'database' ];
$test = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' , 'execute' ));
$test -> config [ 'database' ] = $db ;
$this -> Model = $this -> getMock ( 'TestModel9' , array ( 'getDataSource' ));
$this -> Model -> expects ( $this -> any ())
-> method ( 'getDataSource' )
-> will ( $this -> returnValue ( $test ));
$this -> Model -> TestModel8 = $this -> getMock ( 'TestModel8' , array ( 'getDataSource' ));
$this -> Model -> TestModel8 -> expects ( $this -> any ())
-> method ( 'getDataSource' )
-> will ( $this -> returnValue ( $test ));
2014-03-24 14:04:19 +00:00
$model8Table = $test -> fullTableName ( $this -> Model -> TestModel8 );
$usersTable = $test -> fullTableName ( 'users' );
$search = " LEFT JOIN $model8Table AS `TestModel8` ON " .
2014-03-24 01:09:08 +00:00
" (`TestModel8`.`name` != 'larry' AND `TestModel9`.`test_model8_id` = `TestModel8`.`id`) " .
2014-03-24 14:04:19 +00:00
" LEFT JOIN $usersTable AS `User` ON (`TestModel9`.`id` = `User`.`test_id`) " ;
2014-03-24 01:09:08 +00:00
$test -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
-> with ( $this -> stringContains ( $search ));
$test -> read ( $this -> Model , array (
'joins' => array (
array (
'table' => 'users' ,
'alias' => 'User' ,
'type' => 'LEFT' ,
'conditions' => array ( 'TestModel9.id = User.test_id' )
)
),
'recursive' => 1
));
}
2010-11-04 00:05:15 +00:00
/**
* testGenerateInnerJoinAssociationQuery method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateInnerJoinAssociationQuery () {
2011-12-12 04:07:01 +00:00
$db = $this -> Dbo -> config [ 'database' ];
2011-01-06 03:44:33 +00:00
$test = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' , 'execute' ));
2011-12-12 04:07:01 +00:00
$test -> config [ 'database' ] = $db ;
2010-11-04 00:05:15 +00:00
$this -> Model = $this -> getMock ( 'TestModel9' , array ( 'getDataSource' ));
$this -> Model -> expects ( $this -> any ())
-> method ( 'getDataSource' )
-> will ( $this -> returnValue ( $test ));
$this -> Model -> TestModel8 = $this -> getMock ( 'TestModel8' , array ( 'getDataSource' ));
$this -> Model -> TestModel8 -> expects ( $this -> any ())
-> method ( 'getDataSource' )
-> will ( $this -> returnValue ( $test ));
2011-11-08 05:38:36 +00:00
$testModel8Table = $this -> Model -> TestModel8 -> getDataSource () -> fullTableName ( $this -> Model -> TestModel8 );
2010-11-04 00:05:15 +00:00
$test -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
2011-11-08 05:38:36 +00:00
-> with ( $this -> stringContains ( '`TestModel9` LEFT JOIN ' . $testModel8Table ));
2010-11-04 00:05:15 +00:00
$test -> expects ( $this -> at ( 1 )) -> method ( 'execute' )
2011-11-08 05:38:36 +00:00
-> with ( $this -> stringContains ( 'TestModel9` INNER JOIN ' . $testModel8Table ));
2010-11-04 00:05:15 +00:00
$test -> read ( $this -> Model , array ( 'recursive' => 1 ));
$this -> Model -> belongsTo [ 'TestModel8' ][ 'type' ] = 'INNER' ;
$test -> read ( $this -> Model , array ( 'recursive' => 1 ));
}
2010-11-04 00:10:09 +00:00
/**
* testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQuerySelfJoinWithConditionsInHasOneBinding () {
2010-11-04 00:10:09 +00:00
$this -> Model = new TestModel8 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'hasOne' , 'model' => 'TestModel9' );
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:10:09 +00:00
$this -> assertTrue ( $result );
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`, `TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/FROM\s+\S+`test_model8` AS `TestModel8`\s+LEFT JOIN\s+\S+`test_model9` AS `TestModel9`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel9`\.`name` != \'mariano\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/' , $result );
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:10:09 +00:00
}
/**
* testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQuerySelfJoinWithConditionsInBelongsToBinding () {
2010-11-04 00:10:09 +00:00
$this -> Model = new TestModel9 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'belongsTo' , 'model' => 'TestModel8' );
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:10:09 +00:00
$this -> assertTrue ( $result );
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel9`\.`id`, `TestModel9`\.`test_model8_id`, `TestModel9`\.`name`, `TestModel9`\.`created`, `TestModel9`\.`updated`, `TestModel8`\.`id`, `TestModel8`\.`test_model9_id`, `TestModel8`\.`name`, `TestModel8`\.`created`, `TestModel8`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/FROM\s+\S+`test_model9` AS `TestModel9`\s+LEFT JOIN\s+\S+`test_model8` AS `TestModel8`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel8`\.`name` != \'larry\'\s+AND\s+`TestModel9`.`test_model8_id` = `TestModel8`.`id`\)\s+WHERE/' , $result );
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:10:09 +00:00
}
2010-11-04 00:22:49 +00:00
/**
* testGenerateAssociationQuerySelfJoinWithConditions method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQuerySelfJoinWithConditions () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel4 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'belongsTo' , 'model' => 'TestModel4Parent' );
$queryData = array ( 'conditions' => array ( 'TestModel4Parent.name !=' => 'mariano' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:22:49 +00:00
$this -> assertTrue ( $result );
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel4Parent`\.`id`, `TestModel4Parent`\.`name`, `TestModel4Parent`\.`created`, `TestModel4Parent`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4Parent`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel4`.`parent_id` = `TestModel4Parent`.`id`\)\s+WHERE/' , $result );
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?`TestModel4Parent`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
$this -> Featured2 = new Featured2 ();
$this -> Featured2 -> schema ();
$this -> Featured2 -> bindModel ( array (
'belongsTo' => array (
'ArticleFeatured2' => array (
'conditions' => 'ArticleFeatured2.published = \'Y\'' ,
'fields' => 'id, title, user_id, published'
)
)
));
$this -> _buildRelatedModels ( $this -> Featured2 );
$binding = array ( 'type' => 'belongsTo' , 'model' => 'ArticleFeatured2' );
$queryData = array ( 'conditions' => array ());
$params = & $this -> _prepareAssociationQuery ( $this -> Featured2 , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Featured2 , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:22:49 +00:00
$this -> assertTrue ( $result );
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Featured2 , $queryData );
2010-11-04 00:22:49 +00:00
2011-11-16 00:07:56 +00:00
$this -> assertRegExp (
2012-03-18 17:08:27 +00:00
'/^SELECT\s+`Featured2`\.`id`, `Featured2`\.`article_id`, `Featured2`\.`category_id`, `Featured2`\.`name`,\s+' .
2010-11-04 00:22:49 +00:00
'`ArticleFeatured2`\.`id`, `ArticleFeatured2`\.`title`, `ArticleFeatured2`\.`user_id`, `ArticleFeatured2`\.`published`\s+' .
2011-11-09 07:00:35 +00:00
'FROM\s+\S+`featured2` AS `Featured2`\s+LEFT JOIN\s+\S+`article_featured` AS `ArticleFeatured2`' .
2010-11-04 00:22:49 +00:00
'\s+ON\s+\(`ArticleFeatured2`.`published` = \'Y\'\s+AND\s+`Featured2`\.`article_featured2_id` = `ArticleFeatured2`\.`id`\)' .
'\s+WHERE\s+1\s+=\s+1\s*$/' ,
$result
);
}
/**
* testGenerateAssociationQueryHasOne method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasOne () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel4 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'hasOne' , 'model' => 'TestModel5' );
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:22:49 +00:00
$this -> assertTrue ( $result );
2011-11-09 07:00:35 +00:00
$testModel5Table = $this -> Dbo -> fullTableName ( $this -> Model -> TestModel5 );
2010-11-04 00:22:49 +00:00
$result = $this -> Dbo -> buildJoinStatement ( $queryData [ 'joins' ][ 0 ]);
2011-11-09 07:00:35 +00:00
$expected = ' LEFT JOIN ' . $testModel5Table . ' AS `TestModel5` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)' ;
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( trim ( $expected ), trim ( $result ));
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/`test_model5` AS `TestModel5`\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE/' , $result );
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasOneWithConditions method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasOneWithConditions () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel4 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'hasOne' , 'model' => 'TestModel5' );
$queryData = array ( 'conditions' => array ( 'TestModel5.name !=' => 'mariano' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:22:49 +00:00
$this -> assertTrue ( $result );
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2010-11-04 00:22:49 +00:00
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`, `TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+LEFT JOIN\s+\S+`test_model5` AS `TestModel5`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel5`.`test_model4_id`\s+=\s+`TestModel4`.`id`\)\s+WHERE/' , $result );
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?\s*`TestModel5`.`name`\s+!=\s+\'mariano\'\s*(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryBelongsTo method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryBelongsTo () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
2011-12-01 07:21:31 +00:00
$binding = array ( 'type' => 'belongsTo' , 'model' => 'TestModel4' );
2010-11-04 00:22:49 +00:00
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:22:49 +00:00
$this -> assertTrue ( $result );
2011-11-09 07:00:35 +00:00
$testModel4Table = $this -> Dbo -> fullTableName ( $this -> Model -> TestModel4 , true , true );
2010-11-04 00:22:49 +00:00
$result = $this -> Dbo -> buildJoinStatement ( $queryData [ 'joins' ][ 0 ]);
2012-03-18 17:08:27 +00:00
$expected = ' LEFT JOIN ' . $testModel4Table . ' AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)' ;
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( trim ( $expected ), trim ( $result ));
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/' , $result );
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryBelongsToWithConditions method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryBelongsToWithConditions () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'belongsTo' , 'model' => 'TestModel4' );
$queryData = array ( 'conditions' => array ( 'TestModel5.name !=' => 'mariano' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:22:49 +00:00
$this -> assertTrue ( $result );
2011-11-09 07:00:35 +00:00
$testModel4Table = $this -> Dbo -> fullTableName ( $this -> Model -> TestModel4 , true , true );
2010-11-04 00:22:49 +00:00
$result = $this -> Dbo -> buildJoinStatement ( $queryData [ 'joins' ][ 0 ]);
2012-03-18 17:08:27 +00:00
$expected = ' LEFT JOIN ' . $testModel4Table . ' AS `TestModel4` ON (`TestModel5`.`test_model4_id` = `TestModel4`.`id`)' ;
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( trim ( $expected ), trim ( $result ));
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`, `TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+LEFT JOIN\s+\S+`test_model4` AS `TestModel4`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel5`.`test_model4_id` = `TestModel4`.`id`\)\s+WHERE\s+/' , $result );
$this -> assertRegExp ( '/\s+WHERE\s+`TestModel5`.`name` != \'mariano\'\s*$/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasMany method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasMany () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:22:49 +00:00
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?\s*1 = 1\s*(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasManyWithLimit method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasManyWithLimit () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$this -> Model -> hasMany [ 'TestModel6' ][ 'limit' ] = 2 ;
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp (
2010-11-04 00:22:49 +00:00
'/^SELECT\s+' .
2012-03-18 17:08:27 +00:00
'`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+' .
2011-11-09 07:00:35 +00:00
'FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+' .
2012-03-18 17:08:27 +00:00
'`TestModel6`.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)\s*' .
'LIMIT \d*' .
2010-11-04 00:22:49 +00:00
'\s*$/' , $result
);
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp (
2012-03-18 17:08:27 +00:00
'/^SELECT\s+' .
'`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+' .
'FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+' .
'(?:\()?\s*1 = 1\s*(?:\))?' .
2010-11-04 00:22:49 +00:00
'\s*$/' , $result
);
}
/**
* testGenerateAssociationQueryHasManyWithConditions method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasManyWithConditions () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ( 'conditions' => array ( 'TestModel5.name !=' => 'mariano' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?`TestModel5`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasManyWithOffsetAndLimit method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasManyWithOffsetAndLimit () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
2012-03-18 17:08:27 +00:00
$backup = $this -> Model -> hasMany [ 'TestModel6' ];
2010-11-04 00:22:49 +00:00
$this -> Model -> hasMany [ 'TestModel6' ][ 'offset' ] = 2 ;
$this -> Model -> hasMany [ 'TestModel6' ][ 'limit' ] = 5 ;
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2010-11-04 00:22:49 +00:00
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
$this -> assertRegExp ( '/\s+LIMIT 2,\s*5\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2012-03-18 17:08:27 +00:00
$this -> Model -> hasMany [ 'TestModel6' ] = $backup ;
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasManyWithPageAndLimit method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasManyWithPageAndLimit () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
2012-03-18 17:08:27 +00:00
$backup = $this -> Model -> hasMany [ 'TestModel6' ];
2010-11-04 00:22:49 +00:00
$this -> Model -> hasMany [ 'TestModel6' ][ 'page' ] = 2 ;
$this -> Model -> hasMany [ 'TestModel6' ][ 'limit' ] = 5 ;
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
$this -> assertRegExp ( '/\s+LIMIT 5,\s*5\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`test_model4_id`, `TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2012-03-18 17:08:27 +00:00
$this -> Model -> hasMany [ 'TestModel6' ] = $backup ;
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasManyWithFields method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasManyWithFields () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ( 'fields' => array ( '`TestModel5`.`name`' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`id`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ( 'fields' => array ( '`TestModel5`.`id`, `TestModel5`.`name`' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ( 'fields' => array ( '`TestModel5`.`name`' , '`TestModel5`.`created`' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`test_model5_id`, `TestModel6`\.`name`, `TestModel6`\.`created`, `TestModel6`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`name`, `TestModel5`\.`created`, `TestModel5`\.`id`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
$this -> Model -> hasMany [ 'TestModel6' ][ 'fields' ] = array ( 'name' );
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ( 'fields' => array ( '`TestModel5`.`id`' , '`TestModel5`.`name`' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
unset ( $this -> Model -> hasMany [ 'TestModel6' ][ 'fields' ]);
$this -> Model -> hasMany [ 'TestModel6' ][ 'fields' ] = array ( 'id' , 'name' );
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ( 'fields' => array ( '`TestModel5`.`id`' , '`TestModel5`.`name`' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`id`, `TestModel6`\.`name`, `TestModel6`\.`test_model5_id`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
unset ( $this -> Model -> hasMany [ 'TestModel6' ][ 'fields' ]);
$this -> Model -> hasMany [ 'TestModel6' ][ 'fields' ] = array ( 'test_model5_id' , 'name' );
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
$queryData = array ( 'fields' => array ( '`TestModel5`.`id`' , '`TestModel5`.`name`' ));
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel6`\.`test_model5_id`, `TestModel6`\.`name`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model6` AS `TestModel6`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/WHERE\s+(?:\()?`TestModel6`\.`test_model5_id`\s+=\s+\({\$__cakeID__\$}\)(?:\))?/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel5`\.`id`, `TestModel5`\.`name`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model5` AS `TestModel5`\s+WHERE\s+/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
unset ( $this -> Model -> hasMany [ 'TestModel6' ][ 'fields' ]);
}
/**
* test generateAssociationQuery with a hasMany and an aggregate function .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasManyAndAggregateFunction () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel5 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'hasMany' , 'model' => 'TestModel6' );
2011-07-17 22:12:47 +00:00
$queryData = array ( 'fields' => array ( 'MIN(`TestModel5`.`test_model4_id`)' ));
2010-11-04 00:22:49 +00:00
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
$this -> Model -> recursive = 0 ;
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+MIN\(`TestModel5`\.`test_model4_id`\)\s+FROM/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasAndBelongsToMany method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasAndBelongsToMany () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel4 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
$binding = array ( 'type' => 'hasAndBelongsToMany' , 'model' => 'TestModel7' );
$queryData = array ();
$params = $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-09 07:00:35 +00:00
$assocTable = $this -> Dbo -> fullTableName ( $this -> Model -> TestModel4TestModel7 , true , true );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model7` AS `TestModel7`\s+JOIN\s+' . $assocTable . '/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+AND/' , $result );
$this -> assertRegExp ( '/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)/' , $result );
$this -> assertRegExp ( '/WHERE\s+(?:\()?1 = 1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+WHERE\s+(?:\()?1 = 1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasAndBelongsToManyWithConditions method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasAndBelongsToManyWithConditions () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel4 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
2011-12-01 07:21:31 +00:00
$binding = array ( 'type' => 'hasAndBelongsToMany' , 'model' => 'TestModel7' );
2010-11-04 00:22:49 +00:00
$queryData = array ( 'conditions' => array ( 'TestModel4.name !=' => 'mariano' ));
$params = $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/' , $result );
$this -> assertRegExp ( '/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?`TestModel4`.`name`\s+!=\s+\'mariano\'(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasAndBelongsToManyWithOffsetAndLimit () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel4 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
2012-03-18 17:08:27 +00:00
$backup = $this -> Model -> hasAndBelongsToMany [ 'TestModel7' ];
2010-11-04 00:22:49 +00:00
$this -> Model -> hasAndBelongsToMany [ 'TestModel7' ][ 'offset' ] = 2 ;
$this -> Model -> hasAndBelongsToMany [ 'TestModel7' ][ 'limit' ] = 5 ;
2011-12-01 07:21:31 +00:00
$binding = array ( 'type' => 'hasAndBelongsToMany' , 'model' => 'TestModel7' );
2010-11-04 00:22:49 +00:00
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}\s+/' , $result );
$this -> assertRegExp ( '/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/' , $result );
$this -> assertRegExp ( '/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 2,\s*5\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2012-03-18 17:08:27 +00:00
$this -> Model -> hasAndBelongsToMany [ 'TestModel7' ] = $backup ;
2010-11-04 00:22:49 +00:00
}
/**
* testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testGenerateAssociationQueryHasAndBelongsToManyWithPageAndLimit () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel4 ();
$this -> Model -> schema ();
$this -> _buildRelatedModels ( $this -> Model );
2012-03-18 17:08:27 +00:00
$backup = $this -> Model -> hasAndBelongsToMany [ 'TestModel7' ];
2010-11-04 00:22:49 +00:00
$this -> Model -> hasAndBelongsToMany [ 'TestModel7' ][ 'page' ] = 2 ;
$this -> Model -> hasAndBelongsToMany [ 'TestModel7' ][ 'limit' ] = 5 ;
2011-12-01 07:21:31 +00:00
$binding = array ( 'type' => 'hasAndBelongsToMany' , 'model' => 'TestModel7' );
2010-11-04 00:22:49 +00:00
$queryData = array ();
$params = & $this -> _prepareAssociationQuery ( $this -> Model , $queryData , $binding );
2013-11-09 04:05:19 +00:00
$result = $this -> Dbo -> generateAssociationQuery ( $this -> Model , $params [ 'linkModel' ], $params [ 'type' ], $params [ 'assoc' ], $params [ 'assocData' ], $queryData , $params [ 'external' ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`, `TestModel4TestModel7`\.`test_model4_id`, `TestModel4TestModel7`\.`test_model7_id`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model7`\s+AS\s+`TestModel7`\s+JOIN\s+\S+`test_model4_test_model7`\s+AS\s+`TestModel4TestModel7`/' , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s+ON\s+\(`TestModel4TestModel7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}/' , $result );
$this -> assertRegExp ( '/\s+AND\s+`TestModel4TestModel7`\.`test_model7_id`\s+=\s+`TestModel7`\.`id`\)\s+WHERE\s+/' , $result );
$this -> assertRegExp ( '/\s+(?:\()?1\s+=\s+1(?:\))?\s*\s+LIMIT 5,\s*5\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2013-11-09 08:50:06 +00:00
$result = $this -> Dbo -> buildAssociationQuery ( $this -> Model , $queryData );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/' , $result );
2011-11-09 07:00:35 +00:00
$this -> assertRegExp ( '/\s+FROM\s+\S+`test_model4` AS `TestModel4`\s+WHERE\s+(?:\()?1\s+=\s+1(?:\))?\s*$/' , $result );
2010-11-04 00:22:49 +00:00
2012-03-18 17:08:27 +00:00
$this -> Model -> hasAndBelongsToMany [ 'TestModel7' ] = $backup ;
2010-11-04 00:22:49 +00:00
}
/**
* testSelectDistict method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSelectDistict () {
2010-11-04 00:22:49 +00:00
$this -> Model = new TestModel4 ();
$result = $this -> Dbo -> fields ( $this -> Model , 'Vendor' , " DISTINCT Vendor.id, Vendor.name " );
$expected = array ( 'DISTINCT `Vendor`.`id`' , '`Vendor`.`name`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:22:49 +00:00
}
2010-11-04 00:29:15 +00:00
/**
* testStringConditionsParsing method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testStringConditionsParsing () {
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " ProjectBid.project_id = Project.id " );
$expected = " WHERE `ProjectBid`.`project_id` = `Project`.`id` " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " Candy.name LIKE 'a' AND HardCandy.name LIKE 'c' " );
$expected = " WHERE `Candy`.`name` LIKE 'a' AND `HardCandy`.`name` LIKE 'c' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " HardCandy.name LIKE 'a' AND Candy.name LIKE 'c' " );
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " Post.title = '1.1' " );
$expected = " WHERE `Post`.`title` = '1.1' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " User.id != 0 AND User.user LIKE '%arr%' " );
$expected = " WHERE `User`.`id` != 0 AND `User`.`user` LIKE '%arr%' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " SUM(Post.comments_count) > 500 " );
$expected = " WHERE SUM(`Post`.`comments_count`) > 500 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2013-11-09 07:20:11 +00:00
$date = date ( 'Y-m-d H:i' );
$result = $this -> Dbo -> conditions ( " (Post.created < ' " . $date . " ') GROUP BY YEAR(Post.created), MONTH(Post.created) " );
$expected = " WHERE (`Post`.`created` < ' " . $date . " ') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " score BETWEEN 90.1 AND 95.7 " );
$expected = " WHERE score BETWEEN 90.1 AND 95.7 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2011-12-01 07:21:31 +00:00
$result = $this -> Dbo -> conditions ( array ( 'score' => array ( 2 => 1 , 2 , 10 )));
2012-11-02 13:29:38 +00:00
$expected = " WHERE `score` IN (1, 2, 10) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " Aro.rght = Aro.lft + 1.1 " );
$expected = " WHERE `Aro`.`rght` = `Aro`.`lft` + 1.1 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2013-11-09 07:20:11 +00:00
$date = date ( 'Y-m-d H:i:s' );
$result = $this -> Dbo -> conditions ( " (Post.created < ' " . $date . " ') GROUP BY YEAR(Post.created), MONTH(Post.created) " );
$expected = " WHERE (`Post`.`created` < ' " . $date . " ') GROUP BY YEAR(`Post`.`created`), MONTH(`Post`.`created`) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2' );
$expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2' ;
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/' , $result );
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"' );
$expected = ' WHERE `Sportstaette`.`sportstaettenart_id` = 2 AND `Sportstaette`.`sportstaette` LIKE "%ru%"' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2012-03-18 16:29:39 +00:00
$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' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2012-03-18 16:29:39 +00:00
$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' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2012-03-18 16:29:39 +00:00
$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' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2012-03-18 16:29:39 +00:00
$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`' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " Comment.id = 'a' " );
$expected = " WHERE `Comment`.`id` = 'a' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " lower(Article.title) LIKE 'a%' " );
$expected = " WHERE lower(`Article`.`title`) LIKE 'a%' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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))' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'DATEDIFF(NOW(),Article.published) < 1 && Article.live=1' );
$expected = " WHERE DATEDIFF(NOW(),`Article`.`published`) < 1 && `Article`.`live`=1 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'file = "index.html"' );
$expected = ' WHERE file = "index.html"' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( " file = 'index.html' " );
$expected = " WHERE file = 'index.html' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$letter = $letter = 'd.a' ;
$conditions = array ( 'Company.name like ' => $letter . '%' );
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE `Company`.`name` like 'd.a%' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$conditions = array ( 'Artist.name' => 'JUDY and MARY' );
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE `Artist`.`name` = 'JUDY and MARY' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$conditions = array ( 'Artist.name' => 'JUDY AND MARY' );
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE `Artist`.`name` = 'JUDY AND MARY' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2011-09-18 16:09:04 +00:00
$conditions = array ( 'Company.name similar to ' => 'a word' );
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE `Company`.`name` similar to 'a word' " ;
2011-12-13 04:35:31 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
}
/**
* testQuotesInStringConditions method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testQuotesInStringConditions () {
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.email = \'mariano@cricava.com\'' );
$expected = ' WHERE `Member`.`email` = \'mariano@cricava.com\'' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.email = "mariano@cricava.com"' );
$expected = ' WHERE `Member`.`email` = "mariano@cricava.com"' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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%\'' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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%"' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2012-09-03 17:24:38 +00:00
}
2012-08-31 13:09:30 +00:00
2012-09-03 17:24:38 +00:00
/**
* test that - in conditions and field names works
*
* @ return void
*/
public function testHypenInStringConditionsAndFieldNames () {
2012-08-31 13:09:30 +00:00
$result = $this -> Dbo -> conditions ( 'I18n__title_pt-br.content = "test"' );
$this -> assertEquals ( ' WHERE `I18n__title_pt-br`.`content` = "test"' , $result );
2012-09-03 17:24:38 +00:00
$result = $this -> Dbo -> conditions ( 'Model.field=NOW()-3600' );
$this -> assertEquals ( ' WHERE `Model`.`field`=NOW()-3600' , $result );
$result = $this -> Dbo -> conditions ( 'NOW() - Model.created < 7200' );
$this -> assertEquals ( ' WHERE NOW() - `Model`.`created` < 7200' , $result );
$result = $this -> Dbo -> conditions ( 'NOW()-Model.created < 7200' );
$this -> assertEquals ( ' WHERE NOW()-`Model`.`created` < 7200' , $result );
2010-11-04 00:29:15 +00:00
}
/**
* testParenthesisInStringConditions method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testParenthesisInStringConditions () {
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'(lu\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \')lu\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'va(lu\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'va)lu\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'va(lu)\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'va(lu)e\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'(mariano)\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'(mariano)iglesias\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'(mariano) iglesias\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'(mariano word) iglesias\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'(mariano.iglesias)\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'Mariano Iglesias (mariano.iglesias)\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'Mariano Iglesias (mariano.iglesias) CakePHP\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( 'Member.name = \'(mariano.iglesias) CakePHP\'' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/' , $result );
2010-11-04 00:29:15 +00:00
}
/**
* testParenthesisInArrayConditions method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testParenthesisInArrayConditions () {
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => '(lu' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(lu\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => ')lu' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\)lu\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => 'va(lu' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => 'va)lu' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\)lu\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => 'va(lu)' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => 'va(lu)e' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'va\(lu\)e\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => '(mariano)' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => '(mariano)iglesias' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\)iglesias\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => '(mariano) iglesias' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano\) iglesias\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => '(mariano word) iglesias' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano word\) iglesias\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => '(mariano.iglesias)' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\)\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => 'Mariano Iglesias (mariano.iglesias)' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\)\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => 'Mariano Iglesias (mariano.iglesias) CakePHP' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'Mariano Iglesias \(mariano.iglesias\) CakePHP\'$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => '(mariano.iglesias) CakePHP' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s+WHERE\s+`Member`.`name`\s+=\s+\'\(mariano.iglesias\) CakePHP\'$/' , $result );
2010-11-04 00:29:15 +00:00
}
/**
* testArrayConditionsParsing method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testArrayConditionsParsing () {
2010-11-04 00:29:15 +00:00
$this -> loadFixtures ( 'Post' , 'Author' );
$result = $this -> Dbo -> conditions ( array ( 'Stereo.type' => 'in dash speakers' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s+WHERE \ s+`Stereo`.`type` \ s+= \ s+'in dash speakers'/ " , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Candy.name LIKE' => 'a' , 'HardCandy.name LIKE' => 'c' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s+WHERE \ s+`Candy`.`name` LIKE \ s+'a' \ s+AND \ s+`HardCandy`.`name` \ s+LIKE \ s+'c'/ " , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'HardCandy.name LIKE' => 'a' , 'Candy.name LIKE' => 'c' ));
$expected = " WHERE `HardCandy`.`name` LIKE 'a' AND `Candy`.`name` LIKE 'c' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'HardCandy.name LIKE' => 'a%' , 'Candy.name LIKE' => '%c%' ));
$expected = " WHERE `HardCandy`.`name` LIKE 'a%' AND `Candy`.`name` LIKE '%c%' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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%' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2012-12-03 01:59:56 +00:00
$result = $this -> Dbo -> conditions ( array (
" Person.name || ' ' || Person.surname ILIKE " => '%mark%'
));
$expected = " WHERE `Person`.`name` || ' ' || `Person`.`surname` ILIKE '%mark%' " ;
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'score BETWEEN ? AND ?' => array ( 90.1 , 95.7 )));
2011-09-18 16:09:04 +00:00
$expected = " WHERE `score` BETWEEN 90.1 AND 95.7 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Post.title' => 1.1 ));
2011-09-18 16:09:04 +00:00
$expected = " WHERE `Post`.`title` = 1.1 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Post.title' => 1.1 ), true , true , new Post ());
$expected = " WHERE `Post`.`title` = '1.1' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'SUM(Post.comments_count) >' => '500' ));
$expected = " WHERE SUM(`Post`.`comments_count`) > '500' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'MAX(Post.rating) >' => '50' ));
$expected = " WHERE MAX(`Post`.`rating`) > '50' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2012-03-18 17:08:27 +00:00
$result = $this -> Dbo -> conditions ( array ( 'lower(Article.title)' => 'secrets' ));
2011-09-18 16:09:04 +00:00
$expected = " WHERE lower(`Article`.`title`) = 'secrets' " ;
2011-12-13 04:35:31 +00:00
$this -> assertEquals ( $expected , $result );
2011-09-18 16:09:04 +00:00
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'title LIKE' => '%hello' ));
$expected = " WHERE `title` LIKE '%hello' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Post.name' => 'mad(g)ik' ));
$expected = " WHERE `Post`.`name` = 'mad(g)ik' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'score' => array ( 1 , 2 , 10 )));
2012-11-02 13:29:38 +00:00
$expected = " WHERE `score` IN (1, 2, 10) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'score' => array ()));
2012-03-18 16:29:39 +00:00
$expected = " WHERE `score` IS NULL " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'score !=' => array ()));
2012-03-18 16:29:39 +00:00
$expected = " WHERE `score` IS NOT NULL " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'score !=' => '20' ));
$expected = " WHERE `score` != '20' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'score >' => '20' ));
$expected = " WHERE `score` > '20' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'client_id >' => '20' ), true , true , new TestModel ());
$expected = " WHERE `client_id` > 20 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'OR' => array (
array ( 'User.user' => 'mariano' ),
array ( 'User.user' => 'nate' )
)));
$expected = " WHERE ((`User`.`user` = 'mariano') OR (`User`.`user` = 'nate')) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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')) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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')) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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')) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array (
'published' => 1 , 'or' => array ( 'score >' => '2' , array ( 'score >' => '20' ))
));
$expected = " WHERE `published` = 1 AND ((`score` > '2') OR (`score` > '20')) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( array ( 'Project.removed' => false )));
$expected = " WHERE `Project`.`removed` = '0' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( array ( 'Project.removed' => true )));
$expected = " WHERE `Project`.`removed` = '1' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( array ( 'Project.removed' => null )));
2012-03-18 16:29:39 +00:00
$expected = " WHERE `Project`.`removed` IS NULL " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( array ( 'Project.removed !=' => null )));
2012-03-18 16:29:39 +00:00
$expected = " WHERE `Project`.`removed` IS NOT NULL " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( '(Usergroup.permissions) & 4' => 4 ));
$expected = " WHERE (`Usergroup`.`permissions`) & 4 = 4 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( '((Usergroup.permissions) & 4)' => 4 ));
$expected = " WHERE ((`Usergroup`.`permissions`) & 4) = 4 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Post.modified >=' => 'DATE_SUB(NOW(), INTERVAL 7 DAY)' ));
$expected = " WHERE `Post`.`modified` >= 'DATE_SUB(NOW(), INTERVAL 7 DAY)' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Post.modified >= DATE_SUB(NOW(), INTERVAL 7 DAY)' ));
$expected = " WHERE `Post`.`modified` >= DATE_SUB(NOW(), INTERVAL 7 DAY) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array (
2013-08-07 23:03:21 +00:00
'NOT' => array ( 'Course.id' => null , 'Course.vet' => 'N' , 'level_of_education_id' => array ( 912 , 999 )),
2010-11-04 00:29:15 +00:00
'Enrollment.yearcompleted >' => '0' )
);
2012-11-02 13:29:38 +00:00
$this -> assertRegExp ( '/^\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 );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'id <>' => '8' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*WHERE\s+`id`\s+<>\s+\'8\'\s*$/' , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'TestModel.field =' => 'gribe$@()lu' ));
$expected = " WHERE `TestModel`.`field` = 'gribe $ @()lu' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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%')) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'MD5(CONCAT(Reg.email,Reg.id))' => 'blah' ));
$expected = " WHERE MD5(CONCAT(`Reg`.`email`,`Reg`.`id`)) = 'blah' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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') " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$conditions = array ( 'id' => array ( 2 , 5 , 6 , 9 , 12 , 45 , 78 , 43 , 76 ));
$result = $this -> Dbo -> conditions ( $conditions );
2012-11-02 13:29:38 +00:00
$expected = " WHERE `id` IN (2, 5, 6, 9, 12, 45, 78, 43, 76) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2013-04-19 01:52:22 +00:00
$conditions = array ( '`Correction`.`source` collate utf8_bin' => array ( 'kiwi' , 'pear' ));
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE `Correction`.`source` collate utf8_bin IN ('kiwi', 'pear') " ;
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$conditions = array ( 'title' => 'user(s)' );
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE `title` = 'user(s)' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$conditions = array ( 'title' => 'user(s) data' );
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE `title` = 'user(s) data' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$conditions = array ( 'title' => 'user(s,arg) data' );
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE `title` = 'user(s,arg) data' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( " Book.book_name " => 'Java(TM)' ));
$expected = " WHERE `Book`.`book_name` = 'Java(TM)' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( " Book.book_name " => 'Java(TM) ' ));
$expected = " WHERE `Book`.`book_name` = 'Java(TM) ' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( " Book.id " => 0 ));
$expected = " WHERE `Book`.`id` = 0 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2011-09-30 00:28:52 +00:00
$result = $this -> Dbo -> conditions ( array ( " Book.id " => null ));
2012-03-18 16:29:39 +00:00
$expected = " WHERE `Book`.`id` IS NULL " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
2011-09-30 00:28:52 +00:00
$conditions = array ( 'MysqlModel.id' => '' );
$result = $this -> Dbo -> conditions ( $conditions , true , true , $this -> model );
2012-03-18 16:29:39 +00:00
$expected = " WHERE `MysqlModel`.`id` IS NULL " ;
2011-12-13 04:35:31 +00:00
$this -> assertEquals ( $expected , $result );
2011-09-30 00:28:52 +00:00
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Listing.beds >=' => 0 ));
$expected = " WHERE `Listing`.`beds` >= 0 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
}
2013-12-24 18:12:39 +00:00
/**
* test conditions () with replacements .
*
* @ return void
*/
public function testConditionsWithReplacements () {
$result = $this -> Dbo -> conditions ( array (
'score BETWEEN :0 AND :1' => array ( 90.1 , 95.7 )
));
$expected = " WHERE `score` BETWEEN 90.1 AND 95.7 " ;
$this -> assertEquals ( $expected , $result );
$result = $this -> Dbo -> conditions ( array (
'score BETWEEN ? AND ?' => array ( 90.1 , 95.7 )
));
$expected = " WHERE `score` BETWEEN 90.1 AND 95.7 " ;
$this -> assertEquals ( $expected , $result );
}
2013-04-19 01:52:22 +00:00
/**
* Test that array conditions with only one element work .
*
2013-07-05 15:19:22 +00:00
* @ return void
2013-04-19 01:52:22 +00:00
*/
public function testArrayConditionsOneElement () {
$conditions = array ( 'id' => array ( 1 ));
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE id = (1) " ;
$this -> assertEquals ( $expected , $result );
$conditions = array ( 'id NOT' => array ( 1 ));
$result = $this -> Dbo -> conditions ( $conditions );
$expected = " WHERE NOT (id = (1)) " ;
$this -> assertEquals ( $expected , $result );
}
2010-11-04 00:29:15 +00:00
/**
* testArrayConditionsParsingComplexKeys method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testArrayConditionsParsingComplexKeys () {
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array (
'CAST(Book.created AS DATE)' => '2008-08-02'
));
$expected = " WHERE CAST(`Book`.`created` AS DATE) = '2008-08-02' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array (
'CAST(Book.created AS DATE) <=' => '2008-08-02'
));
$expected = " WHERE CAST(`Book`.`created` AS DATE) <= '2008-08-02' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array (
'(Stats.clicks * 100) / Stats.views >' => 50
));
$expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
}
/**
* testMixedConditionsParsing method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testMixedConditionsParsing () {
2010-11-04 00:29:15 +00:00
$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' " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$conditions = array (
'Thread.project_id' => 5 ,
'Thread.buyer_id' => 14 ,
'1=1 GROUP BY Thread.project_id'
);
$result = $this -> Dbo -> conditions ( $conditions );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\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 );
2010-11-04 00:29:15 +00:00
}
/**
* testConditionsOptionalArguments method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testConditionsOptionalArguments () {
2012-05-19 14:48:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Member.name' => 'Mariano' ), true , false );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*`Member`.`name`\s*=\s*\'Mariano\'\s*$/' , $result );
2010-11-04 00:29:15 +00:00
2012-05-19 14:48:15 +00:00
$result = $this -> Dbo -> conditions ( array (), true , false );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*1\s*=\s*1\s*$/' , $result );
2010-11-04 00:29:15 +00:00
}
/**
* testConditionsWithModel
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testConditionsWithModel () {
2010-11-04 00:29:15 +00:00
$this -> Model = new Article2 ();
$result = $this -> Dbo -> conditions ( array ( 'Article2.viewed >=' => 0 ), true , true , $this -> Model );
$expected = " WHERE `Article2`.`viewed` >= 0 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Article2.viewed >=' => '0' ), true , true , $this -> Model );
$expected = " WHERE `Article2`.`viewed` >= 0 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> conditions ( array ( 'Article2.viewed >=' => '1' ), true , true , $this -> Model );
$expected = " WHERE `Article2`.`viewed` >= 1 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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 " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
}
/**
* testFieldParsing method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testFieldParsing () {
2010-11-04 00:29:15 +00:00
$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`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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` " );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , 'round( (3.55441 * fooField), 3 ) AS test' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( array ( 'round( (3.55441 * fooField), 3 ) AS test' ), $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( array ( 'ROUND(`Rating`.`rate_total` / `Rating`.`rate_count`,2) AS rating' ), $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( array ( 'ROUND(Rating.rate_total / Rating.rate_count,2) AS rating' ), $result );
2010-11-04 00:29:15 +00:00
$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 " );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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'
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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` " );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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`'
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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`'
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , 'COUNT(*)' );
$expected = array ( 'COUNT(*)' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , 'name, count(*)' );
$expected = array ( '`TestModel`.`name`' , 'count(*)' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , 'count(*), name' );
$expected = array ( 'count(*)' , '`TestModel`.`name`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields (
$this -> Model , null , 'field1, field2, field3, count(*), name'
);
$expected = array (
'`TestModel`.`field1`' , '`TestModel`.`field2`' ,
'`TestModel`.`field3`' , 'count(*)' , '`TestModel`.`name`'
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , array ( 'dayofyear(now())' ));
$expected = array ( 'dayofyear(now())' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , array ( 'MAX(Model.field) As Max' ));
$expected = array ( 'MAX(`Model`.`field`) As Max' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , array ( 'Model.field AS AnotherName' ));
$expected = array ( '`Model`.`field` AS `AnotherName`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , array ( 'field AS AnotherName' ));
$expected = array ( '`field` AS `AnotherName`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$result = $this -> Dbo -> fields ( $this -> Model , null , array (
'TestModel.field AS AnotherName'
));
$expected = array ( '`TestModel`.`field` AS `AnotherName`' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
$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'
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
}
/**
* test that fields () will accept objects made from DboSource :: expression
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testFieldsWithExpression () {
2010-11-04 00:29:15 +00:00
$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 "
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-04 00:29:15 +00:00
}
2010-11-09 05:56:15 +00:00
/**
* testRenderStatement method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testRenderStatement () {
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> renderStatement ( 'select' , array (
'fields' => 'id' , 'table' => 'table' , 'conditions' => 'WHERE 1=1' ,
'alias' => '' , 'joins' => '' , 'order' => '' , 'limit' => '' , 'group' => ''
));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*SELECT\s+id\s+FROM\s+table\s+WHERE\s+1=1\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> renderStatement ( 'update' , array ( 'fields' => 'value=2' , 'table' => 'table' , 'conditions' => 'WHERE 1=1' , 'alias' => '' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*UPDATE\s+table\s+SET\s+value=2\s+WHERE\s+1=1\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> renderStatement ( 'update' , array ( 'fields' => 'value=2' , 'table' => 'table' , 'conditions' => 'WHERE 1=1' , 'alias' => 'alias' , 'joins' => '' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*UPDATE\s+table\s+AS\s+alias\s+SET\s+value=2\s+WHERE\s+1=1\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> renderStatement ( 'delete' , array ( 'fields' => 'value=2' , 'table' => 'table' , 'conditions' => 'WHERE 1=1' , 'alias' => '' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*DELETE\s+FROM\s+table\s+WHERE\s+1=1\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> renderStatement ( 'delete' , array ( 'fields' => 'value=2' , 'table' => 'table' , 'conditions' => 'WHERE 1=1' , 'alias' => 'alias' , 'joins' => '' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*DELETE\s+alias\s+FROM\s+table\s+AS\s+alias\s+WHERE\s+1=1\s*$/' , $result );
2010-11-09 05:56:15 +00:00
}
/**
* testSchema method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSchema () {
2010-11-09 05:56:15 +00:00
$Schema = new CakeSchema ();
$Schema -> tables = array ( 'table' => array (), 'anotherTable' => array ());
$result = $this -> Dbo -> dropSchema ( $Schema , 'non_existing' );
$this -> assertTrue ( empty ( $result ));
$result = $this -> Dbo -> dropSchema ( $Schema , 'table' );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*DROP TABLE IF EXISTS\s+' . $this -> Dbo -> fullTableName ( 'table' ) . ';\s*$/s' , $result );
2010-11-09 05:56:15 +00:00
}
2011-09-10 16:23:28 +00:00
/**
* testDropSchemaNoSchema method
*
* @ expectedException PHPUnit_Framework_Error
* @ return void
*/
public function testDropSchemaNoSchema () {
2013-01-23 12:45:50 +00:00
$this -> Dbo -> dropSchema ( null );
2011-10-23 19:03:29 +00:00
}
2011-09-10 16:23:28 +00:00
2010-11-09 05:56:15 +00:00
/**
* testOrderParsing method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testOrderParsing () {
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " ADDTIME(Event.time_begin, '-06:00:00') ASC " );
$expected = " ORDER BY ADDTIME(`Event`.`time_begin`, '-06:00:00') ASC " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " title, id " );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " title desc, id desc " );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( array ( " title desc, id desc " ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( array ( " title " , " id " ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( array ( array ( 'title' ), array ( 'id' )));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( array ( " Post.title " => 'asc' , " Post.id " => 'desc' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( array ( array ( " Post.title " => 'asc' , " Post.id " => 'desc' )));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( array ( " title " ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`title`\s+ASC\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( array ( array ( " title " )));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/^\s*ORDER BY\s+`title`\s+ASC\s*$/' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " Dealer.id = 7 desc, Dealer.id = 3 desc, Dealer.title asc " );
$expected = " ORDER BY `Dealer`.`id` = 7 desc, `Dealer`.`id` = 3 desc, `Dealer`.`title` asc " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( array ( " Page.name " => " ='test' DESC " ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s*ORDER BY \ s+`Page` \ .`name` \ s*='test' \ s+DESC \ s* $ / " , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " Page.name = 'view' DESC " );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s*ORDER BY \ s+`Page` \ .`name` \ s*= \ s*'view' \ s+DESC \ s* $ / " , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " (Post.views) " );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s*ORDER BY \ s+ \ (`Post` \ .`views` \ ) \ s+ASC \ s* $ / " , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " (Post.views)*Post.views " );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s*ORDER BY \ s+ \ (`Post` \ .`views` \ ) \ *`Post` \ .`views` \ s+ASC \ s* $ / " , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " (Post.views) * Post.views " );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s*ORDER BY \ s+ \ (`Post` \ .`views` \ ) \ * `Post` \ .`views` \ s+ASC \ s* $ / " , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " (Model.field1 + Model.field2) * Model.field3 " );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s*ORDER BY \ s+ \ (`Model` \ .`field1` \ + `Model` \ .`field2` \ ) \ * `Model` \ .`field3` \ s+ASC \ s* $ / " , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " Model.name+0 ASC " );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s*ORDER BY \ s+`Model` \ .`name` \ +0 \ s+ASC \ s* $ / " , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " Anuncio.destaque & 2 DESC " );
$expected = ' ORDER BY `Anuncio`.`destaque` & 2 DESC' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> order ( " 3963.191 * id " );
$expected = ' ORDER BY 3963.191 * id ASC' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
2012-03-18 16:29:39 +00:00
$result = $this -> Dbo -> order ( array ( 'Property.sale_price IS NULL' ));
$expected = ' ORDER BY `Property`.`sale_price` IS NULL ASC' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
}
/**
* testComplexSortExpression method
*
* @ return void
*/
public function testComplexSortExpression () {
$result = $this -> Dbo -> order ( array ( '(Model.field > 100) DESC' , 'Model.field ASC' ));
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( " /^ \ s*ORDER BY \ s+ \ (`Model` \ .`field` \ s+> \ s+100 \ ) \ s+DESC, \ s+`Model` \ .`field` \ s+ASC \ s* $ / " , $result );
2010-11-09 05:56:15 +00:00
}
/**
* testCalculations method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testCalculations () {
2010-12-04 19:15:32 +00:00
$this -> Model = new TestModel ();
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'count' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'COUNT(*) AS `count`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'count' , array ( 'id' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'COUNT(`id`) AS `count`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate (
$this -> Model ,
'count' ,
array ( $this -> Dbo -> expression ( 'DISTINCT id' ))
);
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'COUNT(DISTINCT id) AS `count`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'count' , array ( 'id' , 'id_count' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'COUNT(`id`) AS `id_count`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'count' , array ( 'Model.id' , 'id_count' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'COUNT(`Model`.`id`) AS `id_count`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'max' , array ( 'id' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'MAX(`id`) AS `id`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'max' , array ( 'Model.id' , 'id' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'MAX(`Model`.`id`) AS `id`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'max' , array ( '`Model`.`id`' , 'id' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'MAX(`Model`.`id`) AS `id`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'min' , array ( '`Model`.`id`' , 'id' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'MIN(`Model`.`id`) AS `id`' , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> calculate ( $this -> Model , 'min' , 'left' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'MIN(`left`) AS `left`' , $result );
2010-11-09 05:56:15 +00:00
}
/**
* testLength method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testLength () {
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> length ( 'varchar(255)' );
$expected = 255 ;
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> length ( 'int(11)' );
$expected = 11 ;
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> length ( 'float(5,3)' );
$expected = '5,3' ;
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> length ( 'decimal(5,2)' );
$expected = '5,2' ;
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> length ( " enum('test','me','now') " );
$expected = 4 ;
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> length ( " set('a','b','cd') " );
$expected = 2 ;
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> length ( false );
$this -> assertTrue ( $result === null );
$result = $this -> Dbo -> length ( 'datetime' );
$expected = null ;
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> length ( 'text' );
$expected = null ;
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
}
/**
* testBuildIndex method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testBuildIndex () {
2010-11-09 05:56:15 +00:00
$data = array (
'PRIMARY' => array ( 'column' => 'id' )
);
$result = $this -> Dbo -> buildIndex ( $data );
$expected = array ( 'PRIMARY KEY (`id`)' );
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $expected , $result );
2010-11-09 05:56:15 +00:00
$data = array (
'MyIndex' => array ( 'column' => 'id' , 'unique' => true )
);
$result = $this -> Dbo -> buildIndex ( $data );
$expected = array ( 'UNIQUE KEY `MyIndex` (`id`)' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$data = array (
'MyIndex' => array ( 'column' => array ( 'id' , 'name' ), 'unique' => true )
);
$result = $this -> Dbo -> buildIndex ( $data );
$expected = array ( 'UNIQUE KEY `MyIndex` (`id`, `name`)' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2012-09-20 03:21:26 +00:00
$data = array (
'MyFtIndex' => array ( 'column' => array ( 'name' , 'description' ), 'type' => 'fulltext' )
);
$result = $this -> Dbo -> buildIndex ( $data );
$expected = array ( 'FULLTEXT KEY `MyFtIndex` (`name`, `description`)' );
2013-02-12 14:00:57 +00:00
$this -> assertEquals ( $expected , $result );
2012-12-14 01:15:36 +00:00
2012-12-03 03:58:57 +00:00
$data = array (
'MyTextIndex' => array ( 'column' => 'text_field' , 'length' => array ( 'text_field' => 20 ))
);
$result = $this -> Dbo -> buildIndex ( $data );
$expected = array ( 'KEY `MyTextIndex` (`text_field`(20))' );
$this -> assertEquals ( $expected , $result );
$data = array (
'MyMultiTextIndex' => array ( 'column' => array ( 'text_field1' , 'text_field2' ), 'length' => array ( 'text_field1' => 20 , 'text_field2' => 20 ))
);
$result = $this -> Dbo -> buildIndex ( $data );
$expected = array ( 'KEY `MyMultiTextIndex` (`text_field1`(20), `text_field2`(20))' );
2012-09-20 03:21:26 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
}
/**
* testBuildColumn method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testBuildColumn2 () {
2010-11-09 05:56:15 +00:00
$data = array (
'name' => 'testName' ,
'type' => 'string' ,
'length' => 255 ,
'default' ,
'null' => true ,
'key'
);
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`testName` varchar(255) DEFAULT NULL' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$data = array (
'name' => 'int_field' ,
'type' => 'integer' ,
'default' => '' ,
'null' => false ,
);
$restore = $this -> Dbo -> columns ;
$this -> Dbo -> columns = array ( 'integer' => array ( 'name' => 'int' , 'limit' => '11' , 'formatter' => 'intval' ), );
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`int_field` int(11) NOT NULL' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$this -> Dbo -> fieldParameters [ 'param' ] = array (
'value' => 'COLLATE' ,
'quote' => false ,
'join' => ' ' ,
'column' => 'Collate' ,
'position' => 'beforeDefault' ,
'options' => array ( 'GOOD' , 'OK' )
);
$data = array (
'name' => 'int_field' ,
'type' => 'integer' ,
'default' => '' ,
'null' => false ,
'param' => 'BAD'
);
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`int_field` int(11) NOT NULL' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$data = array (
'name' => 'int_field' ,
'type' => 'integer' ,
'default' => '' ,
'null' => false ,
'param' => 'GOOD'
);
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`int_field` int(11) COLLATE GOOD NOT NULL' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$this -> Dbo -> columns = $restore ;
$data = array (
'name' => 'created' ,
'type' => 'timestamp' ,
'default' => 'current_timestamp' ,
'null' => false ,
2012-11-21 14:39:03 +00:00
);
2010-11-09 05:56:15 +00:00
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$data = array (
'name' => 'created' ,
'type' => 'timestamp' ,
'default' => 'CURRENT_TIMESTAMP' ,
'null' => true ,
);
$result = $this -> Dbo -> buildColumn ( $data );
$expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$data = array (
'name' => 'modified' ,
'type' => 'timestamp' ,
'null' => true ,
);
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`modified` timestamp NULL' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
$data = array (
'name' => 'modified' ,
'type' => 'timestamp' ,
'default' => null ,
'null' => true ,
);
$result = $this -> Dbo -> buildColumn ( $data );
2012-03-18 16:29:39 +00:00
$expected = '`modified` timestamp NULL' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 05:56:15 +00:00
}
2011-09-10 16:23:28 +00:00
/**
* testBuildColumnBadType method
*
* @ expectedException PHPUnit_Framework_Error
* @ return void
*/
public function testBuildColumnBadType () {
$data = array (
'name' => 'testName' ,
'type' => 'varchar(255)' ,
'default' ,
'null' => true ,
'key'
);
$this -> Dbo -> buildColumn ( $data );
}
2013-11-13 23:11:30 +00:00
/**
2013-11-13 23:20:21 +00:00
* Test `unsigned` field parameter
2013-11-13 23:11:30 +00:00
*
* @ param array $data Column data
* @ param string $expected Expected sql part
2014-05-04 07:58:53 +00:00
*
2013-11-13 23:11:30 +00:00
* @ return void
2014-05-04 07:58:53 +00:00
*
2013-11-13 23:20:21 +00:00
* @ dataProvider buildColumnUnsignedProvider
2013-11-13 23:11:30 +00:00
*/
2013-11-17 20:37:01 +00:00
public function testBuildColumnUnsigned ( $data , $expected ) {
2013-11-13 23:11:30 +00:00
$result = $this -> Dbo -> buildColumn ( $data );
$this -> assertEquals ( $expected , $result );
}
2013-11-14 00:39:16 +00:00
2013-11-13 23:11:30 +00:00
/**
2013-11-13 23:20:21 +00:00
* Data provider testBuildColumnUnsigned method
2013-11-13 23:11:30 +00:00
*
* @ return array
*/
2013-11-13 23:20:21 +00:00
public function buildColumnUnsignedProvider () {
2013-11-13 23:11:30 +00:00
return array (
//set #0
array (
array (
'name' => 'testName' ,
'type' => 'integer' ,
2013-11-13 23:33:11 +00:00
'length' => 11 ,
2013-11-13 23:11:30 +00:00
'unsigned' => true
),
2013-11-13 23:33:11 +00:00
'`testName` int(11) UNSIGNED'
2013-11-13 23:11:30 +00:00
),
//set #1
array (
array (
'name' => 'testName' ,
'type' => 'biginteger' ,
2013-11-13 23:33:11 +00:00
'length' => 20 ,
2013-11-13 23:11:30 +00:00
'unsigned' => true
),
2013-11-13 23:33:11 +00:00
'`testName` bigint(20) UNSIGNED'
2013-11-13 23:11:30 +00:00
),
//set #2
array (
array (
'name' => 'testName' ,
'type' => 'float' ,
'unsigned' => true
),
2013-11-13 23:33:11 +00:00
'`testName` float UNSIGNED'
2013-11-13 23:11:30 +00:00
),
//set #3
array (
array (
'name' => 'testName' ,
'type' => 'string' ,
2013-11-13 23:33:11 +00:00
'length' => 255 ,
2013-11-13 23:11:30 +00:00
'unsigned' => true
),
2013-11-13 23:33:11 +00:00
'`testName` varchar(255)'
2013-11-13 23:11:30 +00:00
),
//set #4
array (
array (
'name' => 'testName' ,
2013-11-13 23:33:11 +00:00
'type' => 'date' ,
2013-11-13 23:11:30 +00:00
'unsigned' => true
),
2013-11-13 23:33:11 +00:00
'`testName` date'
2013-11-13 23:11:30 +00:00
),
//set #5
array (
array (
'name' => 'testName' ,
2013-11-13 23:33:11 +00:00
'type' => 'date' ,
2013-11-13 23:11:30 +00:00
'unsigned' => false
),
2013-11-13 23:33:11 +00:00
'`testName` date'
2013-11-13 23:11:30 +00:00
),
//set #6
array (
array (
'name' => 'testName' ,
'type' => 'integer' ,
2013-11-13 23:33:11 +00:00
'length' => 11 ,
2013-11-13 23:11:30 +00:00
'unsigned' => false
),
2013-11-13 23:33:11 +00:00
'`testName` int(11)'
),
//set #7
array (
array (
'name' => 'testName' ,
2013-11-17 19:23:13 +00:00
'type' => 'decimal' ,
2013-11-13 23:33:11 +00:00
'unsigned' => true
),
'`testName` decimal UNSIGNED'
2013-11-13 23:36:27 +00:00
),
2013-11-22 14:17:25 +00:00
//set #8
2013-11-13 23:36:27 +00:00
array (
array (
'name' => 'testName' ,
2013-11-17 19:23:13 +00:00
'type' => 'decimal' ,
2013-11-13 23:36:27 +00:00
'unsigned' => true ,
'default' => 1
),
'`testName` decimal UNSIGNED DEFAULT 1'
2013-11-13 23:11:30 +00:00
)
);
}
2011-09-10 16:23:28 +00:00
2013-11-17 20:37:01 +00:00
/**
* Test getting `unsigned` field parameter from DB
2014-05-04 07:58:53 +00:00
*
2013-11-17 20:37:01 +00:00
* @ return void
*/
public function testSchemaUnsigned () {
$this -> loadFixtures ( 'Unsigned' );
$Model = ClassRegistry :: init ( 'Model' );
$Model -> setSource ( 'unsigned' );
$types = $this -> Dbo -> fieldParameters [ 'unsigned' ][ 'types' ];
$schema = $Model -> schema ();
foreach ( $types as $type ) {
2013-11-17 21:12:51 +00:00
$this -> assertArrayHasKey ( 'unsigned' , $schema [ 'u' . $type ]);
$this -> assertTrue ( $schema [ 'u' . $type ][ 'unsigned' ]);
2013-11-17 20:37:01 +00:00
$this -> assertArrayHasKey ( 'unsigned' , $schema [ $type ]);
$this -> assertFalse ( $schema [ $type ][ 'unsigned' ]);
}
2013-11-17 21:10:53 +00:00
$this -> assertArrayNotHasKey ( 'unsigned' , $schema [ 'string' ]);
2013-11-17 20:37:01 +00:00
}
2010-11-09 05:56:15 +00:00
/**
* test hasAny ()
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testHasAny () {
2011-12-12 04:07:01 +00:00
$db = $this -> Dbo -> config [ 'database' ];
2011-01-06 03:44:33 +00:00
$this -> Dbo = $this -> getMock ( 'Mysql' , array ( 'connect' , '_execute' , 'execute' , 'value' ));
2011-12-12 04:07:01 +00:00
$this -> Dbo -> config [ 'database' ] = $db ;
2010-11-09 05:56:15 +00:00
$this -> Model = $this -> getMock ( 'TestModel' , array ( 'getDataSource' ));
$this -> Model -> expects ( $this -> any ())
-> method ( 'getDataSource' )
-> will ( $this -> returnValue ( $this -> Dbo ));
$this -> Dbo -> expects ( $this -> at ( 0 )) -> method ( 'value' )
-> with ( 'harry' )
-> will ( $this -> returnValue ( " 'harry' " ));
2011-11-08 05:38:36 +00:00
$modelTable = $this -> Dbo -> fullTableName ( $this -> Model );
2010-11-09 05:56:15 +00:00
$this -> Dbo -> expects ( $this -> at ( 1 )) -> method ( 'execute' )
2012-03-18 17:08:27 +00:00
-> with ( 'SELECT COUNT(`TestModel`.`id`) AS count FROM ' . $modelTable . ' AS `TestModel` WHERE `TestModel`.`name` = \'harry\'' );
2010-11-09 05:56:15 +00:00
$this -> Dbo -> expects ( $this -> at ( 2 )) -> method ( 'execute' )
2012-03-18 17:08:27 +00:00
-> with ( 'SELECT COUNT(`TestModel`.`id`) AS count FROM ' . $modelTable . ' AS `TestModel` WHERE 1 = 1' );
2010-11-09 05:56:15 +00:00
$this -> Dbo -> hasAny ( $this -> Model , array ( 'TestModel.name' => 'harry' ));
$this -> Dbo -> hasAny ( $this -> Model , array ());
}
2010-11-09 06:08:13 +00:00
/**
* test fields generating usable virtual fields to use in query
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testVirtualFields () {
2011-02-23 16:36:41 +00:00
$this -> loadFixtures ( 'Article' , 'Comment' , 'Tag' );
2010-11-09 06:08:13 +00:00
$this -> Dbo -> virtualFieldSeparator = '__' ;
$Article = ClassRegistry :: init ( 'Article' );
2011-11-08 05:38:36 +00:00
$commentsTable = $this -> Dbo -> fullTableName ( 'comments' , false , false );
2010-11-09 06:08:13 +00:00
$Article -> virtualFields = array (
'this_moment' => 'NOW()' ,
'two' => '1 + 1' ,
2011-02-24 04:14:48 +00:00
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentsTable .
' WHERE Article.id = ' . $commentsTable . '.article_id'
2010-11-09 06:08:13 +00:00
);
$result = $this -> Dbo -> fields ( $Article );
$expected = array (
'`Article`.`id`' ,
'`Article`.`user_id`' ,
'`Article`.`title`' ,
'`Article`.`body`' ,
'`Article`.`published`' ,
'`Article`.`created`' ,
'`Article`.`updated`' ,
'(NOW()) AS `Article__this_moment`' ,
'(1 + 1) AS `Article__two`' ,
2011-02-24 04:14:48 +00:00
" (SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = ` $commentsTable `.`article_id`) AS `Article__comment_count` "
2010-11-09 06:08:13 +00:00
);
2011-02-24 04:14:48 +00:00
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
$result = $this -> Dbo -> fields ( $Article , null , array ( 'this_moment' , 'title' ));
$expected = array (
'`Article`.`title`' ,
'(NOW()) AS `Article__this_moment`' ,
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
$result = $this -> Dbo -> fields ( $Article , null , array ( 'Article.title' , 'Article.this_moment' ));
$expected = array (
'`Article`.`title`' ,
'(NOW()) AS `Article__this_moment`' ,
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
$result = $this -> Dbo -> fields ( $Article , null , array ( 'Article.this_moment' , 'Article.title' ));
$expected = array (
'`Article`.`title`' ,
'(NOW()) AS `Article__this_moment`' ,
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
$result = $this -> Dbo -> fields ( $Article , null , array ( 'Article.*' ));
$expected = array (
'`Article`.*' ,
'(NOW()) AS `Article__this_moment`' ,
'(1 + 1) AS `Article__two`' ,
2011-02-24 04:14:48 +00:00
" (SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = ` $commentsTable `.`article_id`) AS `Article__comment_count` "
2010-11-09 06:08:13 +00:00
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
$result = $this -> Dbo -> fields ( $Article , null , array ( '*' ));
$expected = array (
'*' ,
'(NOW()) AS `Article__this_moment`' ,
'(1 + 1) AS `Article__two`' ,
2011-02-24 04:14:48 +00:00
" (SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = ` $commentsTable `.`article_id`) AS `Article__comment_count` "
2010-11-09 06:08:13 +00:00
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
}
2014-10-30 18:14:04 +00:00
/**
* test find () generating usable virtual fields to use in query
*
* @ return void
*/
public function testVirtualFieldsWithSubquery () {
$this -> loadFixtures ( 'Article' , 'Comment' , 'User' , 'Tag' , 'ArticlesTag' );
$this -> Dbo -> virtualFieldSeparator = '__' ;
$Article = ClassRegistry :: init ( 'Article' );
$commentsTable = $this -> Dbo -> fullTableName ( 'comments' , false , false );
$Article -> Comment -> virtualFields = array (
'extra' => 'SELECT id FROM ' . $commentsTable . ' WHERE id = (SELECT 1)' ,
);
$conditions = array ( 'Article.id' => array ( 1 , 2 ));
$contain = array ( 'Comment.extra' );
$result = $Article -> find ( 'all' , compact ( 'conditions' , 'contain' ));
$expected = 'SELECT `Comment`.`id`, `Comment`.`article_id`, `Comment`.`user_id`, `Comment`.`comment`, `Comment`.`published`, `Comment`.`created`,' .
' `Comment`.`updated`, (SELECT id FROM comments WHERE id = (SELECT 1)) AS `Comment__extra`' .
' FROM `cake_test`.`comments` AS `Comment` WHERE `Comment`.`article_id` IN (1, 2)' ;
$test = ConnectionManager :: getDatasource ( 'test' );
$log = $test -> getLog ();
$this -> assertTextEquals ( $expected , $log [ 'log' ][ 116 ][ 'query' ]);
}
2010-11-09 06:08:13 +00:00
/**
* test conditions to generate query conditions for virtual fields
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testVirtualFieldsInConditions () {
2010-11-09 06:08:13 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2011-11-08 05:38:36 +00:00
$commentsTable = $this -> Dbo -> fullTableName ( 'comments' , false , false );
2011-02-24 06:08:11 +00:00
2010-11-09 06:08:13 +00:00
$Article -> virtualFields = array (
'this_moment' => 'NOW()' ,
'two' => '1 + 1' ,
2011-02-24 04:14:48 +00:00
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentsTable .
' WHERE Article.id = ' . $commentsTable . '.article_id'
2010-11-09 06:08:13 +00:00
);
$conditions = array ( 'two' => 2 );
$result = $this -> Dbo -> conditions ( $conditions , true , false , $Article );
$expected = '(1 + 1) = 2' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
2013-08-07 23:03:21 +00:00
$conditions = array ( 'this_moment BETWEEN ? AND ?' => array ( 1 , 2 ));
2010-11-09 06:08:13 +00:00
$expected = 'NOW() BETWEEN 1 AND 2' ;
$result = $this -> Dbo -> conditions ( $conditions , true , false , $Article );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
$conditions = array ( 'comment_count >' => 5 );
2011-02-24 04:14:48 +00:00
$expected = " (SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = ` $commentsTable `.`article_id`) > 5 " ;
2010-11-09 06:08:13 +00:00
$result = $this -> Dbo -> conditions ( $conditions , true , false , $Article );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
$conditions = array ( 'NOT' => array ( 'two' => 2 ));
$result = $this -> Dbo -> conditions ( $conditions , true , false , $Article );
$expected = 'NOT ((1 + 1) = 2)' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:08:13 +00:00
}
/**
* test that virtualFields with complex functions and aliases work .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testConditionsWithComplexVirtualFields () {
2011-02-23 16:36:41 +00:00
$Article = ClassRegistry :: init ( 'Article' , 'Comment' , 'Tag' );
2010-11-09 06:08:13 +00:00
$Article -> virtualFields = array (
'distance' => ' ACOS ( SIN ( 20 * PI () / 180 )
* SIN ( Article . latitude * PI () / 180 )
+ COS ( 20 * PI () / 180 )
* COS ( Article . latitude * PI () / 180 )
* COS (( 50 - Article . longitude ) * PI () / 180 )
) * 180 / PI () * 60 * 1.1515 * 1.609344 '
);
$conditions = array ( 'distance >=' => 20 );
$result = $this -> Dbo -> conditions ( $conditions , true , true , $Article );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/\) >= 20/' , $result );
$this -> assertRegExp ( '/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/' , $result );
$this -> assertRegExp ( '/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/' , $result );
2010-11-09 06:08:13 +00:00
}
2010-11-09 06:13:23 +00:00
/**
* test calculate to generate claculate statements on virtual fields
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testVirtualFieldsInCalculate () {
2010-11-09 06:13:23 +00:00
$Article = ClassRegistry :: init ( 'Article' );
2011-11-08 05:38:36 +00:00
$commentsTable = $this -> Dbo -> fullTableName ( 'comments' , false , false );
2010-11-09 06:13:23 +00:00
$Article -> virtualFields = array (
'this_moment' => 'NOW()' ,
'two' => '1 + 1' ,
2011-02-24 04:14:48 +00:00
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentsTable .
' WHERE Article.id = ' . $commentsTable . '.article_id'
2010-11-09 06:13:23 +00:00
);
$result = $this -> Dbo -> calculate ( $Article , 'count' , array ( 'this_moment' ));
$expected = 'COUNT(NOW()) AS `count`' ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:13:23 +00:00
$result = $this -> Dbo -> calculate ( $Article , 'max' , array ( 'comment_count' ));
2011-02-24 04:14:48 +00:00
$expected = " MAX(SELECT COUNT(*) FROM $commentsTable WHERE `Article`.`id` = ` $commentsTable `.`article_id`) AS `comment_count` " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:13:23 +00:00
}
/**
* test reading virtual fields containing newlines when recursive > 0
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testReadVirtualFieldsWithNewLines () {
2010-11-09 06:13:23 +00:00
$Article = new Article ();
$Article -> recursive = 1 ;
$Article -> virtualFields = array (
'test' => '
User . id + User . id
'
);
$result = $this -> Dbo -> fields ( $Article , null , array ());
$result = $this -> Dbo -> fields ( $Article , $Article -> alias , $result );
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/' , $result [ 7 ]);
2010-11-09 06:13:23 +00:00
}
/**
* test group to generate GROUP BY statements on virtual fields
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testVirtualFieldsInGroup () {
2010-11-09 06:13:23 +00:00
$Article = ClassRegistry :: init ( 'Article' );
$Article -> virtualFields = array (
'this_year' => 'YEAR(Article.created)'
);
$result = $this -> Dbo -> group ( 'this_year' , $Article );
$expected = " GROUP BY (YEAR(`Article`.`created`)) " ;
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-09 06:13:23 +00:00
}
/**
* test that virtualFields with complex functions and aliases work .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testFieldsWithComplexVirtualFields () {
2010-11-09 06:13:23 +00:00
$Article = new Article ();
$Article -> virtualFields = array (
'distance' => ' ACOS ( SIN ( 20 * PI () / 180 )
* SIN ( Article . latitude * PI () / 180 )
+ COS ( 20 * PI () / 180 )
* COS ( Article . latitude * PI () / 180 )
* COS (( 50 - Article . longitude ) * PI () / 180 )
) * 180 / PI () * 60 * 1.1515 * 1.609344 '
);
$fields = array ( 'id' , 'distance' );
$result = $this -> Dbo -> fields ( $Article , null , $fields );
$qs = $this -> Dbo -> startQuote ;
$qe = $this -> Dbo -> endQuote ;
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " { $qs } Article { $qe } . { $qs } id { $qe } " , $result [ 0 ]);
2011-11-16 00:07:56 +00:00
$this -> assertRegExp ( '/Article__distance/' , $result [ 1 ]);
$this -> assertRegExp ( '/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/' , $result [ 1 ]);
$this -> assertRegExp ( '/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/' , $result [ 1 ]);
2010-11-09 06:13:23 +00:00
}
2010-11-11 04:01:54 +00:00
/**
* test that execute runs queries .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testExecute () {
2010-11-11 04:01:54 +00:00
$query = 'SELECT * FROM ' . $this -> Dbo -> fullTableName ( 'articles' ) . ' WHERE 1 = 1' ;
$this -> Dbo -> took = null ;
$this -> Dbo -> affected = null ;
$result = $this -> Dbo -> execute ( $query , array ( 'log' => false ));
$this -> assertNotNull ( $result , 'No query performed! %s' );
$this -> assertNull ( $this -> Dbo -> took , 'Stats were set %s' );
$this -> assertNull ( $this -> Dbo -> affected , 'Stats were set %s' );
$result = $this -> Dbo -> execute ( $query );
$this -> assertNotNull ( $result , 'No query performed! %s' );
$this -> assertNotNull ( $this -> Dbo -> took , 'Stats were not set %s' );
$this -> assertNotNull ( $this -> Dbo -> affected , 'Stats were not set %s' );
}
/**
* test a full example of using virtual fields
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testVirtualFieldsFetch () {
2010-11-11 04:01:54 +00:00
$this -> loadFixtures ( 'Article' , 'Comment' );
$Article = ClassRegistry :: init ( 'Article' );
$Article -> virtualFields = array (
'comment_count' => 'SELECT COUNT(*) FROM ' . $this -> Dbo -> fullTableName ( 'comments' ) .
' WHERE Article.id = ' . $this -> Dbo -> fullTableName ( 'comments' ) . '.article_id'
);
$conditions = array ( 'comment_count >' => 2 );
2012-09-15 10:06:02 +00:00
$query = 'SELECT ' . implode ( ',' , $this -> Dbo -> fields ( $Article , null , array ( 'id' , 'comment_count' ))) .
2012-03-18 17:08:27 +00:00
' FROM ' . $this -> Dbo -> fullTableName ( $Article ) . ' Article ' . $this -> Dbo -> conditions ( $conditions , true , true , $Article );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> fetchAll ( $query );
$expected = array ( array (
'Article' => array ( 'id' => 1 , 'comment_count' => 4 )
));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-11 04:01:54 +00:00
}
/**
* test reading complex virtualFields with subqueries .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testVirtualFieldsComplexRead () {
2010-12-04 18:52:38 +00:00
$this -> loadFixtures ( 'DataTest' , 'Article' , 'Comment' , 'User' , 'Tag' , 'ArticlesTag' );
2010-11-11 04:01:54 +00:00
$Article = ClassRegistry :: init ( 'Article' );
$commentTable = $this -> Dbo -> fullTableName ( 'comments' );
$Article = ClassRegistry :: init ( 'Article' );
$Article -> virtualFields = array (
2011-05-16 22:49:00 +00:00
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable .
2010-11-11 04:01:54 +00:00
' 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 );
$DataTest = ClassRegistry :: init ( 'DataTest' );
$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 );
}
/**
* testIntrospectType method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testIntrospectType () {
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( 0 ));
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( 2 ));
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( '2' ));
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( '2.2' ));
$this -> assertEquals ( 'float' , $this -> Dbo -> introspectType ( 2.2 ));
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( 'stringme' ));
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( '0stringme' ));
2010-11-11 04:01:54 +00:00
$data = array ( 2.2 );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'float' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( '2.2' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'float' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2 );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( '2' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 'string' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2.2 , '2.2' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'float' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2 , '2' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 'string one' , 'string two' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( '2.2' , 3 );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( '2.2' , '0stringme' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2.2 , 3 );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2.2 , '0stringme' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2 , 'stringme' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2 , '2.2' , 'stringgme' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'string' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2 , '2.2' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
$data = array ( 2 , 2.2 );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'integer' , $this -> Dbo -> introspectType ( $data ));
2010-11-11 04:01:54 +00:00
2012-03-18 17:08:27 +00:00
// null
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( null , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'NULL' , $result );
2010-11-11 04:01:54 +00:00
// EMPTY STRING
$result = $this -> Dbo -> value ( '' , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '0' " , $result );
2010-11-11 04:01:54 +00:00
// BOOLEAN
$result = $this -> Dbo -> value ( 'true' , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 'false' , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( true , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( false , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '0' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 1 , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 0 , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '0' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 'abc' , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 1.234 , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '1.234e05' , 'boolean' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1' " , $result );
2010-11-11 04:01:54 +00:00
// NUMBERS
$result = $this -> Dbo -> value ( 123 , 'integer' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 123 , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '123' , 'integer' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '123' , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '0123' , 'integer' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '0123' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '0x123ABC' , 'integer' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '0x123ABC' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '0x123' , 'integer' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '0x123' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 1.234 , 'float' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 1.234 , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '1.234' , 'float' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '1.234' , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( ' 1.234 ' , 'float' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " ' 1.234 ' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '1.234e05' , 'float' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1.234e05' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '1.234e+5' , 'float' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1.234e+5' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( '1,234' , 'float' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1,234' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 'FFF' , 'integer' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " 'FFF' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 'abc' , 'integer' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " 'abc' " , $result );
2010-11-11 04:01:54 +00:00
// STRINGS
$result = $this -> Dbo -> value ( '123' , 'string' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '123' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 123 , 'string' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '123' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 1.234 , 'string' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " '1.234' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 'abc' , 'string' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " 'abc' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( ' abc ' , 'string' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " ' abc ' " , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> value ( 'a bc' , 'string' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( " 'a bc' " , $result );
2010-11-11 04:01:54 +00:00
}
/**
* testRealQueries method
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testRealQueries () {
2010-12-04 18:52:38 +00:00
$this -> loadFixtures ( 'Apple' , 'Article' , 'User' , 'Comment' , 'Tag' , 'Sample' , 'ArticlesTag' );
2010-11-11 04:01:54 +00:00
$Apple = ClassRegistry :: init ( 'Apple' );
$Article = ClassRegistry :: init ( 'Article' );
$result = $this -> Dbo -> rawQuery ( 'SELECT color, name FROM ' . $this -> Dbo -> fullTableName ( 'apples' ));
$this -> assertTrue ( ! empty ( $result ));
$result = $this -> Dbo -> fetchRow ( $result );
2011-11-08 05:38:36 +00:00
$expected = array ( $this -> Dbo -> fullTableName ( 'apples' , false , false ) => array (
2010-11-11 04:01:54 +00:00
'color' => 'Red 1' ,
'name' => 'Red Apple 1'
));
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> fetchAll ( 'SELECT name FROM ' . $this -> Dbo -> fullTableName ( 'apples' ) . ' ORDER BY id' );
$expected = array (
2011-11-08 05:38:36 +00:00
array ( $this -> Dbo -> fullTableName ( 'apples' , false , false ) => array ( 'name' => 'Red Apple 1' )),
array ( $this -> Dbo -> fullTableName ( 'apples' , false , false ) => array ( 'name' => 'Bright Red Apple' )),
array ( $this -> Dbo -> fullTableName ( 'apples' , false , false ) => array ( 'name' => 'green blue' )),
array ( $this -> Dbo -> fullTableName ( 'apples' , false , false ) => array ( 'name' => 'Test Name' )),
array ( $this -> Dbo -> fullTableName ( 'apples' , false , false ) => array ( 'name' => 'Blue Green' )),
array ( $this -> Dbo -> fullTableName ( 'apples' , false , false ) => array ( 'name' => 'My new apple' )),
array ( $this -> Dbo -> fullTableName ( 'apples' , false , false ) => array ( 'name' => 'Some odd color' ))
2010-11-11 04:01:54 +00:00
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-11 04:01:54 +00:00
2011-11-08 05:38:36 +00:00
$result = $this -> Dbo -> field ( $this -> Dbo -> fullTableName ( 'apples' , false , false ), 'SELECT color, name FROM ' . $this -> Dbo -> fullTableName ( 'apples' ) . ' ORDER BY id' );
2010-11-11 04:01:54 +00:00
$expected = array (
'color' => 'Red 1' ,
'name' => 'Red Apple 1'
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-11 04:01:54 +00:00
$Apple -> unbindModel ( array (), false );
$result = $this -> Dbo -> read ( $Apple , array (
'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' ))
);
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $expected , $result );
2010-11-11 04:01:54 +00:00
$result = $this -> Dbo -> read ( $Article , array (
'fields' => array ( 'id' , 'user_id' , 'title' ),
'conditions' => null ,
'recursive' => 1
));
$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 ));
}
2011-01-04 04:34:22 +00:00
/**
* @ expectedException MissingConnectionException
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testExceptionOnBrokenConnection () {
2013-01-23 12:45:50 +00:00
new Mysql ( array (
2011-01-04 04:34:22 +00:00
'driver' => 'mysql' ,
'host' => 'imaginary_host' ,
'login' => 'mark' ,
'password' => 'inyurdatabase' ,
'database' => 'imaginary'
));
}
2011-12-14 02:26:03 +00:00
/**
* testStatements method
*
* @ return void
*/
public function testUpdateStatements () {
$this -> loadFixtures ( 'Article' , 'User' );
$test = ConnectionManager :: getDatasource ( 'test' );
2011-12-14 03:11:06 +00:00
$db = $test -> config [ 'database' ];
2011-12-14 02:26:03 +00:00
$this -> Dbo = $this -> getMock ( 'Mysql' , array ( 'execute' ), array ( $test -> config ));
$this -> Dbo -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
2011-12-14 03:11:06 +00:00
-> with ( " UPDATE ` $db `.`articles` SET `field1` = 'value1' WHERE 1 = 1 " );
2011-12-14 02:26:03 +00:00
$this -> Dbo -> expects ( $this -> at ( 1 )) -> method ( 'execute' )
2012-03-03 23:36:21 +00:00
-> with ( " UPDATE ` $db `.`articles` AS `Article` LEFT JOIN ` $db `.`users` AS `User` ON " .
2011-12-14 03:11:06 +00:00
" (`Article`.`user_id` = `User`.`id`) " .
2011-12-14 02:26:03 +00:00
" SET `Article`.`field1` = 2 WHERE 2=2 " );
$this -> Dbo -> expects ( $this -> at ( 2 )) -> method ( 'execute' )
2011-12-14 03:11:06 +00:00
-> with ( " UPDATE ` $db `.`articles` AS `Article` LEFT JOIN ` $db `.`users` AS `User` ON " .
" (`Article`.`user_id` = `User`.`id`) " .
2011-12-14 02:26:03 +00:00
" SET `Article`.`field1` = 'value' WHERE `index` = 'val' " );
$Article = new Article ();
$this -> Dbo -> update ( $Article , array ( 'field1' ), array ( 'value1' ));
$this -> Dbo -> update ( $Article , array ( 'field1' ), array ( '2' ), '2=2' );
$this -> Dbo -> update ( $Article , array ( 'field1' ), array ( " 'value' " ), array ( 'index' => 'val' ));
}
/**
* Test deletes with a mock .
*
* @ return void
*/
public function testDeleteStatements () {
$this -> loadFixtures ( 'Article' , 'User' );
$test = ConnectionManager :: getDatasource ( 'test' );
2011-12-14 03:11:06 +00:00
$db = $test -> config [ 'database' ];
2011-12-14 02:26:03 +00:00
$this -> Dbo = $this -> getMock ( 'Mysql' , array ( 'execute' ), array ( $test -> config ));
$this -> Dbo -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
2011-12-14 03:11:06 +00:00
-> with ( " DELETE FROM ` $db `.`articles` WHERE 1 = 1 " );
2011-12-14 02:26:03 +00:00
$this -> Dbo -> expects ( $this -> at ( 1 )) -> method ( 'execute' )
2012-03-03 23:36:21 +00:00
-> with ( " DELETE `Article` FROM ` $db `.`articles` AS `Article` LEFT JOIN ` $db `.`users` AS `User` " .
2011-12-14 03:11:06 +00:00
" ON (`Article`.`user_id` = `User`.`id`) " .
2011-12-14 02:26:03 +00:00
" WHERE 1 = 1 " );
$this -> Dbo -> expects ( $this -> at ( 2 )) -> method ( 'execute' )
2011-12-14 03:11:06 +00:00
-> with ( " DELETE `Article` FROM ` $db `.`articles` AS `Article` LEFT JOIN ` $db `.`users` AS `User` " .
" ON (`Article`.`user_id` = `User`.`id`) " .
2011-12-14 02:26:03 +00:00
" WHERE 2=2 " );
$Article = new Article ();
$this -> Dbo -> delete ( $Article );
$this -> Dbo -> delete ( $Article , true );
$this -> Dbo -> delete ( $Article , '2=2' );
}
2011-12-14 07:35:20 +00:00
/**
* Test truncate with a mock .
*
* @ return void
*/
public function testTruncateStatements () {
$this -> loadFixtures ( 'Article' , 'User' );
$db = ConnectionManager :: getDatasource ( 'test' );
$schema = $db -> config [ 'database' ];
$Article = new Article ();
$this -> Dbo = $this -> getMock ( 'Mysql' , array ( 'execute' ), array ( $db -> config ));
$this -> Dbo -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
-> with ( " TRUNCATE TABLE ` $schema `.`articles` " );
$this -> Dbo -> truncate ( $Article );
$this -> Dbo -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
-> with ( " TRUNCATE TABLE ` $schema `.`articles` " );
$this -> Dbo -> truncate ( 'articles' );
// #2355: prevent duplicate prefix
$this -> Dbo -> config [ 'prefix' ] = 'tbl_' ;
$Article -> tablePrefix = 'tbl_' ;
$this -> Dbo -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
-> with ( " TRUNCATE TABLE ` $schema `.`tbl_articles` " );
$this -> Dbo -> truncate ( $Article );
$this -> Dbo -> expects ( $this -> at ( 0 )) -> method ( 'execute' )
-> with ( " TRUNCATE TABLE ` $schema `.`tbl_articles` " );
$this -> Dbo -> truncate ( 'articles' );
}
2012-04-01 04:01:11 +00:00
/**
* Test nested transaction
*
* @ return void
*/
public function testNestedTransaction () {
2012-04-27 00:53:18 +00:00
$nested = $this -> Dbo -> useNestedTransactions ;
$this -> Dbo -> useNestedTransactions = true ;
2012-04-27 00:19:03 +00:00
if ( $this -> Dbo -> nestedTransactionSupported () === false ) {
2012-04-27 00:53:18 +00:00
$this -> Dbo -> useNestedTransactions = $nested ;
2012-04-25 01:42:19 +00:00
$this -> skipIf ( true , 'The MySQL server do not support nested transaction' );
}
2012-04-01 04:21:12 +00:00
2012-04-25 01:42:19 +00:00
$this -> loadFixtures ( 'Inno' );
$model = ClassRegistry :: init ( 'Inno' );
2012-04-01 04:01:11 +00:00
$model -> hasOne = $model -> hasMany = $model -> belongsTo = $model -> hasAndBelongsToMany = array ();
$model -> cacheQueries = false ;
$this -> Dbo -> cacheMethods = false ;
$this -> assertTrue ( $this -> Dbo -> begin ());
$this -> assertNotEmpty ( $model -> read ( null , 1 ));
$this -> assertTrue ( $this -> Dbo -> begin ());
$this -> assertTrue ( $model -> delete ( 1 ));
$this -> assertEmpty ( $model -> read ( null , 1 ));
$this -> assertTrue ( $this -> Dbo -> rollback ());
$this -> assertNotEmpty ( $model -> read ( null , 1 ));
$this -> assertTrue ( $this -> Dbo -> begin ());
$this -> assertTrue ( $model -> delete ( 1 ));
$this -> assertEmpty ( $model -> read ( null , 1 ));
$this -> assertTrue ( $this -> Dbo -> commit ());
$this -> assertEmpty ( $model -> read ( null , 1 ));
$this -> assertTrue ( $this -> Dbo -> rollback ());
$this -> assertNotEmpty ( $model -> read ( null , 1 ));
2012-04-25 01:42:19 +00:00
2012-04-27 00:53:18 +00:00
$this -> Dbo -> useNestedTransactions = $nested ;
2012-04-01 04:01:11 +00:00
}
2008-05-30 11:40:08 +00:00
}