|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Tests\Types; |
| 6 | + |
| 7 | +use BcMath\Number; |
| 8 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 9 | +use Doctrine\DBAL\Types\Exception\InvalidType; |
| 10 | +use Doctrine\DBAL\Types\Exception\ValueNotConvertible; |
| 11 | +use Doctrine\DBAL\Types\NumberType; |
| 12 | +use PHPUnit\Framework\Attributes\RequiresPhp; |
| 13 | +use PHPUnit\Framework\Attributes\RequiresPhpExtension; |
| 14 | +use PHPUnit\Framework\Attributes\TestWith; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use stdClass; |
| 18 | + |
| 19 | +#[RequiresPhp('8.4')] |
| 20 | +#[RequiresPhpExtension('bcmath')] |
| 21 | +final class NumberTest extends TestCase |
| 22 | +{ |
| 23 | + private AbstractPlatform&MockObject $platform; |
| 24 | + private NumberType $type; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->platform = $this->createMock(AbstractPlatform::class); |
| 29 | + $this->type = new NumberType(); |
| 30 | + } |
| 31 | + |
| 32 | + #[TestWith(['5.5'])] |
| 33 | + #[TestWith(['5.5000'])] |
| 34 | + #[TestWith([5.5])] |
| 35 | + public function testDecimalConvertsToPHPValue(mixed $dbValue): void |
| 36 | + { |
| 37 | + $phpValue = $this->type->convertToPHPValue($dbValue, $this->platform); |
| 38 | + |
| 39 | + self::assertInstanceOf(Number::class, $phpValue); |
| 40 | + self::assertSame(0, $phpValue->compare(new Number('5.5'))); |
| 41 | + } |
| 42 | + |
| 43 | + public function testDecimalNullConvertsToPHPValue(): void |
| 44 | + { |
| 45 | + self::assertNull($this->type->convertToPHPValue(null, $this->platform)); |
| 46 | + } |
| 47 | + |
| 48 | + public function testNumberConvertsToDecimalString(): void |
| 49 | + { |
| 50 | + self::assertSame('5.5', $this->type->convertToDatabaseValue(new Number('5.5'), $this->platform)); |
| 51 | + } |
| 52 | + |
| 53 | + public function testNumberNullConvertsToNull(): void |
| 54 | + { |
| 55 | + self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); |
| 56 | + } |
| 57 | + |
| 58 | + #[TestWith(['5.5'])] |
| 59 | + #[TestWith([new stdClass()])] |
| 60 | + public function testInvalidPhpValuesTriggerException(mixed $value): void |
| 61 | + { |
| 62 | + self::expectException(InvalidType::class); |
| 63 | + |
| 64 | + $this->type->convertToDatabaseValue($value, $this->platform); |
| 65 | + } |
| 66 | + |
| 67 | + #[TestWith(['foo'])] |
| 68 | + #[TestWith([true])] |
| 69 | + public function testUnexpectedValuesReturnedByTheDatabaseTriggerException(mixed $value): void |
| 70 | + { |
| 71 | + self::expectException(ValueNotConvertible::class); |
| 72 | + |
| 73 | + $this->type->convertToPHPValue($value, $this->platform); |
| 74 | + } |
| 75 | +} |
0 commit comments