From 7c62ad259bd8c8a4e13afb2ca4b29468714a051d Mon Sep 17 00:00:00 2001 From: Lars Lauger Date: Wed, 30 Oct 2024 17:00:22 +0100 Subject: [PATCH] fix: Restore old exception behaviour to repair unit tests This fixes the unit tests which are broken since 2.1.0. --- src/Upsert.php | 35 +++++++++++++++++++++++------------ test/UpsertTest.php | 7 ++++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/Upsert.php b/src/Upsert.php index 7869b2f..6d9a7c6 100644 --- a/src/Upsert.php +++ b/src/Upsert.php @@ -41,7 +41,7 @@ public function forTable(string $table): self public function withIdentifier(string $column, mixed $value, int $parameterType = ParameterType::STRING): self { - $this->throwErrorIsColumnExists($column); + $this->throwErrorIfColumnExists($column, 'identifier'); if (is_object($value) && method_exists($value, 'rawType')) { $parameterType = $value->rawType(); @@ -63,7 +63,7 @@ public function withField( int $parameterType = ParameterType::STRING, bool $insertOnly = false ): self { - $this->throwErrorIsColumnExists($column); + $this->throwErrorIfColumnExists($column, 'field'); if (is_object($value) && method_exists($value, 'rawType')) { $parameterType = $value->rawType(); @@ -153,11 +153,11 @@ private function getValue(mixed $value): int|string|float|bool|null } if ($value instanceof Stringable) { - $value = (string) $value; + $value = (string)$value; } if ($value instanceof BackedEnum) { - $value = (string) $value->value; + $value = (string)$value->value; } if (is_object($value) && method_exists($value, 'rawValue')) { @@ -167,17 +167,28 @@ private function getValue(mixed $value): int|string|float|bool|null return $value; } - private function throwErrorIsColumnExists(string $column): void + private function throwErrorIfColumnExists(string $column, string $type): void { - if (array_key_exists($column, $this->fields)) { - throw new Exception\FieldAlreadyInUse(sprintf('The field "%s" has already been set!', $column), 1603196457); + if ($type === 'field') { + if (array_key_exists($column, $this->fields)) { + throw new Exception\FieldAlreadyInUse(sprintf('The field "%s" has already been set!', $column), + 1603196457); + } + if (array_key_exists($column, $this->identifiers)) { + throw new Exception\FieldRegisteredAsIdentifier(sprintf('The field "%s" has already been set as identifier!', + $column), 1603197691); + } } - if (array_key_exists($column, $this->identifiers)) { - throw new Exception\IdentifierRegisteredAsField( - sprintf('The field "%s" has already been set as identifier!', $column), - 1603197691 - ); + if ($type === 'identifier') { + if (array_key_exists($column, $this->identifiers)) { + throw new Exception\IdentifierAlreadyInUse(sprintf('The identifier "%s" has already been set!', + $column), 1603196381); + } + if (array_key_exists($column, $this->fields)) { + throw new Exception\IdentifierRegisteredAsField(sprintf('The identifier "%s" has already been set as field!', + $column), 1603197666); + } } } } diff --git a/test/UpsertTest.php b/test/UpsertTest.php index a356fc0..9e7cb30 100644 --- a/test/UpsertTest.php +++ b/test/UpsertTest.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\ForwardCompatibility\DriverResultStatement; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Platforms\AbstractPlatform; use Netlogix\Doctrine\Upsert\Exception; use Netlogix\Doctrine\Upsert\Upsert; @@ -275,7 +276,11 @@ private function getMockConnection() ->disableOriginalConstructor() ->getMock(); - $platform = $this->getMockBuilder(AbstractPlatform::class) + $platformClass = AbstractPlatform::class; + if (class_exists(SqlitePlatform::class)) { + $platformClass = SqlitePlatform::class; + } + $platform = $this->getMockBuilder($platformClass) ->getMock(); $platform