BlobTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Functional;
  3. use Doctrine\DBAL\Types\Type;
  4. use Doctrine\DBAL\Connection;
  5. use PDO;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * @group DBAL-6
  9. */
  10. class BlobTest extends \Doctrine\Tests\DbalFunctionalTestCase
  11. {
  12. public function setUp()
  13. {
  14. parent::setUp();
  15. try {
  16. /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
  17. $table = new \Doctrine\DBAL\Schema\Table("blob_table");
  18. $table->addColumn('id', 'integer');
  19. $table->addColumn('clobfield', 'text');
  20. $table->addColumn('blobfield', 'blob');
  21. $table->setPrimaryKey(array('id'));
  22. $sm = $this->_conn->getSchemaManager();
  23. $sm->createTable($table);
  24. } catch(\Exception $e) {
  25. }
  26. $this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('blob_table'));
  27. }
  28. public function testInsert()
  29. {
  30. $ret = $this->_conn->insert('blob_table',
  31. array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test'),
  32. array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB)
  33. );
  34. $this->assertEquals(1, $ret);
  35. }
  36. public function testSelect()
  37. {
  38. $ret = $this->_conn->insert('blob_table',
  39. array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test'),
  40. array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB)
  41. );
  42. $this->assertBlobContains('test');
  43. }
  44. public function testUpdate()
  45. {
  46. $ret = $this->_conn->insert('blob_table',
  47. array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test'),
  48. array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB)
  49. );
  50. $this->_conn->update('blob_table',
  51. array('blobfield' => 'test2'),
  52. array('id' => 1),
  53. array(\PDO::PARAM_LOB, \PDO::PARAM_INT)
  54. );
  55. $this->assertBlobContains('test2');
  56. }
  57. private function assertBlobContains($text)
  58. {
  59. $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');
  60. $this->assertEquals(1, count($rows));
  61. $row = array_change_key_case($rows[0], CASE_LOWER);
  62. $blobValue = Type::getType('blob')->convertToPHPValue($row['blobfield'], $this->_conn->getDatabasePlatform());
  63. $this->assertInternalType('resource', $blobValue);
  64. $this->assertEquals($text, stream_get_contents($blobValue));
  65. }
  66. }