PageRenderTime 234ms CodeModel.GetById 100ms app.highlight 84ms RepoModel.GetById 37ms app.codeStats 1ms

/test/testsuite/generator/builder/om/GeneratedObjectTest.php

https://github.com/trendone/Propel
PHP | 1548 lines | 1217 code | 190 blank | 141 comment | 13 complexity | 7dbd92177908946ecf34baa04834fb1f MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3/**
   4 * This file is part of the Propel package.
   5 * For the full copyright and license information, please view the LICENSE
   6 * file that was distributed with this source code.
   7 *
   8 * @license    MIT License
   9 */
  10
  11require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreTestBase.php';
  12require_once dirname(__FILE__) . '/../../../../../generator/lib/util/PropelQuickBuilder.php';
  13
  14/**
  15 * Tests the generated Object classes.
  16 *
  17 * This test uses generated Bookstore classes to test the behavior of various
  18 * object operations.  The _idea_ here is to test every possible generated method
  19 * from Object.tpl; if necessary, bookstore will be expanded to accommodate this.
  20 *
  21 * The database is relaoded before every test and flushed after every test.  This
  22 * means that you can always rely on the contents of the databases being the same
  23 * for each test method in this class.  See the BookstoreDataPopulator::populate()
  24 * method for the exact contents of the database.
  25 *
  26 * @see        BookstoreDataPopulator
  27 * @author     Hans Lellelid <hans@xmpl.org>
  28 * @package    generator.builder.om
  29 */
  30class GeneratedObjectTest extends BookstoreTestBase
  31{
  32    protected function setUp()
  33    {
  34        parent::setUp();
  35        require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/behavior/TestAuthor.php';
  36    }
  37
  38    /**
  39     * Test saving an object after setting default values for it.
  40     */
  41    public function testSaveWithDefaultValues()
  42    {
  43        // From the schema.xml, I am relying on the following:
  44        //  - that 'Penguin' is the default Name for a Publisher
  45        //  - that 2001-01-01 is the default ReviewDate for a Review
  46
  47        // 1) check regular values (VARCHAR)
  48        $pub = new Publisher();
  49        $pub->setName('Penguin');
  50        $pub->save();
  51        $this->assertTrue($pub->getId() !== null, "Expect Publisher to have been saved when default value set.");
  52
  53        // 2) check date/time values
  54        $review = new Review();
  55        // note that this is different from how it's represented in schema, but should resolve to same unix timestamp
  56        $review->setReviewDate('2001-01-01');
  57        $this->assertTrue($review->isModified(), "Expect Review to have been marked 'modified' after default date/time value set.");
  58
  59    }
  60
  61    /**
  62     * Test isModified() to be false after setting default value second time
  63     */
  64    public function testDefaultValueSetTwice()
  65    {
  66        $pub = new Publisher();
  67        $pub->setName('Penguin');
  68        $pub->save();
  69
  70        $pubId = $pub->getId();
  71
  72        PublisherPeer::clearInstancePool();
  73
  74        $pub2 = PublisherPeer::retrieveByPK($pubId);
  75        $pub2->setName('Penguin');
  76        $this->assertFalse($pub2->isModified(), "Expect Publisher to be not modified after setting default value second time.");
  77    }
  78
  79    public function testHasApplyDefaultValues()
  80    {
  81        $this->assertTrue(method_exists('Publisher', 'applyDefaultValues'), 'Tables with default values should have an applyDefaultValues() method');
  82        $this->assertFalse(method_exists('Book', 'applyDefaultValues'), 'Tables with no default values should not have an applyDefaultValues() method');
  83    }
  84
  85    /**
  86     * Test default return values.
  87     */
  88    public function testDefaultValues()
  89    {
  90        $r = new Review();
  91        $this->assertEquals('2001-01-01', $r->getReviewDate('Y-m-d'));
  92
  93        $this->assertFalse($r->isModified(), "expected isModified() to be false");
  94
  95        $acct = new BookstoreEmployeeAccount();
  96        $this->assertEquals(true, $acct->getEnabled());
  97        $this->assertFalse($acct->isModified());
  98
  99        $acct->setLogin("testuser");
 100        $acct->setPassword("testpass");
 101        $this->assertTrue($acct->isModified());
 102    }
 103
 104    /**
 105     * Tests the use of default expressions and the reloadOnInsert and reloadOnUpdate attributes.
 106     *
 107     * @link       http://propel.phpdb.org/trac/ticket/378
 108     * @link       http://propel.phpdb.org/trac/ticket/555
 109     */
 110    public function testDefaultExpresions()
 111    {
 112        if (Propel::getDb(BookstoreEmployeePeer::DATABASE_NAME) instanceof DBSqlite) {
 113            $this->markTestSkipped("Cannot test default expressions with SQLite");
 114        }
 115        BookstoreEmployeeAccountPeer::doDeleteAll();
 116
 117        $b = new Bookstore();
 118        $b->setStoreName("Foo!");
 119        $b->save();
 120
 121        $employee = new BookstoreEmployee();
 122        $employee->setName("Johnny Walker");
 123
 124        $acct = new BookstoreEmployeeAccount();
 125        $acct->setBookstoreEmployee($employee);
 126        $acct->setLogin("test-login");
 127
 128        $this->assertNull($acct->getCreated(), "Expected created column to be NULL.");
 129        $this->assertNull($acct->getAuthenticator(), "Expected authenticator column to be NULL.");
 130
 131        $acct->save();
 132
 133        $acct = BookstoreEmployeeAccountPeer::retrieveByPK($acct->getEmployeeId());
 134
 135        $this->assertNotNull($acct->getAuthenticator(), "Expected a valid (non-NULL) authenticator column after save.");
 136        $this->assertEquals('Password', $acct->getAuthenticator(), "Expected authenticator='Password' after save.");
 137        $this->assertNotNull($acct->getCreated(), "Expected a valid date after retrieving saved object.");
 138
 139        $now = new DateTime("now");
 140        $this->assertEquals($now->format("Y-m-d"), $acct->getCreated("Y-m-d"));
 141
 142        $acct->setCreated($now);
 143        $this->assertEquals($now->format("Y-m-d"), $acct->getCreated("Y-m-d"));
 144
 145        // Unfortunately we can't really test the conjunction of reloadOnInsert and reloadOnUpdate when using just
 146        // default values. (At least not in a cross-db way.)
 147    }
 148
 149    /**
 150     * Tests the use of default expressions and the reloadOnInsert attribute.
 151     *
 152     * @link       http://propel.phpdb.org/trac/ticket/378
 153     * @link       http://propel.phpdb.org/trac/ticket/555
 154     */
 155    public function testDefaultExpresions_ReloadOnInsert()
 156    {
 157        if (Propel::getDb(BookstoreEmployeePeer::DATABASE_NAME) instanceof DBSqlite) {
 158            $this->markTestSkipped("Cannot test default date expressions with SQLite");
 159        }
 160
 161        // Create a new bookstore, contest, bookstore_contest, and bookstore_contest_entry
 162
 163        $b = new Bookstore();
 164        $b->setStoreName("Barnes & Noble");
 165        $b->save();
 166
 167        $c = new Contest();
 168        $c->setName("Bookathon Contest");
 169        $c->save();
 170
 171        $bc = new BookstoreContest();
 172        $bc->setBookstore($b);
 173        $bc->setContest($c);
 174        $bc->save();
 175
 176        $c = new Customer();
 177        $c->setName("Happy Customer");
 178        $c->save();
 179
 180        $bce = new BookstoreContestEntry();
 181        $bce->setBookstore($b);
 182        $bce->setBookstoreContest($bc);
 183        $bce->setCustomer($c);
 184        $bce->save();
 185
 186        $this->assertNotNull($bce->getEntryDate(), "Expected a non-null entry_date after save.");
 187    }
 188
 189    /**
 190     * Tests the overriding reloadOnInsert at runtime.
 191     *
 192     * @link       http://propel.phpdb.org/trac/ticket/378
 193     * @link       http://propel.phpdb.org/trac/ticket/555
 194     */
 195    public function testDefaultExpresions_ReloadOnInsert_Override()
 196    {
 197        if (Propel::getDb(BookstoreEmployeePeer::DATABASE_NAME) instanceof DBSqlite) {
 198            $this->markTestSkipped("Cannot test default date expressions with SQLite");
 199        }
 200
 201        // Create a new bookstore, contest, bookstore_contest, and bookstore_contest_entry
 202        $b = new Bookstore();
 203        $b->setStoreName("Barnes & Noble");
 204        $b->save();
 205
 206        $c = new Contest();
 207        $c->setName("Bookathon Contest");
 208        $c->save();
 209
 210        $bc = new BookstoreContest();
 211        $bc->setBookstore($b);
 212        $bc->setContest($c);
 213        $bc->save();
 214
 215        $c = new Customer();
 216        $c->setName("Happy Customer");
 217        $c->save();
 218
 219        $bce = new BookstoreContestEntry();
 220        $bce->setBookstore($b);
 221        $bce->setBookstoreContest($bc);
 222        $bce->setCustomer($c);
 223        $bce->save(null, $skipReload=true);
 224
 225        $this->assertNull($bce->getEntryDate(), "Expected a NULL entry_date after save.");
 226    }
 227
 228    /**
 229     * Tests the use of default expressions and the reloadOnUpdate attribute.
 230     *
 231     * @link       http://propel.phpdb.org/trac/ticket/555
 232     */
 233    public function testDefaultExpresions_ReloadOnUpdate()
 234    {
 235        $b = new Bookstore();
 236        $b->setStoreName("Foo!");
 237        $b->save();
 238
 239        $sale = new BookstoreSale();
 240        $sale->setBookstore(BookstorePeer::doSelectOne(new Criteria()));
 241        $sale->setSaleName("Spring Sale");
 242        $sale->save();
 243
 244        // Expect that default values are set, but not default expressions
 245        $this->assertNull($sale->getDiscount(), "Expected discount to be NULL.");
 246
 247        $sale->setSaleName("Winter Clearance");
 248        $sale->save();
 249        // Since reloadOnUpdate = true, we expect the discount to be set now.
 250
 251        $this->assertNotNull($sale->getDiscount(), "Expected discount to be non-NULL after save.");
 252    }
 253
 254    /**
 255     * Tests the overriding reloadOnUpdate at runtime.
 256     *
 257     * @link       http://propel.phpdb.org/trac/ticket/378
 258     * @link       http://propel.phpdb.org/trac/ticket/555
 259     */
 260    public function testDefaultExpresions_ReloadOnUpdate_Override()
 261    {
 262        $b = new Bookstore();
 263        $b->setStoreName("Foo!");
 264        $b->save();
 265
 266        $sale = new BookstoreSale();
 267        $sale->setBookstore(BookstorePeer::doSelectOne(new Criteria()));
 268        $sale->setSaleName("Spring Sale");
 269        $sale->save();
 270
 271        // Expect that default values are set, but not default expressions
 272        $this->assertNull($sale->getDiscount(), "Expected discount to be NULL.");
 273
 274        $sale->setSaleName("Winter Clearance");
 275        $sale->save(null, $skipReload=true);
 276
 277        // Since reloadOnUpdate = true, we expect the discount to be set now.
 278
 279        $this->assertNull($sale->getDiscount(), "Expected NULL value for discount after save.");
 280    }
 281
 282    /**
 283     * Testing creating & saving new object & instance pool.
 284     */
 285    public function testObjectInstances_New()
 286    {
 287        $emp = new BookstoreEmployee();
 288        $emp->setName(md5(microtime()));
 289        $emp->save();
 290        $id = $emp->getId();
 291
 292        $retrieved = BookstoreEmployeePeer::retrieveByPK($id);
 293        $this->assertSame($emp, $retrieved, "Expected same object (from instance pool)");
 294    }
 295
 296    /**
 297     *
 298     */
 299    public function testObjectInstances_Fkeys()
 300    {
 301        // Establish a relationship between one employee and account
 302        // and then change the employee_id and ensure that the account
 303        // is not pulling the old employee.
 304
 305        $pub1 = new Publisher();
 306        $pub1->setName('Publisher 1');
 307        $pub1->save();
 308
 309        $pub2 = new Publisher();
 310        $pub2->setName('Publisher 2');
 311        $pub2->save();
 312
 313        $book = new Book();
 314        $book->setTitle("Book Title");
 315        $book->setISBN("1234");
 316        $book->setPublisher($pub1);
 317        $book->save();
 318
 319        $this->assertSame($pub1, $book->getPublisher());
 320
 321        // now change values behind the scenes
 322        $con = Propel::getConnection(BookstoreEmployeeAccountPeer::DATABASE_NAME);
 323        $con->exec("UPDATE " . BookPeer::TABLE_NAME . " SET "
 324            . " publisher_id = " . $pub2->getId()
 325            . " WHERE id = " . $book->getId());
 326
 327        $book2 = BookPeer::retrieveByPK($book->getId());
 328        $this->assertSame($book, $book2, "Expected same book object instance");
 329
 330        $this->assertEquals($pub1->getId(), $book->getPublisherId(), "Expected book to have OLD publisher id before reload()");
 331
 332        $book->reload();
 333
 334        $this->assertEquals($pub2->getId(), $book->getPublisherId(), "Expected book to have new publisher id");
 335        $this->assertSame($pub2, $book->getPublisher(), "Expected book to have new publisher object associated.");
 336
 337        // Now let's set it back, just to be double sure ...
 338
 339        $con->exec("UPDATE " . BookPeer::TABLE_NAME . " SET "
 340            . " publisher_id = " . $pub1->getId()
 341            . " WHERE id = " . $book->getId());
 342
 343        $book->reload();
 344
 345        $this->assertEquals($pub1->getId(), $book->getPublisherId(), "Expected book to have old publisher id (again).");
 346        $this->assertSame($pub1, $book->getPublisher(), "Expected book to have old publisher object associated (again).");
 347
 348    }
 349
 350    /**
 351     * Test the effect of typecast on primary key values and instance pool retrieval.
 352     */
 353    public function testObjectInstancePoolTypecasting()
 354    {
 355        $reader = new BookReader();
 356        $reader->setName("Tester");
 357        $reader->save();
 358        $readerId = $reader->getId();
 359
 360        $book = new Book();
 361        $book->setTitle("BookTest");
 362        $book->setISBN("TEST");
 363        $book->save();
 364        $bookId = $book->getId();
 365
 366        $opinion = new BookOpinion();
 367        $opinion->setBookId((string) $bookId);
 368        $opinion->setReaderId((string) $readerId);
 369        $opinion->setRating(5);
 370        $opinion->setRecommendToFriend(false);
 371        $opinion->save();
 372
 373
 374        $opinion2 = BookOpinionPeer::retrieveByPK($bookId, $readerId);
 375
 376        $this->assertSame($opinion, $opinion2, "Expected same object to be retrieved from differently type-casted primary key values.");
 377
 378    }
 379
 380    /**
 381     * Test saving an object and getting correct number of affected rows from save().
 382     * This includes tests of cascading saves to fk-related objects.
 383     */
 384    public function testSaveReturnValues()
 385    {
 386
 387        $author = new Author();
 388        $author->setFirstName("Mark");
 389        $author->setLastName("Kurlansky");
 390        // do not save
 391
 392        $pub = new Publisher();
 393        $pub->setName("Penguin Books");
 394        // do not save
 395
 396        $book = new Book();
 397        $book->setTitle("Salt: A World History");
 398        $book->setISBN("0142001619");
 399        $book->setAuthor($author);
 400        $book->setPublisher($pub);
 401
 402        $affected = $book->save();
 403        $this->assertEquals(3, $affected, "Expected 3 affected rows when saving book + publisher + author.");
 404
 405        // change nothing ...
 406        $affected = $book->save();
 407        $this->assertEquals(0, $affected, "Expected 0 affected rows when saving already-saved book.");
 408
 409        // modify the book (UPDATE)
 410        $book->setTitle("Salt A World History");
 411        $affected = $book->save();
 412        $this->assertEquals(1, $affected, "Expected 1 affected row when saving modified book.");
 413
 414        // modify the related author
 415        $author->setLastName("Kurlanski");
 416        $affected = $book->save();
 417        $this->assertEquals(1, $affected, "Expected 1 affected row when saving book with updated author.");
 418
 419        // modify both the related author and the book
 420        $author->setLastName("Kurlansky");
 421        $book->setTitle("Salt: A World History");
 422        $affected = $book->save();
 423        $this->assertEquals(2, $affected, "Expected 2 affected rows when saving updated book with updated author.");
 424
 425    }
 426
 427    /*
 428    WTF!!!!!!
 429    public function testSaveCanInsertEmptyObjects()
 430    {
 431        $b = new Book();
 432        $b->save();
 433        $this->assertFalse($b->isNew());
 434        $this->assertNotNull($b->getId());
 435    }
 436    */
 437
 438    public function testSaveCanInsertNonEmptyObjects()
 439    {
 440        $b = new Book();
 441        $b->setTitle('foo');
 442        $b->setIsbn('124');
 443        $b->save();
 444        $this->assertFalse($b->isNew());
 445        $this->assertNotNull($b->getId());
 446    }
 447
 448    /**
 449     *
 450     */
 451    public function testNoColsModified()
 452    {
 453        $e1 = new BookstoreEmployee();
 454        $e1->setName('Employee 1');
 455
 456        $e2 = new BookstoreEmployee();
 457        $e2->setName('Employee 2');
 458
 459        $super = new BookstoreEmployee();
 460        // we don't know who the supervisor is yet
 461        $super->addSubordinate($e1);
 462        $super->addSubordinate($e2);
 463
 464        $affected = $super->save();
 465
 466    }
 467
 468    public function testIsModifiedIsFalseForNewObjects()
 469    {
 470        $a = new Author();
 471        $this->assertFalse($a->isModified());
 472    }
 473
 474    public function testIsModifiedIsTrueForNewObjectsWithModifications()
 475    {
 476        $a = new Author();
 477        $a->setFirstName('Foo');
 478        $this->assertTrue($a->isModified());
 479    }
 480
 481    public function testIsModifiedIsFalseForNewObjectsWithNullModifications()
 482    {
 483        $a = new Author();
 484        $a->setFirstName(null);
 485        $this->assertFalse($a->isModified());
 486    }
 487
 488    public function testIsModifiedIsFalseForObjectsAfterResetModified()
 489    {
 490        $a = new Author();
 491        $a->setFirstName('Foo');
 492        $a->resetModified();
 493        $this->assertFalse($a->isModified());
 494    }
 495
 496    public function testIsModifiedIsFalseForSavedObjects()
 497    {
 498        $a = new Author();
 499        $a->setFirstName('Foo');
 500        $a->setLastName('Bar');
 501        $a->save();
 502        $this->assertFalse($a->isModified());
 503    }
 504
 505    public function testIsModifiedIsTrueForSavedObjectsWithModifications()
 506    {
 507        $a = new Author();
 508        $a->setFirstName('Foo');
 509        $this->assertTrue($a->isModified());
 510    }
 511
 512    public function testIsModifiedIsFalseAfterSetToDefaultValueOnNewObject()
 513    {
 514        $p = new Publisher();
 515        $p->setName('Penguin'); // default column value
 516        $this->assertFalse($p->isModified());
 517    }
 518
 519    public function testIsModifiedIsTrueAfterModifyingOnNonDefaultValueOnNewObject()
 520    {
 521        $p = new Publisher();
 522        $p->setName('Puffin Books');
 523        $this->assertTrue($p->isModified());
 524    }
 525
 526    public function testIsModifiedIsTrueAfterSetToDefaultValueOnModifiedObject()
 527    {
 528        $p = new Publisher();
 529        $p->setName('Puffin Books');
 530        $p->resetModified();
 531        $p->setName('Penguin'); // default column value
 532        $this->assertTrue($p->isModified());
 533    }
 534
 535    public function testIsModifiedIsFalseAfterChangingColumnTypeButNotValue()
 536    {
 537        $a = new Author();
 538        $a->setFirstName('1');
 539        $a->setAge(25);
 540        $a->resetModified();
 541
 542        $a->setAge('25');
 543        $this->assertFalse($a->isModified());
 544
 545        $a->setFirstName(1);
 546        $this->assertFalse($a->isModified());
 547    }
 548
 549    public function testIsModifiedAndNullValues()
 550    {
 551        $a = new Author();
 552        $a->setFirstName('');
 553        $a->setLastName('Bar');
 554        $a->setAge(0);
 555        $a->save();
 556
 557        $a->setFirstName(null);
 558        $this->assertTrue($a->isModified(), "Expected Author to be modified after changing empty string column value to NULL.");
 559
 560        $a->setAge(null);
 561        $this->assertTrue($a->isModified(), "Expected Author to be modified after changing 0-value int column to NULL.");
 562
 563        $a->setFirstName('');
 564        $this->assertTrue($a->isModified(), "Expected Author to be modified after changing NULL column value to empty string.");
 565
 566        $a->setAge(0);
 567        $this->assertTrue($a->isModified(), "Expected Author to be modified after changing NULL column to 0-value int.");
 568    }
 569
 570    /**
 571     * Test checking for non-default values.
 572     * @see        http://propel.phpdb.org/trac/ticket/331
 573     */
 574    public function testHasOnlyDefaultValues()
 575    {
 576        $emp = new BookstoreEmployee();
 577        $emp->setName(md5(microtime()));
 578
 579        $acct2 = new BookstoreEmployeeAccount();
 580
 581        $acct = new BookstoreEmployeeAccount();
 582        $acct->setBookstoreEmployee($emp);
 583        $acct->setLogin("foo");
 584        $acct->setPassword("bar");
 585        $acct->save();
 586
 587        $this->assertFalse($acct->isModified(), "Expected BookstoreEmployeeAccount NOT to be modified after save().");
 588
 589        $acct->setEnabled(true);
 590        $acct->setPassword($acct2->getPassword());
 591
 592        $this->assertTrue($acct->isModified(), "Expected BookstoreEmployeeAccount to be modified after setting default values.");
 593
 594        $this->assertTrue($acct->hasOnlyDefaultValues(), "Expected BookstoreEmployeeAccount to not have only default values.");
 595
 596        $acct->setPassword("bar");
 597        $this->assertFalse($acct->hasOnlyDefaultValues(), "Expected BookstoreEmployeeAccount to have at one non-default value after setting one value to non-default.");
 598
 599        // Test a default date/time value
 600        $r = new Review();
 601        $r->setReviewDate(new DateTime("now"));
 602        $this->assertFalse($r->hasOnlyDefaultValues());
 603    }
 604
 605    public function testCountRefFk()
 606    {
 607        $book = new Book();
 608        $book->setTitle("Test Book");
 609        $book->setISBN("TT-EE-SS-TT");
 610
 611        $num = 5;
 612
 613        for ($i=2; $i < $num + 2; $i++) {
 614            $r = new Review();
 615            $r->setReviewedBy('Hans ' . $num);
 616            $dt = new DateTime("now");
 617            $dt->modify("-".$i." weeks");
 618            $r->setReviewDate($dt);
 619            $r->setRecommended(($i % 2) == 0);
 620            $book->addReview($r);
 621        }
 622
 623        $this->assertEquals($num, $book->countReviews(), "Expected countReviews to return $num");
 624        $this->assertEquals($num, count($book->getReviews()), "Expected getReviews to return $num reviews");
 625
 626        $book->save();
 627
 628        BookPeer::clearInstancePool();
 629        ReviewPeer::clearInstancePool();
 630
 631        $book = BookPeer::retrieveByPK($book->getId());
 632        $this->assertEquals($num, $book->countReviews(), "Expected countReviews() to return $num (after save)");
 633        $this->assertEquals($num, count($book->getReviews()), "Expected getReviews() to return $num (after save)");
 634
 635        // Now set different criteria and expect different results
 636        $c = new Criteria();
 637        $c->add(ReviewPeer::RECOMMENDED, false);
 638        $this->assertEquals(floor($num/2), $book->countReviews($c), "Expected " . floor($num/2) . " results from countReviews(recomm=false)");
 639
 640        // Change Criteria, run again -- expect different.
 641        $c = new Criteria();
 642        $c->add(ReviewPeer::RECOMMENDED, true);
 643        $this->assertEquals(ceil($num/2), count($book->getReviews($c)), "Expected " . ceil($num/2) . " results from getReviews(recomm=true)");
 644
 645        $this->assertEquals($num, $book->countReviews(), "Expected countReviews to return $num with new empty Criteria");
 646    }
 647
 648    /**
 649     * Test copying when an object has composite primary key.
 650     * @link http://propel.phpdb.org/trac/ticket/618
 651     */
 652    public function testCopy_CompositePK()
 653    {
 654        $br = new BookReader();
 655        $br->setName("TestReader");
 656        $br->save();
 657        $br->copy();
 658
 659        $b = new Book();
 660        $b->setTitle("TestBook");
 661        $b->setISBN("XX-XX-XX-XX");
 662        $b->save();
 663
 664        $op = new BookOpinion();
 665        $op->setBookReader($br);
 666        $op->setBook($b);
 667        $op->setRating(10);
 668        $op->setRecommendToFriend(true);
 669        $op->save();
 670
 671        $br2 = $br->copy(true);
 672
 673        $this->assertNull($br2->getId());
 674
 675        $opinions = $br2->getBookOpinions();
 676        $this->assertEquals(1, count($opinions), "Expected to have a related BookOpinion after copy()");
 677
 678        // We DO expect the reader_id to be null
 679        $this->assertNull($opinions[0]->getReaderId());
 680        // but we DO NOT expect the book_id to be null
 681        $this->assertEquals($op->getBookId(), $opinions[0]->getBookId());
 682    }
 683
 684    /**
 685     *
 686     */
 687    public function testCopyConcretInheritance()
 688    {
 689        $concreteArticle = new ConcreteArticle();
 690        $concreteArticle->setBody('TestBody');
 691        $concreteArticle->save();
 692
 693        $copy = $concreteArticle->copy();
 694
 695        $this->assertNull($copy->getId(), "single PK not autoincremented shouldn't be copied");
 696    }
 697
 698    public function testDeepCopyConcretInheritance()
 699    {
 700        $comment1 = new ConcreteComment();
 701        $comment1->setMessage('my-new-comment1');
 702        $concreteArticle = new ConcreteArticle();
 703        $concreteArticle->setBody('TestBody');
 704        $concreteArticle->addConcreteComment($comment1);
 705        $concreteArticle->save();
 706        $comment1->save();
 707
 708        $this->assertNotNull($comment1->getId());
 709        $this->assertNotNull($concreteArticle->getId());
 710        $this->assertEquals(1, $concreteArticle->countConcreteComments());
 711        $this->assertEquals($comment1->getConcreteContentId(), $concreteArticle->getId());
 712
 713        $copy = $concreteArticle->copy(true);
 714        $this->assertNull($copy->getId());
 715        $this->assertEquals(1, $concreteArticle->countConcreteComments());
 716
 717        $comments = $copy->getConcreteContent()->getConcreteComments();
 718        $this->assertEquals('my-new-comment1', $comments[0]->getMessage());
 719    }
 720
 721    public function testToArray()
 722    {
 723        $b = new Book();
 724        $b->setTitle('Don Juan');
 725
 726        $arr1 = $b->toArray();
 727        $expectedKeys = array(
 728            'Id',
 729            'Title',
 730            'ISBN',
 731            'Price',
 732            'PublisherId',
 733            'AuthorId'
 734        );
 735        $this->assertEquals($expectedKeys, array_keys($arr1), 'toArray() returns an associative array with BasePeer::TYPE_PHPNAME keys by default');
 736        $this->assertEquals('Don Juan', $arr1['Title'], 'toArray() returns an associative array representation of the object');
 737    }
 738
 739    public function testToArrayKeyType()
 740    {
 741        $b = new Book();
 742        $b->setTitle('Don Juan');
 743
 744        $arr1 = $b->toArray(BasePeer::TYPE_COLNAME);
 745        $expectedKeys = array(
 746            BookPeer::ID,
 747            BookPeer::TITLE,
 748            BookPeer::ISBN,
 749            BookPeer::PRICE,
 750            BookPeer::PUBLISHER_ID,
 751            BookPeer::AUTHOR_ID
 752        );
 753        $this->assertEquals($expectedKeys, array_keys($arr1), 'toArray() accepts a $keyType parameter to change the result keys');
 754        $this->assertEquals('Don Juan', $arr1[BookPeer::TITLE], 'toArray() returns an associative array representation of the object');
 755    }
 756
 757    public function testToArrayKeyTypePreDefined()
 758    {
 759        $schema = <<<EOF
 760<database name="test">
 761    <table name="test_key_type_table">
 762        <column name="id_key_type" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
 763        <column name="name_key_type" type="VARCHAR" />
 764    </table>
 765</database>
 766EOF;
 767        $builder = new PropelQuickBuilder();
 768        $builder->setSchema($schema);
 769        $builder->getConfig()->setBuildProperty('defaultKeyType', 'studlyPhpName');
 770        $builder->buildClasses();
 771
 772        $expectedKeys = array(
 773            'idKeyType',
 774            'nameKeyType',
 775        );
 776
 777        $object = new TestKeyTypeTable();
 778
 779        $this->assertEquals($expectedKeys, array_keys($object->toArray()), 'toArray() returns an associative array with pre-defined key type in properties.');
 780    }
 781
 782    /**
 783     * Test regexp validator for ticket:542
 784     * @link       http://propel.phpdb.org/trac/ticket/542
 785     */
 786    public function testRegexValidator()
 787    {
 788        $b = new Bookstore();
 789        $b->setWebsite("http://this.is.valid.com/foo.bar");
 790        $res = $b->validate();
 791        $this->assertTrue($res, "Expected URL to validate");
 792    }
 793
 794    /**
 795     * Test that setting the auto-increment primary key will result in exception.
 796     */
 797    public function testSettingAutoIncrementPK()
 798    {
 799        // The whole test is in a transaction, but this test needs real transactions
 800        $this->con->commit();
 801
 802        $b = new Bookstore();
 803        $b->setId(1);
 804        $b->setStoreName("Test");
 805        try {
 806            $b->save();
 807            $this->fail("Expected setting auto-increment primary key to result in Exception");
 808        } catch (Exception $x) {
 809            $this->assertInstanceOf('PropelException', $x);
 810        }
 811
 812        // ... but we should silently ignore NULL values, since these are really
 813        // the same as "not set" in PHP world.
 814        $b = new Bookstore();
 815        $b->setId(null);
 816        $b->setStoreName("Test2");
 817        try {
 818            $b->save();
 819        } catch (Exception $x) {
 820            $this->fail("Expected no exception when setting auto-increment primary key to NULL");
 821        }
 822        // success ...
 823
 824        $this->con->beginTransaction();
 825    }
 826
 827    /**
 828     * Checks wether we are allowed to specify the primary key on a
 829     * table with allowPkInsert=true set
 830     *
 831     * saves the object, gets it from data-source again and then compares
 832     * them for equality (thus the instance pool is also checked)
 833     */
 834    public function testAllowPkInsertOnIdMethodNativeTable()
 835    {
 836        CustomerPeer::doDeleteAll();
 837        $cu = new Customer;
 838        $cu->setPrimaryKey(100000);
 839        $cu->save();
 840
 841        $this->assertEquals(100000, $cu->getPrimaryKey());
 842
 843        $cu2 = CustomerPeer::retrieveByPk(100000);
 844
 845        $this->assertSame($cu, $cu2);
 846    }
 847
 848    /**
 849     * Checks if it is allowed to save new, empty objects with a auto increment column
 850     */
 851    public function testAllowEmptyWithAutoIncrement()
 852    {
 853        $bookreader = new BookReader();
 854        $bookreader->save();
 855
 856        $this->assertFalse($bookreader->isNew());
 857    }
 858
 859    /**
 860     * Test foreign key relationships based on references to unique cols but not PK.
 861     * @link       http://propel.phpdb.org/trac/ticket/691
 862     */
 863    public function testUniqueFkRel()
 864    {
 865        BookstoreEmployeeAccountPeer::doDeleteAll();
 866
 867        $employee = new BookstoreEmployee();
 868        $employee->setName("Johnny Walker");
 869
 870        $acct = new BookstoreEmployeeAccount();
 871        $acct->setBookstoreEmployee($employee);
 872        $acct->setLogin("test-login");
 873        $acct->save();
 874        $acctId = $acct->getEmployeeId();
 875
 876        $al = new AcctAuditLog();
 877        $al->setBookstoreEmployeeAccount($acct);
 878        $al->save();
 879        $alId = $al->getId();
 880
 881        BookstoreEmployeePeer::clearInstancePool();
 882        BookstoreEmployeeAccountPeer::clearInstancePool();
 883        AcctAuditLogPeer::clearInstancePool();
 884
 885        $al2 = AcctAuditLogPeer::retrieveByPK($alId);
 886        /* @var $al2 AcctAuditLog */
 887        $mapacct = $al2->getBookstoreEmployeeAccount();
 888        $lookupacct = BookstoreEmployeeAccountPeer::retrieveByPK($acctId);
 889
 890        $logs = $lookupacct->getAcctAuditLogs();
 891
 892        $this->assertTrue(count($logs) == 1, "Expected 1 audit log result.");
 893        $this->assertEquals($logs[0]->getId(), $al->getId(), "Expected returned audit log to match created audit log.");
 894    }
 895
 896    public function testIsPrimaryKeyNull()
 897    {
 898        $b = new Book();
 899        $this->assertTrue($b->isPrimaryKeyNull());
 900        $b->setPrimaryKey(123);
 901        $this->assertFalse($b->isPrimaryKeyNull());
 902        $b->setPrimaryKey(null);
 903        $this->assertTrue($b->isPrimaryKeyNull());
 904    }
 905
 906    public function testIsPrimaryKeyNullCompmosite()
 907    {
 908        $b = new BookOpinion();
 909        $this->assertTrue($b->isPrimaryKeyNull());
 910        $b->setPrimaryKey(array(123, 456));
 911        $this->assertFalse($b->isPrimaryKeyNull());
 912        $b->setPrimaryKey(array(123, null));
 913        $this->assertFalse($b->isPrimaryKeyNull());
 914        $b->setPrimaryKey(array(null, 456));
 915        $this->assertFalse($b->isPrimaryKeyNull());
 916        $b->setPrimaryKey(array(null, null));
 917        $this->assertTrue($b->isPrimaryKeyNull());
 918    }
 919
 920    public function testAddToStringDefault()
 921    {
 922        $this->assertTrue(method_exists('Author', '__toString'), 'addPrimaryString() adds a __toString() method even if no column has the primaryString attribute');
 923        $author = new Author();
 924        $author->setFirstName('Leo');
 925        $author->setLastName('Tolstoi');
 926        $expected = <<<EOF
 927Id: null
 928FirstName: Leo
 929LastName: Tolstoi
 930Email: null
 931Age: null
 932
 933EOF;
 934        $this->assertEquals($expected, (string) $author, 'addPrimaryString() adds a __toString() method returning the YAML representation of the object where no column is defined as primaryString');
 935    }
 936
 937    public function testAddToStringPrimaryString()
 938    {
 939        $this->assertTrue(method_exists('Book', '__toString'), 'addPrimaryString() adds a __toString() method if a column has the primaryString attribute');
 940        $book = new Book();
 941        $book->setTitle('foo');
 942        $this->assertEquals('foo', (string) $book, 'addPrimaryString() adds a __toString() method returning the value of the the first column where primaryString is true');
 943    }
 944
 945    public function testPreInsert()
 946    {
 947        $author = new TestAuthor();
 948        $author->setFirstName("bogus");
 949        $author->setLastName("Lastname");
 950        $author->save();
 951        $this->assertEquals('PreInsertedFirstname', $author->getFirstName());
 952    }
 953
 954    public function testPreUpdate()
 955    {
 956        $author = new TestAuthor();
 957        $author->setFirstName("bogus");
 958        $author->setLastName("Lastname");
 959        $author->save();
 960        $author->setNew(false);
 961        $author->save();
 962        $this->assertEquals('PreUpdatedFirstname', $author->getFirstName());
 963    }
 964
 965    public function testPostInsert()
 966    {
 967        $author = new TestAuthor();
 968        $author->setFirstName("bogus");
 969        $author->setLastName("Lastname");
 970        $author->save();
 971        $this->assertEquals('PostInsertedLastName', $author->getLastName());
 972    }
 973
 974    public function testPostUpdate()
 975    {
 976        $author = new TestAuthor();
 977        $author->setFirstName("bogus");
 978        $author->setLastName("Lastname");
 979        $author->save();
 980        $author->setNew(false);
 981        $author->save();
 982        $this->assertEquals('PostUpdatedLastName', $author->getLastName());
 983    }
 984
 985    public function testPreSave()
 986    {
 987        $author = new TestAuthor();
 988        $author->setFirstName("bogus");
 989        $author->setLastName("Lastname");
 990        $author->save();
 991        $this->assertEquals('pre@save.com', $author->getEmail());
 992    }
 993
 994    public function testPreSaveFalse()
 995    {
 996        $con = Propel::getConnection(AuthorPeer::DATABASE_NAME);
 997        $author = new TestAuthorSaveFalse();
 998        $author->setFirstName("bogus");
 999        $author->setLastName("Lastname");
1000        $res = $author->save($con);
1001        $this->assertEquals(0, $res);
1002        $this->assertEquals('pre@save.com', $author->getEmail());
1003        $this->assertNotEquals(115, $author->getAge());
1004        $this->assertTrue($author->isNew());
1005        $this->assertEquals(1, $con->getNestedTransactionCount());
1006    }
1007
1008    public function testPostSave()
1009    {
1010        $author = new TestAuthor();
1011        $author->setFirstName("bogus");
1012        $author->setLastName("Lastname");
1013        $author->save();
1014        $this->assertEquals(115, $author->getAge());
1015    }
1016
1017    public function testPreDelete()
1018    {
1019        $author = new TestAuthor();
1020        $author->setFirstName("bogus");
1021        $author->setLastName("Lastname");
1022        $author->save();
1023        $author->delete();
1024        $this->assertEquals("Pre-Deleted", $author->getFirstName());
1025    }
1026
1027    public function testPreDeleteFalse()
1028    {
1029        $con = Propel::getConnection(AuthorPeer::DATABASE_NAME);
1030        $author = new TestAuthorDeleteFalse();
1031        $author->setFirstName("bogus");
1032        $author->setLastName("Lastname");
1033        $author->save($con);
1034        $author->delete($con);
1035        $this->assertEquals("Pre-Deleted", $author->getFirstName());
1036        $this->assertNotEquals("Post-Deleted", $author->getLastName());
1037        $this->assertFalse($author->isDeleted());
1038        $this->assertEquals(1, $con->getNestedTransactionCount());
1039    }
1040
1041    public function testPostDelete()
1042    {
1043        $author = new TestAuthor();
1044        $author->setFirstName("bogus");
1045        $author->setLastName("Lastname");
1046        $author->save();
1047        $author->delete();
1048        $this->assertEquals("Post-Deleted", $author->getLastName());
1049    }
1050    
1051    public function testPostHydrate()
1052    {
1053        $author = new TestAuthor();
1054        $author->hydrate(array(1, 'bogus', 'Lastname', 'bogus@mail.com', 21));
1055        $this->assertEquals("Post-Hydrated", $author->getLastName());
1056    }
1057    
1058    public function testMagicVirtualColumnGetter()
1059    {
1060        $book = new Book();
1061        $book->setVirtualColumn('Foo', 'bar');
1062        $this->assertEquals('bar', $book->getFoo(), 'generated __call() catches getters for virtual columns');
1063        $book = new Book();
1064        $book->setVirtualColumn('foo', 'bar');
1065        $this->assertEquals('bar', $book->getFoo(), 'generated __call() catches getters for virtual columns starting with a lowercase character');
1066    }
1067
1068    /**
1069     * @expectedException PropelException
1070     */
1071    public function testMagicCallUndefined()
1072    {
1073        $book = new Book();
1074        $book->fooMethodName();
1075    }
1076
1077    public static function conditionsForTestReadOnly()
1078    {
1079        return array(
1080            array('reload'),
1081            array('delete'),
1082            array('save'),
1083            array('doSave'),
1084        );
1085    }
1086
1087    /**
1088     * @dataProvider conditionsForTestReadOnly
1089     */
1090    public function testReadOnly($method)
1091    {
1092        $cv = new ContestView();
1093        $this->assertFalse(method_exists($cv, $method), 'readOnly tables end up with no ' . $method . ' method in the generated object class');
1094    }
1095
1096    public function testSetterOneToMany()
1097    {
1098        // Ensure no data
1099        BookQuery::create()->deleteAll();
1100        AuthorQuery::create()->deleteAll();
1101
1102        $coll = new PropelObjectCollection();
1103        $coll->setModel('Book');
1104
1105        for ($i = 0; $i < 3; $i++) {
1106            $b = new Book();
1107            $b->setTitle('Title ' . $i);
1108            $b->setIsbn('1204' . $i);
1109
1110            $coll[] = $b;
1111        }
1112
1113        $this->assertEquals(3, $coll->count());
1114
1115        $a = new Author();
1116        $a->setFirstName('Foo');
1117        $a->setLastName('Bar');
1118        $a->setBooks($coll);
1119        $a->save();
1120
1121        $this->assertInstanceOf('PropelObjectCollection', $a->getBooks());
1122        $this->assertEquals(3, $a->getBooks()->count());
1123        $this->assertEquals(1, AuthorQuery::create()->count());
1124        $this->assertEquals(3, BookQuery::create()->count());
1125
1126        $coll->shift();
1127        $this->assertEquals(2, $coll->count());
1128
1129        $a->setBooks($coll);
1130        $a->save();
1131
1132        $this->assertEquals(2, $a->getBooks()->count());
1133        $this->assertEquals(1, AuthorQuery::create()->count());
1134        //The book is not deleted because his fk is not required
1135        $this->assertEquals(3, BookQuery::create()->count());
1136
1137        $newBook = new Book();
1138        $newBook->setTitle('My New Book');
1139        $newBook->setIsbn(1234);
1140
1141        // Kind of new collection
1142        $coll = clone $coll;
1143        $coll[] = $newBook;
1144
1145        $a->setBooks($coll);
1146        $a->save();
1147
1148        $this->assertEquals(3, $coll->count());
1149        $this->assertEquals(3, $a->getBooks()->count());
1150        $this->assertEquals(1, AuthorQuery::create()->count());
1151        $this->assertEquals(4, BookQuery::create()->count());
1152
1153        // Add a new object
1154        $newBook1 = new Book();
1155        $newBook1->setTitle('My New Book1');
1156        $newBook1->setIsbn(1256);
1157
1158        // Existing collection - The fix around reference is tested here.
1159        $coll[] = $newBook1;
1160
1161        $a->setBooks($coll);
1162        $a->save();
1163
1164        $this->assertEquals(4, $coll->count());
1165        $this->assertEquals(4, $a->getBooks()->count());
1166        $this->assertEquals(1, AuthorQuery::create()->count());
1167        $this->assertEquals(5, BookQuery::create()->count());
1168
1169        // Add the same collection
1170        $books = $a->getBooks();
1171
1172        $a->setBooks($books);
1173        $a->save();
1174
1175        $this->assertEquals(4, $books->count());
1176        $this->assertEquals(4, $a->getBooks()->count());
1177        $this->assertEquals(1, AuthorQuery::create()->count());
1178        $this->assertEquals(5, BookQuery::create()->count());
1179    }
1180
1181    public function testSetterOneToManyWithFkRequired()
1182    {
1183        // Ensure no data
1184        BookSummaryQuery::create()->deleteAll();
1185        BookQuery::create()->deleteAll();
1186
1187        $coll = new PropelObjectCollection();
1188        $coll->setModel('BookSummary');
1189
1190        for ($i = 0; $i < 3; $i++) {
1191            $bs = new BookSummary();
1192            $bs->setSummary('A summary ' . $i);
1193
1194            $coll[] = $bs;
1195        }
1196
1197        $this->assertEquals(3, $coll->count());
1198
1199        $b = new Book();
1200        $b->setTitle('myBook');
1201        $b->setBookSummarys($coll);
1202        $b->setIsbn('12242');
1203        $b->save();
1204
1205        $this->assertInstanceOf('PropelObjectCollection', $b->getBookSummarys());
1206        $this->assertEquals(3, $b->getBookSummarys()->count());
1207        $this->assertEquals(1, BookQuery::create()->count());
1208        $this->assertEquals(3, BookSummaryQuery::create()->count());
1209
1210        $coll->shift();
1211        $this->assertEquals(2, $coll->count());
1212
1213        $b->setBookSummarys($coll);
1214        $b->save();
1215
1216        $this->assertEquals(2, $b->getBookSummarys()->count());
1217        $this->assertEquals(1, BookQuery::create()->count());
1218        $this->assertEquals(2, BookSummaryQuery::create()->count());
1219
1220        $newBookSammary = new BookSummary();
1221        $newBookSammary->setSummary('My sammary');
1222
1223        // Kind of new collection
1224        $coll = clone $coll;
1225        $coll[] = $newBookSammary;
1226
1227        $b->setBookSummarys($coll);
1228        $b->save();
1229
1230        $this->assertEquals(3, $coll->count());
1231        $this->assertEquals(3, $b->getBookSummarys()->count());
1232        $this->assertEquals(1, BookQuery::create()->count());
1233        $this->assertEquals(3, BookSummaryQuery::create()->count());
1234
1235        // Add a new object
1236        $newBookSammary1 = new BookSummary();
1237        $newBookSammary1->setSummary('My sammary 1');
1238
1239        // Existing collection - The fix around reference is tested here.
1240        $coll[] = $newBookSammary1;
1241
1242        $b->setBookSummarys($coll);
1243        $b->save();
1244
1245        $this->assertEquals(4, $coll->count());
1246        $this->assertEquals(4, $b->getBookSummarys()->count());
1247        $this->assertEquals(1, BookQuery::create()->count());
1248        $this->assertEquals(4, BookSummaryQuery::create()->count());
1249
1250        // Add the same collection
1251        $bookSummaries = $b->getBookSummarys();
1252
1253        $b->setBookSummarys($bookSummaries);
1254        $b->save();
1255
1256        $this->assertEquals(4, $coll->count());
1257        $this->assertEquals(4, $b->getBookSummarys()->count());
1258        $this->assertEquals(1, BookQuery::create()->count());
1259        $this->assertEquals(4, BookSummaryQuery::create()->count());
1260    }
1261
1262    public function testSetterOneToManyWithNoData()
1263    {
1264        // Ensure no data
1265        BookQuery::create()->deleteAll();
1266        AuthorQuery::create()->deleteAll();
1267
1268        $books = new PropelObjectCollection();
1269        $this->assertEquals(0, $books->count());
1270
1271        // Basic usage
1272        $a = new Author();
1273        $a->setFirstName('Foo');
1274        $a->setLastName('Bar');
1275        $a->setBooks($books);
1276        $a->save();
1277
1278        $this->assertEquals(0, $a->getBooks()->count());
1279        $this->assertEquals(1, AuthorQuery::create()->count());
1280        $this->assertEquals(0, BookQuery::create()->count());
1281    }
1282
1283    public function testSetterOneToManySavesForeignObjects()
1284    {
1285        // Ensure no data
1286        BookQuery::create()->deleteAll();
1287        AuthorQuery::create()->deleteAll();
1288
1289        $book = new Book();
1290        $book->setTitle('My Book');
1291        $book->setIsbn('1245');
1292        $book->save();
1293
1294        // Modify it but don't save it
1295        $book->setTitle('My Title');
1296
1297        $coll = new PropelObjectCollection();
1298        $coll[] = $book;
1299
1300        BookPeer::clearInstancePool();
1301        $book = BookQuery::create()->findPk($book->getPrimaryKey());
1302
1303        $a = new Author();
1304        $a->setFirstName('Foo');
1305        $a->setLastName('Bar');
1306        $a->setBooks($coll);
1307        $a->save();
1308
1309        $this->assertEquals(1, $a->getBooks()->count());
1310        $this->assertEquals(1, AuthorQuery::create()->count());
1311        $this->assertEquals(1, BookQuery::create()->count());
1312
1313        $result = BookQuery::create()
1314            ->filterById($book->getId())
1315            ->select('Title')
1316            ->findOne();
1317        $this->assertSame('My Title', $result);
1318    }
1319
1320    public function testSetterOneToManyWithNewObjects()
1321    {
1322        // Ensure no data
1323        BookQuery::create()->deleteAll();
1324        AuthorQuery::create()->deleteAll();
1325
1326        $coll = new PropelObjectCollection();
1327        $coll->setModel('Book');
1328
1329        for ($i = 0; $i < 3; $i++) {
1330            $b = new Book();
1331            $b->setTitle('Book ' . $i);
1332            $b->setIsbn('124' . $i);
1333
1334            $coll[] = $b;
1335        }
1336
1337        $a = new Author();
1338        $a->setFirstName('Foo');
1339        $a->setLastName('Bar');
1340        $a->setBooks($coll);
1341        $a->save();
1342
1343        $this->assertEquals(3, $coll->count());
1344        $this->assertEquals(3, count($a->getBooks()));
1345        $this->assertSame($coll, $a->getBooks());
1346        $this->assertEquals(1, AuthorQuery::create()->count());
1347        $this->assertEquals(3, BookQuery::create()->count());
1348    }
1349
1350    public function testSetterOneToManyWithExistingObjects()
1351    {
1352        // Ensure no data
1353        BookQuery::create()->deleteAll();
1354        AuthorQuery::create()->deleteAll();
1355
1356        for ($i = 0; $i < 3; $i++) {
1357            $b = new Book();
1358            $b->setTitle('Book ' . $i);
1359            $b->setIsbn('21234' . $i);
1360            $b->save();
1361        }
1362
1363        BookPeer::clearInstancePool();
1364        $books = BookQuery::create()->find();
1365
1366        $a = new Author();
1367        $a->setFirstName('Foo');
1368        $a->setLastName('Bar');
1369        $a->setBooks($books);
1370        $a->save();
1371
1372        $this->assertEquals(3, count($a->getBooks()));
1373        $this->assertEquals(1, AuthorQuery::create()->count());
1374        $this->assertEquals(3, BookQuery::create()->count());
1375
1376        $i = 0;
1377        foreach ($a->getBooks() as $book) {
1378            $this->assertEquals('Book ' . $i++, $book->getTitle());
1379        }
1380    }
1381
1382    public function testSetterOneToManyWithEmptyCollection()
1383    {
1384        // Ensure no data
1385        BookQuery::create()->deleteAll();
1386        AuthorQuery::create()->deleteAll();
1387
1388        $a = new Author();
1389        $a->setFirstName('Foo');
1390        $a->setLastName('Bar');
1391        $a->setBooks(new PropelObjectCollection());
1392        $a->save();
1393
1394        $this->assertEquals(0, count($a->getBooks()));
1395
1396        $this->assertEquals(0, BookQuery::create()->count());
1397        $this->assertEquals(1, AuthorQuery::create()->count());
1398    }
1399
1400    public function testSetterOneToManyReplacesOldObjectsByNewObjects()
1401    {
1402        // Ensure no data
1403        BookQuery::create()->deleteAll();
1404        AuthorQuery::create()->deleteAll();
1405
1406        $books = new PropelObjectCollection();
1407        foreach (array('foo', 'bar') as $title) {
1408            $b = new Book();
1409            $b->setTitle($title);
1410            $b->setIsbn('12354');
1411
1412            $books[] = $b;
1413        }
1414
1415        $a = new Author();
1416        $a->setFirstName('Foo');
1417        $a->setLastName('Bar');
1418        $a->setBooks($books);
1419        $a->save();
1420
1421        $books = $a->getBooks();
1422        $this->assertEquals('foo', $books[0]->getTitle());
1423        $this->assertEquals('bar', $books[1]->getTitle());
1424
1425        $books = new PropelObjectCollection();
1426        foreach (array('bam', 'bom') as $title) {
1427            $b = new Book();
1428            $b->setTitle($title);
1429            $b->setIsbn('1235');
1430
1431            $books[] = $b;
1432        }
1433
1434        $a->setBooks($books);
1435        $a->save();
1436
1437        $books = $a->getBooks();
1438        $this->assertEquals('bam', $books[0]->getTitle());
1439        $this->assertEquals('bom', $books[1]->getTitle());
1440
1441        $this->assertEquals(1, AuthorQuery::create()->count());
1442        // the replaced book are still there because the PK is not required
1443        $this->assertEquals(4, BookQuery::create()->count());
1444    }
1445
1446    public function testSetterOneToManyReplacesOldObjectsByNewObjectsWithFkRequired()
1447    {
1448        // Ensure no data
1449        BookSummaryQuery::create()->deleteAll();
1450        BookQuery::create()->deleteAll();
1451
1452        $bookSummaries = new PropelObjectCollection();
1453        foreach (array('foo', 'bar') as $summary) {
1454            $s = new BookSummary();
1455            $s->setSummary($summary);
1456            $bookSummaries[] = $s;
1457        }
1458
1459        $b = new Book();
1460        $b->setTitle('Hello');
1461        $b->setBookSummarys($bookSummaries);
1462        $b->setIsbn('1234');
1463        $b->save();
1464
1465        $bookSummaries = $b->getBookSummarys();
1466        $this->assertEquals('foo', $bookSummaries[0]->getSummary());
1467        $this->assertEquals('bar', $bookSummaries[1]->getSummary());
1468
1469        $bookSummaries = new PropelObjectCollection();
1470        foreach (array('bam', 'bom') as $summary) {
1471            $s = new BookSummary();
1472            $s->setSummary($summary);
1473            $bookSummaries[] = $s;
1474        }
1475
1476        $b->setBookSummarys($bookSummaries);
1477        $b->save();
1478
1479        $bookSummaries = $b->getBookSummarys();
1480        $this->assertEquals('bam', $bookSummaries[0]->getSummary());
1481        $this->assertEquals('bom', $bookSummaries[1]->getSummary());
1482
1483        $this->assertEquals(1, BookQuery::create()->count());
1484        $this->assertEquals(2, BookSummaryQuery::create()->count());
1485    }
1486
1487    public function testHooksCall()
1488    {
1489        AuthorQuery::create()->deleteAll();
1490        BookQuery::create()->deleteAll();
1491
1492        $author = new CountableAuthor();
1493        $author->setFirstName('Foo');
1494        $author->setLastName('Bar');
1495
1496        $book = new Book();
1497        $book->setTitle('A title');
1498        $book->setIsbn('13456');
1499
1500        $author->addBook($book);
1501        $author->save();
1502
1503        $this->assertEquals(1, AuthorQuery::create()->count());
1504        $this->assertEquals(1, BookQuery::create()->count());
1505        $this->assertEquals(1, $author->nbCallPreSave);
1506    }
1507
1508    /**
1509     * @expectedException PropelException
1510     */
1511    public function testDoInsert()
1512    {
1513        if (!class_exists('Unexistent')) {
1514            $schema = <<<EOF
1515<database name="a-database">
1516    <table name="unexistent">
1517        <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
1518        <column name="name" type="VARCHAR" />
1519    </table>
1520</database>
1521EOF;
1522            $build…

Large files files are truncated, but you can click here to view the full file