| | 65 | * Test default return values. |
|---|
| | 66 | */ |
|---|
| | 67 | public function testDefaultValues() |
|---|
| | 68 | { |
|---|
| | 69 | $r = new Review(); |
|---|
| | 70 | $this->assertEquals('2001-01-01', $r->getReviewDate('Y-m-d')); |
|---|
| | 71 | |
|---|
| | 72 | $this->assertFalse($r->isModified(), "expected isModified() to be falseb"); |
|---|
| | 73 | |
|---|
| | 74 | $acct = new BookstoreEmployeeAccount(); |
|---|
| | 75 | $this->assertEquals(true, $acct->getEnabled()); |
|---|
| | 76 | $this->assertFalse($acct->isModified()); |
|---|
| | 77 | |
|---|
| | 78 | $acct->setLogin("testuser"); |
|---|
| | 79 | $acct->setPassword("testpass"); |
|---|
| | 80 | $this->assertTrue($acct->isModified()); |
|---|
| | 81 | } |
|---|
| | 82 | |
|---|
| | 83 | /** |
|---|
| | 84 | * Tests the use of default expressions. |
|---|
| | 85 | * (Also indirectly tests the reload() method.) |
|---|
| | 86 | */ |
|---|
| | 87 | public function testDefaultExpresions() |
|---|
| | 88 | { |
|---|
| | 89 | if (Propel::getDb(BookstoreEmployeePeer::DATABASE_NAME) instanceof DBSqlite) { |
|---|
| | 90 | $this->markTestSkipped("Cannot test default expressions with SQLite"); |
|---|
| | 91 | } |
|---|
| | 92 | |
|---|
| | 93 | $employee = new BookstoreEmployee(); |
|---|
| | 94 | $employee->setName("Johnny Walker"); |
|---|
| | 95 | |
|---|
| | 96 | $acct = new BookstoreEmployeeAccount(); |
|---|
| | 97 | $acct->setBookstoreEmployee($employee); |
|---|
| | 98 | $acct->setLogin("test-login"); |
|---|
| | 99 | |
|---|
| | 100 | $this->assertNull($acct->getCreated()); |
|---|
| | 101 | |
|---|
| | 102 | $acct->save(); |
|---|
| | 103 | |
|---|
| | 104 | // BookstoreEmployeeAccountPeer::removeInstanceFromPool($acct); |
|---|
| | 105 | |
|---|
| | 106 | $acct = BookstoreEmployeeAccountPeer::retrieveByPK($acct->getEmployeeId()); |
|---|
| | 107 | $this->assertNotNull($acct->getCreated(), "Expected a valid date after retrieving saved object."); |
|---|
| | 108 | |
|---|
| | 109 | $now = new DateTime("now"); |
|---|
| | 110 | $this->assertEquals($now->format("Y-m-d"), $acct->getCreated("Y-m-d")); |
|---|
| | 111 | |
|---|
| | 112 | $acct->setCreated($now); |
|---|
| | 113 | $this->assertEquals($now->format("Y-m-d"), $acct->getCreated("Y-m-d")); |
|---|
| | 114 | |
|---|
| | 115 | } |
|---|
| | 116 | |
|---|
| | 117 | /** |
|---|
| | 118 | * Test the behavior of date/time/values. |
|---|
| | 119 | * This requires that the model was built with propel.useDateTimeClass=true. |
|---|
| | 120 | */ |
|---|
| | 121 | public function testTemporalValues_PreEpoch() |
|---|
| | 122 | { |
|---|
| | 123 | $r = new Review(); |
|---|
| | 124 | |
|---|
| | 125 | $preEpochDate = new DateTime('1602-02-02'); |
|---|
| | 126 | |
|---|
| | 127 | $r->setReviewDate($preEpochDate); |
|---|
| | 128 | |
|---|
| | 129 | $this->assertEquals('1602-02-02', $r->getReviewDate(null)->format("Y-m-d")); |
|---|
| | 130 | |
|---|
| | 131 | $r->setReviewDate('1702-02-02'); |
|---|
| | 132 | |
|---|
| | 133 | $this->assertTrue($r->isModified()); |
|---|
| | 134 | |
|---|
| | 135 | $this->assertEquals('1702-02-02', $r->getReviewDate(null)->format("Y-m-d")); |
|---|
| | 136 | |
|---|
| | 137 | // Now test for setting null |
|---|
| | 138 | $r->setReviewDate(null); |
|---|
| | 139 | $this->assertNull($r->getReviewDate()); |
|---|
| | 140 | |
|---|
| | 141 | } |
|---|
| | 142 | |
|---|
| | 143 | /** |
|---|
| | 144 | * Test setting invalid date/time. |
|---|
| | 145 | */ |
|---|
| | 146 | public function _disabled_testSetTemporalValue_Invalid() |
|---|
| | 147 | { |
|---|
| | 148 | // FIXME - Figure out why this doesn't work (doesn't throw Exception) in the Phing+PHPUnit context |
|---|
| | 149 | $r = new Review(); |
|---|
| | 150 | try { |
|---|
| | 151 | $r->setReviewDate("Invalid Date"); |
|---|
| | 152 | $this->fail("Expected PropelException when setting date column w/ invalid date"); |
|---|
| | 153 | } catch (PropelException $x) { |
|---|
| | 154 | print "Caught expected PropelException: " . $x->__toString(); |
|---|
| | 155 | } |
|---|
| | 156 | } |
|---|
| | 157 | |
|---|
| | 158 | /** |
|---|
| | 159 | * Test setting TIMESTAMP columns w/ unix int timestamp. |
|---|
| | 160 | */ |
|---|
| | 161 | public function testTemporalValues_Unix() |
|---|
| | 162 | { |
|---|
| | 163 | $store = new Bookstore(); |
|---|
| | 164 | $store->setStoreName("test"); |
|---|
| | 165 | $store->setStoreOpenTime(strtotime('12:55')); |
|---|
| | 166 | $store->save(); |
|---|
| | 167 | $this->assertEquals('12:55', $store->getStoreOpenTime(null)->format('H:i')); |
|---|
| | 168 | } |
|---|
| | 169 | |
|---|
| | 170 | /** |
|---|
| | 171 | * Test setting TIME columns. |
|---|
| | 172 | */ |
|---|
| | 173 | public function testTemporalValues_TimeSetting() |
|---|
| | 174 | { |
|---|
| | 175 | $store = new Bookstore(); |
|---|
| | 176 | $store->setStoreName("test"); |
|---|
| | 177 | $store->setStoreOpenTime("12:55"); |
|---|
| | 178 | $store->save(); |
|---|
| | 179 | |
|---|
| | 180 | $store = new Bookstore(); |
|---|
| | 181 | $store->setStoreName("test2"); |
|---|
| | 182 | $store->setStoreOpenTime(new DateTime("12:55")); |
|---|
| | 183 | $store->save(); |
|---|
| | 184 | } |
|---|
| | 185 | |
|---|
| | 186 | /** |
|---|
| | 187 | * Test setting TIME columns. |
|---|
| | 188 | */ |
|---|
| | 189 | public function testTemporalValues_DateSetting() |
|---|
| | 190 | { |
|---|
| | 191 | $r = new Review(); |
|---|
| | 192 | $r->setBook(BookPeer::doSelectOne(new Criteria())); |
|---|
| | 193 | $r->setReviewDate(new DateTime('1999-12-20')); |
|---|
| | 194 | $r->setReviewedBy("Hans"); |
|---|
| | 195 | $r->setRecommended(false); |
|---|
| | 196 | $r->save(); |
|---|
| | 197 | } |
|---|
| | 198 | |
|---|
| | 199 | /** |
|---|
| | 200 | * Testing creating & saving new object & instance pool. |
|---|
| | 201 | */ |
|---|
| | 202 | public function testObjectInstances_New() |
|---|
| | 203 | { |
|---|
| | 204 | $emp = new BookstoreEmployee(); |
|---|
| | 205 | $emp->setName(md5(microtime())); |
|---|
| | 206 | $emp->save(); |
|---|
| | 207 | $id = $emp->getId(); |
|---|
| | 208 | |
|---|
| | 209 | $retrieved = BookstoreEmployeePeer::retrieveByPK($id); |
|---|
| | 210 | $this->assertSame($emp, $retrieved, "Expected same object (from instance pool)"); |
|---|
| | 211 | } |
|---|
| | 212 | |
|---|
| | 213 | /** |
|---|
| | 214 | * |
|---|
| | 215 | */ |
|---|
| | 216 | public function testObjectInstances_Fkeys() |
|---|
| | 217 | { |
|---|
| | 218 | // Establish a relationship between one employee and account |
|---|
| | 219 | // and then change the employee_id and ensure that the account |
|---|
| | 220 | // is not pulling the old employee. |
|---|
| | 221 | |
|---|
| | 222 | $pub1 = new Publisher(); |
|---|
| | 223 | $pub1->setName('Publisher 1'); |
|---|
| | 224 | $pub1->save(); |
|---|
| | 225 | |
|---|
| | 226 | $pub2 = new Publisher(); |
|---|
| | 227 | $pub2->setName('Publisher 2'); |
|---|
| | 228 | $pub2->save(); |
|---|
| | 229 | |
|---|
| | 230 | $book = new Book(); |
|---|
| | 231 | $book->setTitle("Book Title"); |
|---|
| | 232 | $book->setISBN("1234"); |
|---|
| | 233 | $book->setPublisher($pub1); |
|---|
| | 234 | $book->save(); |
|---|
| | 235 | |
|---|
| | 236 | $this->assertSame($pub1, $book->getPublisher()); |
|---|
| | 237 | |
|---|
| | 238 | // now change values behind the scenes |
|---|
| | 239 | $con = Propel::getConnection(BookstoreEmployeeAccountPeer::DATABASE_NAME); |
|---|
| | 240 | $con->exec("UPDATE " . BookPeer::TABLE_NAME . " SET " |
|---|
| | 241 | . " publisher_id = " . $pub2->getId() |
|---|
| | 242 | . " WHERE id = " . $book->getId()); |
|---|
| | 243 | |
|---|
| | 244 | |
|---|
| | 245 | $book2 = BookPeer::retrieveByPK($book->getId()); |
|---|
| | 246 | $this->assertSame($book, $book2, "Expected same book object instance"); |
|---|
| | 247 | |
|---|
| | 248 | $this->assertEquals($pub2->getId(), $book->getPublisherId(), "Expected book to have new publisher id"); |
|---|
| | 249 | $this->assertSame($pub2, $book->getPublisher(), "Expected book to have new publisher object associated."); |
|---|
| | 250 | |
|---|
| | 251 | // Now let's set it back and also verify that reload() works ... |
|---|
| | 252 | |
|---|
| | 253 | $con->exec("UPDATE " . BookPeer::TABLE_NAME . " SET " |
|---|
| | 254 | . " publisher_id = " . $pub1->getId() |
|---|
| | 255 | . " WHERE id = " . $book->getId()); |
|---|
| | 256 | |
|---|
| | 257 | $book->reload(); |
|---|
| | 258 | |
|---|
| | 259 | $this->assertEquals($pub1->getId(), $book->getPublisherId(), "Expected book to have old publisher id (again)."); |
|---|
| | 260 | $this->assertSame($pub1, $book->getPublisher(), "Expected book to have old publisher object associated (again)."); |
|---|
| | 261 | |
|---|
| | 262 | } |
|---|
| | 263 | |
|---|
| | 264 | /** |
|---|
| | 265 | * Test the reload() method. |
|---|
| | 266 | */ |
|---|
| | 267 | public function testReload() |
|---|
| | 268 | { |
|---|
| | 269 | $a = AuthorPeer::doSelectOne(new Criteria()); |
|---|
| | 270 | |
|---|
| | 271 | $origName = $a->getFirstName(); |
|---|
| | 272 | |
|---|
| | 273 | $a->setFirstName(md5(time())); |
|---|
| | 274 | |
|---|
| | 275 | $this->assertNotEquals($origName, $a->getFirstName()); |
|---|
| | 276 | $this->assertTrue($a->isModified()); |
|---|
| | 277 | |
|---|
| | 278 | $a->reload(); |
|---|
| | 279 | |
|---|
| | 280 | $this->assertEquals($origName, $a->getFirstName()); |
|---|
| | 281 | $this->assertFalse($a->isModified()); |
|---|
| | 282 | |
|---|
| | 283 | } |
|---|
| | 284 | |
|---|
| | 285 | /** |
|---|
| | 286 | * Test reload(deep=true) method. |
|---|
| | 287 | */ |
|---|
| | 288 | public function testReloadDeep() |
|---|
| | 289 | { |
|---|
| | 290 | // arbitrary book |
|---|
| | 291 | $b = BookPeer::doSelectOne(new Criteria()); |
|---|
| | 292 | |
|---|
| | 293 | // arbitrary, different author |
|---|
| | 294 | $c = new Criteria(); |
|---|
| | 295 | $c->add(AuthorPeer::ID, $b->getAuthorId(), Criteria::NOT_EQUAL); |
|---|
| | 296 | $a = AuthorPeer::doSelectOne($c); |
|---|
| | 297 | |
|---|
| | 298 | $origAuthor = $b->getAuthor(); |
|---|
| | 299 | |
|---|
| | 300 | $b->setAuthor($a); |
|---|
| | 301 | |
|---|
| | 302 | $this->assertNotEquals($origAuthor, $b->getAuthor(), "Expected just-set object to be different from obj from DB"); |
|---|
| | 303 | $this->assertTrue($b->isModified()); |
|---|
| | 304 | |
|---|
| | 305 | $b->reload($deep=true); |
|---|
| | 306 | |
|---|
| | 307 | $this->assertEquals($origAuthor, $b->getAuthor(), "Expected object in DB to be restored"); |
|---|
| | 308 | $this->assertFalse($a->isModified()); |
|---|
| | 309 | } |
|---|
| | 310 | |
|---|
| | 311 | /** |
|---|
| 201 | | $this->assertType('double', $r2->getBook()->getPrice(), "Expected Book->getPrice() to return a float."); |
|---|
| 202 | | |
|---|
| 203 | | } |
|---|
| | 449 | $this->assertType('float', $r2->getBook()->getPrice(), "Expected Book->getPrice() to return a float."); |
|---|
| | 450 | $this->assertType('DateTime', $r2->getReviewDate(null), "Expected Book->getReviewDate() to return a DateTime."); |
|---|
| | 451 | |
|---|
| | 452 | } |
|---|
| | 453 | |
|---|
| | 454 | /** |
|---|
| | 455 | * This is a test for expected exceptions when saving UNIQUE. |
|---|
| | 456 | * See http://propel.phpdb.org/trac/ticket/2 |
|---|
| | 457 | */ |
|---|
| | 458 | public function testSaveUnique() |
|---|
| | 459 | { |
|---|
| | 460 | $emp = new BookstoreEmployee(); |
|---|
| | 461 | $emp->setName(md5(microtime())); |
|---|
| | 462 | |
|---|
| | 463 | $acct = new BookstoreEmployeeAccount(); |
|---|
| | 464 | $acct->setBookstoreEmployee($emp); |
|---|
| | 465 | $acct->setLogin("foo"); |
|---|
| | 466 | $acct->setPassword("bar"); |
|---|
| | 467 | $acct->save(); |
|---|
| | 468 | |
|---|
| | 469 | // now attempt to create a new acct |
|---|
| | 470 | $acct2 = $acct->copy(); |
|---|
| | 471 | |
|---|
| | 472 | try { |
|---|
| | 473 | $acct2->save(); |
|---|
| | 474 | $this->fail("Expected PropelException in first attempt to save object with duplicate value for UNIQUE constraint."); |
|---|
| | 475 | } catch (Exception $x) { |
|---|
| | 476 | try { |
|---|
| | 477 | // attempt to save it again |
|---|
| | 478 | $acct3 = $acct->copy(); |
|---|
| | 479 | $acct3->save(); |
|---|
| | 480 | $this->fail("Expected PropelException in second attempt to save object with duplicate value for UNIQUE constraint."); |
|---|
| | 481 | } catch (Exception $x) { |
|---|
| | 482 | // this is expected. |
|---|
| | 483 | } |
|---|
| | 484 | // now let's double check that it can succeed if we're not violating the constraint. |
|---|
| | 485 | $acct3->setLogin("foo2"); |
|---|
| | 486 | $acct3->save(); |
|---|
| | 487 | } |
|---|
| | 488 | } |
|---|
| | 489 | |
|---|
| | 490 | /** |
|---|
| | 491 | * Test for correct reporting of isModified(). |
|---|
| | 492 | */ |
|---|
| | 493 | public function testIsModified() |
|---|
| | 494 | { |
|---|
| | 495 | // 1) Basic test |
|---|
| | 496 | |
|---|
| | 497 | $a = new Author(); |
|---|
| | 498 | $a->setFirstName("John"); |
|---|
| | 499 | $a->setLastName("Doe"); |
|---|
| | 500 | $a->setAge(25); |
|---|
| | 501 | |
|---|
| | 502 | $this->assertTrue($a->isModified(), "Expected Author to be modified after setting values."); |
|---|
| | 503 | |
|---|
| | 504 | $a->save(); |
|---|
| | 505 | |
|---|
| | 506 | $this->assertFalse($a->isModified(), "Expected Author to be unmodified after saving set values."); |
|---|
| | 507 | |
|---|
| | 508 | // 2) Test behavior with setting vars of different types |
|---|
| | 509 | |
|---|
| | 510 | // checking setting int col to string val |
|---|
| | 511 | $a->setAge('25'); |
|---|
| | 512 | $this->assertFalse($a->isModified(), "Expected Author to be unmodified after setting int column to string-cast of same value."); |
|---|
| | 513 | |
|---|
| | 514 | $a->setFirstName("John2"); |
|---|
| | 515 | $this->assertTrue($a->isModified(), "Expected Author to be modified after changing string column value."); |
|---|
| | 516 | |
|---|
| | 517 | // checking setting string col to int val |
|---|
| | 518 | $a->setFirstName("1"); |
|---|
| | 519 | $a->save(); |
|---|
| | 520 | $this->assertFalse($a->isModified(), "Expected Author to be unmodified after saving set values."); |
|---|
| | 521 | |
|---|
| | 522 | $a->setFirstName(1); |
|---|
| | 523 | $this->assertFalse($a->isModified(), "Expected Author to be unmodified after setting string column to int-cast of same value."); |
|---|
| | 524 | |
|---|
| | 525 | // 3) Test for appropriate behavior of NULL |
|---|
| | 526 | |
|---|
| | 527 | // checking "" -> NULL |
|---|
| | 528 | $a->setFirstName(""); |
|---|
| | 529 | $a->save(); |
|---|
| | 530 | $this->assertFalse($a->isModified(), "Expected Author to be unmodified after saving set values."); |
|---|
| | 531 | |
|---|
| | 532 | $a->setFirstName(null); |
|---|
| | 533 | $this->assertTrue($a->isModified(), "Expected Author to be modified after changing empty string column value to NULL."); |
|---|
| | 534 | |
|---|
| | 535 | $a->setFirstName("John"); |
|---|
| | 536 | $a->setAge(0); |
|---|
| | 537 | $a->save(); |
|---|
| | 538 | $this->assertFalse($a->isModified(), "Expected Author to be unmodified after saving set values."); |
|---|
| | 539 | |
|---|
| | 540 | $a->setAge(null); |
|---|
| | 541 | $this->assertTrue($a->isModified(), "Expected Author to be modified after changing 0-value int column to NULL."); |
|---|
| | 542 | |
|---|
| | 543 | $a->save(); |
|---|
| | 544 | $this->assertFalse($a->isModified(), "Expected Author to be unmodified after saving set values."); |
|---|
| | 545 | |
|---|
| | 546 | $a->setAge(0); |
|---|
| | 547 | $this->assertTrue($a->isModified(), "Expected Author to be modified after changing NULL-value int column to 0."); |
|---|
| | 548 | |
|---|
| | 549 | } |
|---|
| | 550 | |
|---|
| | 551 | /** |
|---|
| | 552 | * Test the BaseObject#equals(). |
|---|
| | 553 | */ |
|---|
| | 554 | public function testEquals() |
|---|
| | 555 | { |
|---|
| | 556 | $b = BookPeer::doSelectOne(new Criteria()); |
|---|
| | 557 | $c = new Book(); |
|---|
| | 558 | $c->setId($b->getId()); |
|---|
| | 559 | $this->assertTrue($b->equals($c), "Expected Book objects to be equal()"); |
|---|
| | 560 | |
|---|
| | 561 | $a = new Author(); |
|---|
| | 562 | $a->setId($b->getId()); |
|---|
| | 563 | $this->assertFalse($b->equals($a), "Expected Book and Author with same primary key NOT to match."); |
|---|
| | 564 | } |
|---|
| | 565 | |
|---|
| | 566 | /** |
|---|
| | 567 | * Test checking for non-default values. |
|---|
| | 568 | * @see http://propel.phpdb.org/trac/ticket/331 |
|---|
| | 569 | */ |
|---|
| | 570 | public function testHasOnlyDefaultValues() |
|---|
| | 571 | { |
|---|
| | 572 | $emp = new BookstoreEmployee(); |
|---|
| | 573 | $emp->setName(md5(microtime())); |
|---|
| | 574 | |
|---|
| | 575 | $acct2 = new BookstoreEmployeeAccount(); |
|---|
| | 576 | |
|---|
| | 577 | $acct = new BookstoreEmployeeAccount(); |
|---|
| | 578 | $acct->setBookstoreEmployee($emp); |
|---|
| | 579 | $acct->setLogin("foo"); |
|---|
| | 580 | $acct->setPassword("bar"); |
|---|
| | 581 | $acct->save(); |
|---|
| | 582 | |
|---|
| | 583 | $this->assertFalse($acct->isModified(), "Expected BookstoreEmployeeAccount NOT to be modified after save()."); |
|---|
| | 584 | |
|---|
| | 585 | $acct->setEnabled(true); |
|---|
| | 586 | $acct->setPassword($acct2->getPassword()); |
|---|
| | 587 | |
|---|
| | 588 | $this->assertTrue($acct->isModified(), "Expected BookstoreEmployeeAccount to be modified after setting default values."); |
|---|
| | 589 | |
|---|
| | 590 | $this->assertTrue($acct->hasOnlyDefaultValues(), "Expected BookstoreEmployeeAccount to not have only default values."); |
|---|
| | 591 | |
|---|
| | 592 | $acct->setPassword("bar"); |
|---|
| | 593 | $this->assertFalse($acct->hasOnlyDefaultValues(), "Expected BookstoreEmployeeAccount to have at one non-default value after setting one value to non-default."); |
|---|
| | 594 | |
|---|
| | 595 | // Test a default date/time value |
|---|
| | 596 | $r = new Review(); |
|---|
| | 597 | $r->setReviewDate(new DateTime("now")); |
|---|
| | 598 | $this->assertFalse($r->hasOnlyDefaultValues()); |
|---|
| | 599 | } |
|---|
| | 600 | |
|---|
| | 601 | /** |
|---|
| | 602 | * Test the LOB results returned in a resultset. |
|---|
| | 603 | */ |
|---|
| | 604 | public function testLobResults() |
|---|
| | 605 | { |
|---|
| | 606 | |
|---|
| | 607 | $blob_path = TESTS_BASE_DIR . '/etc/lob/tin_drum.gif'; |
|---|
| | 608 | $clob_path = TESTS_BASE_DIR . '/etc/lob/tin_drum.txt'; |
|---|
| | 609 | |
|---|
| | 610 | $book = BookPeer::doSelectOne(new Criteria()); |
|---|
| | 611 | |
|---|
| | 612 | $m1 = new Media(); |
|---|
| | 613 | $m1->setBook($book); |
|---|
| | 614 | $m1->setCoverImage(file_get_contents($blob_path)); |
|---|
| | 615 | $m1->setExcerpt(file_get_contents($clob_path)); |
|---|
| | 616 | $m1->save(); |
|---|
| | 617 | $m1_id = $m1->getId(); |
|---|
| | 618 | |
|---|
| | 619 | $m1->reload(); |
|---|
| | 620 | |
|---|
| | 621 | $img = $m1->getCoverImage(); |
|---|
| | 622 | $txt = $m1->getExcerpt(); |
|---|
| | 623 | |
|---|
| | 624 | $this->assertType('resource', $img, "Expected results of BLOB method to be a resource."); |
|---|
| | 625 | $this->assertType('string', $txt, "Expected results of CLOB method to be a string."); |
|---|
| | 626 | |
|---|
| | 627 | $stat = fstat($img); |
|---|
| | 628 | $size = $stat['size']; |
|---|
| | 629 | |
|---|
| | 630 | $this->assertEquals(filesize($blob_path), $size, "Expected filesize to match stat(blobrsc)"); |
|---|
| | 631 | $this->assertEquals(filesize($clob_path), strlen($txt), "Expected filesize to match clob strlen"); |
|---|
| | 632 | } |
|---|
| | 633 | |
|---|
| | 634 | /** |
|---|
| | 635 | * Tests the setting of LOB (BLOB and CLOB) values. |
|---|
| | 636 | */ |
|---|
| | 637 | public function testLobSetting() |
|---|
| | 638 | { |
|---|
| | 639 | $blob_path = TESTS_BASE_DIR . '/etc/lob/tin_drum.gif'; |
|---|
| | 640 | $blob2_path = TESTS_BASE_DIR . '/etc/lob/propel.gif'; |
|---|
| | 641 | |
|---|
| | 642 | $clob_path = TESTS_BASE_DIR . '/etc/lob/tin_drum.txt'; |
|---|
| | 643 | $book = BookPeer::doSelectOne(new Criteria()); |
|---|
| | 644 | |
|---|
| | 645 | $m1 = new Media(); |
|---|
| | 646 | $m1->setBook($book); |
|---|
| | 647 | $m1->setCoverImage(file_get_contents($blob_path)); |
|---|
| | 648 | $m1->setExcerpt(file_get_contents($clob_path)); |
|---|
| | 649 | $m1->save(); |
|---|
| | 650 | $m1_id = $m1->getId(); |
|---|
| | 651 | |
|---|
| | 652 | // 1) Assert that we've got a valid stream to start with |
|---|
| | 653 | $img = $m1->getCoverImage(); |
|---|
| | 654 | $this->assertType('resource', $img, "Expected results of BLOB method to be a resource."); |
|---|
| | 655 | |
|---|
| | 656 | // 2) Test setting a BLOB column with file contents |
|---|
| | 657 | $m1->setCoverImage(file_get_contents($blob2_path)); |
|---|
| | 658 | $this->assertType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting BLOB with file contents."); |
|---|
| | 659 | |
|---|
| | 660 | // commit those changes & reload |
|---|
| | 661 | $m1->save(); |
|---|
| | 662 | |
|---|
| | 663 | // 3) Verify that we've got a valid resource after reload |
|---|
| | 664 | $m1->reload(); |
|---|
| | 665 | $this->assertType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting reloading object."); |
|---|
| | 666 | |
|---|
| | 667 | // 4) Test isModified() behavior |
|---|
| | 668 | $fp = fopen("php://temp", "r+"); |
|---|
| | 669 | fwrite($fp, file_get_contents($blob2_path)); |
|---|
| | 670 | |
|---|
| | 671 | $m1->setCoverImage($fp); |
|---|
| | 672 | $this->assertTrue($m1->isModified(), "Expected Media object to be modified, despite fact that stream is to same data"); |
|---|
| | 673 | |
|---|
| | 674 | // 5) Test external modification of the stream (and re-setting it into the object) |
|---|
| | 675 | $stream = $m1->getCoverImage(); |
|---|
| | 676 | fwrite($stream, file_get_contents($blob_path)); // change the contents of the stream |
|---|
| | 677 | |
|---|
| | 678 | $m1->setCoverImage($stream); |
|---|
| | 679 | |
|---|
| | 680 | $this->assertTrue($m1->isModified(), "Expected Media object to be modified when stream contents changed."); |
|---|
| | 681 | $this->assertNotEquals(file_get_contents($blob2_path), stream_get_contents($m1->getCoverImage())); |
|---|
| | 682 | |
|---|
| | 683 | $m1->save(); |
|---|
| | 684 | |
|---|
| | 685 | // 6) Assert that when we call the setter with a stream, that the file in db gets updated. |
|---|
| | 686 | |
|---|
| | 687 | $m1->reload(); // start with a fresh copy from db |
|---|
| | 688 | |
|---|
| | 689 | // Ensure that object is set up correctly |
|---|
| | 690 | $this->assertNotEquals(file_get_contents($blob_path), stream_get_contents($m1->getCoverImage()), "The object is not correctly set up to verify the stream-setting test."); |
|---|
| | 691 | |
|---|
| | 692 | $fp = fopen($blob_path, "r"); |
|---|
| | 693 | $m1->setCoverImage($fp); |
|---|
| | 694 | $m1->save(); |
|---|
| | 695 | $m1->reload(); // refresh from db |
|---|
| | 696 | |
|---|
| | 697 | // Assert that we've updated the db |
|---|
| | 698 | $this->assertEquals(file_get_contents($blob_path), stream_get_contents($m1->getCoverImage()), "Expected the updated BLOB value after setting with a stream."); |
|---|
| | 699 | |
|---|
| | 700 | // 7) Assert that 'w' mode works |
|---|
| | 701 | |
|---|
| | 702 | } |
|---|
| | 703 | |
|---|
| | 704 | public function testLobSetting_WriteMode() |
|---|
| | 705 | { |
|---|
| | 706 | $blob_path = TESTS_BASE_DIR . '/etc/lob/tin_drum.gif'; |
|---|
| | 707 | $blob2_path = TESTS_BASE_DIR . '/etc/lob/propel.gif'; |
|---|
| | 708 | |
|---|
| | 709 | $clob_path = TESTS_BASE_DIR . '/etc/lob/tin_drum.txt'; |
|---|
| | 710 | $book = BookPeer::doSelectOne(new Criteria()); |
|---|
| | 711 | |
|---|
| | 712 | $m1 = new Media(); |
|---|
| | 713 | $m1->setBook($book); |
|---|
| | 714 | $m1->setCoverImage(file_get_contents($blob_path)); |
|---|
| | 715 | $m1->setExcerpt(file_get_contents($clob_path)); |
|---|
| | 716 | $m1->save(); |
|---|
| | 717 | |
|---|
| | 718 | MediaPeer::clearInstancePool(); |
|---|
| | 719 | |
|---|
| | 720 | // make sure we have the latest from the db: |
|---|
| | 721 | $m2 = MediaPeer::retrieveByPK($m1->getId()); |
|---|
| | 722 | |
|---|
| | 723 | // now attempt to assign a temporary stream, opened in 'w' mode, to the db |
|---|
| | 724 | |
|---|
| | 725 | $stream = fopen("php://memory", 'w'); |
|---|
| | 726 | fwrite($stream, file_get_contents($blob2_path)); |
|---|
| | 727 | $m2->setCoverImage($stream); |
|---|
| | 728 | $m2->save(); |
|---|
| | 729 | fclose($stream); |
|---|
| | 730 | |
|---|
| | 731 | $m2->reload(); |
|---|
| | 732 | $this->assertEquals(file_get_contents($blob2_path), stream_get_contents($m2->getCoverImage()), "Expected contents to match when setting stream w/ 'w' mode"); |
|---|
| | 733 | |
|---|
| | 734 | $stream2 = fopen("php://memory", 'w+'); |
|---|
| | 735 | fwrite($stream2, file_get_contents($blob_path)); |
|---|
| | 736 | rewind($stream2); |
|---|
| | 737 | $this->assertEquals(file_get_contents($blob_path), stream_get_contents($stream2), "Expecting setup to be correct"); |
|---|
| | 738 | |
|---|
| | 739 | $m2->setCoverImage($stream2); |
|---|
| | 740 | $m2->save(); |
|---|
| | 741 | $m2->reload(); |
|---|
| | 742 | |
|---|
| | 743 | $this->assertEquals(file_get_contents($blob_path), stream_get_contents($m2->getCoverImage()), "Expected contents to match when setting stream w/ 'w+' mode"); |
|---|
| | 744 | |
|---|
| | 745 | } |
|---|
| | 746 | |
|---|
| | 747 | |
|---|
| | 748 | public function testDefaultFkColVal() |
|---|
| | 749 | { |
|---|
| | 750 | $sale = new BookstoreSale(); |
|---|
| | 751 | $this->assertEquals(1, $sale->getBookstoreId(), "Expected BookstoreSale object to have a default ID."); |
|---|
| | 752 | |
|---|
| | 753 | $bookstore = BookstorePeer::doSelectOne(new Criteria()); |
|---|
| | 754 | |
|---|
| | 755 | $sale->setBookstore($bookstore); |
|---|
| | 756 | $this->assertEquals($bookstore->getId(), $sale->getBookstoreId(), "Expected FK id to have changed when assigned a valid FK."); |
|---|
| | 757 | |
|---|
| | 758 | $sale->setBookstore(null); |
|---|
| | 759 | $this->assertEquals(1, $sale->getBookstoreId(), "Expected BookstoreSale object to have reset to default ID."); |
|---|
| | 760 | |
|---|
| | 761 | $sale->setPublisher(null); |
|---|
| | 762 | $this->assertEquals(null, $sale->getPublisherId(), "Expected BookstoreSale object to have reset to NULL publisher ID."); |
|---|
| | 763 | } |
|---|
| | 764 | |
|---|
| | 765 | public function testCountRefFk() |
|---|
| | 766 | { |
|---|
| | 767 | $book = new Book(); |
|---|
| | 768 | $book->setTitle("Test Book"); |
|---|
| | 769 | $book->setISBN("TT-EE-SS-TT"); |
|---|
| | 770 | |
|---|
| | 771 | $num = 5; |
|---|
| | 772 | |
|---|
| | 773 | for ($i=2; $i < $num + 2; $i++) { |
|---|
| | 774 | $r = new Review(); |
|---|
| | 775 | $r->setReviewedBy('Hans ' . $num); |
|---|
| | 776 | $dt = new DateTime("now"); |
|---|
| | 777 | $dt->modify("-".$i." weeks"); |
|---|
| | 778 | $r->setReviewDate($dt); |
|---|
| | 779 | $r->setRecommended(($i % 2) == 0); |
|---|
| | 780 | $book->addReview($r); |
|---|
| | 781 | } |
|---|
| | 782 | |
|---|
| | 783 | $this->assertEquals($num, $book->countReviews(), "Expected countReviews to return $num"); |
|---|
| | 784 | $this->assertEquals($num, count($book->getReviews()), "Expected getReviews to return $num reviews"); |
|---|
| | 785 | |
|---|
| | 786 | $book->save(); |
|---|
| | 787 | |
|---|
| | 788 | BookPeer::clearInstancePool(); |
|---|
| | 789 | ReviewPeer::clearInstancePool(); |
|---|
| | 790 | |
|---|
| | 791 | $book = BookPeer::retrieveByPK($book->getId()); |
|---|
| | 792 | $this->assertEquals($num, $book->countReviews(), "Expected countReviews() to return $num (after save)"); |
|---|
| | 793 | $this->assertEquals($num, count($book->getReviews()), "Expected getReviews() to return $num (after save)"); |
|---|
| | 794 | |
|---|
| | 795 | // Now set different criteria and expect different results |
|---|
| | 796 | $c = new Criteria(); |
|---|
| | 797 | $c->add(ReviewPeer::RECOMMENDED, false); |
|---|
| | 798 | $this->assertEquals(floor($num/2), $book->countReviews($c), "Expected " . floor($num/2) . " results from countReviews(recomm=false)"); |
|---|
| | 799 | |
|---|
| | 800 | // Change Criteria, run again -- expect different. |
|---|
| | 801 | $c = new Criteria(); |
|---|
| | 802 | $c->add(ReviewPeer::RECOMMENDED, true); |
|---|
| | 803 | $this->assertEquals(ceil($num/2), count($book->getReviews($c)), "Expected " . ceil($num/2) . " results from getReviews(recomm=true)"); |
|---|
| | 804 | |
|---|
| | 805 | $this->assertEquals($num, $book->countReviews(), "Expected countReviews to return $num with new empty Criteria"); |
|---|
| | 806 | } |
|---|
| | 807 | |
|---|
| | 808 | /** |
|---|
| | 809 | * Test copyInto method. |
|---|
| | 810 | */ |
|---|
| | 811 | public function testCopyInto_Deep() |
|---|
| | 812 | { |
|---|
| | 813 | // Test a "normal" object |
|---|
| | 814 | $c = new Criteria(); |
|---|
| | 815 | $c->add(BookPeer::TITLE, 'Harry%', Criteria::LIKE); |
|---|
| | 816 | |
|---|
| | 817 | $book = BookPeer::doSelectOne($c); |
|---|
| | 818 | $reviews = $book->getReviews(); |
|---|
| | 819 | |
|---|
| | 820 | $b2 = $book->copy(true); |
|---|
| | 821 | $this->assertType('Book', $b2); |
|---|
| | 822 | $this->assertNull($b2->getId()); |
|---|
| | 823 | |
|---|
| | 824 | $r2 = $b2->getReviews(); |
|---|
| | 825 | |
|---|
| | 826 | $this->assertEquals(count($reviews), count($r2)); |
|---|
| | 827 | |
|---|
| | 828 | // Test a one-to-one object |
|---|
| | 829 | $emp = BookstoreEmployeePeer::doSelectOne(new Criteria()); |
|---|
| | 830 | $e2 = $emp->copy(true); |
|---|
| | 831 | |
|---|
| | 832 | $this->assertType('BookstoreEmployee', $e2); |
|---|
| | 833 | $this->assertNull($e2->getId()); |
|---|
| | 834 | |
|---|
| | 835 | $this->assertEquals($emp->getBookstoreEmployeeAccount()->getLogin(), $e2->getBookstoreEmployeeAccount()->getLogin()); |
|---|
| | 836 | } |
|---|
| | 837 | |
|---|