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_DC39_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_DC39_TestCase extends Doctrine_UnitTestCase
34{
35    public function prepareData()
36    {
37        $g1 = new Ticket_DC39_Group();
38        $g1['name'] = 'group1';
39        $g1->save();
40
41        $g2 = new Ticket_DC39_Group();
42        $g2['name'] = 'group2';
43        $g2->save();
44
45        $u1 = new Ticket_DC39_User();
46        $u1['group_id'] = 1;
47        $u1['name'] = 'user1';
48        $u1->save();
49
50        $u2 = new Ticket_DC39_User();
51        $u2['group_id'] = 2;
52        $u2['name'] = 'user2';
53        $u2->save();
54    }
55
56    public function prepareTables()
57    {
58        $this->tables[] = 'Ticket_DC39_Group';
59        $this->tables[] = 'Ticket_DC39_User';
60        parent::prepareTables();
61    }
62
63    public function testOneToManyRelationsWithSynchronizeWithArray()
64    {
65    		// link group (id 2) with users (id 1,2)
66    		$group = Doctrine_Core::getTable('Ticket_DC39_Group')->find(2);
67    		$group->synchronizeWithArray(array(
68    			'Users' => array(1, 2)
69    		));
70    		$group->save();
71
72    		// update the user-objects with real data from database
73    		$user1 = Doctrine_Core::getTable('Ticket_DC39_User')->find(1);
74    		$user2 = Doctrine_Core::getTable('Ticket_DC39_User')->find(2);
75
76    		// compare the group_id (should be 2) with the group_id set through $group->synchronizeWithArray
77    		$this->assertEqual($group->Users[0]->group_id, 2);
78    		$this->assertEqual($group->Users[1]->group_id, 2);
79    }
80
81}
82
83class Ticket_DC39_Group extends Doctrine_Record
84{
85	public function setTableDefinition()
86	{
87		$this->hasColumn('name', 'string', 255);
88	}
89
90	public function setUp()
91	{
92		$this->hasMany('Ticket_DC39_User as Users', array(
93			'local' => 'id',
94			'foreign' => 'group_id'
95		));
96	}
97}
98
99class Ticket_DC39_User extends Doctrine_Record
100{
101	public function setTableDefinition()
102	{
103		$this->hasColumn('group_id', 'integer');
104		$this->hasColumn('name', 'string', 255);
105	}
106
107	public function setUp()
108	{
109		$this->hasOne('Ticket_DC39_Group as Group', array(
110			'local' => 'group_id',
111			'foreign' => 'id'
112		));
113	}
114}