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

Fix compatibility with DBAL 4.2 #11592

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
18 changes: 0 additions & 18 deletions tests/Performance/ArrayResultFactory.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Performance/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\Performance;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDO\SQLite\Driver;
Expand All @@ -17,6 +16,7 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\TestUtil;

use function array_map;
Expand Down Expand Up @@ -67,7 +67,7 @@ public static function makeEntityManagerWithNoResultsConnection(): EntityManager
/** {@inheritDoc} */
public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile|null $qcp = null): Result
{
return new Result(new ArrayResult([]), $this);
return ArrayResultFactory::createWrapperResultFromArray([], $this);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\ArrayHydrator;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Performance\ArrayResultFactory;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsUser;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
Expand Down Expand Up @@ -62,7 +62,7 @@ public function init(): void
];
}

$this->result = ArrayResultFactory::createFromArray($resultSet);
$this->result = ArrayResultFactory::createWrapperResultFromArray($resultSet);
$this->hydrator = new ArrayHydrator(EntityManagerFactory::getEntityManager([]));
$this->rsm = new ResultSetMapping();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Performance\ArrayResultFactory;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsUser;
Expand Down Expand Up @@ -49,7 +49,7 @@ public function init(): void
];
}

$this->result = ArrayResultFactory::createFromArray($resultSet);
$this->result = ArrayResultFactory::createWrapperResultFromArray($resultSet);
$this->hydrator = new ObjectHydrator(EntityManagerFactory::getEntityManager([]));
$this->rsm = new ResultSetMapping();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\ArrayHydrator;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Performance\ArrayResultFactory;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\Models\CMS\CmsUser;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;

Expand Down Expand Up @@ -53,7 +53,7 @@ public function init(): void
];
}

$this->result = ArrayResultFactory::createFromArray($resultSet);
$this->result = ArrayResultFactory::createWrapperResultFromArray($resultSet);
$this->hydrator = new ArrayHydrator(EntityManagerFactory::getEntityManager([]));
$this->rsm = new ResultSetMapping();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Performance\ArrayResultFactory;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsUser;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
Expand Down Expand Up @@ -44,7 +44,7 @@ public function init(): void
];
}

$this->result = ArrayResultFactory::createFromArray($resultSet);
$this->result = ArrayResultFactory::createWrapperResultFromArray($resultSet);
$this->hydrator = new ObjectHydrator(EntityManagerFactory::getEntityManager([]));
$this->rsm = new ResultSetMapping();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\ScalarHydrator;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Performance\ArrayResultFactory;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\Models\CMS\CmsUser;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;

Expand Down Expand Up @@ -53,7 +53,7 @@ public function init(): void
];
}

$this->result = ArrayResultFactory::createFromArray($resultSet);
$this->result = ArrayResultFactory::createWrapperResultFromArray($resultSet);
$this->hydrator = new ScalarHydrator(EntityManagerFactory::getEntityManager([]));
$this->rsm = new ResultSetMapping();

Expand Down
42 changes: 42 additions & 0 deletions tests/Tests/Mocks/ArrayResultFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Mocks;

use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDO\SQLite\Driver;
use Doctrine\DBAL\Result;
use ReflectionMethod;

use function array_keys;
use function array_map;
use function array_values;

final class ArrayResultFactory
{
/** @param list<array<string, mixed>> $resultSet */
public static function createDriverResultFromArray(array $resultSet): ArrayResult
{
if ((new ReflectionMethod(ArrayResult::class, '__construct'))->getNumberOfRequiredParameters() < 2) {
// DBAL < 4.2
return new ArrayResult($resultSet);
}

// DBAL 4.2+
return new ArrayResult(
array_keys($resultSet[0] ?? []),
array_map(array_values(...), $resultSet),
);
}

/** @param list<array<string, mixed>> $resultSet */
public static function createWrapperResultFromArray(array $resultSet, Connection|null $connection = null): Result
{
return new Result(
self::createDriverResultFromArray($resultSet),
$connection ?? new Connection([], new Driver()),
);
}
}
5 changes: 2 additions & 3 deletions tests/Tests/ORM/Functional/Ticket/GH6362Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
Expand All @@ -19,6 +17,7 @@
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

