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_632_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_632_TestCase extends Doctrine_UnitTestCase
34{
35    public function prepareTables()
36    {
37        $this->tables[] = 'Ticket_632_User';
38        $this->tables[] = 'Ticket_632_Group';
39        $this->tables[] = 'Ticket_632_UserGroup';
40        parent::prepareTables();
41    }
42
43    public function prepareData()
44    {
45        $user = new Ticket_632_User();
46        $user->username = 'jwage';
47        $user->password = 'changeme';
48        $user->Groups[]->name = 'Group One';
49        $user->Groups[]->name = 'Group Two';
50        $user->Groups[]->name = 'Group Three';
51        $user->save();
52    }
53
54    public function testTest()
55    {
56        $user = Doctrine_Query::create()
57            ->from('Ticket_632_User u, u.Groups g')
58            ->where('u.username = ?', 'jwage')
59            ->limit(1)
60            ->fetchOne();
61        $this->assertEqual($user->Groups->count(), 3);
62        unset($user->Groups[2]);
63        $this->assertEqual($user->Groups->count(), 2);
64        $user->save(); // This deletes the UserGroup association and the Group record
65
66        // We should still have 3 groups
67        $groups = Doctrine_Core::getTable('Ticket_632_Group')->findAll();
68        $this->assertEqual($groups->count(), 3);
69    }
70}
71
72class Ticket_632_User extends Doctrine_Record
73{
74    public function setTableDefinition()
75    {
76        $this->hasColumn('username', 'string', 255);
77        $this->hasColumn('password', 'string', 255);
78    }
79
80    public function setUp()
81    {
82        $this->hasMany('Ticket_632_Group as Groups', array('local'   => 'user_id',
83                                                           'foreign' => 'group_id',
84                                                           'refClass' => 'Ticket_632_UserGroup'));
85    }
86}
87
88class Ticket_632_Group extends Doctrine_Record
89{
90    public function setTableDefinition()
91    {
92        $this->hasColumn('name', 'string', 255);
93    }
94
95    public function setUp()
96    {
97        $this->hasMany('Ticket_632_User as Users', array('local'   => 'group_id',
98                                                         'foreign' => 'user_id',
99                                                         'refClass' => 'Ticket_632_UserGroup'));
100    }
101}
102
103class Ticket_632_UserGroup extends Doctrine_Record
104{
105    public function setTableDefinition()
106    {
107        $this->hasColumn('user_id', 'integer', 4, array('primary' => true));
108        $this->hasColumn('group_id', 'integer', 4, array('primary' => true));
109    }
110
111    public function setUp()
112    {
113        $this->hasOne('Ticket_632_User as User', array('local'   => 'user_id',
114                                                       'foreign' => 'id'));
115
116        $this->hasOne('Ticket_632_Group as Group', array('local' => 'group_id',
117                                                         'foreign' => 'id'));
118    }
119}