-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
462 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Types; | ||
|
||
use BcMath\Number; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Exception\InvalidType; | ||
use Doctrine\DBAL\Types\Exception\ValueNotConvertible; | ||
use TypeError; | ||
use ValueError; | ||
|
||
use function is_float; | ||
|
||
final class NumberType extends Type | ||
{ | ||
/** {@inheritDoc} */ | ||
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
{ | ||
return $platform->getDecimalTypeDeclarationSQL($column); | ||
} | ||
|
||
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (! $value instanceof Number) { | ||
throw InvalidType::new($value, static::class, ['null', Number::class]); | ||
} | ||
|
||
return (string) $value; | ||
} | ||
|
||
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Number | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
// SQLite might return a decimal as float. | ||
if (is_float($value)) { | ||
$value = (string) $value; | ||
} | ||
|
||
try { | ||
return new Number($value); | ||
} catch (TypeError | ValueError $e) { | ||
throw ValueNotConvertible::new($value, static::class, previous: $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Types; | ||
|
||
use BcMath\Number; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Types\Types; | ||
use PHPUnit\Framework\Attributes\RequiresPhp; | ||
use PHPUnit\Framework\Attributes\RequiresPhpExtension; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
|
||
#[RequiresPhp('8.4')] | ||
#[RequiresPhpExtension('bcmath')] | ||
final class NumberTest extends FunctionalTestCase | ||
{ | ||
#[TestWith(['13.37'])] | ||
#[TestWith(['13.0'])] | ||
public function testInsertAndRetrieveNumber(string $numberAsString): void | ||
{ | ||
$expected = new Number($numberAsString); | ||
|
||
$table = new Table('number_table'); | ||
$table->addColumn('val', Types::NUMBER, ['precision' => 4, 'scale' => 2]); | ||
|
||
$this->dropAndCreateTable($table); | ||
|
||
$this->connection->insert( | ||
'number_table', | ||
['val' => $expected], | ||
['val' => Types::NUMBER], | ||
); | ||
|
||
$value = $this->connection->convertToPHPValue( | ||
$this->connection->fetchOne('SELECT val FROM number_table'), | ||
Types::NUMBER, | ||
); | ||
|
||
self::assertInstanceOf(Number::class, $value); | ||
self::assertSame(0, $expected <=> $value); | ||
} | ||
|
||
public function testCompareNumberTable(): void | ||
{ | ||
$table = new Table('number_table'); | ||
$table->addColumn('val', Types::NUMBER, ['precision' => 4, 'scale' => 2]); | ||
|
||
$this->dropAndCreateTable($table); | ||
|
||
$schemaManager = $this->connection->createSchemaManager(); | ||
|
||
self::assertTrue( | ||
$schemaManager->createComparator() | ||
->compareTables($schemaManager->introspectTable('number_table'), $table) | ||
->isEmpty(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} |