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
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Types;
use BcMath\Number;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\NumberType;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use stdClass;
#[RequiresPhp('8.4')]
#[RequiresPhpExtension('bcmath')]
final class NumberTest extends TestCase
{
private AbstractPlatform&MockObject $platform;
private NumberType $type;
protected function setUp(): void
{
$this->platform = $this->createMock(AbstractPlatform::class);
$this->type = new NumberType();
}
#[TestWith(['5.5'])]
#[TestWith(['5.5000'])]
#[TestWith([5.5])]
public function testDecimalConvertsToPHPValue(mixed $dbValue): void
{
$phpValue = $this->type->convertToPHPValue($dbValue, $this->platform);
self::assertInstanceOf(Number::class, $phpValue);
self::assertSame(0, $phpValue <=> new Number('5.5'));
}
public function testDecimalNullConvertsToPHPValue(): void
{
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
}
public function testNumberConvertsToDecimalString(): void
{
self::assertSame('5.5', $this->type->convertToDatabaseValue(new Number('5.5'), $this->platform));
}
public function testNumberNullConvertsToNull(): void
{
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}
#[TestWith(['5.5'])]
#[TestWith([new stdClass()])]
public function testInvalidPhpValuesTriggerException(mixed $value): void
{
self::expectException(InvalidType::class);
$this->type->convertToDatabaseValue($value, $this->platform);
}
#[TestWith(['foo'])]
#[TestWith([true])]
public function testUnexpectedValuesReturnedByTheDatabaseTriggerException(mixed $value): void
{
self::expectException(ValueNotConvertible::class);
$this->type->convertToPHPValue($value, $this->platform);
}
}
|