root/trunk/generator/test/classes/propel/GeneratedObjectRelTest.php

Revision 1082, 7.9 kB (checked in by tony, 3 months ago)

Refs #683: Added support for PHP 5.3 namespaces

  • Property svn:keywords set to Id Rev Date Author HeadURL Revision
Line 
1 <?php
2
3 use bookstore::Model as PropelModel;
4 use bookstore::Peer as PropelPeer;
5
6 /*
7  *  $Id$
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
13  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
14  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
16  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
17  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  *
21  * This software consists of voluntary contributions made by many individuals
22  * and is licensed under version 3 of the LGPL. For more information please see
23  * <http://propel.phpdb.org>.
24  */
25
26 require_once 'bookstore/BookstoreTestBase.php';
27
28 /**
29  * Tests relationships between generated Object classes.
30  *
31  * This test uses generated Bookstore classes to test the behavior of various
32  * object operations.  The _idea_ here is to test every possible generated method
33  * from Object.tpl; if necessary, bookstore will be expanded to accommodate this.
34  *
35  * The database is relaoded before every test and flushed after every test.  This
36  * means that you can always rely on the contents of the databases being the same
37  * for each test method in this class.  See the BookstoreDataPopulator::populate()
38  * method for the exact contents of the database.
39  *
40  * @see        BookstoreDataPopulator
41  * @author     Hans Lellelid <hans@xmpl.org>
42  */
43 class GeneratedObjectRelTest extends BookstoreTestBase {
44
45     /**
46      * Tests one side of a bi-directional setting of many-to-many relationships.
47      */
48     public function testManyToMany_Dir1()
49     {
50         $list = new PropelModel::BookClubList();
51         $list->setGroupLeader('Archimedes Q. Porter');
52         // No save ...
53
54         $book = new PropelModel::Book();
55         $book->setTitle( "Jungle Expedition Handbook" );
56         $book->setISBN('TEST');
57         // No save ...
58
59         $this->assertEquals(0, count($list->getBookListRels()) );
60         $this->assertEquals(0, count($book->getBookListRels()) );
61         $this->assertEquals(0, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
62
63         $xref = new PropelModel::BookListRel();
64         $xref->setBook($book);
65         $list->addBookListRel($xref);
66
67         $this->assertEquals(1, count($list->getBookListRels()));
68         $this->assertEquals(1, count($book->getBookListRels()) );
69         $this->assertEquals(0, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
70
71         $list->save();
72
73         $this->assertEquals(1, count($list->getBookListRels()) );
74         $this->assertEquals(1, count($book->getBookListRels()) );
75         $this->assertEquals(1, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
76
77     }
78
79     /**
80      * Tests reverse setting of one of many-to-many relationship, with all saves cascaded.
81      */
82     public function testManyToMany_Dir2_Unsaved()
83     {
84         $list = new PropelModel::BookClubList();
85         $list->setGroupLeader('Archimedes Q. Porter');
86         // No save ...
87
88         $book = new PropelModel::Book();
89         $book->setTitle( "Jungle Expedition Handbook" );
90         $book->setISBN('TEST');
91         // No save (yet) ...
92
93         $this->assertEquals(0, count($list->getBookListRels()) );
94         $this->assertEquals(0, count($book->getBookListRels()) );
95         $this->assertEquals(0, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
96
97         $xref = new PropelModel::BookListRel();
98         $xref->setBookClubList($list);
99         $book->addBookListRel($xref);
100
101         $this->assertEquals(1, count($list->getBookListRels()) );
102         $this->assertEquals(1, count($book->getBookListRels()) );
103         $this->assertEquals(0, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
104         $book->save();
105
106         $this->assertEquals(1, count($list->getBookListRels()) );
107         $this->assertEquals(1, count($book->getBookListRels()) );
108         $this->assertEquals(1, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
109
110     }
111
112     /**
113      * Tests reverse setting of relationships, saving one of the objects first.
114      * @link       http://propel.phpdb.org/trac/ticket/508
115      */
116     public function testManyToMany_Dir2_Saved()
117     {
118         $list = new PropelModel::BookClubList();
119         $list->setGroupLeader('Archimedes Q. Porter');
120         $list->save();
121
122         $book = new PropelModel::Book();
123         $book->setTitle( "Jungle Expedition Handbook" );
124         $book->setISBN('TEST');
125         // No save (yet) ...
126
127         $this->assertEquals(0, count($list->getBookListRels()) );
128         $this->assertEquals(0, count($book->getBookListRels()) );
129         $this->assertEquals(0, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
130
131         // Now set the relationship from the opposite direction.
132
133         $xref = new PropelModel::BookListRel();
134         $xref->setBookClubList($list);
135         $book->addBookListRel($xref);
136
137         $this->assertEquals(1, count($list->getBookListRels()) );
138         $this->assertEquals(1, count($book->getBookListRels()) );
139         $this->assertEquals(0, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
140         $book->save();
141
142         $this->assertEquals(1, count($list->getBookListRels()) );
143         $this->assertEquals(1, count($book->getBookListRels()) );
144         $this->assertEquals(1, count(PropelPeer::BookListRelPeer::doSelect(new ::Criteria())) );
145
146     }
147
148     /**
149      * Test behavior of columns that are implicated in multiple foreign keys.
150      * @link       http://propel.phpdb.org/trac/ticket/228
151      */
152     public function testMultiFkImplication()
153     {
154         // Create a new bookstore, contest, bookstore_contest, and bookstore_contest_entry
155         $b = new PropelModel::Bookstore();
156         $b->setStoreName("Foo!");
157         $b->save();
158
159         $c = new PropelModel::Contest();
160         $c->setName("Bookathon Contest");
161         $c->save();
162
163         $bc = new PropelModel::BookstoreContest();
164         $bc->setBookstore($b);
165         $bc->setContest($c);
166         $bc->save();
167
168         $c = new PropelModel::Customer();
169         $c->setName("Happy Customer");
170         $c->save();
171
172         $bce = new PropelModel::BookstoreContestEntry();
173         $bce->setBookstore($b);
174         $bce->setBookstoreContest($bc);
175         $bce->setCustomer($c);
176         $bce->save();
177
178         $bce->setBookstoreId(null);
179
180         $this->assertNull($bce->getBookstoreContest());
181         $this->assertNull($bce->getBookstore());
182     }
183
184     /**
185      * Test the clearing of related object collection.
186      * @link       http://propel.phpdb.org/trac/ticket/529
187      */
188     public function testClearRefFk()
189     {
190         $book = new PropelModel::Book();
191         $book->setISBN("Foo-bar-baz");
192         $book->setTitle("The book title");
193
194         // No save ...
195
196         $r = new PropelModel::Review();
197         $r->setReviewedBy('Me');
198         $r->setReviewDate(new DateTime("now"));
199
200         $book->addReview($r);
201
202         // No save (yet) ...
203
204         $this->assertEquals(1, count($book->getReviews()) );
205         $book->clearReviews();
206         $this->assertEquals(0, count($book->getReviews()));
207     }
208
209     /**
210      * This tests to see whether modified objects are being silently overwritten by calls to fk accessor methods.
211      * @link       http://propel.phpdb.org/trac/ticket/509#comment:5
212      */
213     public function testModifiedObjectOverwrite()
214     {
215         $author = new PropelModel::Author();
216         $author->setFirstName("John");
217         $author->setLastName("Public");
218
219         $books = $author->getBooks(); // empty, of course
220         $this->assertEquals(array(), $books, "Expected empty array.");
221
222         $book = new PropelModel::Book();
223         $book->setTitle("A sample book");
224         $book->setISBN("INITIAL ISBN");
225
226         $author->addBook($book);
227
228         $author->save();
229
230         $book->setISBN("MODIFIED ISBN");
231
232         $books = $author->getBooks();
233         $this->assertEquals(1, count($books), "Expected 1 book.");
234         $this->assertSame($book, $books[0], "Expected the same object to be returned by fk accessor.");
235         $this->assertEquals("MODIFIED ISBN", $books[0]->getISBN(), "Expected the modified value NOT to have been overwritten.");
236     }
237 }
238
Note: See TracBrowser for help on using the browser.