Skip to content

Commit 7f7c307

Browse files
authored
Fix compatibility with doctrine/collections v3 and bump phpstan to v2 (#51)
* Bump phpstan version to 2.2 * Fix support for doctrine/collections v3
1 parent 26bd3fc commit 7f7c307

5 files changed

Lines changed: 37 additions & 26 deletions

File tree

‎composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"require-dev": {
2626
"codeception/stub": "^4.1.3",
2727
"doctrine/annotations": "^2.0.1",
28-
"doctrine/data-fixtures": "^1.6",
28+
"doctrine/data-fixtures": "^1.7",
2929
"doctrine/orm": "^2.14 || ^3.0",
30-
"phpstan/phpstan": "^1.10.58",
30+
"phpstan/phpstan": "^2.2",
3131
"symfony/cache": "^5.4.35 || ^6.4.3 || ^7.0",
3232
"symfony/doctrine-bridge": "^5.4.35 || ^6.4.3 || ^7.0",
3333
"symfony/uid": "^5.4.35 || ^6.4.3 || ^7.0"

‎phpstan.neon‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ parameters:
99
- tests/data/doctrine_fixtures/TestFixture2.php
1010
- tests/_support/UnitTester.php
1111
reportUnmatchedIgnoredErrors: true
12+
treatPhpDocTypesAsCertain: false
1213
ignoreErrors:
1314
- path: tests/
1415
message: '#Property \S+ is never read, only written#'
@@ -20,4 +21,6 @@ parameters:
2021
message: '#Method \S+ has no return type specified#'
2122
- path: tests/
2223
message: '#(?:Method|Property) .+ with generic (?:interface|class) \S+ does not specify its types#'
24+
- path: tests/
25+
identifier: property.unusedType
2326
- identifier: missingType.iterableValue

‎src/Codeception/Module/Doctrine.php‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,8 @@ protected function retrieveEntityManager(): void
254254
}
255255

256256

257-
if (!($this->em instanceof EntityManagerInterface)) {
258-
throw new ModuleConfigException(
259-
__CLASS__,
260-
"Connection object is not an instance of \\Doctrine\\ORM\\EntityManagerInterface.\n"
261-
. "Use `connection_callback` or dependent framework modules to specify one"
262-
);
263-
}
264-
265257
$connection = $this->em->getConnection();
258+
// @phpstan-ignore-next-line
266259
if (method_exists($connection, 'getNativeConnection')) {
267260
$connection->getNativeConnection();
268261
} else {
@@ -732,7 +725,7 @@ public function loadFixtures($fixtures, bool $append = true): void
732725

733726
try {
734727
$fixtureInstance = new $fixture();
735-
} catch (Exception $exception) { // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/6574
728+
} catch (Exception $exception) {
736729
throw new ModuleException(
737730
__CLASS__,
738731
sprintf(
@@ -991,6 +984,7 @@ protected function _buildAssociationQuery(QueryBuilder $qb, string $assoc, strin
991984
{
992985
$data = $this->em->getClassMetadata($assoc);
993986
foreach ($params as $key => $val) {
987+
// @phpstan-ignore-next-line
994988
if ($data->associationMappings !== null && array_key_exists($key, $data->associationMappings)) {
995989
$map = $data->associationMappings[$key];
996990
if (is_array($val)) {
@@ -1005,7 +999,9 @@ protected function _buildAssociationQuery(QueryBuilder $qb, string $assoc, strin
1005999
} elseif ($val instanceof Criteria) {
10061000
$qb->addCriteria($val);
10071001
} elseif ($val instanceof Expression) {
1008-
$qb->addCriteria(Criteria::create(true)->where($val));
1002+
// Doctrine Collections 2.x support
1003+
$criteria = defined(Criteria::class . '::ASC') ? Criteria::create(true) : Criteria::create();
1004+
$qb->addCriteria($criteria->where($val));
10091005
} else {
10101006
$qb->andWhere(sprintf('%s.%s = ?%s', $alias, $key, $paramIndex));
10111007
$qb->setParameter($paramIndex, $val);

‎src/Codeception/Util/ReflectionPropertyAccessor.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private function setPropertiesForClass(?object $obj, string $class, array $data)
7272
*/
7373
public function setProperties(?object $obj, array $data): void
7474
{
75-
if (!$obj || !is_object($obj)) {
75+
if (!is_object($obj)) {
7676
throw new InvalidArgumentException('Cannot set properties for "' . gettype($obj) . '", expecting object');
7777
}
7878
$class = get_class($obj);

‎tests/unit/Codeception/Module/Doctrine2Test.php‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Codeception\Stub;
1010
use Codeception\Test\Unit;
1111
use Doctrine\Common\Collections\Criteria;
12+
use Doctrine\Common\Collections\Order;
1213
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
1314
use Doctrine\Common\DataFixtures\Loader;
1415
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
@@ -71,14 +72,7 @@ protected function _setUp()
7172
require_once $dir . "/CircularRelations/C.php";
7273
require_once $dir . '/EntityWithUlid.php';
7374

74-
$sqliteDriver = 'sqlite3';
75-
// The driver "sqlite3" is only available as-of doctrine/dbal:3.5
76-
// Use "pdo_sqlite" for older versions
77-
if (version_compare(InstalledVersions::getVersion('doctrine/dbal'), '3.5', '<')) {
78-
$sqliteDriver = 'pdo_sqlite';
79-
}
80-
81-
$connection = DriverManager::getConnection(['driver' => $sqliteDriver, 'memory' => true]);
75+
$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
8276

8377
if (version_compare(InstalledVersions::getVersion('doctrine/orm'), '3', '>=')) {
8478
$configuration = ORMSetup::createAttributeMetadataConfiguration([$dir], true);
@@ -258,17 +252,17 @@ public function testCriteria()
258252
{
259253
$this->module->haveInRepository(PlainEntity::class, ['name' => 'Test 1']);
260254
$this->module->seeInRepository(PlainEntity::class, [
261-
Criteria::create(true)->where(
255+
self::createCriteria()->where(
262256
Criteria::expr()->eq('name', 'Test 1')
263257
),
264258
]);
265259
$this->module->seeInRepository(PlainEntity::class, [
266-
Criteria::create(true)->where(
260+
self::createCriteria()->where(
267261
Criteria::expr()->contains('name', 'est')
268262
),
269263
]);
270264
$this->module->seeInRepository(PlainEntity::class, [
271-
Criteria::create(true)->where(
265+
self::createCriteria()->where(
272266
Criteria::expr()->in('name', ['Test 1'])
273267
),
274268
]);
@@ -296,14 +290,26 @@ public function testOrderBy()
296290

297291
$getName = fn($entity) => $entity->getName();
298292

293+
$collectionsVersion = InstalledVersions::getVersion('doctrine/collections');
294+
if (version_compare($collectionsVersion, '3.0', '<')) {
295+
$ascending = 'asc';
296+
$descending = 'desc';
297+
} elseif (version_compare($collectionsVersion, '3.1', '<')) {
298+
$ascending = Order::Ascending;
299+
$descending = Order::Descending;
300+
} else {
301+
$ascending = \SortDirection::Ascending;
302+
$descending = \SortDirection::Descending;
303+
}
304+
299305
$this->assertSame(
300306
[
301307
'a',
302308
'b',
303309
'c',
304310
],
305311
array_map($getName, $this->module->grabEntitiesFromRepository(PlainEntity::class, [
306-
Criteria::create(true)->orderBy(['name' => 'asc']),
312+
self::createCriteria()->orderBy(['name' => $ascending]),
307313
]))
308314
);
309315

@@ -314,7 +320,7 @@ public function testOrderBy()
314320
'a',
315321
],
316322
array_map($getName, $this->module->grabEntitiesFromRepository(PlainEntity::class, [
317-
Criteria::create(true)->orderBy(['name' => 'desc']),
323+
self::createCriteria()->orderBy(['name' => $descending]),
318324
]))
319325
);
320326
}
@@ -597,4 +603,10 @@ public function testHaveFakeRepository()
597603
$this->assertContains($e1, $entities);
598604
$this->assertContains($e2, $entities);
599605
}
606+
607+
private static function createCriteria(): Criteria
608+
{
609+
// Doctrine Collections 2.x support
610+
return defined(Criteria::class . '::ASC') ? Criteria::create(true) : Criteria::create();
611+
}
600612
}

0 commit comments

Comments
 (0)