/** * Tests the setting of LOB (BLOB and CLOB) values. */ public function testLobSetting() { $blob_path = $this->getLobFile('tin_drum.gif'); $blob2_path = $this->getLobFile('propel.gif'); $clob_path = $this->getLobFile('tin_drum.txt'); $book = BookPeer::doSelectOne(new Criteria()); $m1 = new Media(); $m1->setBook($book); $m1->setCoverImage(file_get_contents($blob_path)); $m1->setExcerpt(file_get_contents($clob_path)); $m1->save(); $m1_id = $m1->getId(); // 1) Assert that we've got a valid stream to start with $img = $m1->getCoverImage(); $this->assertInternalType('resource', $img, "Expected results of BLOB method to be a resource."); // 2) Test setting a BLOB column with file contents $m1->setCoverImage(file_get_contents($blob2_path)); $this->assertInternalType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting BLOB with file contents."); // commit those changes & reload $m1->save(); // 3) Verify that we've got a valid resource after reload $m1->reload(); $this->assertInternalType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting reloading object."); // 4) Test isModified() behavior $fp = fopen("php://temp", "r+"); fwrite($fp, file_get_contents($blob2_path)); $m1->setCoverImage($fp); $this->assertTrue($m1->isModified(), "Expected Media object to be modified, despite fact that stream is to same data"); // 5) Test external modification of the stream (and re-setting it into the object) $stream = $m1->getCoverImage(); fwrite($stream, file_get_contents($blob_path)); // change the contents of the stream $m1->setCoverImage($stream); $this->assertTrue($m1->isModified(), "Expected Media object to be modified when stream contents changed."); $this->assertNotEquals(file_get_contents($blob2_path), stream_get_contents($m1->getCoverImage())); $m1->save(); // 6) Assert that when we call the setter with a stream, that the file in db gets updated. $m1->reload(); // start with a fresh copy from db // Ensure that object is set up correctly $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."); $fp = fopen($blob_path, "r"); $m1->setCoverImage($fp); $m1->save(); $m1->reload(); // refresh from db // Assert that we've updated the db $this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($m1->getCoverImage())), "Expected the updated BLOB value after setting with a stream."); // 7) Assert that 'w' mode works }