Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
</testsuite>
</testsuites>

<logging>
<log type="coverage-text" target="php://stdout"/>
</logging>

<filter>
<whitelist>
<directory suffix=".php">./src</directory>
Expand Down
20 changes: 19 additions & 1 deletion src/TreeHouse/Queue/Message/Serializer/DoctrineSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,26 @@ public function __construct(ManagerRegistry $doctrine)
*/
public function serialize($value)
{
return json_encode($this->getIdentifierValues($value));
}

/**
* @param $value
*
* @return array
*/
protected function getIdentifierValues($value)
{
// if a raw identifier is passed, return it in an array.
// this would be the same if we passed in an object with that id
if (is_numeric($value)) {
return $value;
return [$value];
}

$class = get_class($value);
$metadata = $this->doctrine->getManager()->getClassMetadata($class);
$id = $metadata->getIdentifierValues($value);

return array_values($id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace TreeHouse\Queue\Tests\Message\Serializer;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Tests\Common\Persistence\ObjectManagerDecoratorTest;
use TreeHouse\Queue\Message\Serializer\DoctrineSerializer;

class DoctrineSerializerTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -31,16 +34,42 @@ public function testSerialize($value, $expected)
public function getTestData()
{
return [
[2, 2], // assume integers are identifiers
// TODO test with actual entity
[2, "[2]"], // assume integers are identifiers
[new EntityMock(1234), "[1234]"]
];
}

protected function setUp()
{
$this->doctrine = $this
->getMockBuilder(ManagerRegistry::class)
->getMock()
$meta = $this->getMockBuilder(ClassMetadata::class)->getMock();
$meta->expects($this->any())->method('getIdentifierValues')->will($this->returnCallback(function ($value) {
/** @var EntityMock $value */
return ['id' => $value->getId()];
}));

$manager = $this->getMockBuilder(ObjectManager::class)->getMock();
$manager->expects($this->any())->method('getClassMetadata')->will($this->returnValue($meta));

$this->doctrine = $this->getMockBuilder(ManagerRegistry::class)->getMock();
$this->doctrine
->expects($this->any())
->method('getManager')
->will($this->returnValue($manager))
;
}
}

class EntityMock
{
private $id;

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

public function getId()
{
return $this->id;
}
}