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_2355_TestCase
24 *
25 * @package     Doctrine
26 * @author      Fabien Pennequin <fabien.pennequin@gmail.com>
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_2355_TestCase extends Doctrine_UnitTestCase
34{
35    public function prepareTables()
36    {
37        $this->tables[] = 'News';
38        $this->tables[] = 'Episode';
39        $this->tables[] = 'Writer';
40        $this->tables[] = 'WriterEpisode';
41        $this->tables[] = 'Director';
42        $this->tables[] = 'DirectorEpisode';
43        parent::prepareTables();
44    }
45
46    public function testImport()
47    {
48        $yml = <<<END
49Director:
50  david_nutter:
51    name: David Nutter
52
53
54Writer:
55  alfred_gough:
56    name: Alfred Gough
57  miles_millar:
58    name: Miles Millar
59
60
61News:
62  News_1:
63    title: Date de retour de Smallville aux Etats-Unis
64
65  News_2:
66    title: Audiences de l'épisode 8.22 Doomsday aux Etats-Unis
67
68
69Episode:
70  Episode_101:
71    season: 1
72    number: 1
73    title_us: Pilot
74    title_fr: Bienvenue sur Terre
75    Directors: [david_nutter]
76    Writers: [alfred_gough, miles_millar]
77END;
78        try {
79            file_put_contents('test.yml', $yml);
80            Doctrine_Core::loadData('test.yml', true);
81
82            $this->conn->clear();
83
84            $query = new Doctrine_Query();
85            $query->from('Episode e, e.Directors, e.Writers');
86
87            $e = $query->execute();
88
89            $this->assertEqual($e->count(), 1);
90            $this->assertEqual($e[0]->season, 1);
91            $this->assertEqual($e[0]->number, 1);
92            $this->assertEqual($e[0]->title_us, 'Pilot');
93            $this->assertEqual($e[0]->title_fr, 'Bienvenue sur Terre');
94            $this->assertEqual($e[0]->Directors->count(), 1);
95            $this->assertEqual($e[0]->Directors[0]->name, 'David Nutter');
96            $this->assertEqual($e[0]->Writers->count(), 2);
97            $this->assertEqual($e[0]->Writers[0]->name, 'Alfred Gough');
98            $this->assertEqual($e[0]->Writers[1]->name, 'Miles Millar');
99
100            $query = new Doctrine_Query();
101            $query->from('News n');
102
103            $n = $query->execute();
104
105            $this->assertEqual($n->count(), 2);
106            $this->assertEqual($n[0]->title, 'Date de retour de Smallville aux Etats-Unis');
107            $this->assertEqual($n[1]->title, 'Audiences de l\'épisode 8.22 Doomsday aux Etats-Unis');
108
109            $this->pass();
110        } catch (Exception $e) {
111            $this->fail();
112        }
113
114        unlink('test.yml');
115    }
116}
117
118class News extends Doctrine_Record
119{
120    public function setTableDefinition()
121    {
122        $this->hasColumn('title', 'string', 255, array(
123             'type' => 'string',
124             'notnull' => true,
125             'notblank' => true,
126             'length' => '255',
127             ));
128    }
129}
130
131class Episode extends Doctrine_Record
132{
133    public function setTableDefinition()
134    {
135        $this->hasColumn('season', 'integer', 1, array(
136             'type' => 'integer',
137             'length' => 1,
138             'notnull' => true,
139             'notblank' => true,
140             ));
141        $this->hasColumn('number', 'integer', 1, array(
142             'type' => 'integer',
143             'length' => 1,
144             'notnull' => true,
145             'notblank' => true,
146             ));
147        $this->hasColumn('title_us', 'string', 100, array(
148             'type' => 'string',
149             'notnull' => true,
150             'notblank' => true,
151             'length' => '100',
152             ));
153        $this->hasColumn('title_fr', 'string', 100, array(
154             'type' => 'string',
155             'length' => '100',
156             ));
157
158
159        $this->index('episode', array(
160             'fields' =>
161             array(
162              0 => 'season',
163              1 => 'number',
164             ),
165             'type' => 'unique',
166             ));
167    }
168
169    public function setUp()
170    {
171        $this->hasMany('Writer as Writers', array(
172             'refClass' => 'WriterEpisode',
173             'local' => 'episode_id',
174             'foreign' => 'writer_id'));
175
176        $this->hasMany('Director as Directors', array(
177             'refClass' => 'DirectorEpisode',
178             'local' => 'episode_id',
179             'foreign' => 'director_id'));
180
181        $this->hasMany('WriterEpisode', array(
182             'local' => 'id',
183             'foreign' => 'episode_id'));
184
185        $this->hasMany('DirectorEpisode', array(
186             'local' => 'id',
187             'foreign' => 'episode_id'));
188    }
189}
190
191class Writer extends Doctrine_Record
192{
193    public function setTableDefinition()
194    {
195        $this->hasColumn('name', 'string', 150, array(
196             'type' => 'string',
197             'notnull' => true,
198             'notblank' => true,
199             'unique' => true,
200             'length' => '150',
201             ));
202    }
203
204    public function setUp()
205    {
206        $this->hasMany('Episode', array(
207             'refClass' => 'WriterEpisode',
208             'local' => 'writer_id',
209             'foreign' => 'episode_id'));
210
211        $this->hasMany('WriterEpisode', array(
212             'local' => 'id',
213             'foreign' => 'writer_id'));
214    }
215}
216
217class WriterEpisode extends Doctrine_Record
218{
219    public function setTableDefinition()
220    {
221        $this->hasColumn('episode_id', 'integer', null, array(
222             'type' => 'integer',
223             'primary' => true,
224             ));
225        $this->hasColumn('writer_id', 'integer', null, array(
226             'type' => 'integer',
227             'primary' => true,
228             ));
229    }
230
231    public function setUp()
232    {
233        $this->hasOne('Writer', array(
234             'local' => 'writer_id',
235             'foreign' => 'id',
236             'onDelete' => 'CASCADE'));
237
238        $this->hasOne('Episode', array(
239             'local' => 'episode_id',
240             'foreign' => 'id',
241             'onDelete' => 'CASCADE'));
242    }
243}
244
245class Director extends Doctrine_Record
246{
247    public function setTableDefinition()
248    {
249        $this->hasColumn('name', 'string', 150, array(
250             'type' => 'string',
251             'notnull' => true,
252             'notblank' => true,
253             'unique' => true,
254             'length' => '150',
255             ));
256    }
257
258    public function setUp()
259    {
260        $this->hasMany('Episode', array(
261             'refClass' => 'DirectorEpisode',
262             'local' => 'director_id',
263             'foreign' => 'episode_id'));
264
265        $this->hasMany('DirectorEpisode', array(
266             'local' => 'id',
267             'foreign' => 'director_id'));
268    }
269}
270
271class DirectorEpisode extends Doctrine_Record
272{
273    public function setTableDefinition()
274    {
275        $this->hasColumn('episode_id', 'integer', null, array(
276             'type' => 'integer',
277             'primary' => true,
278             ));
279        $this->hasColumn('director_id', 'integer', null, array(
280             'type' => 'integer',
281             'primary' => true,
282             ));
283    }
284
285    public function setUp()
286    {
287        $this->hasOne('Director', array(
288             'local' => 'director_id',
289             'foreign' => 'id',
290             'onDelete' => 'CASCADE'));
291
292        $this->hasOne('Episode', array(
293             'local' => 'episode_id',
294             'foreign' => 'id',
295             'onDelete' => 'CASCADE'));
296    }
297}