Skip to content

Commit

Permalink
Merge pull request #6 from netlogix/fix/unit-tests
Browse files Browse the repository at this point in the history
fix: Restore old exception behaviour to repair unit tests
  • Loading branch information
stephanschuler authored Oct 30, 2024
2 parents 31efa89 + 7c62ad2 commit 19acc2d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
35 changes: 23 additions & 12 deletions src/Upsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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')) {
Expand All @@ -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);
}
}
}
}
7 changes: 6 additions & 1 deletion test/UpsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 19acc2d

Please sign in to comment.