123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611 |
- <?php
- /**
- * Lithium: the most rad php framework
- *
- * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
- * @license http://opensource.org/licenses/bsd-license.php The BSD License
- */
- namespace lithium\tests\cases\data\source;
- use lithium\data\model\Query;
- use lithium\data\entity\Record;
- use lithium\data\collection\RecordSet;
- use lithium\tests\mocks\data\model\MockDatabase;
- use lithium\tests\mocks\data\model\MockDatabasePost;
- use lithium\tests\mocks\data\model\MockDatabaseComment;
- use lithium\tests\mocks\data\model\MockDatabaseTagging;
- use lithium\tests\mocks\data\model\MockDatabasePostRevision;
- use lithium\tests\mocks\data\model\mock_database\MockResult;
- class DatabaseTest extends \lithium\test\Unit {
- public $db = null;
- protected $_configs = array();
- protected $_model = 'lithium\tests\mocks\data\model\MockDatabasePost';
- protected $_gallery = 'lithium\tests\mocks\data\model\MockGallery';
- protected $_imageTag = 'lithium\tests\mocks\data\model\MockImageTag';
- public function setUp() {
- MockDatabasePost::config();
- MockDatabaseComment::config();
- MockDatabaseTagging::config();
- MockDatabasePostRevision::config();
- $this->db = new MockDatabase();
- MockDatabasePost::$connection = $this->db;
- MockDatabaseComment::$connection = $this->db;
- MockDatabaseTagging::$connection = $this->db;
- MockDatabasePostRevision::$connection = $this->db;
- }
- public function tearDown() {
- $this->db->logs = array();
- $this->db->return = array();
- }
- public function testDefaultConfig() {
- $expected = array(
- 'persistent' => true,
- 'host' => 'localhost',
- 'login' => 'root',
- 'password' => '',
- 'database' => null,
- 'encoding' => null,
- 'dsn' => null,
- 'options' => array(),
- 'autoConnect' => true,
- 'init' => true
- );
- $result = $this->db->testConfig();
- $this->assertEqual($expected, $result);
- }
- public function testModifyConfig() {
- $db = new MockDatabase(array('host' => '127.0.0.1', 'login' => 'bob'));
- $expected = array(
- 'persistent' => true,
- 'host' => '127.0.0.1',
- 'login' => 'bob',
- 'password' => '',
- 'database' => null,
- 'encoding' => null,
- 'dsn' => null,
- 'options' => array(),
- 'autoConnect' => true,
- 'init' => true
- );
- $result = $db->testConfig();
- $this->assertEqual($expected, $result);
- }
- public function testName() {
- $result = $this->db->name("name");
- $this->assertEqual("{name}", $result);
- $result = $this->db->name("Model.name");
- $this->assertEqual("{Model}.{name}", $result);
- }
- public function testValueWithSchema() {
- $result = $this->db->value(null);
- $this->assertIdentical('NULL', $result);
- $result = $this->db->value('string', array('type' => 'string'));
- $this->assertEqual("'string'", $result);
- $result = $this->db->value('true', array('type' => 'boolean'));
- $this->assertIdentical(1, $result);
- $result = $this->db->value('1', array('type' => 'integer'));
- $this->assertIdentical(1, $result);
- $result = $this->db->value('1.1', array('type' => 'float'));
- $this->assertIdentical(1.1, $result);
- $result = $this->db->value('1', array('type' => 'string'));
- $this->assertIdentical("'1'", $result);
- $result = $this->db->value((object) 'CURRENT_TIMESTAMP', array('type' => 'timestamp'));
- $this->assertIdentical('CURRENT_TIMESTAMP', $result);
- $result = $this->db->value((object) 'REGEXP "^fo$"');
- $this->assertIdentical('REGEXP "^fo$"', $result);
- $date = date_default_timezone_get();
- date_default_timezone_set('UTC');
- $result = $this->db->value('Hello World', array('type' => 'timestamp'));
- $this->assertIdentical("'1970-01-01 00:00:00'", $result);
- date_default_timezone_set($date);
- $result = $this->db->value('2012-05-25 22:44:00', array('type' => 'timestamp'));
- $this->assertIdentical("'2012-05-25 22:44:00'", $result);
- $result = $this->db->value('2012-05-25', array('type' => 'date'));
- $this->assertIdentical("'2012-05-25'", $result);
- $result = $this->db->value('now', array('type' => 'timestamp'));
- $this->assertPattern("/^'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'/", $result);
- $result = $this->db->value('now', array('type' => 'date'));
- $this->assertPattern("/^'\d{4}-\d{2}-\d{2}'/", $result);
- $result = $this->db->value('now', array('type' => 'time'));
- $this->assertPattern("/^'\d{2}:\d{2}:\d{2}'/", $result);
- }
- public function testValueByIntrospect() {
- $result = $this->db->value("string");
- $this->assertIdentical("'string'", $result);
- $result = $this->db->value(true);
- $this->assertIdentical(1, $result);
- $result = $this->db->value('1');
- $this->assertIdentical(1, $result);
- $result = $this->db->value('1.1');
- $this->assertIdentical(1.1, $result);
- }
- public function testSchema() {
- $model = $this->_model;
- $model::config();
- $modelName = '';
- $expected = array($modelName => array('id', 'author_id', 'title', 'created'));
- $result = $this->db->schema(new Query(compact('model')));
- $this->assertEqual($expected, $result);
- $query = new Query(compact('model') + array('fields' => '*'));
- $result = $this->db->schema($query);
- $this->assertEqual($expected, $result);
- $query = new Query(array(
- 'model' => $this->_model,
- 'fields' => array('MockDatabaseComment'),
- 'with' => array('MockDatabaseComment')
- ));
- $expected = array(
- '' => array('id'),
- 'MockDatabaseComment' => array(
- 'id', 'post_id', 'author_id', 'body', 'created'
- )
- );
- $result = $this->db->schema($query);
- $this->assertEqual($expected, $result);
- $options = array(
- 'model' => $this->_model,
- 'with' => 'MockDatabaseComment'
- );
- $options['fields'] = array('id', 'title');
- $result = $this->db->schema(new Query($options));
- $expected = array($modelName => $options['fields']);
- $this->assertEqual($expected, $result);
- $options['fields'] = array(
- 'MockDatabasePost.id',
- 'MockDatabasePost.title',
- 'MockDatabaseComment.body'
- );
- $result = $this->db->schema(new Query($options));
- $expected = array(
- $modelName => array('id', 'title'),
- 'MockDatabaseComment' => array('body')
- );
- $this->assertEqual($expected, $result);
- $options['fields'] = array(
- 'MockDatabasePost' => array('id', 'title'),
- 'MockDatabaseComment' => array('body', 'created')
- );
- $result = $this->db->schema(new Query($options));
- $expected = array(
- $modelName => array('id', 'title'),
- 'MockDatabaseComment' => array('body', 'created')
- );
- $this->assertEqual($expected, $result);
- $options['fields'] = array('MockDatabasePost', 'MockDatabaseComment');
- $result = $this->db->schema(new Query($options));
- $expected = array(
- $modelName => array('id', 'author_id', 'title', 'created'),
- 'MockDatabaseComment' => array('id', 'post_id', 'author_id', 'body', 'created')
- );
- $this->assertEqual($expected, $result);
- }
- public function testSchemaFromManualFieldList() {
- $fields = array('id', 'name', 'created');
- $result = $this->db->schema(new Query(compact('fields')));
- $this->assertEqual(array('' => $fields), $result);
- }
- public function testSimpleQueryRender() {
- $fieldList = '{MockDatabasePost}.{id}, {MockDatabasePost}.{title},';
- $fieldList .= ' {MockDatabasePost}.{created}';
- $table = '{mock_database_posts} AS {MockDatabasePost}';
- $result = $this->db->renderCommand(new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'fields' => array('id', 'title', 'created')
- )));
- $this->assertEqual("SELECT {$fieldList} FROM {$table};", $result);
- $result = $this->db->renderCommand(new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'fields' => array('id', 'title', 'created'),
- 'limit' => 1
- )));
- $this->assertEqual("SELECT {$fieldList} FROM {$table} LIMIT 1;", $result);
- $result = $this->db->renderCommand(new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'fields' => array('id', 'title', 'created'),
- 'limit' => 1,
- 'conditions' => 'Post.id = 2'
- )));
- $this->assertEqual("SELECT {$fieldList} FROM {$table} WHERE Post.id = 2 LIMIT 1;", $result);
- }
- public function testNestedQueryConditions() {
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'fields' => array('MockDatabasePost.title', 'MockDatabasePost.body'),
- 'conditions' => array('Post.id' => new Query(array(
- 'type' => 'read',
- 'fields' => array('post_id'),
- 'model' => 'lithium\tests\mocks\data\model\MockDatabaseTagging',
- 'conditions' => array('MockDatabaseTag.tag' => array('foo', 'bar', 'baz'))
- )))
- ));
- $result = $this->db->renderCommand($query);
- $expected = "SELECT {MockDatabasePost}.{title}, {MockDatabasePost}.{body} FROM";
- $expected .= " {mock_database_posts} AS {MockDatabasePost} WHERE {Post}.{id} IN";
- $expected .= " (SELECT {MockDatabaseTagging}.{post_id} FROM {mock_database_taggings} AS ";
- $expected .= "{MockDatabaseTagging} WHERE {MockDatabaseTag}.{tag} IN";
- $expected .= " ('foo', 'bar', 'baz'));";
- $this->assertEqual($expected, $result);
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'fields' => array('MockDatabasePost.title', 'MockDatabasePost.body'),
- 'conditions' => array('Post.id' => array('!=' => new Query(array(
- 'type' => 'read',
- 'fields' => array('post_id'),
- 'model' => 'lithium\tests\mocks\data\model\MockDatabaseTagging',
- 'conditions' => array('MockDatabaseTag.tag' => array('foo', 'bar', 'baz'))
- ))))
- ));
- $result = $this->db->renderCommand($query);
- $expected = "SELECT {MockDatabasePost}.{title}, {MockDatabasePost}.{body} FROM";
- $expected .= " {mock_database_posts} AS {MockDatabasePost} WHERE ({Post}.{id} NOT IN";
- $expected .= " (SELECT {MockDatabaseTagging}.{post_id} FROM {mock_database_taggings} AS ";
- $expected .= "{MockDatabaseTagging} WHERE {MockDatabaseTag}.{tag} IN ";
- $expected .= "('foo', 'bar', 'baz')));";
- $this->assertEqual($expected, $result);
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'conditions' => array(
- 'or' => array(
- '{MockDatabasePost}.{id}' => 'value1',
- '{MockDatabasePost}.{title}' => 'value2'
- )
- )
- ));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ";
- $sql .= "({MockDatabasePost}.{id} = 'value1' OR {MockDatabasePost}.{title} = 'value2');";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- public function testCastingQueryConditionsWithSchemaWithAlias() {
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'conditions' => array(
- 'MockDatabasePost.title' => '007'
- )
- ));
- $result = $this->db->renderCommand($query);
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ";
- $sql .= "{MockDatabasePost}.{title} = '007';";
- $this->assertEqual($sql, $result);
- }
- public function testQueryJoin() {
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'fields' => array('MockDatabasePost.title', 'MockDatabasePost.body'),
- 'conditions' => array('MockDatabaseTag.tag' => array('foo', 'bar', 'baz')),
- 'joins' => array(new Query(array(
- 'model' => 'lithium\tests\mocks\data\model\MockDatabaseTag',
- 'constraints' => '{MockDatabaseTagging}.{tag_id} = {MockDatabaseTag}.{id}'
- )))
- ));
- $result = $this->db->renderCommand($query);
- $expected = "SELECT {MockDatabasePost}.{title}, {MockDatabasePost}.{body} FROM";
- $expected .= " {mock_database_posts} AS {MockDatabasePost} JOIN {mock_database_tags} AS";
- $expected .= " {MockDatabaseTag} ON ";
- $expected .= "{MockDatabaseTagging}.{tag_id} = {MockDatabaseTag}.{id}";
- $expected .= " WHERE {MockDatabaseTag}.{tag} IN ('foo', 'bar', 'baz');";
- $this->assertEqual($expected, $result);
- }
- public function testItem() {
- $data = array('title' => 'new post', 'content' => 'This is a new post.');
- $item = $this->db->item($this->_model, $data);
- $result = $item->data();
- $this->assertEqual($data, $result);
- }
- public function testCreate() {
- $entity = new Record(array(
- 'model' => $this->_model,
- 'data' => array('title' => 'new post', 'body' => 'the body')
- ));
- $query = new Query(compact('entity') + array(
- 'type' => 'create',
- 'model' => $this->_model
- ));
- $hash = $query->export($this->db);
- ksort($hash);
- $expected = sha1(serialize($hash));
- $result = $this->db->create($query);
- $this->assertTrue($result);
- $result = $query->entity()->id;
- $this->assertEqual($expected, $result);
- $expected = "INSERT INTO {mock_database_posts} ({title}, {body})";
- $expected .= " VALUES ('new post', 'the body');";
- $result = $this->db->sql;
- $this->assertEqual($expected, $result);
- }
- public function testCreateGenericSyntax() {
- $entity = new Record(array(
- 'model' => $this->_model,
- 'data' => array('data' => array('title' => 'new post', 'body' => 'the body'))
- ));
- $query = new Query(compact('entity') + array(
- 'type' => 'create',
- 'model' => $this->_model
- ));
- $hash = $query->export($this->db);
- ksort($hash);
- $expected = sha1(serialize($hash));
- $result = $this->db->create($query);
- $this->assertTrue($result);
- $result = $query->entity()->id;
- $this->assertEqual($expected, $result);
- $expected = "INSERT INTO {mock_database_posts} ({title}, {body})";
- $expected .= " VALUES ('new post', 'the body');";
- $result = $this->db->sql;
- $this->assertEqual($expected, $result);
- }
- public function testCreateWithValueBySchema() {
- $entity = new Record(array(
- 'model' => $this->_model,
- 'data' => array('title' => '007', 'body' => 'the body')
- ));
- $query = new Query(compact('entity') + array(
- 'type' => 'create',
- 'model' => $this->_model
- ));
- $hash = $query->export($this->db);
- ksort($hash);
- $expected = sha1(serialize($hash));
- $result = $this->db->create($query);
- $this->assertTrue($result);
- $result = $query->entity()->id;
- $this->assertEqual($expected, $result);
- $expected = "INSERT INTO {mock_database_posts} ({title}, {body})";
- $expected .= " VALUES ('007', 'the body');";
- $result = $this->db->sql;
- $this->assertEqual($expected, $result);
- }
- public function testCreateWithKey() {
- $entity = new Record(array(
- 'model' => $this->_model,
- 'data' => array('id' => 1, 'title' => 'new post', 'body' => 'the body')
- ));
- $query = new Query(compact('entity') + array('type' => 'create'));
- $expected = 1;
- $result = $this->db->create($query);
- $this->assertTrue($result);
- $result = $query->entity()->id;
- $this->assertEqual($expected, $result);
- $expected = "INSERT INTO {mock_database_posts} ({id}, {title}, {body})";
- $expected .= " VALUES (1, 'new post', 'the body');";
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testReadWithQueryStringReturnResource() {
- $result = $this->db->read('SELECT * from mock_database_posts AS MockDatabasePost;', array(
- 'return' => 'resource'
- ));
- $this->assertTrue($result);
- $expected = "SELECT * from mock_database_posts AS MockDatabasePost;";
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testCalculation() {
- $options = array('type' => 'read', 'model' => $this->_model);
- $this->expectException('Undefined offset: 0');
- $result = $this->db->calculation('count', new Query($options), $options);
- $expected = 'SELECT COUNT(*) as count FROM {mock_database_posts} AS {MockDatabasePost};';
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testReadWithQueryStringReturnArrayWithSchema() {
- $result = $this->db->read('SELECT * FROM {:table} WHERE user_id = {:uid};', array(
- 'table' => 'mock_database_posts',
- 'uid' => '3',
- 'schema' => array('id', 'title', 'text')
- ));
- $expected = 'SELECT * FROM \'mock_database_posts\' WHERE user_id = 3;';
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testReadWithQueryObjectRecordSet() {
- $query = new Query(array('type' => 'read', 'model' => $this->_model));
- $result = $this->db->read($query);
- $this->assertTrue($result instanceof RecordSet);
- $expected = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost};";
- $result = $this->db->sql;
- $this->assertEqual($expected, $result);
- }
- public function testReadWithQueryObjectArray() {
- $query = new Query(array('type' => 'read', 'model' => $this->_model));
- $result = $this->db->read($query, array('return' => 'array'));
- $this->assertTrue(is_array($result));
- $expected = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost};";
- $result = $this->db->sql;
- $this->assertEqual($expected, $result);
- }
- public function testUpdate() {
- $entity = new Record(array(
- 'model' => $this->_model,
- 'data' => array('id' => 1, 'title' => 'new post', 'body' => 'the body'),
- 'exists' => true
- ));
- $query = new Query(compact('entity') + array('type' => 'update'));
- $result = $this->db->update($query);
- $this->assertTrue($result);
- $this->assertEqual(1, $query->entity()->id);
- $expected = "UPDATE {mock_database_posts} SET";
- $expected .= " {id} = 1, {title} = 'new post', {body} = 'the body' WHERE {id} = 1;";
- $this->assertEqual($expected, $this->db->sql);
- $entity = new Record(array(
- 'model' => $this->_model,
- 'data' => array('id' => 2, 'count' => (object) '{count} + 1'),
- 'exists' => true
- ));
- $query = new Query(compact('entity') + array('type' => 'update'));
- $result = $this->db->update($query);
- $this->assertTrue($result);
- $this->assertEqual(2, $query->entity()->id);
- $expected = "UPDATE {mock_database_posts} SET";
- $expected .= " {id} = 2, {count} = {count} + 1 WHERE {id} = 2;";
- $this->assertEqual($expected, $this->db->sql);
- $query = new Query(array(
- 'type' => 'update',
- 'data' => array('modified' => (object) 'NOW()'),
- 'model' => $this->_model
- ));
- $sql = "UPDATE {mock_database_posts} SET {modified} = NOW();";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- public function testUpdateWithValueBySchema() {
- $entity = new Record(array(
- 'model' => $this->_model,
- 'data' => array('id' => 1, 'title' => '007', 'body' => 'the body'),
- 'exists' => true
- ));
- $query = new Query(compact('entity') + array('type' => 'update'));
- $result = $this->db->update($query);
- $this->assertTrue($result);
- $this->assertEqual(1, $query->entity()->id);
- $expected = "UPDATE {mock_database_posts} SET";
- $expected .= " {id} = 1, {title} = '007', {body} = 'the body' WHERE {id} = 1;";
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testDelete() {
- $entity = new Record(array(
- 'model' => $this->_model,
- 'data' => array('id' => 1, 'title' => 'new post', 'body' => 'the body'),
- 'exists' => true
- ));
- $query = new Query(compact('entity') + array('type' => 'delete'));
- $this->assertTrue($entity->exists());
- $this->assertTrue($this->db->delete($query));
- $this->assertEqual(1, $query->entity()->id);
- $expected = "DELETE FROM {mock_database_posts} WHERE {id} = 1;";
- $this->assertEqual($expected, $this->db->sql);
- $this->assertFalse($entity->exists());
- }
- public function testOrder() {
- $query = new Query(array('model' => $this->_model));
- $result = $this->db->order("foo_bar", $query);
- $expected = 'ORDER BY foo_bar ASC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order("title", $query);
- $expected = 'ORDER BY {MockDatabasePost}.{title} ASC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order("title", $query);
- $expected = 'ORDER BY {MockDatabasePost}.{title} ASC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order(array("title"), $query);
- $expected = 'ORDER BY {MockDatabasePost}.{title} ASC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order(array("title" => "desc"), $query);
- $expected = 'ORDER BY {MockDatabasePost}.{title} desc';
- $this->assertEqual($expected, $result);
- $result = $this->db->order(array("title" => "dasc"), $query);
- $expected = 'ORDER BY {MockDatabasePost}.{title} ASC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order(array("title" => array()), $query);
- $expected = 'ORDER BY {MockDatabasePost}.{title} ASC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order(array('author_id', "title" => "DESC"), $query);
- $expected = 'ORDER BY {MockDatabasePost}.{author_id} ASC, {MockDatabasePost}.{title} DESC';
- $this->assertEqual($expected, $result);
- }
- public function testOrderOnRelated() {
- $query = new Query(array(
- 'model' => $this->_model,
- 'with' => array('MockDatabaseComment')
- ));
- $result = $this->db->order('MockDatabaseComment.created DESC', $query);
- $expected = 'ORDER BY MockDatabaseComment.created DESC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order(array('MockDatabaseComment.created' => 'DESC'), $query);
- $expected = 'ORDER BY MockDatabaseComment.created DESC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order(
- array(
- 'MockDatabasePost.title' => 'ASC',
- 'MockDatabaseComment.created' => 'DESC'
- ),
- $query
- );
- $expected = 'ORDER BY MockDatabasePost.title ASC, MockDatabaseComment.created DESC';
- $this->assertEqual($expected, $result);
- $result = $this->db->order(
- array(
- 'title' => 'ASC',
- 'MockDatabaseComment.created' => 'DESC'
- ),
- $query
- );
- $expected = 'ORDER BY {MockDatabasePost}.{title} ASC, MockDatabaseComment.created DESC';
- $this->assertEqual($expected, $result);
- }
- public function testScopedDelete() {
- $query = new Query(array(
- 'type' => 'delete',
- 'conditions' => array('published' => false),
- 'model' => $this->_model
- ));
- $sql = 'DELETE FROM {mock_database_posts} WHERE {published} = 0;';
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- public function testScopedUpdate() {
- $query = new Query(array(
- 'type' => 'update',
- 'conditions' => array('expires' => array('>=' => '2010-05-13')),
- 'data' => array('published' => false, 'comments' => null),
- 'model' => $this->_model
- ));
- $sql = "UPDATE {mock_database_posts} SET {published} = 0, {comments} = NULL WHERE ";
- $sql .= "({expires} >= '2010-05-13');";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- public function testQueryOperators() {
- $query = new Query(array('type' => 'read', 'model' => $this->_model, 'conditions' => array(
- 'score' => array('between' => array(90, 100))
- )));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ({score} ";
- $sql .= "BETWEEN 90 AND 100);";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $query = new Query(array('type' => 'read', 'model' => $this->_model, 'conditions' => array(
- 'score' => array('>' => 90, '<' => 100)
- )));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ";
- $sql .= "({score} > 90 AND {score} < 100);";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $query = new Query(array('type' => 'read', 'model' => $this->_model, 'conditions' => array(
- 'score' => array('!=' => array(98, 99, 100))
- )));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} ";
- $sql .= "WHERE ({score} NOT IN (98, 99, 100));";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $query = new Query(array('type' => 'read', 'model' => $this->_model, 'conditions' => array(
- 'scorer' => array('like' => '%howard%')
- )));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} ";
- $sql .= "WHERE ({scorer} like '%howard%');";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $conditions = "custom conditions string";
- $query = new Query(compact('conditions') + array(
- 'type' => 'read', 'model' => $this->_model
- ));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE {$conditions};";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'conditions' => array(
- 'field' => array('like' => '%value%', 'not like' => '%value2%')
- )
- ));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ";
- $sql .= "({field} like '%value%' AND {field} not like '%value2%');";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- public function testConditions() {
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'conditions' => array(
- 'or' => array(
- 'id' => 'value1',
- 'title' => 'value2',
- 'and' => array(
- 'author_id' => '1',
- 'created' => '2012-05-25 23:41:00'
- ),
- array('title' => 'value2'),
- array('title' => null)
- ),
- 'id' => '3',
- 'author_id' => false
- )
- ));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ";
- $sql .= "({MockDatabasePost}.{id} = 0 OR {MockDatabasePost}.{title} = 'value2' OR ";
- $sql .= "({MockDatabasePost}.{author_id} = 1 AND {MockDatabasePost}.{created} = ";
- $sql .= "'2012-05-25 23:41:00') OR ({MockDatabasePost}.{title} = 'value2') OR ";
- $sql .= "({MockDatabasePost}.{title} IS NULL)) AND {MockDatabasePost}.{id} = 3 AND ";
- $sql .= "{MockDatabasePost}.{author_id} = 0;";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'conditions' => array('title' => array('0900'))
- ));
- $sql = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost}';
- $sql .= ' WHERE {title} IN (\'0900\');';
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $sql = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ';
- $sql .= 'lower(title) = \'test\';';
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'conditions' => array('lower(title)' => 'test')
- ));
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'conditions' => array( (object) 'lower(title) = \'test\'')
- ));
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $sql = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} WHERE ';
- $sql .= 'lower(title) = REGEXP \'^test$\';';
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'conditions' => array( (object) 'lower(title) = REGEXP \'^test$\'')
- ));
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'conditions' => array( 'lower(title)' => (object) 'REGEXP \'^test$\'')
- ));
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- public function testHaving() {
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'having' => array(
- 'or' => array(
- 'id' => 'value1',
- 'title' => 'value2',
- 'and' => array(
- 'author_id' => '1',
- 'created' => '2012-05-25 23:41:00'
- ),
- array('title' => 'value2'),
- array('title' => null)
- ),
- 'id' => '3',
- 'author_id' => false
- )
- ));
- $sql = "SELECT * FROM {mock_database_posts} AS {MockDatabasePost} HAVING ";
- $sql .= "({MockDatabasePost}.{id} = 0 OR {MockDatabasePost}.{title} = 'value2' OR ";
- $sql .= "({MockDatabasePost}.{author_id} = 1 AND {MockDatabasePost}.{created} = ";
- $sql .= "'2012-05-25 23:41:00') OR ({MockDatabasePost}.{title} = 'value2') OR ";
- $sql .= "({MockDatabasePost}.{title} IS NULL)) AND {MockDatabasePost}.{id} = 3 AND ";
- $sql .= "{MockDatabasePost}.{author_id} = 0;";
- $this->assertEqual($sql, $this->db->renderCommand($query));
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model,
- 'having' => array('title' => array('0900'))
- ));
- $sql = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost}' .
- ' HAVING {title} IN (\'0900\');';
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- public function testConstraints() {
- $model = $this->_model;
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'with' => array(
- 'MockDatabaseComment' => array(
- 'constraints' => array(
- 'or' => array(
- array('custom_id' => 'MockDatabasePost.value_id'),
- array('custom_id' => 'id'),
- 'and' => array(
- 'id' => 'MockDatabasePost.id',
- 'title' => 'MockDatabasePost.title'
- ),
- array('title' => (object) $this->db->value('value2')),
- array('title' => null)
- ),
- 'id' => 5
- )))));
- $sql = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} LEFT JOIN ';
- $sql .= '{mock_database_comments} AS {MockDatabaseComment} ON ';
- $sql .= '(({MockDatabasePost}.{custom_id} = {MockDatabasePost}.{value_id}) OR ';
- $sql .= '({MockDatabasePost}.{custom_id} = {MockDatabaseComment}.{id}) OR ';
- $sql .= '({MockDatabasePost}.{id} = {MockDatabasePost}.{id} ';
- $sql .= 'AND {MockDatabasePost}.{title} = {MockDatabasePost}.{title}) ';
- $sql .= 'OR ({MockDatabasePost}.{title} = \'value2\') ';
- $sql .= 'OR ({MockDatabasePost}.{title} IS NULL)) AND {MockDatabasePost}.{id} = 5;';
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- public function testReadConditionsWithModel() {
- $model = $this->_model;
- $options = array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'conditions' => array('id' => 1, 'MockDatabaseComment.id' => 2),
- 'with' => array('MockDatabaseComment')
- );
- $result = $this->db->read(new Query($options), $options);
- $expected = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} LEFT JOIN ';
- $expected .= '{mock_database_comments} AS {MockDatabaseComment} ON ';
- $expected .= '{MockDatabasePost}.{id} = {MockDatabaseComment}.{mock_database_post_id} ';
- $expected .= 'WHERE {MockDatabasePost}.{id} = 1 AND {MockDatabaseComment}.{id} = 2;';
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testFields() {
- $query = new Query(array(
- 'model' => $this->_model,
- 'with' => array('MockDatabaseComment')
- ));
- $fields = array('id', 'title');
- $result = $this->db->fields($fields, $query);
- $expected = '{MockDatabasePost}.{id}, {MockDatabasePost}.{title}';
- $this->assertEqual($expected,$result);
- $fields = array(
- 'MockDatabasePost' => array('id', 'title', 'created'),
- 'MockDatabaseComment' => array('body')
- );
- $result = $this->db->fields($fields, $query);
- $expected = '{MockDatabasePost}.{id}, {MockDatabasePost}.{title},';
- $expected .= ' {MockDatabasePost}.{created}, {MockDatabaseComment}.{body}';
- $this->assertEqual($expected,$result);
- $fields = array('MockDatabasePost', 'MockDatabaseComment');
- $result = $this->db->fields($fields, $query);
- $expected = '{MockDatabasePost}.*, {MockDatabaseComment}.*';
- $this->assertEqual($expected, $result);
- $fields = array('MockDatabasePost.id as idPost', 'MockDatabaseComment.id AS idComment');
- $result = $this->db->fields($fields, $query);
- $expected = '{MockDatabasePost}.{id} as idPost, {MockDatabaseComment}.{id} as idComment';
- $this->assertEqual($expected, $result);
- $expected = array('' => array('idPost'), 'MockDatabaseComment' => array('idComment'));
- $this->assertEqual($expected, $query->map());
- $fields = array(array('count(MockDatabasePost.id)'));
- $expected = 'count(MockDatabasePost.id)';
- $result = $this->db->fields($fields, $query);
- $this->assertEqual($expected, $result);
- $fields = array(array((object) 'count(MockDatabasePost.id)'));
- $expected = 'count(MockDatabasePost.id)';
- $result = $this->db->fields($fields, $query);
- $this->assertEqual($expected, $result);
- }
- public function testFieldsWithEmptyAlias() {
- $query = new Query();
- $result = $this->db->fields(array('id', 'name', 'created'), $query);
- $expected = '{id}, {name}, {created}';
- $this->assertEqual($expected, $result);
- }
- public function testRawConditions() {
- $query = new Query(array('type' => 'read', 'model' => $this->_model, 'conditions' => null));
- $this->assertFalse($this->db->conditions(5, $query));
- $this->assertFalse($this->db->conditions(null, $query));
- $this->assertEqual("WHERE CUSTOM", $this->db->conditions("CUSTOM", $query));
- }
- public function testRawHaving() {
- $query = new Query(array('type' => 'read', 'model' => $this->_model, 'having' => null));
- $this->assertFalse($this->db->having(5, $query));
- $this->assertFalse($this->db->having(null, $query));
- $this->assertEqual("HAVING CUSTOM", $this->db->having("CUSTOM", $query));
- }
- public function testRelationshipGeneration() {
- $comment = 'lithium\tests\mocks\data\model\MockDatabaseComment';
- $hasMany = $this->db->relationship($this->_model, 'hasMany', 'Comments', array(
- 'to' => $comment
- ));
- $this->assertEqual(array('id' => 'mock_database_post_id'), $hasMany->key());
- $this->assertEqual('comments', $hasMany->fieldName());
- $belongsTo = $this->db->relationship($comment, 'belongsTo', 'Posts', array(
- 'to' => $this->_model
- ));
- $this->assertEqual(array('post_id' => 'id'), $belongsTo->key());
- $this->assertEqual('post', $belongsTo->fieldName());
- }
- public function testRelationshipGenerationWithNullConstraint() {
- $postRevision = 'lithium\tests\mocks\data\model\MockDatabasePostRevision';
- $hasMany = $this->db->relationship($this->_model, 'hasMany', 'PostRevisions', array(
- 'to' => $postRevision,
- 'constraints' => array('MockDatabasePostRevision.deleted' => null)
- ));
- $this->assertEqual(array('id' => 'mock_database_post_id'), $hasMany->key());
- $this->assertEqual('post_revisions', $hasMany->fieldName());
- $expected = array(
- 'MockDatabasePostRevision.deleted' => null,
- 'MockDatabasePost.id' => 'PostRevisions.mock_database_post_id'
- );
- $result = $this->db->on($hasMany);
- $this->assertEqual($expected, $result);
- $belongsTo = $this->db->relationship($postRevision, 'belongsTo', 'Posts', array(
- 'to' => $this->_model
- ));
- $this->assertEqual(array('post_id' => 'id'), $belongsTo->key());
- $this->assertEqual('post', $belongsTo->fieldName());
- }
- public function testInvalidQueryType() {
- $this->expectException('Invalid query type `fakeType`.');
- $this->db->read(new Query(array('type' => 'fakeType')));
- }
- public function testReadWithRelationship() {
- $options = array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'with' => array('MockDatabaseComment')
- );
- $result = $this->db->read(new Query($options), $options);
- $expected = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} LEFT JOIN ';
- $expected .= '{mock_database_comments} AS {MockDatabaseComment} ON ';
- $expected .= '{MockDatabasePost}.{id} = {MockDatabaseComment}.{mock_database_post_id};';
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testReadWithRelationshipWithNullConstraint() {
- $options = array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'with' => array('MockDatabasePostRevision')
- );
- $result = $this->db->read(new Query($options), $options);
- $expected = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} LEFT JOIN ';
- $expected .= '{mock_database_post_revisions} AS {MockDatabasePostRevision} ON ';
- $expected .= '{MockDatabasePostRevision}.{deleted} IS NULL AND ';
- $expected .= '{MockDatabasePost}.{id} = {MockDatabasePostRevision}.';
- $expected .= '{mock_database_post_id};';
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testReadWithHasManyAndLimit() {
- $options = array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'with' => array('MockDatabaseComment'),
- 'limit' => 1
- );
- $result = $this->db->read(new Query($options), $options);
- $this->assertFalse($result instanceof RecordSet);
- }
- public function testGroup() {
- $result = $this->db->group(array('id ASC'));
- $expected = 'GROUP BY id ASC';
- $this->assertEqual($expected, $result);
- }
- public function testLimit() {
- MockDatabasePost::find('all', array('limit' => 15));
- $result = $this->db->sql;
- $expected = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} LIMIT 15;';
- $this->assertEqual($expected, $result);
- MockDatabasePost::find('all', array('limit' => 10, 'page' => 3));
- $result = $this->db->sql;
- $expected = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} LIMIT 10 OFFSET 20;';
- $this->assertEqual($expected, $result);
- }
- /**
- * Tests that various syntaxes for the `'order'` key of the query object produce the correct
- * SQL.
- */
- public function testQueryOrderSyntaxes() {
- $query = new Query(array(
- 'type' => 'read', 'model' => $this->_model, 'order' => array('created' => 'ASC')
- ));
- $sql = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} ';
- $sql .= 'ORDER BY {MockDatabasePost}.{created} ASC;';
- $this->assertEqual($sql, $this->db->renderCommand($query));
- }
- /**
- * Tests that complex model constraints with custom operators render correct constraint strings.
- */
- public function testRenderArrayJoinConstraintComplex() {
- $model = 'lithium\tests\mocks\data\model\MockQueryComment';
- $query = new Query(compact('model') + array(
- 'type' => 'read',
- 'source' => 'comments',
- 'alias' => 'Comments',
- 'conditions' => array('Comment.id' => 1),
- 'joins' => array(array(
- 'type' => 'INNER',
- 'source' => 'posts',
- 'alias' => 'Post',
- 'constraints' => array('Comment.post_id' => array('<=' => 'Post.id'))
- ))
- ));
- $expected = "SELECT * FROM {comments} AS {Comments} INNER JOIN {posts} AS {Post} ON ";
- $expected .= "({Comment}.{post_id} <= {Post}.{id}) WHERE {Comment}.{id} = 1;";
- $result = $this->db->renderCommand($query);
- $this->assertEqual($expected, $result);
- }
- /**
- * Tests that complex model constraints with custom operators render correct constraint strings.
- */
- public function testRenderArrayJoinConstraintComplexArray() {
- $model = 'lithium\tests\mocks\data\model\MockQueryComment';
- $query = new Query(compact('model') + array(
- 'type' => 'read',
- 'source' => 'comments',
- 'alias' => 'Comments',
- 'conditions' => array('Comment.id' => 1),
- 'joins' => array(array(
- 'type' => 'LEFT',
- 'source' => 'posts',
- 'alias' => 'Post',
- 'constraints' => array(
- "Comment.post_id" => array(
- '<=' => 'Post.id',
- '>=' => 'Post.id'
- )
- )
- )
- )));
- $expected = "SELECT * FROM {comments} AS {Comments} LEFT JOIN {posts} AS {Post} ON ";
- $expected .= "({Comment}.{post_id} <= {Post}.{id} AND {Comment}.{post_id} >= {Post}.{id}) ";
- $expected .= "WHERE {Comment}.{id} = 1;";
- $result = $this->db->renderCommand($query);
- $this->assertEqual($expected, $result);
- $query = new Query(compact('model') + array(
- 'type' => 'read',
- 'source' => 'comments',
- 'alias' => 'Comments',
- 'joins' => array(array(
- 'type' => 'LEFT',
- 'source' => 'posts',
- 'alias' => 'Post',
- 'constraints' => array(
- 'Comment.post_id' => array('=>' => 'Post.id')
- )
- ))
- ));
- $this->expectException("Unsupported operator `=>`.");
- $this->db->renderCommand($query);
- }
- public function testRenderArrayJoin() {
- $model = 'lithium\tests\mocks\data\model\MockQueryComment';
- $query = new Query(compact('model') + array(
- 'type' => 'read',
- 'source' => 'comments',
- 'alias' => 'Comment',
- 'conditions' => array('Comment.id' => 1),
- 'joins' => array(array(
- 'type' => 'INNER',
- 'source' => 'posts',
- 'alias' => 'Post',
- 'constraints' => array('Comment.post_id' => 'Post.id')
- ))
- ));
- $expected = "SELECT * FROM {comments} AS {Comment} INNER JOIN {posts} AS {Post} ON ";
- $expected .= "{Comment}.{post_id} = {Post}.{id} WHERE {Comment}.{id} = 1;";
- $result = $this->db->renderCommand($query);
- $this->assertEqual($expected, $result);
- }
- public function testModelFindBy() {
- $this->db->log = true;
- MockDatabasePost::findById(5, array('with' => 'MockDatabaseComment'));
- $this->db->log = false;
- $result = MockDatabasePost::$connection->logs[0];
- $expected = "SELECT DISTINCT({MockDatabasePost}.{id}) AS _ID_ FROM {mock_database_posts}";
- $expected .= " AS {MockDatabasePost} LEFT JOIN {mock_database_comments} AS ";
- $expected .= "{MockDatabaseComment} ON {MockDatabasePost}.{id} = ";
- $expected .= "{MockDatabaseComment}.{mock_database_post_id} WHERE ";
- $expected .= "{MockDatabasePost}.{id} = 5 LIMIT 1;";
- $this->assertEqual($expected, $result);
- }
- public function testsplitFieldname() {
- $result = $this->db->invokeMethod('_splitFieldname', array('Alias.fieldname'));
- $this->assertEqual(array('Alias', 'fieldname'), $result);
- $result = $this->db->invokeMethod('_splitFieldname', array('fieldname'));
- $this->assertEqual(array(null, 'fieldname'), $result);
- $result = $this->db->invokeMethod('_splitFieldname', array('fieldname'));
- $this->assertEqual(array(null, 'fieldname'), $result);
- $result = $this->db->invokeMethod('_splitFieldname', array('lower(Alias.fieldname)'));
- $this->assertEqual(array(null, 'lower(Alias.fieldname)'), $result);
- $result = $this->db->invokeMethod('_splitFieldname', array('Alias.*'));
- $this->assertEqual(array('Alias', '*'), $result);
- }
- public function testOn() {
- $conn = MockDatabasePost::connection();
- $expected = array(
- 'MockDatabasePost.id' => 'MockDatabaseComment.mock_database_post_id'
- );
- $result = $conn->on(MockDatabasePost::relations('MockDatabaseComment'));
- $this->assertEqual($expected, $result);
- $expected = array(
- 'MockDatabaseComment.mock_database_post_id' => 'MockDatabasePost.id'
- );
- $result = $conn->on(MockDatabaseComment::relations('MockDatabasePost'));
- $this->assertEqual($expected, $result);
- $expected = array(
- 'MockDatabasePost.id' => 'MockDatabaseComment.mock_database_post_id',
- 'MockDatabasePost.published' => (object) "'yes'"
- );
- $rel = MockDatabasePost::relations('MockDatabaseComment');
- $result = $conn->on($rel, null, null, array('published' => (object) "'yes'"));
- $this->assertEqual($expected, $result);
- $expected = array(
- 'CustomPost.id' => 'CustomComment.mock_database_post_id',
- 'CustomPost.published' => (object) "'no'"
- );
- $constraints = array('published' => (object) "'no'");
- $result = $conn->on($rel, 'CustomPost', 'CustomComment', $constraints);
- $this->assertEqual($expected, $result);
- $expected = array(
- 'CustomPost.id' => 'CustomComment.post_id'
- );
- $constraints = array('CustomPost.id' => 'CustomComment.post_id');
- $result = $conn->on($rel, 'CustomPost', 'CustomComment', $constraints);
- $this->assertEqual($expected, $result);
- }
- public function testWithGeneration() {
- $model = $this->_gallery;
- $options = array(
- 'type' => 'read',
- 'model' => $model,
- 'with' => array('Image.ImageTag.Tag')
- );
- $result = $this->db->read(new Query($options));
- $expected = 'SELECT * FROM {mock_gallery} AS {Gallery} LEFT JOIN {mock_image} AS {Image} ';
- $expected .= 'ON {Gallery}.{id} = {Image}.{gallery_id} LEFT JOIN {mock_image_tag} AS ';
- $expected .= '{ImageTag} ON {Image}.{id} = {ImageTag}.{image_id} LEFT JOIN {mock_tag} ';
- $expected .= 'AS {Tag} ON {ImageTag}.{tag_id} = {Tag}.{id};';
- $this->assertEqual($expected, $this->db->sql);
- $model = $this->_imageTag;
- $options = array(
- 'type' => 'read',
- 'model' => $model,
- 'with' => array('Image', 'Tag')
- );
- $result = $this->db->read(new Query($options));
- $expected = 'SELECT * FROM {mock_image_tag} AS {ImageTag} LEFT JOIN {mock_image} AS ';
- $expected .= '{Image} ON {ImageTag}.{image_id} = {Image}.{id} LEFT JOIN {mock_tag} AS ';
- $expected .= '{Tag} ON {ImageTag}.{tag_id} = {Tag}.{id};';
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testWithOptionAndInlineConstraint() {
- $model = $this->_gallery;
- $options = array(
- 'type' => 'read',
- 'model' => $model,
- 'with' => array(
- 'Image' => array(
- 'constraints' => array(
- 'Image.title' => (object) "'MyImage'"
- )),
- 'Image.ImageTag.Tag' => array(
- 'constraints' => array(
- 'Tag.name' => (object) "'MyTag'"
- )
- )
- )
- );
- $result = $this->db->read(new Query($options));
- $expected = 'SELECT * FROM {mock_gallery} AS {Gallery} ';
- $expected .= 'LEFT JOIN {mock_image} AS {Image} ON {Image}.{title} = \'MyImage\' ';
- $expected .= 'AND {Gallery}.{id} = {Image}.{gallery_id} LEFT JOIN ';
- $expected .= '{mock_image_tag} AS {ImageTag} ON ';
- $expected .= '{Image}.{id} = {ImageTag}.{image_id} LEFT JOIN {mock_tag} AS {Tag} ON ';
- $expected .= '{Tag}.{name} = \'MyTag\' AND {ImageTag}.{tag_id} = {Tag}.{id};';
- $this->assertEqual($expected, $this->db->sql);
- $to = 'lithium\tests\mocks\data\model\MockImage';
- $model::bind('hasMany', 'Image', array('to' => $to));
- $to::bind('belongsTo', 'Gallery', array('to' => $model));
- $result = $this->db->read(new Query(array(
- 'type' => 'read',
- 'model' => $model,
- 'with' => array(
- 'Image.Gallery' => array(
- 'alias' => 'Gallery2',
- 'constraints' => array(
- 'Gallery.custom_id' => 'Gallery2.id'
- )
- )
- )
- )));
- $expected = 'SELECT * FROM {mock_gallery} AS {Gallery} LEFT JOIN {mock_image} AS {Image}';
- $expected .= ' ON {Gallery}.{id} = {Image}.{gallery_id} LEFT JOIN {mock_gallery} AS ';
- $expected .= '{Gallery2} ON {Gallery}.{custom_id} = {Gallery2}.{id} AND ';
- $expected .= '{Image}.{gallery_id} = {Gallery2}.{id};';
- $this->assertEqual($expected, $this->db->sql);
- $model::reset();
- }
- public function testWithOptionAndConstraintInRelation() {
- $model = 'lithium\tests\mocks\data\model\MockGallery';
- $to = 'lithium\tests\mocks\data\model\MockImage';
- $model::bind('hasMany', 'Image', array(
- 'to' => $to,
- 'constraints' => array(
- 'Image.title' => (object) "'MyImage'"
- )));
- $result = $this->db->read(new Query(array(
- 'type' => 'read',
- 'model' => $model,
- 'with' => array(
- 'Image.ImageTag.Tag' => array(
- 'constraints' => array(
- 'Tag.name' => (object) "'MyTag'"
- )
- )
- )
- )));
- $expected = 'SELECT * FROM {mock_gallery} AS {Gallery} ';
- $expected .= 'LEFT JOIN {mock_image} AS {Image} ON {Image}.{title} = \'MyImage\' AND ';
- $expected .= '{Gallery}.{id} = {Image}.{gallery_id} LEFT JOIN {mock_image_tag} AS ';
- $expected .= '{ImageTag} ON {Image}.{id} = {ImageTag}.{image_id} LEFT JOIN {mock_tag} AS ';
- $expected .= '{Tag} ON {Tag}.{name} = \'MyTag\' AND {ImageTag}.{tag_id} = {Tag}.{id};';
- $this->assertEqual($expected, $this->db->sql);
- $model::reset();
- }
- public function testWithOptionWithNullConstraint() {
- $options = array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'with' => array('MockDatabasePostRevision')
- );
- $result = $this->db->read(new Query($options));
- $expected = 'SELECT * FROM {mock_database_posts} AS {MockDatabasePost} LEFT JOIN ';
- $expected .= '{mock_database_post_revisions} AS {MockDatabasePostRevision} ON ';
- $expected .= '{MockDatabasePostRevision}.{deleted} IS NULL AND ';
- $expected .= '{MockDatabasePost}.{id} = {MockDatabasePostRevision}.';
- $expected .= '{mock_database_post_id};';
- $this->assertEqual($expected, $this->db->sql);
- }
- public function testWithOptionAndCustomAlias() {
- $model = 'lithium\tests\mocks\data\model\MockGallery';
- $model::bind('hasMany', 'Image', array(
- 'to' => 'lithium\tests\mocks\data\model\MockImage',
- 'constraints' => array(
- 'Image.title' => (object) "'MyImage'"
- )
- ));
- $options = array(
- 'type' => 'read',
- 'model' => $model,
- 'alias' => 'MyGallery',
- 'with' => array(
- 'Image' => array('alias' => 'MyImage')
- )
- );
- $result = $this->db->read(new Query($options));
- $query = new Query($options);
- $expected = 'SELECT * FROM {mock_gallery} AS {MyGallery} LEFT JOIN ';
- $expected .= '{mock_image} AS {MyImage} ON {MyImage}.{title} = \'MyImage\' ';
- $expected .= 'AND {MyGallery}.{id} = {MyImage}.{gallery_id};';
- $this->assertEqual($expected, $this->db->sql);
- $model::reset();
- }
- public function testJoin() {
- $model = $this->_model;
- $conn = $model::connection();
- $query = new Query(array('type' => 'read', 'model' => $model));
- $rel = $model::relations('MockDatabaseComment');
- $conn->join($query, $rel, null, null, array('published' => (object) "'yes'"));
- $joins = $query->joins();
- $expected = array(
- 'MockDatabaseComment' => array(
- 'constraints' => array(
- 'MockDatabasePost.id' => 'MockDatabaseComment.mock_database_post_id',
- 'MockDatabasePost.published' => (object) "'yes'"
- ),
- 'model' => 'lithium\tests\mocks\data\model\MockDatabaseComment',
- 'type' => 'LEFT',
- 'alias' => 'MockDatabaseComment'
- )
- );
- $this->assertEqual($expected, $joins);
- $query = new Query(array('type' => 'read', 'model' => $model));
- $rel = $model::relations('MockDatabaseComment');
- $conn->join($query, $rel, null, null, (object) array('published' => (object) "'yes'"));
- $joins = $query->joins();
- $expected = array(
- 'MockDatabaseComment' => array(
- 'constraints' => array(
- 'published' => (object) "'yes'"
- ),
- 'model' => 'lithium\tests\mocks\data\model\MockDatabaseComment',
- 'type' => 'LEFT',
- 'alias' => 'MockDatabaseComment'
- )
- );
- $this->assertEqual($expected, $joins);
- }
- public function testExportedFieldsWithJoinedStrategy() {
- $query = new Query(array(
- 'model' => $this->_gallery,
- 'with' => array('Image.ImageTag.Tag')
- ));
- $result = $query->export($this->db);
- $this->assertEqual('*', $result['fields']);
- $query = new Query(array(
- 'model' => $this->_gallery,
- 'fields' => 'id',
- 'with' => array('Image.ImageTag.Tag')
- ));
- $result = $query->export($this->db);
- $expected = '{Gallery}.{id}';
- $this->assertEqual($expected, $result['fields']);
- $query = new Query(array(
- 'model' => $this->_gallery,
- 'fields' => 'Tag.id',
- 'with' => array('Image.ImageTag.Tag')
- ));
- $result = $query->export($this->db);
- $expected = '{Gallery}.{id}, {Tag}.{id}, {Image}.{id}, {ImageTag}.{id}';
- $this->assertEqual($expected, $result['fields']);
- $query = new Query(array(
- 'model' => $this->_gallery,
- 'fields' => 'Tag',
- 'with' => array('Image.ImageTag.Tag')
- ));
- $result = $query->export($this->db);
- $expected = '{Gallery}.{id}, {Tag}.*, {Image}.{id}, {ImageTag}.{id}';
- $this->assertEqual($expected, $result['fields']);
- $query = new Query(array(
- 'model' => $this->_gallery,
- 'fields' => 'Tag.*',
- 'with' => array('Image.ImageTag.Tag')
- ));
- $result = $query->export($this->db);
- $expected = '{Gallery}.{id}, {Tag}.*, {Image}.{id}, {ImageTag}.{id}';
- $this->assertEqual($expected, $result['fields']);
- }
- public function testExportedFieldsWithJoinedStrategyAndRecursiveRelation() {
- $query = new Query(array(
- 'model' => $this->_gallery,
- 'with' => array('Parent.Parent')
- ));
- $result = $query->export($this->db);
- $expected = '*';
- $this->assertEqual($expected, $result['fields']);
- $query = new Query(array(
- 'model' => $this->_gallery,
- 'fields' => 'Parent.name',
- 'with' => array('Parent.Parent')
- ));
- $result = $query->export($this->db);
- $expected = '{Gallery}.{id}, {Parent}.{name}';
- $this->assertEqual($expected, $result['fields']);
- $query = new Query(array(
- 'model' => $this->_gallery,
- 'fields' => 'ParentOfParent.name',
- 'with' => array('Parent.Parent' => array('alias' => 'ParentOfParent'))
- ));
- $result = $query->export($this->db);
- $expected = '{Gallery}.{id}, {ParentOfParent}.{name}, {Parent}.{id}';
- $this->assertEqual($expected, $result['fields']);
- }
- public function testCustomField() {
- $field = "(CASE `title` WHEN 'Lotus Flower' THEN 'Found' ELSE 'Not Found' END) as extra";
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_gallery,
- 'fields' => array('*', $field)
- ));
- $result = $this->db->read($query);
- $expected = 'SELECT (CASE `title` WHEN \'Lotus Flower\' THEN \'Found\' ELSE \'Not Found\' ';
- $expected .= 'END) as extra, {Gallery}.* FROM {mock_gallery} AS {Gallery};';
- $this->assertEqual($expected, $this->db->sql);
- $map = array('' => array('extra', 'id', 'title'));
- $this->assertEqual($map, $query->map());
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_gallery,
- 'fields' => array('*', (object) $field)
- ));
- $result = $this->db->read($query);
- $this->assertEqual($expected, $this->db->sql);
- $this->assertEqual($map, $query->map());
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_gallery,
- 'fields' => array('*', array($field))
- ));
- $result = $this->db->read($query);
- $this->assertEqual($expected, $this->db->sql);
- $this->assertEqual($map, $query->map());
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_gallery,
- 'fields' => array((object) 'count(Image.id) as count', 'Image'),
- 'group' => 'Gallery.id',
- 'with' => array('Image')
- ));
- $result = $this->db->read($query);
- $expected = 'SELECT count(Image.id) as count, {Gallery}.{id}, {Image}.* FROM ';
- $expected .= '{mock_gallery} AS {Gallery} LEFT JOIN {mock_image} AS {Image} ON ';
- $expected .= '{Gallery}.{id} = {Image}.{gallery_id} GROUP BY Gallery.id;';
- $this->assertEqual($expected, $this->db->sql);
- $map = array(
- '' => array('count', 'id'),
- 'Image' => array('id', 'title', 'image', 'gallery_id')
- );
- $this->assertEqual($map, $query->map());
- }
- public function testReturnArrayOnReadWithString() {
- $data = new MockResult(array('records' => array(
- array ('id', 'int(11)', 'NO', 'PRI', null, 'auto_increment'),
- array ('name', 'varchar(256)', 'YES', '', null, '')
- )));
- $this->db->return = array(
- 'schema' => array('field', 'type', 'null', 'key', 'default', 'extra'),
- '_execute' => $data
- );
- $result = $this->db->read('DESCRIBE {table};', array('return' => 'array'));
- $expected = array(
- array(
- 'field' => 'id',
- 'type' => 'int(11)',
- 'null' => 'NO',
- 'key' => 'PRI',
- 'default' => null,
- 'extra' => 'auto_increment',
- ),
- array(
- 'field' => 'name',
- 'type' => 'varchar(256)',
- 'null' => 'YES',
- 'key' => '',
- 'default' => null,
- 'extra' => '',
- )
- );
- $this->assertEqual($expected, $result);
- }
- public function testReturnArrayOnReadWithQuery() {
- $data = new MockResult(array('records' => array(array(
- '1',
- '2',
- 'Post title',
- '2012-12-17 17:04:00',
- '3',
- '1',
- '2',
- 'Very good post',
- '2012-12-17 17:05:00',
- '1',
- '2',
- 'Post title',
- '2012-12-17 17:04:00',
- ))));
- $this->db->return = array(
- '_execute' => $data
- );
- $query = new Query(array(
- 'type' => 'read',
- 'model' => $this->_model,
- 'with' => array('MockDatabaseComment.MockDatabasePost')
- ));
- $result = $this->db->read($query, array('return' => 'array'));
- $expected = array(array(
- 'id' => '1',
- 'author_id' => '2',
- 'title' => 'Post title',
- 'created' => '2012-12-17 17:04:00',
- 'MockDatabaseComment' => array(
- 'id' => '3',
- 'post_id' => '1',
- 'author_id' => '2',
- 'body' => 'Very good post',
- 'created' => '2012-12-17 17:05:00',
- 'MockDatabasePost' => array(
- 'id' => '1',
- 'author_id' => '2',
- 'title' => 'Post title',
- 'created' => '2012-12-17 17:04:00',
- )
- )
- ));
- $this->assertEqual($expected, $result);
- }
- }
- ?>
|