1<?php
2/*
3 *  $Id$
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information, see
19 * <http://www.doctrine-project.org>.
20 */
21
22/**
23 * Doctrine_Ticket_1381_TestCase
24 *
25 * @package     Doctrine
26 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
27 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28 * @category    Object Relational Mapping
29 * @link        www.doctrine-project.org
30 * @since       1.0
31 * @version     $Revision$
32 */
33class Doctrine_Ticket_1381_TestCase extends Doctrine_UnitTestCase
34{
35    public function prepareTables()
36    {
37        $this->tables[] = 'T1381_Comment';
38        $this->tables[] = 'T1381_Article';
39
40        parent::prepareTables();
41    }
42
43
44    public function prepareData()
45    {
46        $a = new T1381_Article();
47        $a->title = 'When cleanData worked as expected!';
48        $a->save();
49
50        $c = new T1381_Comment();
51        $c->article_id = $a->id;
52        $c->body = 'Yeah! It will work one day.';
53        $c->save();
54
55        $c = new T1381_Comment();
56        $c->article_id = $a->id;
57        $c->body = 'It will!';
58        $c->save();
59
60        // Cleaning up IdentityMap
61        Doctrine_Core::getTable('T1381_Article')->clear();
62        Doctrine_Core::getTable('T1381_Comment')->clear();
63    }
64
65    public function testTicket()
66    {
67        try {
68            // Now we fetch with data we want (it seems it overrides calculates columns of already fetched objects)
69            $dql = 'SELECT c.*, a.* FROM T1381_Comment c INNER JOIN c.T1381_Article a';
70            $items = Doctrine_Query::create()->query($dql, array(), Doctrine_Core::HYDRATE_ARRAY);
71
72            // This should result in false, since we didn't fetch for this column
73            $this->assertFalse(array_key_exists('ArticleTitle', $items[0]['T1381_Article']));
74
75            // We fetch for data including new columns
76            $dql = 'SELECT c.*, a.title as ArticleTitle FROM T1381_Comment c INNER JOIN c.T1381_Article a WHERE c.id = ?';
77            $items = Doctrine_Query::create()->query($dql, array(1), Doctrine_Core::HYDRATE_ARRAY);
78            $comment = $items[0];
79
80            $this->assertTrue(array_key_exists('ArticleTitle', $comment));
81        } catch (Doctrine_Exception $e) {
82            $this->fail($e->getMessage());
83        }
84    }
85
86
87    public function testTicketInverse()
88    {
89        try {
90            // We fetch for data including new columns
91            $dql = 'SELECT c.*, a.title as ArticleTitle FROM T1381_Comment c INNER JOIN c.T1381_Article a WHERE c.id = ?';
92            $items = Doctrine_Query::create()->query($dql, array(1), Doctrine_Core::HYDRATE_ARRAY);
93            $comment = $items[0];
94
95            $this->assertTrue(array_key_exists('ArticleTitle', $comment));
96
97            // Now we fetch with data we want (it seems it overrides calculates columns of already fetched objects)
98            $dql = 'SELECT c.*, a.* FROM T1381_Comment c INNER JOIN c.T1381_Article a';
99            $items = Doctrine_Query::create()->query($dql, array(), Doctrine_Core::HYDRATE_ARRAY);
100
101            // This should result in false, since we didn't fetch for this column
102            $this->assertFalse(array_key_exists('ArticleTitle', $items[0]['T1381_Article']));
103
104            // Assert that our existent component still has the column, even after new hydration on same object
105            $this->assertTrue(array_key_exists('ArticleTitle', $comment));
106
107            // Fetch including new columns again
108            $dql = 'SELECT c.id, a.*, a.id as ArticleTitle FROM T1381_Comment c INNER JOIN c.T1381_Article a';
109            $items = Doctrine_Query::create()->query($dql, array(), Doctrine_Core::HYDRATE_ARRAY);
110
111            // Assert that new calculated column with different content do not override the already fetched one
112            $this->assertTrue(array_key_exists('ArticleTitle', $items[0]));
113
114            // Assert that our existent component still has the column, even after new hydration on same object
115            $this->assertTrue(array_key_exists('ArticleTitle', $comment));
116            $this->assertTrue($comment, 'When cleanData worked as expected!');
117        } catch (Doctrine_Exception $e) {
118            $this->fail($e->getMessage());
119        }
120    }
121}
122
123
124class T1381_Article extends Doctrine_Record
125{
126    public function setTableDefinition() {
127        $this->hasColumn('id', 'integer', null, array('primary' => true, 'autoincrement' => true));
128        $this->hasColumn('title', 'string', 255, array('notnull' => true));
129    }
130
131    public function setUp() {
132        $this->hasMany(
133            'T1381_Comment',
134            array(
135                'local' => 'id',
136                'foreign' => 'article_id'
137            )
138        );
139    }
140}
141
142
143class T1381_Comment extends Doctrine_Record
144{
145    public function setTableDefinition() {
146        $this->hasColumn('id', 'integer', null, array('primary' => true, 'autoincrement' => true));
147        $this->hasColumn('body', 'string', null, array('notnull' => true));
148        $this->hasColumn('article_id', 'integer', null, array('notnull' => true));
149    }
150
151    public function setUp() {
152        $this->hasOne(
153            'T1381_Article',
154            array(
155                'local' => 'article_id',
156                'foreign' => 'id'
157            )
158        );
159    }
160}