Inheritance: extends lithium\data\Model
Esempio n. 1
0
    public function testHasManyRelationWithLimitAndOrder()
    {
        $this->_db->log = true;
        $this->_db->return['_execute'] = new MockResult(array('records' => array(array(0 => 5))));
        MockDatabasePost::first(array('with' => array('MockDatabaseComment'), 'order' => array('title', 'id', 'MockDatabaseComment.body' => 'DESC')));
        $this->_db->log = false;
        $result = $this->_db->logs;
        $expected[0] = <<<SQL
SELECT _ID_ FROM (
\t\tSELECT DISTINCT ON({MockDatabasePost}.{id}) {MockDatabasePost}.{id} AS _ID_,
\t\t\t{MockDatabasePost}.{title} AS {_MockDatabasePost_title_},
\t\t\t{MockDatabasePost}.{id} AS {_MockDatabasePost_id_},
\t\t\t{MockDatabaseComment}.{body} AS {_MockDatabaseComment_body_}
\t\t\tFROM {mock_database_posts} AS {MockDatabasePost}
\t\t\tLEFT JOIN {mock_database_comments} AS {MockDatabaseComment}
\t\t\t\tON {MockDatabasePost}.{id} = {MockDatabaseComment}.{mock_database_post_id}
\t) AS _TEMP_
\tORDER BY {_MockDatabasePost_title_} ASC, {_MockDatabasePost_id_} ASC, {_MockDatabaseComment_body_} DESC
\tLIMIT 1;
SQL;
        $expected[1] = <<<SQL
SELECT * FROM {mock_database_posts} AS {MockDatabasePost}
\tLEFT JOIN {mock_database_comments} AS {MockDatabaseComment}
\t\tON {MockDatabasePost}.{id} = {MockDatabaseComment}.{mock_database_post_id}
\tWHERE {MockDatabasePost}.{id} IN (5)
\tORDER BY {MockDatabasePost}.{title} ASC, {MockDatabasePost}.{id} ASC, {MockDatabaseComment}.{body} DESC;
SQL;
        $expected = array_map(function ($v) {
            return preg_replace('/[\\t\\n]+/', ' ', $v);
        }, $expected);
        $this->assertEqual($expected, $result);
    }
Esempio n. 2
0
 public function setUp()
 {
     $this->db = new MockDatabase();
     $this->_configs = Connections::config();
     Connections::reset();
     Connections::config(array('mock-database-connection' => array('object' => &$this->db, 'adapter' => 'MockDatabase')));
     MockDatabasePost::config();
     MockDatabaseComment::config();
 }
 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);
 }
Esempio n. 4
0
 public function tearDown()
 {
     Connections::remove('mock');
     MockDatabasePost::reset();
 }
Esempio n. 5
0
    public function testMultiHasManyRelationsWithLimit()
    {
        $this->_db->log = true;
        $this->_db->return['_execute'] = new MockResult(array('records' => array(array(0 => 5))));
        MockDatabasePost::find('first', array('conditions' => array('id' => 5, 'is_published' => true, 'MockDatabaseComment.is_spam' => false, 'MockDatabasePostRevision.title' => 'foo'), 'with' => array('MockDatabaseComment', 'MockDatabasePostRevision')));
        $this->_db->log = false;
        $result = $this->_db->logs;
        $expected[0] = <<<SQL
SELECT DISTINCT({MockDatabasePost}.{id}) AS _ID_
\tFROM {mock_database_posts} AS {MockDatabasePost}
\tLEFT JOIN {mock_database_comments} AS {MockDatabaseComment}
\t\tON {MockDatabasePost}.{id} = {MockDatabaseComment}.{mock_database_post_id}
\tLEFT JOIN {mock_database_post_revisions} AS {MockDatabasePostRevision}
\t\tON {MockDatabasePostRevision}.{deleted} IS NULL
\t\t\tAND {MockDatabasePost}.{id} = {MockDatabasePostRevision}.{mock_database_post_id}
\tWHERE
\t\t{MockDatabasePost}.{id} = 5
\t\tAND {MockDatabasePost}.{is_published} = 1
\t\tAND {MockDatabaseComment}.{is_spam} = 0
\t\tAND {MockDatabasePostRevision}.{title} = 'foo'
\tLIMIT 1;
SQL;
        $expected[1] = <<<SQL
SELECT * FROM {mock_database_posts} AS {MockDatabasePost}
\tLEFT JOIN {mock_database_comments} AS {MockDatabaseComment}
\t\tON {MockDatabasePost}.{id} = {MockDatabaseComment}.{mock_database_post_id}
\tLEFT JOIN {mock_database_post_revisions} AS {MockDatabasePostRevision}
\t\tON {MockDatabasePostRevision}.{deleted} IS NULL
\t\t\tAND {MockDatabasePost}.{id} = {MockDatabasePostRevision}.{mock_database_post_id}
\tWHERE
\t\t{MockDatabasePost}.{id} IN (5)
\t\tAND {MockDatabaseComment}.{is_spam} = 0
\t\tAND {MockDatabasePostRevision}.{title} = 'foo';
SQL;
        $expected = array_map(function ($v) {
            return preg_replace('/[\\t\\n]+/', ' ', $v);
        }, $expected);
        $this->assertEqual($expected, $result);
    }
 public function setUp()
 {
     $this->db = new MockDatabase();
     MockDatabasePost::__init();
     MockDatabaseComment::__init();
 }