Skip to content

Commit 135d90f

Browse files
BassterNaktibalda
authored andcommitted
[Doctrine2] support non-entity doctrine @id on the haveInRepository debug message. (#5692)
* #5685: support non-entity doctrine @id, e.g. Ramsey\Uuid\Uuid on the haveInRepository debug message. * missing line indention. * check for uuid type first * catch correct MappingException. * Revert "catch correct MappingException." This reverts commit d3d4daf3 * catch both of doctrines MappingExceptions, since they don't share a common base exception. Catching \Exception here would be too vague.
1 parent da1ecef commit 135d90f

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/Codeception/Module/Doctrine2.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ private function debugEntityCreation($instance, $pks)
10311031
}
10321032

10331033
foreach ($pks as $pk) {
1034-
if (is_object($pk)) {
1034+
if ($this->isDoctrineEntity($pk)) {
10351035
$message .= get_class($pk).': '.var_export($this->extractPrimaryKey($pk), true).', ';
10361036
} else {
10371037
$message .= var_export($pk, true).', ';
@@ -1040,4 +1040,26 @@ private function debugEntityCreation($instance, $pks)
10401040

10411041
$this->debug(trim($message, ' ,'));
10421042
}
1043+
1044+
/**
1045+
* @param mixed $pk
1046+
*
1047+
* @return bool
1048+
*/
1049+
private function isDoctrineEntity($pk)
1050+
{
1051+
$isEntity = is_object($pk);
1052+
1053+
if ($isEntity) {
1054+
try {
1055+
$this->em->getClassMetadata(get_class($pk));
1056+
} catch (\Doctrine\ORM\Mapping\MappingException $ex) {
1057+
$isEntity = false;
1058+
} catch (\Doctrine\Common\Persistence\Mapping\MappingException $ex) {
1059+
$isEntity = false;
1060+
}
1061+
}
1062+
1063+
return $isEntity;
1064+
}
10431065
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Doctrine\ORM\Mapping as ORM;
4+
use Ramsey\Uuid\Uuid;
5+
use Ramsey\Uuid\UuidInterface;
6+
7+
/**
8+
* @ORM\Entity
9+
*/
10+
class EntityWithUuid
11+
{
12+
/**
13+
* @var UuidInterface
14+
*
15+
* @ORM\Id
16+
* @ORM\Column(type="uuid", unique=true)
17+
* @ORM\GeneratedValue(strategy="CUSTOM")
18+
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
19+
*/
20+
private $id;
21+
22+
public function __construct()
23+
{
24+
$this->id = Uuid::uuid4();
25+
}
26+
27+
public function getId()
28+
{
29+
return $this->id;
30+
}
31+
}

tests/unit/Codeception/Module/Doctrine2Test.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
use Codeception\Module\Doctrine2;
55
use Codeception\Test\Unit;
66
use Doctrine\Common\Collections\Criteria;
7+
use Doctrine\DBAL\Types\Type;
78
use Doctrine\ORM\EntityManager;
89
use Doctrine\ORM\ORMException;
910
use Doctrine\ORM\Tools\SchemaTool;
1011
use Doctrine\ORM\Tools\Setup;
12+
use Ramsey\Uuid\Doctrine\UuidType;
13+
use Ramsey\Uuid\UuidInterface;
1114

1215
class Doctrine2Test extends Unit
1316
{
@@ -21,6 +24,13 @@ class Doctrine2Test extends Unit
2124
*/
2225
private $module;
2326

27+
protected static function _setUpBeforeClass()
28+
{
29+
if (false === Type::hasType('uuid')) {
30+
Type::addType('uuid', UuidType::class);
31+
}
32+
}
33+
2434
/**
2535
* @throws ORMException
2636
*/
@@ -53,6 +63,8 @@ protected function _setUp()
5363
require_once $dir . "/CircularRelations/A.php";
5464
require_once $dir . "/CircularRelations/B.php";
5565
require_once $dir . "/CircularRelations/C.php";
66+
require_once $dir . '/EntityWithUuid.php';
67+
5668

5769
$this->em = EntityManager::create(
5870
['url' => 'sqlite:///:memory:'],
@@ -77,6 +89,7 @@ protected function _setUp()
7789
$this->em->getClassMetadata(\CircularRelations\A::class),
7890
$this->em->getClassMetadata(\CircularRelations\B::class),
7991
$this->em->getClassMetadata(\CircularRelations\C::class),
92+
$this->em->getClassMetadata(\EntityWithUuid::class),
8093
]);
8194

8295
$this->module = new Doctrine2(make_container(), [
@@ -396,6 +409,17 @@ public function testCompositePrimaryKeyWithEntities()
396409
$this->assertEquals([$a, $b], $pks);
397410
}
398411

412+
/**
413+
* The purpose of this test is to verify that entites with object @id, that are
414+
* not entites itself, e.g. Ramsey\Uuid\UuidInterface, don't break the debug message.
415+
*/
416+
public function testDebugEntityWithNonEntityButObjectId()
417+
{
418+
$pk = $this->module->haveInRepository(EntityWithUuid::class);
419+
420+
self::assertInstanceOf(UuidInterface::class, $pk);
421+
}
422+
399423
public function testRefresh()
400424
{
401425
// We have an entity:

0 commit comments

Comments
 (0)