Failed Conditions
Push — master ( edfbda...298c91 )
by Luís
16s
created

tests/Doctrine/Tests/DBAL/Functional/BlobTest.php (1 issue)

1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
5
use Doctrine\DBAL\Types\Type;
6
use PDO;
7
8
/**
9
 * @group DBAL-6
10
 */
11
class BlobTest extends \Doctrine\Tests\DbalFunctionalTestCase
12
{
13
    protected function setUp()
14
    {
15
        parent::setUp();
16
17
        if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlsrv\Driver) {
18
            $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');
19
        }
20
21
        try {
22
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
23
            $table = new \Doctrine\DBAL\Schema\Table("blob_table");
24
            $table->addColumn('id', 'integer');
25
            $table->addColumn('clobfield', 'text');
26
            $table->addColumn('blobfield', 'blob');
27
            $table->addColumn('binaryfield', 'binary', array('length' => 50));
28
            $table->setPrimaryKey(array('id'));
29
30
            $sm = $this->_conn->getSchemaManager();
31
            $sm->createTable($table);
32
        } catch(\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
33
34
        }
35
        $this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('blob_table'));
36
    }
37
38 View Code Duplication
    public function testInsert()
39
    {
40
        $ret = $this->_conn->insert('blob_table',
41
            array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'),
42
            array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB)
43
        );
44
        self::assertEquals(1, $ret);
45
    }
46
47 View Code Duplication
    public function testSelect()
48
    {
49
        $this->_conn->insert('blob_table',
50
            array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'),
51
            array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB)
52
        );
53
54
        $this->assertBlobContains('test');
55
    }
56
57
    public function testUpdate()
58
    {
59
        $this->_conn->insert('blob_table',
60
            array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'),
61
            array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB)
62
        );
63
64
        $this->_conn->update('blob_table',
65
            array('blobfield' => 'test2', 'binaryfield' => 'test2'),
66
            array('id' => 1),
67
            array(\PDO::PARAM_LOB, \PDO::PARAM_LOB, \PDO::PARAM_INT)
68
        );
69
70
        $this->assertBlobContains('test2');
71
        $this->assertBinaryContains('test2');
72
    }
73
74 View Code Duplication
    private function assertBinaryContains($text)
75
    {
76
        $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');
77
78
        self::assertEquals(1, count($rows));
79
        $row = array_change_key_case($rows[0], CASE_LOWER);
80
81
        $blobValue = Type::getType('binary')->convertToPHPValue($row['binaryfield'], $this->_conn->getDatabasePlatform());
82
83
        self::assertInternalType('resource', $blobValue);
84
        self::assertEquals($text, stream_get_contents($blobValue));
85
    }
86
87 View Code Duplication
    private function assertBlobContains($text)
88
    {
89
        $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');
90
91
        self::assertEquals(1, count($rows));
92
        $row = array_change_key_case($rows[0], CASE_LOWER);
93
94
        $blobValue = Type::getType('blob')->convertToPHPValue($row['blobfield'], $this->_conn->getDatabasePlatform());
95
96
        self::assertInternalType('resource', $blobValue);
97
        self::assertEquals($text, stream_get_contents($blobValue));
98
    }
99
}
100