BlobTest.php 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\Types\Type;
use PDO;

/**
 * @group DBAL-6
 */
class BlobTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
13
    protected function setUp()
14 15 16
    {
        parent::setUp();

17 18 19 20
        if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlsrv\Driver) {
            $this->markTestSkipped('This test does not work on pdo_sqlsrv driver due to a bug. See: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/5a755bdd-41e9-45cb-9166-c9da4475bb94/how-to-set-null-for-varbinarymax-using-bindvalue-using-pdosqlsrv?forum=sqldriverforphp');
        }

21 22 23 24 25 26
        try {
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
            $table = new \Doctrine\DBAL\Schema\Table("blob_table");
            $table->addColumn('id', 'integer');
            $table->addColumn('clobfield', 'text');
            $table->addColumn('blobfield', 'blob');
Steve Müller's avatar
Steve Müller committed
27
            $table->addColumn('binaryfield', 'binary', array('length' => 50));
28 29 30 31 32 33 34 35 36 37 38 39 40
            $table->setPrimaryKey(array('id'));

            $sm = $this->_conn->getSchemaManager();
            $sm->createTable($table);
        } catch(\Exception $e) {

        }
        $this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('blob_table'));
    }

    public function testInsert()
    {
        $ret = $this->_conn->insert('blob_table',
Steve Müller's avatar
Steve Müller committed
41 42
            array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'),
            array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB)
43 44 45 46 47 48 49
        );
        $this->assertEquals(1, $ret);
    }

    public function testSelect()
    {
        $ret = $this->_conn->insert('blob_table',
Steve Müller's avatar
Steve Müller committed
50 51
            array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'),
            array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB)
52 53 54 55 56 57 58 59
        );

        $this->assertBlobContains('test');
    }

    public function testUpdate()
    {
        $ret = $this->_conn->insert('blob_table',
Steve Müller's avatar
Steve Müller committed
60 61
            array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'),
            array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB)
62 63 64
        );

        $this->_conn->update('blob_table',
Steve Müller's avatar
Steve Müller committed
65
            array('blobfield' => 'test2', 'binaryfield' => 'test2'),
66
            array('id' => 1),
Steve Müller's avatar
Steve Müller committed
67
            array(\PDO::PARAM_LOB, \PDO::PARAM_LOB, \PDO::PARAM_INT)
68 69 70
        );

        $this->assertBlobContains('test2');
Steve Müller's avatar
Steve Müller committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
        $this->assertBinaryContains('test2');
    }

    private function assertBinaryContains($text)
    {
        $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');

        $this->assertEquals(1, count($rows));
        $row = array_change_key_case($rows[0], CASE_LOWER);

        $blobValue = Type::getType('binary')->convertToPHPValue($row['binaryfield'], $this->_conn->getDatabasePlatform());

        $this->assertInternalType('resource', $blobValue);
        $this->assertEquals($text, stream_get_contents($blobValue));
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    }

    private function assertBlobContains($text)
    {
        $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');

        $this->assertEquals(1, count($rows));
        $row = array_change_key_case($rows[0], CASE_LOWER);

        $blobValue = Type::getType('blob')->convertToPHPValue($row['blobfield'], $this->_conn->getDatabasePlatform());

        $this->assertInternalType('resource', $blobValue);
        $this->assertEquals($text, stream_get_contents($blobValue));
    }
}