| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
use bookstore::Peer as PropelPeer; |
|---|
| 4 |
use bookstore::Model as PropelModel; |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
require_once 'bookstore/BookstoreTestBase.php'; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
class GeneratedObjectLobTest extends BookstoreTestBase { |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
* Array of filenames pointing to blob/clob files indexed by the basename. |
|---|
| 47 |
* |
|---|
| 48 |
* @var array string[] |
|---|
| 49 |
*/ |
|---|
| 50 |
protected $sampleLobFiles = array(); |
|---|
| 51 |
|
|---|
| 52 |
protected function setUp() |
|---|
| 53 |
{ |
|---|
| 54 |
parent::setUp(); |
|---|
| 55 |
$this->sampleLobFiles['tin_drum.gif'] = TESTS_BASE_DIR . '/etc/lob/tin_drum.gif'; |
|---|
| 56 |
$this->sampleLobFiles['tin_drum.txt'] = TESTS_BASE_DIR . '/etc/lob/tin_drum.txt'; |
|---|
| 57 |
$this->sampleLobFiles['propel.gif'] = TESTS_BASE_DIR . '/etc/lob/propel.gif'; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
* Gets a LOB filename. |
|---|
| 62 |
* |
|---|
| 63 |
* @param string $basename Basename of LOB filename to return (if left blank, will choose random file). |
|---|
| 64 |
* @return string |
|---|
| 65 |
* @throws Exception - if specified basename doesn't correspond to a registered LOB filename |
|---|
| 66 |
*/ |
|---|
| 67 |
protected function getLobFile($basename = null) |
|---|
| 68 |
{ |
|---|
| 69 |
if ($basename === null) { |
|---|
| 70 |
$basename = array_rand($this->sampleLobFiles); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
if (isset($this->sampleLobFiles[$basename])) { |
|---|
| 74 |
return $this->sampleLobFiles[$basename]; |
|---|
| 75 |
} else { |
|---|
| 76 |
throw new Exception("Invalid base LOB filename: $basename"); |
|---|
| 77 |
} |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
* Test the LOB results returned in a resultset. |
|---|
| 82 |
*/ |
|---|
| 83 |
public function testLobResults() |
|---|
| 84 |
{ |
|---|
| 85 |
|
|---|
| 86 |
$blob_path = $this->getLobFile('tin_drum.gif'); |
|---|
| 87 |
$clob_path = $this->getLobFile('tin_drum.txt'); |
|---|
| 88 |
|
|---|
| 89 |
$book = PropelPeer::BookPeer::doSelectOne(new Criteria()); |
|---|
| 90 |
|
|---|
| 91 |
$m1 = new PropelModel::Media(); |
|---|
| 92 |
$m1->setBook($book); |
|---|
| 93 |
$m1->setCoverImage(file_get_contents($blob_path)); |
|---|
| 94 |
$m1->setExcerpt(file_get_contents($clob_path)); |
|---|
| 95 |
$m1->save(); |
|---|
| 96 |
$m1_id = $m1->getId(); |
|---|
| 97 |
|
|---|
| 98 |
$m1->reload(); |
|---|
| 99 |
|
|---|
| 100 |
$img = $m1->getCoverImage(); |
|---|
| 101 |
$txt = $m1->getExcerpt(); |
|---|
| 102 |
|
|---|
| 103 |
$this->assertType('resource', $img, "Expected results of BLOB method to be a resource."); |
|---|
| 104 |
$this->assertType('string', $txt, "Expected results of CLOB method to be a string."); |
|---|
| 105 |
|
|---|
| 106 |
$stat = fstat($img); |
|---|
| 107 |
$size = $stat['size']; |
|---|
| 108 |
|
|---|
| 109 |
$this->assertEquals(filesize($blob_path), $size, "Expected filesize to match stat(blobrsc)"); |
|---|
| 110 |
$this->assertEquals(filesize($clob_path), strlen($txt), "Expected filesize to match clob strlen"); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
* Test to make sure that file pointer is not when it is fetched |
|---|
| 115 |
* from the object. |
|---|
| 116 |
* |
|---|
| 117 |
* This is actually a test for correct behavior and does not completely fix |
|---|
| 118 |
* the associated ticket (which was resolved wontfix). |
|---|
| 119 |
* |
|---|
| 120 |
* This does test the rewind-after-save functionality, however. |
|---|
| 121 |
* |
|---|
| 122 |
* @link http://propel.phpdb.org/trac/ticket/531 |
|---|
| 123 |
*/ |
|---|
| 124 |
public function testLobRepeatRead() |
|---|
| 125 |
{ |
|---|
| 126 |
$blob_path = $this->getLobFile('tin_drum.gif'); |
|---|
| 127 |
$clob_path = $this->getLobFile('tin_drum.txt'); |
|---|
| 128 |
|
|---|
| 129 |
$book = PropelPeer::BookPeer::doSelectOne(new Criteria()); |
|---|
| 130 |
|
|---|
| 131 |
$m1 = new PropelModel::Media(); |
|---|
| 132 |
$m1->setBook($book); |
|---|
| 133 |
$m1->setCoverImage(file_get_contents($blob_path)); |
|---|
| 134 |
$m1->setExcerpt(file_get_contents($clob_path)); |
|---|
| 135 |
$m1->save(); |
|---|
| 136 |
|
|---|
| 137 |
$img = $m1->getCoverImage(); |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
$this->assertEquals(0, ftell($img), "Expected position of cursor in file pointer to be 0"); |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
$this->assertType('resource', $img, "Expected results of BLOB method to be a resource."); |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
$firstBytes = fread($img, 100); |
|---|
| 149 |
|
|---|
| 150 |
$img2 = $m1->getCoverImage(); |
|---|
| 151 |
$this->assertSame($img, $img2, "Assert that the two resources are the same."); |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
$nextBytes = fread($img, 100); |
|---|
| 155 |
|
|---|
| 156 |
$this->assertNotEquals(bin2hex($firstBytes), bin2hex($nextBytes), "Expected the first 100 and next 100 bytes to not be identical."); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
* Tests the setting of LOB (BLOB and CLOB) values. |
|---|
| 161 |
*/ |
|---|
| 162 |
public function testLobSetting() |
|---|
| 163 |
{ |
|---|
| 164 |
$blob_path = $this->getLobFile('tin_drum.gif'); |
|---|
| 165 |
$blob2_path = $this->getLobFile('propel.gif'); |
|---|
| 166 |
|
|---|
| 167 |
$clob_path = $this->getLobFile('tin_drum.txt'); |
|---|
| 168 |
$book = PropelPeer::BookPeer::doSelectOne(new Criteria()); |
|---|
| 169 |
|
|---|
| 170 |
$m1 = new PropelModel::Media(); |
|---|
| 171 |
$m1->setBook($book); |
|---|
| 172 |
$m1->setCoverImage(file_get_contents($blob_path)); |
|---|
| 173 |
$m1->setExcerpt(file_get_contents($clob_path)); |
|---|
| 174 |
$m1->save(); |
|---|
| 175 |
$m1_id = $m1->getId(); |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
$img = $m1->getCoverImage(); |
|---|
| 179 |
$this->assertType('resource', $img, "Expected results of BLOB method to be a resource."); |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
$m1->setCoverImage(file_get_contents($blob2_path)); |
|---|
| 183 |
$this->assertType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting BLOB with file contents."); |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
$m1->save(); |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
$m1->reload(); |
|---|
| 190 |
$this->assertType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting reloading object."); |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
$fp = fopen("php://temp", "r+"); |
|---|
| 194 |
fwrite($fp, file_get_contents($blob2_path)); |
|---|
| 195 |
|
|---|
| 196 |
$m1->setCoverImage($fp); |
|---|
| 197 |
$this->assertTrue($m1->isModified(), "Expected Media object to be modified, despite fact that stream is to same data"); |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
$stream = $m1->getCoverImage(); |
|---|
| 201 |
fwrite($stream, file_get_contents($blob_path)); |
|---|
| 202 |
|
|---|
| 203 |
$m1->setCoverImage($stream); |
|---|
| 204 |
|
|---|
| 205 |
$this->assertTrue($m1->isModified(), "Expected Media object to be modified when stream contents changed."); |
|---|
| 206 |
$this->assertNotEquals(file_get_contents($blob2_path), stream_get_contents($m1->getCoverImage())); |
|---|
| 207 |
|
|---|
| 208 |
$m1->save(); |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
$m1->reload(); |
|---|
| 213 |
|
|---|
| 214 |
// Ensure that object is set up correctly |
|---|
| 215 |
$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."); |
|---|
| 216 |
|
|---|
| 217 |
$fp = fopen($blob_path, "r"); |
|---|
| 218 |
$m1->setCoverImage($fp); |
|---|
| 219 |
$m1->save(); |
|---|
| 220 |
$m1->reload(); |
|---|
| 221 |
|
|---|
| 222 |
// Assert that we've updated the db |
|---|
| 223 |
$this->assertEquals(file_get_contents($blob_path), stream_get_contents($m1->getCoverImage()), "Expected the updated BLOB value after setting with a stream."); |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
} |
|---|
| 228 |
|
|---|
| 229 |
public function testLobSetting_WriteMode() |
|---|
| 230 |
{ |
|---|
| 231 |
$blob_path = $this->getLobFile('tin_drum.gif'); |
|---|
| 232 |
$blob2_path = $this->getLobFile('propel.gif'); |
|---|
| 233 |
|
|---|
| 234 |
$clob_path = $this->getLobFile('tin_drum.txt'); |
|---|
| 235 |
$book = PropelPeer::BookPeer::doSelectOne(new Criteria()); |
|---|
| 236 |
|
|---|
| 237 |
$m1 = new PropelModel::Media(); |
|---|
| 238 |
$m1->setBook($book); |
|---|
| 239 |
$m1->setCoverImage(file_get_contents($blob_path)); |
|---|
| 240 |
$m1->setExcerpt(file_get_contents($clob_path)); |
|---|
| 241 |
$m1->save(); |
|---|
| 242 |
|
|---|
| 243 |
PropelPeer::MediaPeer::clearInstancePool(); |
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
$m2 = PropelPeer::MediaPeer::retrieveByPK($m1->getId()); |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
$stream = fopen("php://memory", 'w'); |
|---|
| 251 |
fwrite($stream, file_get_contents($blob2_path)); |
|---|
| 252 |
$m2->setCoverImage($stream); |
|---|
| 253 |
$m2->save(); |
|---|
| 254 |
fclose($stream); |
|---|
| 255 |
|
|---|
| 256 |
$m2->reload(); |
|---|
| 257 |
$this->assertEquals(file_get_contents($blob2_path), stream_get_contents($m2->getCoverImage()), "Expected contents to match when setting stream w/ 'w' mode"); |
|---|
| 258 |
|
|---|
| 259 |
$stream2 = fopen("php://memory", 'w+'); |
|---|
| 260 |
fwrite($stream2, file_get_contents($blob_path)); |
|---|
| 261 |
rewind($stream2); |
|---|
| 262 |
$this->assertEquals(file_get_contents($blob_path), stream_get_contents($stream2), "Expecting setup to be correct"); |
|---|
| 263 |
|
|---|
| 264 |
$m2->setCoverImage($stream2); |
|---|
| 265 |
$m2->save(); |
|---|
| 266 |
$m2->reload(); |
|---|
| 267 |
|
|---|
| 268 |
$this->assertEquals(file_get_contents($blob_path), stream_get_contents($m2->getCoverImage()), "Expected contents to match when setting stream w/ 'w+' mode"); |
|---|
| 269 |
|
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
?> |
|---|
| 275 |
|
|---|