-
-
Notifications
You must be signed in to change notification settings - Fork 56
feat: add support for arrays of REAL and DOUBLE PRECISION
#307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
3cbad4f
49634db
c13c75e
5a668bf
f6b52ac
59c63a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| # Available types | ||
|
|
||
| | PostgreSQL type | Implemented by | | ||
| |---|---| | ||
| | _bool | `MartinGeorgiev\Doctrine\DBAL\Types\BooleanArray` | | ||
| | _int2 | `MartinGeorgiev\Doctrine\DBAL\Types\SmallIntArray` | | ||
| | _int4 | `MartinGeorgiev\Doctrine\DBAL\Types\IntegerArray` | | ||
| | _int8 | `MartinGeorgiev\Doctrine\DBAL\Types\BigIntArray` | | ||
| | _text | `MartinGeorgiev\Doctrine\DBAL\Types\TextArray` | | ||
| | _jsonb | `MartinGeorgiev\Doctrine\DBAL\Types\JsonbArray` | | ||
| | jsonb | `MartinGeorgiev\Doctrine\DBAL\Types\Jsonb` | | ||
| | PostgreSQL type in practical use | PostgreSQL internal system catalogue name | Implemented by | | ||
| |---|---|---| | ||
| | bool[] | _bool | `MartinGeorgiev\Doctrine\DBAL\Types\BooleanArray` | | ||
| | smallint[] | _int2 | `MartinGeorgiev\Doctrine\DBAL\Types\SmallIntArray` | | ||
| | integer[] | _int4 | `MartinGeorgiev\Doctrine\DBAL\Types\IntegerArray` | | ||
| | bigint[] | _int8 | `MartinGeorgiev\Doctrine\DBAL\Types\BigIntArray` | | ||
| | real[] | _float4 | `MartinGeorgiev\Doctrine\DBAL\Types\RealArray` | | ||
| | double precision[] | _float8 | `MartinGeorgiev\Doctrine\DBAL\Types\DoublePrecisionArray` | | ||
| | text[] | _text | `MartinGeorgiev\Doctrine\DBAL\Types\TextArray` | | ||
| | jsonb[] | _jsonb | `MartinGeorgiev\Doctrine\DBAL\Types\JsonbArray` | | ||
| | jsonb | jsonb | `MartinGeorgiev\Doctrine\DBAL\Types\Jsonb` | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\DBAL\Types; | ||
|
|
||
| use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidFloatValueException; | ||
|
|
||
| /** | ||
| * @since 3.0 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| abstract class BaseFloatArray extends BaseArray | ||
| { | ||
| private const FLOAT_REGEX = '/^-?\d*\.?\d+(?:[eE][-+]?\d+)?$/'; | ||
|
|
||
| abstract protected function getMinValue(): string; | ||
|
|
||
| abstract protected function getMaxValue(): string; | ||
|
|
||
| abstract protected function getMaxPrecision(): int; | ||
|
|
||
| abstract protected function getMinAbsoluteValue(): string; | ||
|
|
||
| public function isValidArrayItemForDatabase(mixed $item): bool | ||
| { | ||
| $isNotANumber = !\is_float($item) && !\is_int($item) && !\is_string($item); | ||
| if ($isNotANumber) { | ||
| return false; | ||
| } | ||
|
|
||
| $stringValue = (string) $item; | ||
| if (!\preg_match(self::FLOAT_REGEX, $stringValue)) { | ||
| return false; | ||
| } | ||
|
|
||
| $floatValue = (float) $stringValue; | ||
|
|
||
| // For scientific notation, convert to standard decimal form before checking precision | ||
| if (\str_contains($stringValue, 'e') || \str_contains($stringValue, 'E')) { | ||
| $standardForm = \sprintf('%.'.($this->getMaxPrecision() + 1).'f', $floatValue); | ||
| $parts = \explode('.', $standardForm); | ||
| if (isset($parts[1]) && \strlen($parts[1]) > $this->getMaxPrecision()) { | ||
| return false; | ||
| } | ||
| } elseif (\str_contains($stringValue, '.')) { | ||
| $parts = \explode('.', $stringValue); | ||
| if (\strlen($parts[1]) > $this->getMaxPrecision()) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| $isBelowMinValue = $floatValue < (float) $this->getMinValue(); | ||
| if ($isBelowMinValue) { | ||
| return false; | ||
| } | ||
|
|
||
| $isAboveMaxValue = $floatValue > (float) $this->getMaxValue(); | ||
| if ($isAboveMaxValue) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if value is too close to zero | ||
| $absoluteValue = \abs($floatValue); | ||
| $isTooCloseToZero = $absoluteValue > 0 && $absoluteValue < (float) $this->getMinAbsoluteValue(); | ||
|
|
||
| return !$isTooCloseToZero; | ||
| } | ||
|
|
||
| public function transformArrayItemForPHP(mixed $item): ?float | ||
| { | ||
| if ($item === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $isNotANumberCandidate = !\is_float($item) && !\is_int($item) && !\is_string($item); | ||
| if ($isNotANumberCandidate) { | ||
| throw InvalidFloatValueException::forValueThatIsNotAValidPHPFloat($item); | ||
| } | ||
|
|
||
| $stringValue = (string) $item; | ||
| $isInvalidPHPFloat = !\preg_match(self::FLOAT_REGEX, $stringValue) | ||
| || $stringValue < $this->getMinValue() | ||
| || $stringValue > $this->getMaxValue(); | ||
|
|
||
| if ($isInvalidPHPFloat) { | ||
| throw InvalidFloatValueException::forValueThatIsNotAValidPHPFloat($item); | ||
| } | ||
|
|
||
| $floatValue = (float) $stringValue; | ||
|
|
||
| // Check if value is too close to zero | ||
| $absValue = \abs($floatValue); | ||
| if ($absValue > 0 && $absValue < (float) $this->getMinAbsoluteValue()) { | ||
| throw InvalidFloatValueException::forValueThatIsTooCloseToZero($item); | ||
| } | ||
|
|
||
| // For scientific notation, convert to standard decimal form before checking precision | ||
| if (\str_contains($stringValue, 'e') || \str_contains($stringValue, 'E')) { | ||
| $standardForm = \sprintf('%.'.($this->getMaxPrecision() + 1).'f', $floatValue); | ||
| $parts = \explode('.', $standardForm); | ||
| if (isset($parts[1]) && \strlen($parts[1]) > $this->getMaxPrecision()) { | ||
| throw InvalidFloatValueException::forValueThatExceedsMaximumPrecision($item, $this->getMaxPrecision()); | ||
| } | ||
| } elseif (\str_contains($stringValue, '.')) { | ||
| $parts = \explode('.', $stringValue); | ||
| if (\strlen($parts[1]) > $this->getMaxPrecision()) { | ||
| throw InvalidFloatValueException::forValueThatExceedsMaximumPrecision($item, $this->getMaxPrecision()); | ||
| } | ||
| } | ||
|
|
||
| return $floatValue; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\DBAL\Types; | ||
|
|
||
| /** | ||
| * Implementation of PostgreSQL DOUBLE PRECISION[] data type. | ||
| * | ||
| * @see https://www.postgresql.org/docs/current/datatype-numeric.html | ||
| * @since 3.0 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class DoublePrecisionArray extends BaseFloatArray | ||
| { | ||
| protected const TYPE_NAME = 'double precision[]'; | ||
|
|
||
| protected function getMinValue(): string | ||
| { | ||
| return '-1.7976931348623157E+308'; | ||
| } | ||
|
|
||
| protected function getMaxValue(): string | ||
| { | ||
| return '1.7976931348623157E+308'; | ||
| } | ||
|
|
||
| protected function getMaxPrecision(): int | ||
| { | ||
| return 15; | ||
| } | ||
|
|
||
| protected function getMinAbsoluteValue(): string | ||
| { | ||
| return '2.2250738585072014E-308'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\DBAL\Types\Exceptions; | ||
|
|
||
| use Doctrine\DBAL\Types\ConversionException; | ||
|
|
||
| /** | ||
| * @since 3.0 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class InvalidFloatValueException extends ConversionException | ||
| { | ||
| public static function forValueThatIsTooCloseToZero(mixed $value): self | ||
| { | ||
| return new self(\sprintf("Given value of '%s' is too close to zero for PostgreSQL DOUBLE PRECISION type", \var_export($value, true))); | ||
| } | ||
|
|
||
| public static function forValueThatIsNotAValidPHPFloat(mixed $value): self | ||
| { | ||
| return new self(\sprintf('Given value of %s content cannot be transformed to valid PHP float.', \var_export($value, true))); | ||
| } | ||
|
|
||
| public static function forValueThatExceedsMaximumPrecision(mixed $value, int $maxPrecision): self | ||
| { | ||
| return new self(\sprintf("Given value of '%s' exceeds maximum precision of %d", \var_export($value, true), $maxPrecision)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\DBAL\Types; | ||
|
|
||
| /** | ||
| * Implementation of PostgreSQL REAL[] data type. | ||
| * | ||
| * @see https://www.postgresql.org/docs/current/datatype-numeric.html | ||
| * @since 3.0 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class RealArray extends BaseFloatArray | ||
| { | ||
| protected const TYPE_NAME = 'real[]'; | ||
|
|
||
| protected function getMinValue(): string | ||
| { | ||
| return '-3.4028235E+38'; | ||
| } | ||
|
|
||
| protected function getMaxValue(): string | ||
| { | ||
| return '3.4028235E+38'; | ||
| } | ||
|
|
||
| protected function getMaxPrecision(): int | ||
| { | ||
| return 6; | ||
| } | ||
|
|
||
| protected function getMinAbsoluteValue(): string | ||
| { | ||
| return '1.17549435E-38'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| declare(strict_types=1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace Tests\MartinGeorgiev\Doctrine\DBAL\Types; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Doctrine\DBAL\Types\ConversionException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use MartinGeorgiev\Doctrine\DBAL\Types\BaseFloatArray; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use PHPUnit\Framework\TestCase; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abstract class BaseFloatArrayTestCase extends TestCase | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected BaseFloatArray $fixture; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @dataProvider provideInvalidTransformations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function can_detect_invalid_for_transformation_php_value(mixed $phpValue): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self::assertFalse($this->fixture->isValidArrayItemForDatabase($phpValue)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return list<mixed> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static function provideInvalidTransformations(): array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [true], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [null], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ['string'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [[]], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [new \stdClass()], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ['1.23e4'], // Scientific notation not allowed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @dataProvider provideValidTransformations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function can_transform_from_php_value(float $phpValue, string $postgresValue): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self::assertTrue($this->fixture->isValidArrayItemForDatabase($phpValue)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+43
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion The test method name doesn't match the assertion. The method is named - public function can_transform_from_php_value(float $phpValue, string $postgresValue): void
+ public function can_validate_php_value_for_database(float $phpValue, string $postgresValue): void
{
self::assertTrue($this->fixture->isValidArrayItemForDatabase($phpValue));
}Alternatively, modify the test to actually check the transformation: public function can_transform_from_php_value(float $phpValue, string $postgresValue): void
{
self::assertTrue($this->fixture->isValidArrayItemForDatabase($phpValue));
+ // Test actual transformation
+ $transformedValue = $this->fixture->convertToDatabaseValue([$phpValue], \Doctrine\DBAL\Platforms\PostgreSQLPlatform::class);
+ self::assertStringContainsString($postgresValue, $transformedValue);
}📝 Committable suggestion
Suggested change
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @dataProvider provideValidTransformations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function can_transform_to_php_value(float $phpValue, string $postgresValue): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self::assertEquals($phpValue, $this->fixture->transformArrayItemForPHP($postgresValue)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return list<array{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * phpValue: float, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * postgresValue: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * }> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abstract public static function provideValidTransformations(): array; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function throws_conversion_exception_when_invalid_array_item_value(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->expectException(ConversionException::class); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->expectExceptionMessage("Given value of '1.23e4' content cannot be transformed to valid PHP float."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->fixture->transformArrayItemForPHP('1.23e4'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion The test hardcodes the scientific notation example. This test expects an exception specifically for the value public function throws_conversion_exception_when_invalid_array_item_value(): void
{
$this->expectException(ConversionException::class);
- $this->expectExceptionMessage("Given value of '1.23e4' content cannot be transformed to valid PHP float.");
+ // Get an invalid value from the data provider instead of hardcoding
+ $invalidValue = static::provideInvalidTransformations()[0][0];
+ $this->expectExceptionMessage("cannot be transformed to valid PHP float");
- $this->fixture->transformArrayItemForPHP('1.23e4');
+ $this->fixture->transformArrayItemForPHP($invalidValue);
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\MartinGeorgiev\Doctrine\DBAL\Types; | ||
|
|
||
| use MartinGeorgiev\Doctrine\DBAL\Types\DoublePrecisionArray; | ||
| use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidFloatValueException; | ||
|
|
||
| class DoublePrecisionArrayTestCase extends BaseFloatArrayTestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $this->fixture = new DoublePrecisionArray(); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function has_name(): void | ||
| { | ||
| self::assertEquals('double precision[]', $this->fixture->getName()); | ||
| } | ||
|
|
||
| public static function provideInvalidTransformations(): array | ||
| { | ||
| return \array_merge(parent::provideInvalidTransformations(), [ | ||
| ['1.7976931348623157E+309'], // Too large | ||
| ['-1.7976931348623157E+309'], // Too small | ||
| ['1.123456789012345678'], // Too many decimal places (>15) | ||
| ['2.2250738585072014E-309'], // Too close to zero | ||
| ['-2.2250738585072014E-309'], // Too close to zero (negative) | ||
| ['not_a_number'], | ||
| ['1.23.45'], | ||
| ['1e'], // Invalid scientific notation | ||
| ['e1'], // Invalid scientific notation | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<int, array{phpValue: float, postgresValue: string}> | ||
| */ | ||
| public static function provideValidTransformations(): array | ||
| { | ||
| return [ | ||
| ['phpValue' => 1.23e4, 'postgresValue' => '1.23e4'], | ||
| ['phpValue' => 1.23e-4, 'postgresValue' => '1.23e-4'], | ||
| ['phpValue' => 1.234567890123456, 'postgresValue' => '1.234567890123456'], | ||
| ['phpValue' => 1.0, 'postgresValue' => '1.0'], | ||
| ['phpValue' => -1.0, 'postgresValue' => '-1.0'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function throws_conversion_exception_when_value_is_too_close_to_zero(): void | ||
| { | ||
| $this->expectException(InvalidFloatValueException::class); | ||
| $this->expectExceptionMessage("Given value of '1.18E-308' is too close to zero for PostgreSQL DOUBLE PRECISION type"); | ||
|
|
||
| $this->fixture->transformArrayItemForPHP('1.18E-308'); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.