Skip to content
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

Fixed passing DateTime instances into DoctrineDatabase::findBy() methods #22

Draft
wants to merge 2 commits into
base: 4.6
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/contracts/Gateway/AbstractDoctrineDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\Common\Collections\Expr\Expression;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Types\ConversionException;
use Ibexa\CorePersistence\Gateway\ExpressionVisitor;
use InvalidArgumentException;

Expand Down Expand Up @@ -116,8 +117,6 @@ public function countAll(): int
}

/**
* @param \Doctrine\Common\Collections\Expr\Expression|array<string, \Doctrine\Common\Collections\Expr\Expression|scalar|array<scalar>|null> $criteria
*
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
* @throws \Ibexa\Contracts\CorePersistence\Exception\MappingException
Expand Down Expand Up @@ -257,7 +256,7 @@ private function applyInheritance(QueryBuilder $qb): void
}

/**
* @param \Doctrine\Common\Collections\Expr\Expression|array<string, \Doctrine\Common\Collections\Expr\Expression|scalar|array<scalar>|null> $criteria
* @param \Doctrine\Common\Collections\Expr\Expression|array<string, \Doctrine\Common\Collections\Expr\Expression|scalar|\DateTimeInterface|array<scalar|\DateTimeInterface>|null> $criteria
*
* @return \Doctrine\DBAL\Query\Expression\CompositeExpression|string|null
*
Expand Down Expand Up @@ -325,7 +324,7 @@ private function buildConditionExpression(ExpressionVisitor $visitor, QueryBuild
}

/**
* @param scalar|array<scalar>|null $value
* @param scalar|\DateTimeInterface|array<scalar|\DateTimeInterface>|null $value
*
* @throws \Doctrine\DBAL\Exception
* @throws \Ibexa\Contracts\CorePersistence\Exception\MappingException
Expand Down Expand Up @@ -382,7 +381,21 @@ private function buildCondition(QueryBuilder $qb, string $column, $value): strin
return $qb->expr()->isNull($fullColumnName);
}

$columnType = $metadata->getColumnType($column);
$platform = $this->connection->getDatabasePlatform();

if (is_array($value)) {
$value = array_map(
static function ($value) use ($columnType, $platform) {
try {
return $columnType->convertToDatabaseValue($value, $platform);
} catch (ConversionException $e) {
return $value;
}
},
$value,
);

$parameter = $qb->createPositionalParameter(
$value,
$columnBinding + Connection::ARRAY_PARAM_OFFSET
Expand All @@ -391,6 +404,11 @@ private function buildCondition(QueryBuilder $qb, string $column, $value): strin
return $qb->expr()->in($fullColumnName, $parameter);
}

try {
$value = $columnType->convertToDatabaseValue($value, $this->connection->getDatabasePlatform());
} catch (ConversionException $e) {
}

$parameter = $qb->createPositionalParameter($value, $columnBinding);

return $qb->expr()->eq($fullColumnName, $parameter);
Expand Down Expand Up @@ -429,7 +447,7 @@ final protected function applyOrderBy(QueryBuilder $qb, ?array $orderBy = []): v
}

/**
* @param \Doctrine\Common\Collections\Expr\Expression|array<string, \Doctrine\Common\Collections\Expr\Expression|scalar|array<scalar>|null> $criteria
* @param \Doctrine\Common\Collections\Expr\Expression|array<string, \Doctrine\Common\Collections\Expr\Expression|scalar|\DateTimeInterface|array<scalar|\DateTimeInterface>|null> $criteria
*/
final protected function applyCriteria(QueryBuilder $qb, $criteria): void
{
Expand Down
6 changes: 3 additions & 3 deletions src/contracts/Gateway/GatewayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getMetadata(): DoctrineSchemaMetadataInterface;
public function countAll(): int;

/**
* @param \Doctrine\Common\Collections\Expr\Expression|array<\Doctrine\Common\Collections\Expr\Expression|scalar|array<scalar>> $criteria
* @param \Doctrine\Common\Collections\Expr\Expression|array<\Doctrine\Common\Collections\Expr\Expression|scalar|\DateTimeInterface|array<scalar|\DateTimeInterface>> $criteria
*/
public function countBy($criteria): int;

Expand All @@ -33,7 +33,7 @@ public function countBy($criteria): int;
public function findAll(?int $limit = null, int $offset = 0): array;

/**
* @param \Doctrine\Common\Collections\Expr\Expression|array<\Doctrine\Common\Collections\Expr\Expression|scalar|array<scalar>|null> $criteria Map of column names to values that will be used as part of WHERE query
* @param \Doctrine\Common\Collections\Expr\Expression|array<\Doctrine\Common\Collections\Expr\Expression|scalar|\DateTimeInterface|array<scalar|\DateTimeInterface>|null> $criteria Map of column names to values that will be used as part of WHERE query
* @param array<string, string>|null $orderBy Map of column names to "ASC" or "DESC", that will be used in SORT query
*
* @phpstan-param array<string, "ASC"|"DESC">|null $orderBy
Expand All @@ -43,7 +43,7 @@ public function findAll(?int $limit = null, int $offset = 0): array;
public function findBy($criteria, ?array $orderBy = null, ?int $limit = null, int $offset = 0): array;

/**
* @param array<string, \Doctrine\Common\Collections\Expr\Expression|scalar|array<scalar>|null> $criteria Map of column names to values that will be used as part of WHERE query
* @param array<string, \Doctrine\Common\Collections\Expr\Expression|scalar|\DateTimeInterface|array<scalar|\DateTimeInterface>|null> $criteria Map of column names to values that will be used as part of WHERE query
* @param array<string, string>|null $orderBy Map of column names to "ASC" or "DESC", that will be used in SORT query
*
* @phpstan-param array<string, "ASC"|"DESC">|null $orderBy
Expand Down
87 changes: 87 additions & 0 deletions tests/lib/Gateway/AbstractDoctrineDatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\CorePersistence\Gateway;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Types\Types;
use Ibexa\Contracts\CorePersistence\Gateway\AbstractDoctrineDatabase;
use Ibexa\Contracts\CorePersistence\Gateway\DoctrineSchemaMetadata;
use Ibexa\Contracts\CorePersistence\Gateway\DoctrineSchemaMetadataInterface;
use Ibexa\Contracts\CorePersistence\Gateway\DoctrineSchemaMetadataRegistryInterface;
use PHPUnit\Framework\TestCase;

/**
* @covers \Ibexa\Contracts\CorePersistence\Gateway\AbstractDoctrineDatabase
*/
final class AbstractDoctrineDatabaseTest extends TestCase
{
public function testDateTimeQueries(): void
{
$connection = $this->createMock(Connection::class);
$connection->expects(self::once())
->method('createQueryBuilder')
->willReturn(new QueryBuilder($connection));

$connection->expects(self::atLeastOnce())
->method('getDatabasePlatform')
->willReturn(new PostgreSQL94Platform());

$connection->expects(self::atLeastOnce())
->method('getExpressionBuilder')
->willReturn(new ExpressionBuilder($connection));

$result = $this->createMock(ResultStatement::class);
$result->expects(self::once())
->method('fetch')
->willReturn(false);

$connection->expects(self::once())
->method('executeQuery')
->with(
self::identicalTo(
'SELECT __foo_table__.id, __foo_table__.date_time_immutable_column FROM __foo_table__ __foo_table__ WHERE __foo_table__.date_time_immutable_column = ?',
),
self::identicalTo([
1 => '2024-01-01 00:00:00',
]),
)
->willReturn($result);

$registry = $this->createMock(DoctrineSchemaMetadataRegistryInterface::class);

$database = new class($connection, $registry) extends AbstractDoctrineDatabase {
protected function getTableName(): string
{
return '__foo_table__';
}

protected function buildMetadata(): DoctrineSchemaMetadataInterface
{
return new DoctrineSchemaMetadata(
$this->connection,
null,
$this->getTableName(),
[
'id' => Types::INTEGER,
'date_time_immutable_column' => Types::DATETIME_IMMUTABLE,
],
['id'],
);
}
};

$database->findBy([
'date_time_immutable_column' => new \DateTimeImmutable('2024-01-01 00:00:00'),
]);
}
}
Loading