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

Revision 1068, 24.7 kB (checked in by ron, 2 days ago)

Merge in changes from 1.3 branch: merge -r 832:1067

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Rev Date Author HeadURL Revision
Line 
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 version 3 of the LGPL. For more information please see
19  * <http://propel.phpdb.org>.
20  */
21
22 require_once 'bookstore/BookstoreTestBase.php';
23
24 /**
25  * Tests the generated Peer classes.
26  *
27  * This test uses generated Bookstore classes to test the behavior of various
28  * peer operations.
29  *
30  * The database is relaoded before every test and flushed after every test.  This
31  * means that you can always rely on the contents of the databases being the same
32  * for each test method in this class.  See the BookstoreDataPopulator::populate()
33  * method for the exact contents of the database.
34  *
35  * @see        BookstoreDataPopulator
36  * @author     Hans Lellelid <hans@xmpl.org>
37  */
38 class GeneratedPeerTest extends BookstoreTestBase {
39
40     /**
41      * Test ability to delete multiple rows via single Criteria object.
42      */
43     public function testDoDelete_MultiTable() {
44
45         $selc = new Criteria();
46         $selc->add(BookPeer::TITLE, "Harry Potter and the Order of the Phoenix");
47         $hp = BookPeer::doSelectOne($selc);
48
49         // print "Attempting to delete [multi-table] by found pk: ";
50         $c = new Criteria();
51         $c->add(BookPeer::ID, $hp->getId());
52         // The only way for multi-delete to work currently
53         // is to specify the author_id and publisher_id (i.e. the fkeys
54         // have to be in the criteria).
55         $c->add(AuthorPeer::ID, $hp->getAuthorId());
56         $c->add(PublisherPeer::ID, $hp->getPublisherId());
57         $c->setSingleRecord(true);
58         BookPeer::doDelete($c);
59
60         //print_r(AuthorPeer::doSelect(new Criteria()));
61
62         // check to make sure the right # of records was removed
63         $this->assertEquals(3, count(AuthorPeer::doSelect(new Criteria())), "Expected 3 authors after deleting.");
64         $this->assertEquals(3, count(PublisherPeer::doSelect(new Criteria())), "Expected 3 publishers after deleting.");
65         $this->assertEquals(3, count(BookPeer::doSelect(new Criteria())), "Expected 3 books after deleting.");
66     }
67
68     /**
69      * Test using a complex criteria to delete multiple rows from a single table.
70      */
71     public function testDoDelete_ComplexCriteria() {
72
73         //print "Attempting to delete books by complex criteria: ";
74         $c = new Criteria();
75         $cn = $c->getNewCriterion(BookPeer::ISBN, "043935806X");
76         $cn->addOr($c->getNewCriterion(BookPeer::ISBN, "0380977427"));
77         $cn->addOr($c->getNewCriterion(BookPeer::ISBN, "0140422161"));
78         $c->add($cn);
79         BookPeer::doDelete($c);
80
81         // now there should only be one book left; "The Tin Drum"
82
83         $books = BookPeer::doSelect(new Criteria());
84
85         $this->assertEquals(1, count($books), "Expected 1 book remaining after deleting.");
86         $this->assertEquals("The Tin Drum", $books[0]->getTitle(), "Expect the only remaining book to be 'The Tin Drum'");
87     }
88
89     /**
90      * Test that cascading deletes are happening correctly (whether emulated or native).
91      */
92     public function testDoDelete_Cascade_Simple()
93     {
94
95         // The 'media' table will cascade from book deletes
96
97         // 1) Assert the row exists right now
98
99         $medias = MediaPeer::doSelect(new Criteria());
100         $this->assertTrue(count($medias) > 0, "Expected to find at least one row in 'media' table.");
101         $media = $medias[0];
102         $mediaId = $media->getId();
103
104         // 2) Delete the owning book
105
106         $owningBookId = $media->getBookId();
107         BookPeer::doDelete($owningBookId);
108
109         // 3) Assert that the media row is now also gone
110
111         $obj = MediaPeer::retrieveByPK($mediaId);
112         $this->assertNull($obj, "Expect NULL when retrieving on no matching Media.");
113
114     }
115
116     /**
117      * Test that cascading deletes are happening correctly for composite pk.
118      * @link       http://propel.phpdb.org/trac/ticket/544
119      */
120     public function testDoDelete_Cascade_CompositePK()
121     {
122
123         $origBceCount = BookstoreContestEntryPeer::doCount(new Criteria());
124
125         $cust1 = new Customer();
126         $cust1->setName("Cust1");
127         $cust1->save();
128
129         $cust2 = new Customer();
130         $cust2->setName("Cust2");
131         $cust2->save();
132
133         $c1 = new Contest();
134         $c1->setName("Contest1");
135         $c1->save();
136
137         $c2 = new Contest();
138         $c2->setName("Contest2");
139         $c2->save();
140
141         $store1 = new Bookstore();
142         $store1->setStoreName("Store1");
143         $store1->save();
144
145         $bc1 = new BookstoreContest();
146         $bc1->setBookstore($store1);
147         $bc1->setContest($c1);
148         $bc1->save();
149
150         $bc2 = new BookstoreContest();
151         $bc2->setBookstore($store1);
152         $bc2->setContest($c2);
153         $bc2->save();
154
155         $bce1 = new BookstoreContestEntry();
156         $bce1->setEntryDate("now");
157         $bce1->setCustomer($cust1);
158         $bce1->setBookstoreContest($bc1);
159         $bce1->save();
160
161         $bce2 = new BookstoreContestEntry();
162         $bce2->setEntryDate("now");
163         $bce2->setCustomer($cust1);
164         $bce2->setBookstoreContest($bc2);
165         $bce2->save();
166
167         // Now, if we remove $bc1, we expect *only* bce1 to be no longer valid.
168
169         BookstoreContestPeer::doDelete($bc1);
170
171         $newCount = BookstoreContestEntryPeer::doCount(new Criteria());
172
173         $this->assertEquals($origBceCount + 1, $newCount, "Expected new number of rows in BCE to be orig + 1");
174
175         $bcetest = BookstoreContestEntryPeer::retrieveByPK($store1->getId(), $c1->getId(), $cust1->getId());
176         $this->assertNull($bcetest, "Expected BCE for store1 to be cascade deleted.");
177
178         $bcetest2 = BookstoreContestEntryPeer::retrieveByPK($store1->getId(), $c2->getId(), $cust1->getId());
179         $this->assertNotNull($bcetest2, "Expected BCE for store2 to NOT be cascade deleted.");
180
181     }
182
183     /**
184      * Test that onDelete="SETNULL" is happening correctly (whether emulated or native).
185      */
186     public function testDoDelete_SetNull() {
187
188         // The 'author_id' column in 'book' table will be set to null when author is deleted.
189
190         // 1) Get an arbitrary book
191         $c = new Criteria();
192         $book = BookPeer::doSelectOne($c);
193         $bookId = $book->getId();
194         $authorId = $book->getAuthorId();
195         unset($book);
196
197         // 2) Delete the author for that book
198         AuthorPeer::doDelete($authorId);
199
200         // 3) Assert that the book.author_id column is now NULL
201
202         $book = BookPeer::retrieveByPK($bookId);
203         $this->assertNull($book->getAuthorId(), "Expect the book.author_id to be NULL after the author was removed.");
204
205     }
206
207     /**
208      * Test deleting a row by passing in the primary key to the doDelete() method.
209      */
210     public function testDoDelete_ByPK() {
211
212         // 1) get an arbitrary book
213         $book = BookPeer::doSelectOne(new Criteria());
214         $bookId = $book->getId();
215
216         // 2) now delete that book
217         BookPeer::doDelete($bookId);
218
219         // 3) now make sure it's gone
220         $obj = BookPeer::retrieveByPK($bookId);
221         $this->assertNull($obj, "Expect NULL when retrieving on no matching Book.");
222
223     }
224
225     /**
226      * Test deleting a row by passing the generated object to doDelete().
227      */
228     public function testDoDelete_ByObj() {
229
230         // 1) get an arbitrary book
231         $book = BookPeer::doSelectOne(new Criteria());
232         $bookId = $book->getId();
233
234         // 2) now delete that book
235         BookPeer::doDelete($book);
236
237         // 3) now make sure it's gone
238         $obj = BookPeer::retrieveByPK($bookId);
239         $this->assertNull($obj, "Expect NULL when retrieving on no matching Book.");
240
241     }
242
243
244     /**
245      * Test the doDeleteAll() method for single table.
246      */
247     public function testDoDeleteAll() {
248
249         BookPeer::doDeleteAll();
250         $this->assertEquals(0, count(BookPeer::doSelect(new Criteria())), "Expect all book rows to have been deleted.");
251     }
252
253     /**
254      * Test the doDeleteAll() method when onDelete="CASCADE".
255      */
256     public function testDoDeleteAll_Cascade() {
257
258         BookPeer::doDeleteAll();
259         $this->assertEquals(0, count(MediaPeer::doSelect(new Criteria())), "Expect all media rows to have been cascade deleted.");
260         $this->assertEquals(0, count(ReviewPeer::doSelect(new Criteria())), "Expect all review rows to have been cascade deleted.");
261     }
262
263     /**
264      * Test the doDeleteAll() method when onDelete="SETNULL".
265      */
266     public function testDoDeleteAll_SetNull() {
267
268         $c = new Criteria();
269         $c->add(BookPeer::AUTHOR_ID, null, Criteria::NOT_EQUAL);
270
271         // 1) make sure there are some books with valid authors
272         $this->assertTrue(count(BookPeer::doSelect($c)) > 0, "Expect some book.author_id columns that are not NULL.");
273
274         // 2) delete all the authors
275         AuthorPeer::doDeleteAll();
276
277         // 3) now verify that the book.author_id columns are all nul
278         $this->assertEquals(0, count(BookPeer::doSelect($c)), "Expect all book.author_id columns to be NULL.");
279     }
280
281     /**
282      * Test the doInsert() method when passed a Criteria object.
283      */
284     public function testDoInsert_Criteria() {
285
286         $name = "A Sample Publisher - " . time();
287
288         $values = new Criteria();
289         $values->add(PublisherPeer::NAME, $name);
290         PublisherPeer::doInsert($values);
291
292         $c = new Criteria();
293         $c->add(PublisherPeer::NAME, $name);
294
295         $matches = PublisherPeer::doSelect($c);
296         $this->assertEquals(1, count($matches), "Expect there to be exactly 1 publisher just-inserted.");
297         $this->assertTrue( 1 != $matches[0]->getId(), "Expected to have different ID than one put in values Criteria.");
298
299     }
300
301     /**
302      * Test the doInsert() method when passed a generated object.
303      */
304     public function testDoInsert_Obj() {
305
306         $name = "A Sample Publisher - " . time();
307
308         $values = new Publisher();
309         $values->setName($name);
310         PublisherPeer::doInsert($values);
311
312         $c = new Criteria();
313         $c->add(PublisherPeer::NAME, $name);
314
315         $matches = PublisherPeer::doSelect($c);
316         $this->assertEquals(1, count($matches), "Expect there to be exactly 1 publisher just-inserted.");
317         $this->assertTrue( 1 != $matches[0]->getId(), "Expected to have different ID than one put in values Criteria.");
318
319     }
320
321     /**
322      * Tests performing doSelect() and doSelectJoin() using LIMITs.
323      */
324     public function testDoSelect_Limit() {
325
326         // 1) get the total number of items in a particular table
327         $count = BookPeer::doCount(new Criteria());
328
329         $this->assertTrue($count > 1, "Need more than 1 record in books table to perform this test.");
330
331         $limitcount = $count - 1;
332
333         $lc = new Criteria();
334         $lc->setLimit($limitcount);
335
336         $results = BookPeer::doSelect($lc);
337
338         $this->assertEquals($limitcount, count($results), "Expected $limitcount results from BookPeer::doSelect()");
339
340         // re-create it just to avoid side-effects
341         $lc2 = new Criteria();
342         $lc2->setLimit($limitcount);
343         $results2 = BookPeer::doSelectJoinAuthor($lc2);
344
345         $this->assertEquals($limitcount, count($results2), "Expected $limitcount results from BookPeer::doSelectJoinAuthor()");
346
347     }
348
349     /**
350      * Test the basic functionality of the doSelectJoin*() methods.
351      */
352     public function testDoSelectJoin()
353     {
354
355         BookPeer::clearInstancePool();
356
357         $c = new Criteria();
358
359         $books = BookPeer::doSelect($c);
360         $obj = $books[0];
361         $size = strlen(serialize($obj));
362
363         BookPeer::clearInstancePool();
364
365         $joinBooks = BookPeer::doSelectJoinAuthor($c);
366         $obj2 = $joinBooks[0];
367         $joinSize = strlen(serialize($obj2));
368
369         $this->assertEquals(count($books), count($joinBooks), "Expected to find same number of rows in doSelectJoin*() call as doSelect() call.");
370
371         $this->assertTrue($joinSize > $size, "Expected a serialized join object to be larger than a non-join object.");
372     }
373
374     /**
375      * Test the doSelectJoin*() methods when the related object is NULL.
376      */
377     public function testDoSelectJoin_NullFk()
378     {
379         $b1 = new Book();
380         $b1->setTitle("Test NULLFK 1");
381         $b1->setISBN("NULLFK-1");
382         $b1->save();
383
384         $b2 = new Book();
385         $b2->setTitle("Test NULLFK 2");
386         $b2->setISBN("NULLFK-2");
387         $b2->setAuthor(new Author());
388         $b2->getAuthor()->setFirstName("Hans")->setLastName("L");
389         $b2->save();
390
391         BookPeer::clearInstancePool();
392         AuthorPeer::clearInstancePool();
393
394         $c = new Criteria();
395         $c->add(BookPeer::ISBN, 'NULLFK-%', Criteria::LIKE);
396         $c->addAscendingOrderByColumn(BookPeer::ISBN);
397
398         $matches = BookPeer::doSelectJoinAuthor($c);
399         $this->assertEquals(2, count($matches), "Expected 2 matches back from new books; got back " . count($matches));
400
401         $this->assertNull($matches[0]->getAuthor(), "Expected first book author to be null");
402         $this->assertType('Author', $matches[1]->getAuthor(), "Expected valid Author object for second book.");
403     }
404
405     public function testObjectInstances()
406     {
407
408         $sample = BookPeer::doSelectOne(new Criteria());
409         $samplePk = $sample->getPrimaryKey();
410
411         // 1) make sure consecutive calls to retrieveByPK() return the same object.
412
413         $b1 = BookPeer::retrieveByPK($samplePk);
414         $b2 = BookPeer::retrieveByPK($samplePk);
415
416         $sampleval = md5(microtime());
417
418         $this->assertTrue($b1 === $b2, "Expected object instances to match for calls with same retrieveByPK() method signature.");
419
420         // 2) make sure that calls to doSelect also return references to the same objects.
421         $allbooks = BookPeer::doSelect(new Criteria());
422         foreach ($allbooks as $testb) {
423             if ($testb->getPrimaryKey() == $b1->getPrimaryKey()) {
424                 $this->assertTrue($testb === $b1, "Expected same object instance from doSelect() as from retrieveByPK()");
425             }
426         }
427
428         // 3) test fetching related objects
429         $book = BookPeer::retrieveByPK($samplePk);
430
431         $bookauthor = $book->getAuthor();
432
433         $author = AuthorPeer::retrieveByPK($bookauthor->getId());
434
435         $this->assertTrue($bookauthor === $author, "Expected same object instance when calling fk object accessor as retrieveByPK()");
436
437         // 4) test a doSelectJoin()
438         $morebooks = BookPeer::doSelectJoinAuthor(new Criteria());
439         for ($i=0,$j=0; $j < count($morebooks); $i++, $j++) {
440             $testb1 = $allbooks[$i];
441             $testb2 = $allbooks[$j];
442             $this->assertTrue($testb1 === $testb2, "Expected the same objects from consecutive doSelect() calls.");
443             // we could probably also test this by just verifying that $book & $testb are the same
444             if ($testb1->getPrimaryKey() === $book) {
445                 $this->assertTrue($book->getAuthor() === $testb1->getAuthor(), "Expected same author object in calls to pkey-matching books.");
446             }
447         }
448
449
450         // 5) test creating a new object, saving it, and then retrieving that object (should all be same instance)
451         $b = new BookstoreEmployee();
452         $b->setName("Testing");
453         $b->setJobTitle("Testing");
454         $b->save();
455
456         $empId = $b->getId();
457
458         $this->assertSame($b, BookstoreEmployeePeer::retrieveByPK($empId), "Expected newly saved object to be same instance as pooled.");
459
460     }
461
462     /**
463      * Test inheritance features.
464      */
465     public function testInheritance()
466     {
467         $manager = new BookstoreManager();
468         $manager->setName("Manager 1");
469         $manager->setJobTitle("Warehouse Manager");
470         $manager->save();
471         $managerId = $manager->getId();
472
473         $employee = new BookstoreEmployee();
474         $employee->setName("Employee 1");
475         $employee->setJobTitle("Janitor");
476         $employee->setSupervisorId($managerId);
477         $employee->save();
478         $empId = $employee->getId();
479
480         $cashier = new BookstoreCashier();
481         $cashier->setName("Cashier 1");
482         $cashier->setJobTitle("Cashier");
483         $cashier->save();
484         $cashierId = $cashier->getId();
485
486         // 1) test the pooled instances'
487         $c = new Criteria();
488         $c->add(BookstoreEmployeePeer::ID, array($managerId, $empId, $cashierId), Criteria::IN);
489         $c->addAscendingOrderByColumn(BookstoreEmployeePeer::ID);
490
491         $objects = BookstoreEmployeePeer::doSelect($c);
492
493         $this->assertEquals(3, count($objects), "Expected 3 objects to be returned.");
494