Expand Down Expand Up @@ -78,7 +77,7 @@ public function testInheritanceJoinAlias(): void
],
];

$stmt = new Result(new ArrayResult($resultSet), $this->createMock(Connection::class));
$stmt = ArrayResultFactory::createWrapperResultFromArray($resultSet, $this->createMock(Connection::class));
$hydrator = new ObjectHydrator($this->_em);
$result = $hydrator->hydrateAll($stmt, $rsm);

Expand Down
5 changes: 2 additions & 3 deletions tests/Tests/ORM/Functional/Ticket/GH9807Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\OrmFunctionalTestCase;

final class GH9807Test extends OrmFunctionalTestCase
Expand Down Expand Up @@ -63,7 +62,7 @@ public function testHydrateJoinedCollectionWithFirstNullishRow(): void
],
];

$stmt = new Result(new ArrayResult($resultSet), $this->createMock(Connection::class));
$stmt = ArrayResultFactory::createWrapperResultFromArray($resultSet, $this->createMock(Connection::class));

/** @var GH9807Main[] $result */
$result = $hydrator->hydrateAll($stmt, $rsm);
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/ORM/Hydration/HydrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Doctrine\Tests\ORM\Hydration;

use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Result;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\OrmTestCase;

class HydrationTestCase extends OrmTestCase
Expand All @@ -22,6 +22,6 @@ protected function setUp(): void

protected function createResultMock(array $resultSet): Result
{
return new Result(new ArrayResult($resultSet), $this->entityManager->getConnection());
return ArrayResultFactory::createWrapperResultFromArray($resultSet, $this->entityManager->getConnection());
}
}
5 changes: 2 additions & 3 deletions tests/Tests/ORM/Hydration/ObjectHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace Doctrine\Tests\ORM\Hydration;

use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Doctrine\ORM\Internal\Hydration\HydrationException;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Proxy\InternalProxy;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\Tests\Models\CMS\CmsComment;
Expand Down Expand Up @@ -1337,7 +1336,7 @@ public function testResultIterationWithAliasedUserEntity(): void
$hydrator = new ObjectHydrator($this->entityManager);
$rowNum = 0;
$iterableResult = $hydrator->toIterable(
new Result(new ArrayResult($resultSet), $this->createMock(Connection::class)),
ArrayResultFactory::createWrapperResultFromArray($resultSet, $this->createMock(Connection::class)),
$rsm,
);

Expand Down
12 changes: 6 additions & 6 deletions tests/Tests/ORM/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Result;
Expand All @@ -22,6 +21,7 @@
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsGroup;
Expand Down Expand Up @@ -361,10 +361,10 @@ public function testResultCacheCaching(): void
{
$entityManager = $this->createTestEntityManagerWithConnection(
$this->createConnection(
new ArrayResult([
ArrayResultFactory::createDriverResultFromArray([
['id_0' => 1],
]),
new ArrayResult([]),
ArrayResultFactory::createDriverResultFromArray([]),
),
);

Expand Down Expand Up @@ -399,14 +399,14 @@ public function testResultCacheEviction(): void
{
$entityManager = $this->createTestEntityManagerWithConnection(
$this->createConnection(
new ArrayResult([
ArrayResultFactory::createDriverResultFromArray([
['id_0' => 1],
]),
new ArrayResult([
ArrayResultFactory::createDriverResultFromArray([
['id_0' => 1],
['id_0' => 2],
]),
new ArrayResult([
ArrayResultFactory::createDriverResultFromArray([
['id_0' => 1],
]),
),
Expand Down
Loading