PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/1989gaurav/Propel
PHP | 293 lines | 153 code | 69 blank | 71 comment | 5 complexity | d1125edce85812dd970dc10d24856dfb MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/../../../../tools/helpers/bookstore/BookstoreEmptyTestBase.php';
  10. if (!defined('TESTS_BASE_DIR')) {
  11. define('TESTS_BASE_DIR', realpath(dirname(__FILE__) . '/../../../..'));
  12. }
  13. /**
  14. * Tests the generated Object classes and LOB behavior.
  15. *
  16. * This test uses generated Bookstore classes to test the behavior of various
  17. * object operations. The _idea_ here is to test every possible generated method
  18. * from Object.tpl; if necessary, bookstore will be expanded to accommodate this.
  19. *
  20. * The database is relaoded before every test and flushed after every test. This
  21. * means that you can always rely on the contents of the databases being the same
  22. * for each test method in this class. See the BookstoreDataPopulator::populate()
  23. * method for the exact contents of the database.
  24. *
  25. * @see BookstoreDataPopulator
  26. * @author Hans Lellelid <hans@xmpl.org>
  27. * @package generator.builder.om
  28. */
  29. class GeneratedObjectLobTest extends BookstoreEmptyTestBase
  30. {
  31. /**
  32. * Array of filenames pointing to blob/clob files indexed by the basename.
  33. *
  34. * @var array string[]
  35. */
  36. protected $sampleLobFiles = array();
  37. protected function setUp()
  38. {
  39. parent::setUp();
  40. BookstoreDataPopulator::populate();
  41. $this->sampleLobFiles['tin_drum.gif'] = TESTS_BASE_DIR . '/etc/lob/tin_drum.gif';
  42. $this->sampleLobFiles['tin_drum.txt'] = TESTS_BASE_DIR . '/etc/lob/tin_drum.txt';
  43. $this->sampleLobFiles['propel.gif'] = TESTS_BASE_DIR . '/etc/lob/propel.gif';
  44. }
  45. /**
  46. * Gets a LOB filename.
  47. *
  48. * @param string $basename Basename of LOB filename to return (if left blank, will choose random file).
  49. * @return string
  50. * @throws Exception - if specified basename doesn't correspond to a registered LOB filename
  51. */
  52. protected function getLobFile($basename = null)
  53. {
  54. if ($basename === null) {
  55. $basename = array_rand($this->sampleLobFiles);
  56. }
  57. if (isset($this->sampleLobFiles[$basename])) {
  58. return $this->sampleLobFiles[$basename];
  59. } else {
  60. throw new Exception("Invalid base LOB filename: $basename");
  61. }
  62. }
  63. /**
  64. * Test the LOB results returned in a resultset.
  65. */
  66. public function testLobResults()
  67. {
  68. $blob_path = $this->getLobFile('tin_drum.gif');
  69. $clob_path = $this->getLobFile('tin_drum.txt');
  70. $book = BookPeer::doSelectOne(new Criteria());
  71. $m1 = new Media();
  72. $m1->setBook($book);
  73. $m1->setCoverImage(file_get_contents($blob_path));
  74. $m1->setExcerpt(file_get_contents($clob_path));
  75. $m1->save();
  76. $m1_id = $m1->getId();
  77. $m1->reload();
  78. $img = $m1->getCoverImage();
  79. $txt = $m1->getExcerpt();
  80. $this->assertInternalType('resource', $img, "Expected results of BLOB method to be a resource.");
  81. $this->assertInternalType('string', $txt, "Expected results of CLOB method to be a string.");
  82. $stat = fstat($img);
  83. $size = $stat['size'];
  84. $this->assertEquals(filesize($blob_path), $size, "Expected filesize to match stat(blobrsc)");
  85. $this->assertEquals(filesize($clob_path), strlen($txt), "Expected filesize to match clob strlen");
  86. }
  87. /**
  88. * Test to make sure that file pointer is not when it is fetched
  89. * from the object.
  90. *
  91. * This is actually a test for correct behavior and does not completely fix
  92. * the associated ticket (which was resolved wontfix).
  93. *
  94. * This does test the rewind-after-save functionality, however.
  95. *
  96. * @link http://propel.phpdb.org/trac/ticket/531
  97. */
  98. public function testLobRepeatRead()
  99. {
  100. $blob_path = $this->getLobFile('tin_drum.gif');
  101. $clob_path = $this->getLobFile('tin_drum.txt');
  102. $book = BookPeer::doSelectOne(new Criteria());
  103. $m1 = new Media();
  104. $m1->setBook($book);
  105. $m1->setCoverImage(file_get_contents($blob_path));
  106. $m1->setExcerpt(file_get_contents($clob_path));
  107. $m1->save();
  108. $img = $m1->getCoverImage();
  109. // 1) Assert that this resource has been rewound.
  110. $this->assertEquals(0, ftell($img), "Expected position of cursor in file pointer to be 0");
  111. // 1) Assert that we've got a valid stream to start with
  112. $this->assertInternalType('resource', $img, "Expected results of BLOB method to be a resource.");
  113. // read first 100 bytes
  114. $firstBytes = fread($img, 100);
  115. $img2 = $m1->getCoverImage();
  116. $this->assertSame($img, $img2, "Assert that the two resources are the same.");
  117. // read next 100 bytes
  118. $nextBytes = fread($img, 100);
  119. $this->assertNotEquals(bin2hex($firstBytes), bin2hex($nextBytes), "Expected the first 100 and next 100 bytes to not be identical.");
  120. }
  121. /**
  122. * Tests the setting of null LOBs
  123. */
  124. public function testLobNulls()
  125. {
  126. $book = BookPeer::doSelectOne(new Criteria());
  127. $m1 = new Media();
  128. $m1->setBook($book);
  129. $this->assertTrue($m1->getCoverImage() === null, "Initial LOB value for a new object should be null.");
  130. $m1->save();
  131. $m1_id = $m1->getId();
  132. $m2 = new Media();
  133. $m2->setBook($book);
  134. $m2->setCoverImage(null);
  135. $this->assertTrue($m2->getCoverImage() === null, "Setting a LOB to null should cause accessor to return null.");
  136. $m2->save();
  137. $m2_id = $m2->getId();
  138. $m1->reload();
  139. $this->assertTrue($m1->getCoverImage() === null, "Default null LOB value should be null after a reload.");
  140. $m2->reload();
  141. $this->assertTrue($m2->getCoverImage() === null, "LOB value set to null should be null after a reload.");
  142. }
  143. /**
  144. * Tests the setting of LOB (BLOB and CLOB) values.
  145. */
  146. public function testLobSetting()
  147. {
  148. $blob_path = $this->getLobFile('tin_drum.gif');
  149. $blob2_path = $this->getLobFile('propel.gif');
  150. $clob_path = $this->getLobFile('tin_drum.txt');
  151. $book = BookPeer::doSelectOne(new Criteria());
  152. $m1 = new Media();
  153. $m1->setBook($book);
  154. $m1->setCoverImage(file_get_contents($blob_path));
  155. $m1->setExcerpt(file_get_contents($clob_path));
  156. $m1->save();
  157. $m1_id = $m1->getId();
  158. // 1) Assert that we've got a valid stream to start with
  159. $img = $m1->getCoverImage();
  160. $this->assertInternalType('resource', $img, "Expected results of BLOB method to be a resource.");
  161. // 2) Test setting a BLOB column with file contents
  162. $m1->setCoverImage(file_get_contents($blob2_path));
  163. $this->assertInternalType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting BLOB with file contents.");
  164. // commit those changes & reload
  165. $m1->save();
  166. // 3) Verify that we've got a valid resource after reload
  167. $m1->reload();
  168. $this->assertInternalType('resource', $m1->getCoverImage(), "Expected to get a resource back after setting reloading object.");
  169. // 4) Test isModified() behavior
  170. $fp = fopen("php://temp", "r+");
  171. fwrite($fp, file_get_contents($blob2_path));
  172. $m1->setCoverImage($fp);
  173. $this->assertTrue($m1->isModified(), "Expected Media object to be modified, despite fact that stream is to same data");
  174. // 5) Test external modification of the stream (and re-setting it into the object)
  175. $stream = $m1->getCoverImage();
  176. fwrite($stream, file_get_contents($blob_path)); // change the contents of the stream
  177. $m1->setCoverImage($stream);
  178. $this->assertTrue($m1->isModified(), "Expected Media object to be modified when stream contents changed.");
  179. $this->assertNotEquals(file_get_contents($blob2_path), stream_get_contents($m1->getCoverImage()));
  180. $m1->save();
  181. // 6) Assert that when we call the setter with a stream, that the file in db gets updated.
  182. $m1->reload(); // start with a fresh copy from db
  183. // Ensure that object is set up correctly
  184. $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.");
  185. $fp = fopen($blob_path, "r");
  186. $m1->setCoverImage($fp);
  187. $m1->save();
  188. $m1->reload(); // refresh from db
  189. // Assert that we've updated the db
  190. $this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($m1->getCoverImage())), "Expected the updated BLOB value after setting with a stream.");
  191. // 7) Assert that 'w' mode works
  192. }
  193. public function testLobSetting_WriteMode()
  194. {
  195. $blob_path = $this->getLobFile('tin_drum.gif');
  196. $blob2_path = $this->getLobFile('propel.gif');
  197. $clob_path = $this->getLobFile('tin_drum.txt');
  198. $book = BookPeer::doSelectOne(new Criteria());
  199. $m1 = new Media();
  200. $m1->setBook($book);
  201. $m1->setCoverImage(file_get_contents($blob_path));
  202. $m1->setExcerpt(file_get_contents($clob_path));
  203. $m1->save();
  204. MediaPeer::clearInstancePool();
  205. // make sure we have the latest from the db:
  206. $m2 = MediaPeer::retrieveByPK($m1->getId());
  207. // now attempt to assign a temporary stream, opened in 'w' mode, to the db
  208. $stream = fopen("php://memory", 'w');
  209. fwrite($stream, file_get_contents($blob2_path));
  210. $m2->setCoverImage($stream);
  211. $m2->save();
  212. fclose($stream);
  213. $m2->reload();
  214. $this->assertEquals(md5(file_get_contents($blob2_path)), md5(stream_get_contents($m2->getCoverImage())), "Expected contents to match when setting stream w/ 'w' mode");
  215. $stream2 = fopen("php://memory", 'w+');
  216. fwrite($stream2, file_get_contents($blob_path));
  217. rewind($stream2);
  218. $this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($stream2)), "Expecting setup to be correct");
  219. $m2->setCoverImage($stream2);
  220. $m2->save();
  221. $m2->reload();
  222. $this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($m2->getCoverImage())), "Expected contents to match when setting stream w/ 'w+' mode");
  223. }
  224. }