Skip to content

Commit

Permalink
Codeception#5685: support non-entity doctrine @id, e.g. Ramsey\Uuid\U…
Browse files Browse the repository at this point in the history
…uid on the haveInRepository debug message.
  • Loading branch information
Basster committed Sep 23, 2019
1 parent afc5260 commit 67f4ad3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -46,7 +46,8 @@
"symfony/process": ">=2.7 <5.0",
"doctrine/orm": "^2",
"doctrine/annotations": "^1",
"doctrine/data-fixtures": "^1"
"doctrine/data-fixtures": "^1",
"ramsey/uuid-doctrine": "^1.5"
},
"suggest": {
"aws/aws-sdk-php": "For using AWS Auth in REST module and Queue module",
Expand Down
23 changes: 22 additions & 1 deletion src/Codeception/Module/Doctrine2.php
Expand Up @@ -19,6 +19,7 @@
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\QueryBuilder;
use Exception;
use InvalidArgumentException;
Expand Down Expand Up @@ -1031,7 +1032,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 +1041,24 @@ 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 (MappingException $ex) {
$isEntity = false;
}
}

return $isEntity;
}
}
31 changes: 31 additions & 0 deletions tests/data/doctrine2_entities/EntityWithUuid.php
@@ -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;
}
}
22 changes: 22 additions & 0 deletions tests/unit/Codeception/Module/Doctrine2Test.php
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,11 @@ class Doctrine2Test extends Unit
*/
private $module;

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

/**
* @throws ORMException
*/
Expand Down Expand Up @@ -53,6 +61,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 +87,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 +407,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 67f4ad3

Please sign in to comment.