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_Template_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_1228_TestCase extends Doctrine_UnitTestCase
34{
35    public function prepareTables()
36    {
37        $this->tables[] = "RelA";
38        $this->tables[] = "RelB";
39        $this->tables[] = "RelC";
40        $this->tables[] = "RelD";
41        $this->tables[] = "RelE";
42        parent::prepareTables();
43    }
44
45    public function prepareData()
46    {
47        // first branch of the 'object hierarchy'
48        $e1 = new RelE();
49        $e1->name = "e 1";
50        $e1->save();
51
52        $d1 = new RelD();
53        $d1->name = "d 1";
54        $d1->rel_e_id = $e1->id;
55        $d1->save();
56
57        $c1 = new RelC();
58        $c1->name = "c 1";
59        $c1->rel_d_id = $d1->id;
60        $c1->save();
61
62        $b1 = new RelB();
63        $b1->name = "b 1";
64        $b1->rel_c_id = $c1->id;
65        $b1->save();
66
67        $a1 = new RelA();
68        $a1->name = "a 1";
69        $a1->rel_b_id = $b1->id;
70        $a1->save();
71
72        // second branch, contains only top level
73        /* uncomment this to make it work
74        $b2 = new RelB();
75        $b2->name = "b 2";
76        $b2->save();
77        */
78        $a2 = new RelA();
79        $a2->name = "a 2";
80        // uncomment this, too
81        // $a2->rel_b_id = $b2->id;
82        $a2->save();
83
84
85        $e2 = new RelE();
86        $e2->name = "e 2";
87        $e2->save();
88
89        // third branch, full depth again
90        $d3 = new RelD();
91        $d3->name = "d 3";
92        $d3->rel_e_id = $e2->id;
93        $d3->save();
94
95        $c3 = new RelC();
96        $c3->name = "c 3";
97        $c3->rel_d_id = $d3->id;
98        $c3->save();
99
100        $b3 = new RelB();
101        $b3->name = "b 3";
102        $b3->rel_c_id = $c3->id;
103        $b3->save();
104
105        $a3 = new RelA();
106        $a3->name = "a 3";
107        $a3->rel_b_id = $b3->id;
108        $a3->save();
109    }
110
111    public function testHydrationSkippingRelationIfNotSetOnSiblingDepth3()
112    {
113        $q = new Doctrine_Query();
114        $q->from('RelA a');
115        $q->leftJoin('a.b ab');
116        $q->leftJoin('ab.c abc');
117        $q->orderBy('a.id ASC');
118        $res = $q->execute();
119        //$res = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
120
121        //var_dump($res/*->toArray(true)*/);
122
123        $this->assertEqual('a 1', $res->getFirst()->get('name'));
124        $this->assertTrue($res->getFirst()->get('b')->exists());
125        $this->assertTrue($res->getFirst()->get('b')->get('c')->exists());
126
127    }
128
129    public function testHydrationSkippingRelationIfNotSetOnSiblingDepth4()
130    {
131        $q = new Doctrine_Query();
132        $q->from('RelA a');
133        $q->leftJoin('a.b ab');
134        $q->leftJoin('ab.c abc');
135        $q->leftJoin('abc.d abcd');
136        $q->orderBy('a.id ASC');
137        $res = $q->execute();
138        //$res = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
139
140        //var_dump($res/*->toArray(true)*/);
141
142        $this->assertEqual('a 1', $res->getFirst()->get('name'));
143        $this->assertTrue($res->getFirst()->get('b')->exists());
144        $this->assertTrue($res->getFirst()->get('b')->get('c')->exists());
145        $this->assertTrue($res->getFirst()->get('b')->get('c')->get('d')->exists());
146
147    }
148
149    public function testHydrationSkippingRelationIfNotSetOnSiblingDepth5()
150    {
151        $q = new Doctrine_Query();
152        $q->from('RelA a');
153        $q->leftJoin('a.b ab');
154        $q->leftJoin('ab.c abc');
155        $q->leftJoin('abc.d abcd');
156        $q->leftJoin('abcd.e abcde');
157        $q->orderBy('a.id ASC');
158        $res = $q->execute();
159        //$res = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
160
161        //var_dump($res/*->toArray(true)*/);
162
163        $this->assertEqual('a 1', $res->getFirst()->get('name'));
164        $this->assertTrue($res->getFirst()->get('b')->exists());
165        $this->assertTrue($res->getFirst()->get('b')->get('c')->exists());
166        $this->assertTrue($res->getFirst()->get('b')->get('c')->get('d')->exists());
167        $this->assertTrue($res->getFirst()->get('b')->get('c')->get('d')->get('e')->exists());
168
169    }
170
171}
172
173class RelA extends Doctrine_Record {
174
175  public function setTableDefinition() {
176    $this->setTableName('rel_a');
177    $this->hasColumn('name', 'string', 25, array());
178    $this->hasColumn('rel_b_id', 'integer', 10, array());
179  }
180
181  public function setUp() {
182    $this->HasOne('RelB as b', array('local' => 'rel_b_id', 'foreign' => 'id'));
183  }
184
185}
186
187class RelB extends Doctrine_Record {
188
189  public function setTableDefinition() {
190    $this->setTableName('rel_b');
191    $this->hasColumn('name', 'string', 25, array());
192    $this->hasColumn('rel_c_id', 'integer', 10, array());
193  }
194
195  public function setUp() {
196    $this->HasOne('RelC as c', array('local' => 'rel_c_id', 'foreign' => 'id'));
197  }
198
199}
200
201class RelC extends Doctrine_Record {
202
203  public function setTableDefinition() {
204    $this->setTableName('rel_c');
205    $this->hasColumn('name', 'string', 25, array());
206    $this->hasColumn('rel_d_id', 'integer', 10, array());
207  }
208
209  public function setUp() {
210    $this->HasOne('RelD as d', array('local' => 'rel_d_id', 'foreign' => 'id'));
211  }
212
213}
214
215class RelD extends Doctrine_Record {
216
217  public function setTableDefinition() {
218    $this->setTableName('rel_d');
219    $this->hasColumn('name', 'string', 25, array());
220    $this->hasColumn('rel_e_id', 'integer', 10, array());
221  }
222
223  public function setUp() {
224      $this->HasOne('RelE as e', array('local' => 'rel_e_id', 'foreign' => 'id'));
225  }
226
227}
228
229class RelE extends Doctrine_Record {
230
231  public function setTableDefinition() {
232    $this->setTableName('rel_e');
233    $this->hasColumn('name', 'string', 25, array());
234  }
235
236  public function setUp() {
237
238  }
239
240}
241