Skip to content

Commit

Permalink
Merge branch '3.1.x' into 4.0.x
Browse files Browse the repository at this point in the history
* 3.1.x:
  Backport QueryParameterTest (#11288)
  Test different ways of settings query parameters
  Be less restrictive in DiscriminatorColumnMapping phpdoc (#11226)
  Allow (Array)ParameterType in QueryBuilder
  Do not implicitly cast glob's return type
  Do not cast file_put_contents's return type
  Do not implicitly cast getLockTime()'s return type
  Do not implicitly cast getLockContent()'s return value
  • Loading branch information
derrabus committed Feb 22, 2024
2 parents cbab4d6 + 2df4d75 commit c5ef72d
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 27 deletions.
12 changes: 5 additions & 7 deletions src/Cache/Region/FileLockRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function isLocked(CacheKey $key, Lock|null $lock = null): bool
$time = $this->getLockTime($filename);
$content = $this->getLockContent($filename);

if (! $content || ! $time) {
if ($content === false || $time === false) {
@unlink($filename);

return false;
Expand Down Expand Up @@ -156,12 +156,10 @@ public function evictAll(): bool
{
// The check below is necessary because on some platforms glob returns false
// when nothing matched (even though no errors occurred)
$filenames = glob(sprintf('%s/*.%s', $this->directory, self::LOCK_EXTENSION));
$filenames = glob(sprintf('%s/*.%s', $this->directory, self::LOCK_EXTENSION)) ?: [];

if ($filenames) {
foreach ($filenames as $filename) {
@unlink($filename);
}
foreach ($filenames as $filename) {
@unlink($filename);
}

return $this->region->evictAll();
Expand All @@ -176,7 +174,7 @@ public function lock(CacheKey $key): Lock|null
$lock = Lock::createLockRead();
$filename = $this->getLockFileName($key);

if (! @file_put_contents($filename, $lock->value, LOCK_EX)) {
if (@file_put_contents($filename, $lock->value, LOCK_EX) === false) {
return null;
}

Expand Down
19 changes: 7 additions & 12 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2108,13 +2108,12 @@ public function addEntityListener(string $eventName, string $class, string $meth
* @param DiscriminatorColumnMapping|mixed[]|null $columnDef
* @psalm-param DiscriminatorColumnMapping|array{
* name: string|null,
* fieldName?: string,
* type?: string,
* length?: int,
* fieldName?: string|null,
* type?: string|null,
* length?: int|null,
* columnDefinition?: string|null,
* enumType?: class-string<BackedEnum>|null,
* options?:array<string,
* mixed>|null
* options?: array<string, mixed>|null
* }|null $columnDef
*
* @throws MappingException
Expand All @@ -2136,13 +2135,9 @@ public function setDiscriminatorColumn(DiscriminatorColumnMapping|array|null $co
throw MappingException::duplicateColumnName($this->name, $columnDef['name']);
}

if (! isset($columnDef['fieldName'])) {
$columnDef['fieldName'] = $columnDef['name'];
}

if (! isset($columnDef['type'])) {
$columnDef['type'] = 'string';
}
$columnDef['fieldName'] ??= $columnDef['name'];
$columnDef['type'] ??= 'string';
$columnDef['options'] ??= [];

if (in_array($columnDef['type'], ['boolean', 'array', 'object', 'datetime', 'time', 'date'], true)) {
throw MappingException::invalidDiscriminatorColumnType($this->name, $columnDef['type']);
Expand Down
10 changes: 5 additions & 5 deletions src/Mapping/DiscriminatorColumnMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function __construct(
* type: string,
* fieldName: string,
* name: string,
* length?: int,
* columnDefinition?: string,
* enumType?: class-string<BackedEnum>,
* options?: array<string, mixed>,
* length?: int|null,
* columnDefinition?: string|null,
* enumType?: class-string<BackedEnum>|null,
* options?: array<string, mixed>|null,
* } $mappingArray
*/
public static function fromMappingArray(array $mappingArray): self
Expand All @@ -54,7 +54,7 @@ public static function fromMappingArray(array $mappingArray): self
}

if (property_exists($mapping, $key)) {
$mapping->$key = $value;
$mapping->$key = $value ?? $mapping->$key;
} else {
throw new Exception('Unknown property ' . $key . ' on class ' . static::class);
}
Expand Down
8 changes: 5 additions & 3 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\ParameterType;
use Doctrine\ORM\Internal\NoUnknownNamedArguments;
use Doctrine\ORM\Internal\QueryType;
use Doctrine\ORM\Query\Expr;
Expand Down Expand Up @@ -431,12 +433,12 @@ public function getRootEntities(): array
* ->setParameter('user_id', 1);
* </code>
*
* @param string|int $key The parameter position or name.
* @param string|int|null $type ParameterType::* or \Doctrine\DBAL\Types\Type::* constant
* @param string|int $key The parameter position or name.
* @param ParameterType|ArrayParameterType|string|int|null $type ParameterType::*, ArrayParameterType::* or \Doctrine\DBAL\Types\Type::* constant
*
* @return $this
*/
public function setParameter(string|int $key, mixed $value, string|int|null $type = null): static
public function setParameter(string|int $key, mixed $value, ParameterType|ArrayParameterType|string|int|null $type = null): static
{
$existingParameter = $this->getParameter($key);

Expand Down
124 changes: 124 additions & 0 deletions tests/Tests/ORM/Functional/QueryParameterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Types;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

#[Group('GH-11278')]
final class QueryParameterTest extends OrmFunctionalTestCase
{
private int $userId;

protected function setUp(): void
{
$this->useModelSet('cms');

parent::setUp();

$user = new CmsUser();
$user->name = 'John Doe';
$user->username = 'john';
$user2 = new CmsUser();
$user2->name = 'Jane Doe';
$user2->username = 'jane';
$user3 = new CmsUser();
$user3->name = 'Just Bill';
$user3->username = 'bill';

$this->_em->persist($user);
$this->_em->persist($user2);
$this->_em->persist($user3);
$this->_em->flush();

$this->userId = $user->id;

$this->_em->clear();
}

public function testParameterTypeInBuilder(): void
{
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.id = :id')
->setParameter('id', $this->userId, ParameterType::INTEGER)
->getQuery()
->getArrayResult();

self::assertSame([['name' => 'John Doe']], $result);
}

public function testParameterTypeInQuery(): void
{
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.id = :id')
->getQuery()
->setParameter('id', $this->userId, ParameterType::INTEGER)
->getArrayResult();

self::assertSame([['name' => 'John Doe']], $result);
}

public function testDbalTypeStringInBuilder(): void
{
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.id = :id')
->setParameter('id', $this->userId, Types::INTEGER)
->getQuery()
->getArrayResult();

self::assertSame([['name' => 'John Doe']], $result);
}

public function testDbalTypeStringInQuery(): void
{
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.id = :id')
->getQuery()
->setParameter('id', $this->userId, Types::INTEGER)
->getArrayResult();

self::assertSame([['name' => 'John Doe']], $result);
}

public function testArrayParameterTypeInBuilder(): void
{
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.username IN (:usernames)')
->orderBy('u.username')
->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING)
->getQuery()
->getArrayResult();

self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result);
}

public function testArrayParameterTypeInQuery(): void
{
$result = $this->_em->createQueryBuilder()
->from(CmsUser::class, 'u')
->select('u.name')
->where('u.username IN (:usernames)')
->orderBy('u.username')
->getQuery()
->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING)
->getArrayResult();

self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result);
}
}

0 comments on commit c5ef72d

Please sign in to comment.