Skip to content

Commit 89428e5

Browse files
ISSUE-89: Added support for discriminated entities
1 parent c93f072 commit 89428e5

File tree

4 files changed

+132
-3
lines changed

4 files changed

+132
-3
lines changed

src/EntityToIdentifierTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function transform($value)
8888

8989
$className = ClassUtils::getClass($value);
9090

91-
if ($className !== $this->className) {
91+
if ($className !== $this->className && !is_subclass_of($className, $this->className)) {
9292
throw new TransformationFailedException(sprintf('Expected entity %s.', $this->className));
9393
}
9494

tests/EntityToIdentifierTransformerTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use DarkWebDesign\SymfonyAddon\Transformer\EntityToIdentifierTransformer;
2424
use DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\City;
25+
use DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\Employee;
2526
use DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\PointOfInterest;
2627
use PHPUnit_Framework_TestCase;
2728
use stdClass;
@@ -61,12 +62,28 @@ protected function setUp()
6162
$this->entityManager->method('getRepository')->willReturn($this->repository);
6263
$this->entityManager->method('getClassMetadata')->willReturn($this->metadata);
6364

64-
$this->metadata->method('getName')->willReturn($this->className);
65-
$this->metadata->method('getIdentifierValues')->willReturn(array('id' => $this->identifier));
65+
$this->metadata->method('getName')->willReturnCallback(array($this, 'getClassName'));
66+
$this->metadata->method('getIdentifierValues')->willReturnCallback(array($this, 'getIdentifier'));
6667

6768
$this->metadata->isIdentifierComposite = false;
6869
}
6970

71+
/**
72+
* @return string
73+
*/
74+
public function getClassName()
75+
{
76+
return $this->className;
77+
}
78+
79+
/**
80+
* @return array
81+
*/
82+
public function getIdentifier()
83+
{
84+
return array('id' => $this->identifier);
85+
}
86+
7087
/**
7188
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
7289
* @expectedExceptionMessage Expected an entity with a single identifier.
@@ -96,6 +113,19 @@ public function testTransformAlias()
96113
$this->assertSame($this->identifier, $identifier);
97114
}
98115

116+
public function testTransformDiscriminated()
117+
{
118+
$this->className = 'DarkWebDesign\SymfonyAddon\Transformer\Tests\Models\AbstractPerson';
119+
120+
$transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className);
121+
122+
$entity = new Employee();
123+
124+
$identifier = $transformer->transform($entity);
125+
126+
$this->assertSame($this->identifier, $identifier);
127+
}
128+
99129
public function testTransformNull()
100130
{
101131
$transformer = new EntityToIdentifierTransformer($this->entityManager, $this->className);

tests/Models/AbstractPerson.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace DarkWebDesign\SymfonyAddon\Transformer\Tests\Models;
4+
5+
/**
6+
* @Table(name="person")
7+
*
8+
* @Entity
9+
*
10+
* @InheritanceType("SINGLE_TABLE")
11+
*
12+
* @DiscriminatorColumn(name="type", type="string")
13+
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"}) */
14+
abstract class AbstractPerson
15+
{
16+
/**
17+
* @var int
18+
*
19+
* @Column(name="id", type="integer", nullable=false)
20+
*
21+
* @Id
22+
*
23+
* @GeneratedValue(strategy="IDENTITY")
24+
*/
25+
protected $id;
26+
27+
/**
28+
* @var string
29+
*
30+
* @Column(name="name", type="string", length=50, nullable=false)
31+
*/
32+
protected $name;
33+
34+
/**
35+
* @return int
36+
*/
37+
public function getId()
38+
{
39+
return $this->id;
40+
}
41+
42+
/**
43+
* @param int $id
44+
*/
45+
public function setId($id)
46+
{
47+
$this->id = $id;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getName()
54+
{
55+
return $this->name;
56+
}
57+
58+
/**
59+
* @param string $name
60+
*/
61+
public function setName($name)
62+
{
63+
$this->name = $name;
64+
}
65+
}

tests/Models/Employee.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace DarkWebDesign\SymfonyAddon\Transformer\Tests\Models;
4+
5+
/**
6+
* @Table(name="employee")
7+
*
8+
* @Entity
9+
*/
10+
class Employee extends AbstractPerson
11+
{
12+
/**
13+
* @var string
14+
*
15+
* @Column(name="function", type="string", length=50, nullable=false)
16+
*/
17+
private $function;
18+
19+
/**
20+
* @return string
21+
*/
22+
public function getFunction()
23+
{
24+
return $this->function;
25+
}
26+
27+
/**
28+
* @param string $function
29+
*/
30+
public function setFunction($function)
31+
{
32+
$this->function = $function;
33+
}
34+
}

0 commit comments

Comments
 (0)