Skip to content

Commit

Permalink
Fix tests for ORM 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 9, 2024
1 parent 35cfb92 commit e56223f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
14 changes: 10 additions & 4 deletions tests/DoctrineIntegration/ORM/EntityManagerTypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ class EntityManagerTypeInferenceTest extends TypeInferenceTestCase
*/
public function dataFileAsserts(): iterable
{
$ormVersion = InstalledVersions::getVersion('doctrine/orm');
$hasOrm2 = $ormVersion !== null && strpos($ormVersion, '2.') === 0;
if ($hasOrm2) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/entityManager-orm2.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/entityManagerMergeReturn.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/entityManagerDynamicReturn.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/entityManagerMergeReturn.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/customRepositoryUsage.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/queryBuilder.php');

$version = InstalledVersions::getVersion('doctrine/dbal');
$hasDbal3 = $version !== null && strpos($version, '3.') === 0;
$hasDbal4 = $version !== null && strpos($version, '4.') === 0;
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
$hasDbal3 = $dbalVersion !== null && strpos($dbalVersion, '3.') === 0;
$hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0;

if ($hasDbal4) {
// nothing to test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ class EntityManagerWithoutObjectManagerLoaderTypeInferenceTest extends TypeInfer
*/
public function dataFileAsserts(): iterable
{
$ormVersion = InstalledVersions::getVersion('doctrine/orm');
$hasOrm2 = $ormVersion !== null && strpos($ormVersion, '2.') === 0;
if ($hasOrm2) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/entityManager-orm2.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/entityManagerMergeReturn.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/entityManagerDynamicReturn.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/entityManagerMergeReturn.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/customRepositoryUsage.php');

$version = InstalledVersions::getVersion('doctrine/dbal');
Expand Down
54 changes: 54 additions & 0 deletions tests/DoctrineIntegration/ORM/data/entityManager-orm2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace PHPStan\DoctrineIntegration\ORM\EntityManagerOrm2;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping as ORM;
use RuntimeException;
use function PHPStan\Testing\assertType;

class Example
{
/**
* @var EntityManagerInterface
*/
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

public function getPartialReferenceDynamicType(): void
{
$test = $this->entityManager->getPartialReference(MyEntity::class, 1);

if ($test === null) {
throw new RuntimeException('Sorry, but no...');
}

assertType(MyEntity::class, $test);

$test->doSomething();
$test->doSomethingElse();
}
}

/**
* @ORM\Entity()
*/
class MyEntity
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @var int
*/
private $id;

public function doSomething(): void
{
}
}
14 changes: 0 additions & 14 deletions tests/DoctrineIntegration/ORM/data/entityManagerDynamicReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,6 @@ public function getReferenceDynamicType(): void
$test->doSomethingElse();
}

public function getPartialReferenceDynamicType(): void
{
$test = $this->entityManager->getPartialReference(MyEntity::class, 1);

if ($test === null) {
throw new RuntimeException('Sorry, but no...');
}

assertType(MyEntity::class, $test);

$test->doSomething();
$test->doSomethingElse();
}

/**
* @param class-string $entityName
*/
Expand Down

0 comments on commit e56223f

Please sign in to comment.