1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Types;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\BinaryType;
use Doctrine\DBAL\Types\ConversionException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use function array_map;
use function base64_encode;
use function fopen;
use function implode;
use function range;
class BinaryTest extends TestCase
{
protected AbstractPlatform&MockObject $platform;
protected BinaryType $type;
protected function setUp(): void
{
$this->platform = $this->createMock(AbstractPlatform::class);
$this->type = new BinaryType();
}
public function testReturnsBindingType(): void
{
self::assertSame(ParameterType::BINARY, $this->type->getBindingType());
}
public function testBinaryNullConvertsToPHPValue(): void
{
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
}
public function testBinaryStringConvertsToPHPValue(): void
{
$databaseValue = $this->getBinaryString();
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
self::assertSame($databaseValue, $phpValue);
}
public function testBinaryResourceConvertsToPHPValue(): void
{
$databaseValue = fopen('data://text/plain;base64,' . base64_encode('binary string'), 'r');
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
self::assertSame('binary string', $phpValue);
}
/**
* Creates a binary string containing all possible byte values.
*/
private function getBinaryString(): string
{
return implode(array_map('chr', range(0, 255)));
}
#[DataProvider('getInvalidDatabaseValues')]
public function testThrowsConversionExceptionOnInvalidDatabaseValue(mixed $value): void
{
$this->expectException(ConversionException::class);
$this->type->convertToPHPValue($value, $this->platform);
}
/** @return mixed[][] */
public static function getInvalidDatabaseValues(): iterable
{
return [
[false],
[true],
[0],
[1],
[-1],
[0.0],
[1.1],
[-1.1],
];
}
}
|