BlobTest.php 1.71 KB
Newer Older
H. Westphal's avatar
H. Westphal committed
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Types;
H. Westphal's avatar
H. Westphal committed
4

5
use Doctrine\DBAL\Platforms\AbstractPlatform;
Sergei Morozov's avatar
Sergei Morozov committed
6
use Doctrine\DBAL\Types\BlobType;
H. Westphal's avatar
H. Westphal committed
7
use Doctrine\DBAL\Types\Type;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10

11 12 13 14
use function base64_encode;
use function chr;
use function fopen;
use function stream_get_contents;
H. Westphal's avatar
H. Westphal committed
15

16
class BlobTest extends TestCase
H. Westphal's avatar
H. Westphal committed
17
{
18
    /** @var AbstractPlatform|MockObject */
19
    protected $platform;
H. Westphal's avatar
H. Westphal committed
20

Sergei Morozov's avatar
Sergei Morozov committed
21
    /** @var BlobType */
22 23
    protected $type;

24
    protected function setUp(): void
H. Westphal's avatar
H. Westphal committed
25
    {
26
        $this->platform = $this->createMock(AbstractPlatform::class);
Sergei Morozov's avatar
Sergei Morozov committed
27
        $this->type     = Type::getType('blob');
H. Westphal's avatar
H. Westphal committed
28 29
    }

30
    public function testBlobNullConvertsToPHPValue(): void
H. Westphal's avatar
H. Westphal committed
31
    {
32
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
33 34
    }

35
    public function testBinaryStringConvertsToPHPValue(): void
36 37 38 39
    {
        $databaseValue = $this->getBinaryString();
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);

40
        self::assertIsResource($phpValue);
41
        self::assertSame($databaseValue, stream_get_contents($phpValue));
42 43
    }

44
    public function testBinaryResourceConvertsToPHPValue(): void
45 46 47 48
    {
        $databaseValue = fopen('data://text/plain;base64,' . base64_encode($this->getBinaryString()), 'r');
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);

49
        self::assertSame($databaseValue, $phpValue);
50 51 52 53 54
    }

    /**
     * Creates a binary string containing all possible byte values.
     */
55
    private function getBinaryString(): string
56 57 58 59 60 61 62 63
    {
        $string = '';

        for ($i = 0; $i < 256; $i++) {
            $string .= chr($i);
        }

        return $string;
H. Westphal's avatar
H. Westphal committed
64 65
    }
}