1<?php
2/**
3 * Doctrine_Ticket_SegmentationFault_TestCase
4 *
5 * @package     Doctrine
6 * @author      Tiago Ribeiro <tiago.ribeiro@gmail.com>
7 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
8 * @category    Object Relational Mapping
9 * @link        www.doctrine-project.org
10 * @since       1.0
11 * @version     $Revision$
12 *
13 */
14
15class Doctrine_Ticket_876_TestCase extends Doctrine_UnitTestCase
16{
17    public function prepareTables()
18    {
19        $this->tables = array('Profile', 'Person', 'sfGuardUser');
20
21        parent::prepareTables();
22    }
23
24    public function prepareData()
25    {
26    }
27
28    public function newPerson($name)
29    {
30      // Creating sfGuardUser
31      $guardUser = new sfGuardUser();
32
33      $guardUser->set('name', $name);
34
35      $guardUser->save();
36
37      // Creating the Person
38      $person = new Person();
39
40      $person->set('name', $name);
41      $person->set('sf_guard_user_id', $guardUser['id']);
42
43      $person->save();
44
45      return $person;
46    }
47
48    public function newProfile($name, $person)
49    {
50      $profile = new Profile();
51
52      $profile->set('name', $name);
53      $profile->set('person_id', $person['id']);
54
55      $profile->save();
56
57      return $profile;
58    }
59
60    public function testBug()
61    {
62      $person = $this->newPerson('Fixe');
63      $profile = $this->newProfile('Work', $person);
64
65      $guardUser = $person->get('sfGuardUser');
66      $id = $guardUser->get('id');
67
68      $guardUser->free();
69
70      $query = new Doctrine_Query();
71
72      $query->select('s.*, p.*, ps.*');
73      $query->from('sfGuardUser s');
74      $query->innerJoin('s.Person p');
75      $query->leftJoin('p.Profiles ps');
76      $query->where('s.id = ?', $id);
77
78      $user = $query->fetchOne();
79      $array = $user->toArray(true);
80
81      $this->assertEqual($array['id'], 1);
82      $this->assertEqual($array['name'], 'Fixe');
83      $this->assertTrue(isset($array['Person']['Profiles'][0]));
84    }
85}
86
87class Person extends Doctrine_Record
88{
89  public function setTableDefinition()
90  {
91    $this->setTableName('person');
92    $this->hasColumn('id', 'integer', 11, array('primary' => true, 'autoincrement' => true));
93    $this->hasColumn('name', 'string', 255);
94    $this->hasColumn('sf_guard_user_id', 'integer', 4);
95  }
96
97  public function setUp()
98  {
99    parent::setUp();
100    $this->hasMany('Profile as Profiles', array('local' => 'id',
101                                                'foreign' => 'person_id'));
102    $this->hasOne('sfGuardUser', array('local' => 'sf_guard_user_id',
103                                       'foreign' => 'id',
104                                       'onDelete' => 'CASCADE'));
105  }
106}
107
108class Profile extends Doctrine_Record
109{
110  public function setTableDefinition()
111  {
112    $this->setTableName('profile');
113    $this->hasColumn('id', 'integer', 11, array('primary' => true, 'autoincrement' => true));
114    $this->hasColumn('name', 'string', 150);
115    $this->hasColumn('person_id', 'integer', 11);
116  }
117
118  public function setUp()
119  {
120    parent::setUp();
121    $this->hasOne('Person', array('local' => 'person_id',
122                                'foreign' => 'id'));
123  }
124}
125
126class sfGuardUser extends Doctrine_Record
127{
128  public function setTableDefinition()
129  {
130    $this->setTableName('sf_guard_user');
131    $this->hasColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true));
132    $this->hasColumn('name', 'string', 128, array('notnull' => true, 'unique' => true));
133  }
134
135  public function setUp()
136  {
137    parent::setUp();
138    $this->hasOne('Person', array('local' => 'id',
139                                  'foreign' => 'sf_guard_user_id'));
140  }
141}