Skip to content

Commit

Permalink
Doctrine types can work with nullable values now
Browse files Browse the repository at this point in the history
  • Loading branch information
sspat committed Feb 21, 2020
1 parent 80ad404 commit 9c221b0
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/Type/BTCMoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ 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;
}

/**
* @param mixed $value
*
* 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
Expand Down
8 changes: 4 additions & 4 deletions src/Type/GaapMoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ 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;
}

/**
* @param mixed $value
*
* 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
Expand Down
8 changes: 4 additions & 4 deletions src/Type/MoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ 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;
}

/**
* @param mixed $value
*
* 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
Expand Down
30 changes: 30 additions & 0 deletions tests/Type/BTCMoneyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions tests/Type/GaapMoneyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions tests/Type/MoneyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down

0 comments on commit 9c221b0

Please sign in to comment.