From 9c221b0b9bf7da78e6b4f412fa34d01f30e78c08 Mon Sep 17 00:00:00 2001 From: Patrik Foldes Date: Fri, 21 Feb 2020 18:38:02 +0300 Subject: [PATCH] Doctrine types can work with nullable values now --- src/Type/BTCMoneyType.php | 8 ++++---- src/Type/GaapMoneyType.php | 8 ++++---- src/Type/MoneyType.php | 8 ++++---- tests/Type/BTCMoneyTypeTest.php | 30 ++++++++++++++++++++++++++++++ tests/Type/GaapMoneyTypeTest.php | 30 ++++++++++++++++++++++++++++++ tests/Type/MoneyTypeTest.php | 30 ++++++++++++++++++++++++++++++ 6 files changed, 102 insertions(+), 12 deletions(-) diff --git a/src/Type/BTCMoneyType.php b/src/Type/BTCMoneyType.php index 7b82ce6..3f4d6a1 100644 --- a/src/Type/BTCMoneyType.php +++ b/src/Type/BTCMoneyType.php @@ -35,9 +35,9 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla * * phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function convertToPHPValue($value, AbstractPlatform $platform) : string + public function convertToPHPValue($value, AbstractPlatform $platform) : ?string { - return (string) bcmul((string) $value, '100000000', 0); + return $value !== null ? (string) bcmul((string) $value, '100000000', 0) : null; } /** @@ -45,9 +45,9 @@ public function convertToPHPValue($value, AbstractPlatform $platform) : string * * phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function convertToDatabaseValue($value, AbstractPlatform $platform) : string + public function convertToDatabaseValue($value, AbstractPlatform $platform) : ?string { - return (string) bcdiv((string) $value, '100000000', 8); + return $value !== null ? (string) bcdiv((string) $value, '100000000', 8) : null; } public function getName() : string diff --git a/src/Type/GaapMoneyType.php b/src/Type/GaapMoneyType.php index 21bccd0..00bae3e 100644 --- a/src/Type/GaapMoneyType.php +++ b/src/Type/GaapMoneyType.php @@ -35,9 +35,9 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla * * phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function convertToPHPValue($value, AbstractPlatform $platform) : string + public function convertToPHPValue($value, AbstractPlatform $platform) : ?string { - return (string) bcmul((string) $value, '10000', 0); + return $value !== null ? (string) bcmul((string) $value, '10000', 0) : null; } /** @@ -45,9 +45,9 @@ public function convertToPHPValue($value, AbstractPlatform $platform) : string * * phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function convertToDatabaseValue($value, AbstractPlatform $platform) : string + public function convertToDatabaseValue($value, AbstractPlatform $platform) : ?string { - return (string) bcdiv((string) $value, '10000', 4); + return $value !== null ? (string) bcdiv((string) $value, '10000', 4) : null; } public function getName() : string diff --git a/src/Type/MoneyType.php b/src/Type/MoneyType.php index 1099c41..1610e27 100644 --- a/src/Type/MoneyType.php +++ b/src/Type/MoneyType.php @@ -35,9 +35,9 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla * * phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function convertToPHPValue($value, AbstractPlatform $platform) : string + public function convertToPHPValue($value, AbstractPlatform $platform) : ?string { - return (string) bcmul((string) $value, '100', 0); + return $value !== null ? (string) bcmul((string) $value, '100', 0) : null; } /** @@ -45,9 +45,9 @@ public function convertToPHPValue($value, AbstractPlatform $platform) : string * * phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function convertToDatabaseValue($value, AbstractPlatform $platform) : string + public function convertToDatabaseValue($value, AbstractPlatform $platform) : ?string { - return (string) bcdiv((string) $value, '100', 2); + return $value !== null ? (string) bcdiv((string) $value, '100', 2) : null; } public function getName() : string diff --git a/tests/Type/BTCMoneyTypeTest.php b/tests/Type/BTCMoneyTypeTest.php index 218f8a2..0e8f8d7 100644 --- a/tests/Type/BTCMoneyTypeTest.php +++ b/tests/Type/BTCMoneyTypeTest.php @@ -75,6 +75,21 @@ public function testConvertToPHPValue() : void Assert::assertSame($expectedPhpValue, $phpValue); } + public function testConvertToNullPHPValue() : void + { + $expectedPhpValue = null; + + $type = Type::getType(BTCMoneyType::TYPE_NAME); + + $databaseValue = null; + /** @var AbstractPlatform&MockObject $platformMock */ + $platformMock = $this->createMock(AbstractPlatform::class); + + $phpValue = $type->convertToPHPValue($databaseValue, $platformMock); + + Assert::assertSame($expectedPhpValue, $phpValue); + } + public function testConvertToDatabaseValue() : void { $expectedDatabaseValue = '100.12345678'; @@ -90,6 +105,21 @@ public function testConvertToDatabaseValue() : void Assert::assertSame($expectedDatabaseValue, $phpValue); } + public function testConvertToNullDatabaseValue() : void + { + $expectedDatabaseValue = null; + + $type = Type::getType(BTCMoneyType::TYPE_NAME); + + $phpValue = null; + /** @var AbstractPlatform&MockObject $platformMock */ + $platformMock = $this->createMock(AbstractPlatform::class); + + $phpValue = $type->convertToDatabaseValue($phpValue, $platformMock); + + Assert::assertSame($expectedDatabaseValue, $phpValue); + } + public function testGetName() : void { $type = Type::getType(BTCMoneyType::TYPE_NAME); diff --git a/tests/Type/GaapMoneyTypeTest.php b/tests/Type/GaapMoneyTypeTest.php index 57d58cf..9f8e312 100644 --- a/tests/Type/GaapMoneyTypeTest.php +++ b/tests/Type/GaapMoneyTypeTest.php @@ -75,6 +75,21 @@ public function testConvertToPHPValue() : void Assert::assertSame($expectedPhpValue, $phpValue); } + public function testConvertToNullPHPValue() : void + { + $expectedPhpValue = null; + + $type = Type::getType(GaapMoneyType::TYPE_NAME); + + $databaseValue = null; + /** @var AbstractPlatform&MockObject $platformMock */ + $platformMock = $this->createMock(AbstractPlatform::class); + + $phpValue = $type->convertToPHPValue($databaseValue, $platformMock); + + Assert::assertSame($expectedPhpValue, $phpValue); + } + public function testConvertToDatabaseValue() : void { $expectedDatabaseValue = '100.1234'; @@ -90,6 +105,21 @@ public function testConvertToDatabaseValue() : void Assert::assertSame($expectedDatabaseValue, $phpValue); } + public function testConvertToNullDatabaseValue() : void + { + $expectedDatabaseValue = null; + + $type = Type::getType(GaapMoneyType::TYPE_NAME); + + $phpValue = null; + /** @var AbstractPlatform&MockObject $platformMock */ + $platformMock = $this->createMock(AbstractPlatform::class); + + $phpValue = $type->convertToDatabaseValue($phpValue, $platformMock); + + Assert::assertSame($expectedDatabaseValue, $phpValue); + } + public function testGetName() : void { $type = Type::getType(GaapMoneyType::TYPE_NAME); diff --git a/tests/Type/MoneyTypeTest.php b/tests/Type/MoneyTypeTest.php index 0eb8cce..c9a8bfc 100644 --- a/tests/Type/MoneyTypeTest.php +++ b/tests/Type/MoneyTypeTest.php @@ -75,6 +75,21 @@ public function testConvertToPHPValue() : void Assert::assertSame($expectedPhpValue, $phpValue); } + public function testConvertToNullPHPValue() : void + { + $expectedPhpValue = null; + + $type = Type::getType(MoneyType::TYPE_NAME); + + $databaseValue = null; + /** @var AbstractPlatform&MockObject $platformMock */ + $platformMock = $this->createMock(AbstractPlatform::class); + + $phpValue = $type->convertToPHPValue($databaseValue, $platformMock); + + Assert::assertSame($expectedPhpValue, $phpValue); + } + public function testConvertToDatabaseValue() : void { $expectedDatabaseValue = '100.12'; @@ -90,6 +105,21 @@ public function testConvertToDatabaseValue() : void Assert::assertSame($expectedDatabaseValue, $phpValue); } + public function testConvertToNullDatabaseValue() : void + { + $expectedDatabaseValue = null; + + $type = Type::getType(MoneyType::TYPE_NAME); + + $phpValue = null; + /** @var AbstractPlatform&MockObject $platformMock */ + $platformMock = $this->createMock(AbstractPlatform::class); + + $phpValue = $type->convertToDatabaseValue($phpValue, $platformMock); + + Assert::assertSame($expectedDatabaseValue, $phpValue); + } + public function testGetName() : void { $type = Type::getType(MoneyType::TYPE_NAME);