Skip to content

Commit

Permalink
[Doctrine2] support non-entity doctrine @id on the haveInRepository d…
Browse files Browse the repository at this point in the history
…ebug 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.
  • Loading branch information
Basster authored and Naktibalda committed Oct 10, 2019
1 parent da1ecef commit 135d90f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Codeception/Module/Doctrine2.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ private function debugEntityCreation($instance, $pks)
}

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

$this->debug(trim($message, ' ,'));
}

/**
* @param mixed $pk
*
* @return bool
*/
private function isDoctrineEntity($pk)
{
$isEntity = is_object($pk);

if ($isEntity) {
try {
$this->em->getClassMetadata(get_class($pk));
} catch (\Doctrine\ORM\Mapping\MappingException $ex) {
$isEntity = false;
} catch (\Doctrine\Common\Persistence\Mapping\MappingException $ex) {
$isEntity = false;
}
}

return $isEntity;
}
}
31 changes: 31 additions & 0 deletions tests/data/doctrine2_entities/EntityWithUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

/**
* @ORM\Entity
*/
class EntityWithUuid
{
/**
* @var UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
*/
private $id;

public function __construct()
{
$this->id = Uuid::uuid4();
}

public function getId()
{
return $this->id;
}
}
24 changes: 24 additions & 0 deletions tests/unit/Codeception/Module/Doctrine2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
use Codeception\Module\Doctrine2;
use Codeception\Test\Unit;
use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use Ramsey\Uuid\Doctrine\UuidType;
use Ramsey\Uuid\UuidInterface;

class Doctrine2Test extends Unit
{
Expand All @@ -21,6 +24,13 @@ class Doctrine2Test extends Unit
*/
private $module;

protected static function _setUpBeforeClass()
{
if (false === Type::hasType('uuid')) {
Type::addType('uuid', UuidType::class);
}
}

/**
* @throws ORMException
*/
Expand Down Expand Up @@ -53,6 +63,8 @@ protected function _setUp()
require_once $dir . "/CircularRelations/A.php";
require_once $dir . "/CircularRelations/B.php";
require_once $dir . "/CircularRelations/C.php";
require_once $dir . '/EntityWithUuid.php';


$this->em = EntityManager::create(
['url' => 'sqlite:///:memory:'],
Expand All @@ -77,6 +89,7 @@ protected function _setUp()
$this->em->getClassMetadata(\CircularRelations\A::class),
$this->em->getClassMetadata(\CircularRelations\B::class),
$this->em->getClassMetadata(\CircularRelations\C::class),
$this->em->getClassMetadata(\EntityWithUuid::class),
]);

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

/**
* The purpose of this test is to verify that entites with object @id, that are
* not entites itself, e.g. Ramsey\Uuid\UuidInterface, don't break the debug message.
*/
public function testDebugEntityWithNonEntityButObjectId()
{
$pk = $this->module->haveInRepository(EntityWithUuid::class);

self::assertInstanceOf(UuidInterface::class, $pk);
}

public function testRefresh()
{
// We have an entity:
Expand Down

0 comments on commit 135d90f

Please sign in to comment.