Skip to content

Commit 1fab031

Browse files
beberleifabpot
authored andcommitted
Add missing EntityToIDTransformer files
1 parent f667b69 commit 1fab031

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\DoctrineBundle\Form\ValueTransformer;
4+
5+
use Symfony\Component\Form\ValueTransformer\BaseValueTransformer;
6+
use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
7+
8+
/*
9+
* This file is part of the Symfony framework.
10+
*
11+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
12+
*
13+
* This source file is subject to the MIT license that is bundled
14+
* with this source code in the file LICENSE.
15+
*/
16+
17+
/**
18+
* Transforms a Doctrine Entity into its identifier value and back.
19+
*
20+
* This only works with single-field primary key fields.
21+
*
22+
* @author Benjamin Eberlei <kontakt@beberlei.de>
23+
*/
24+
class EntityToIDTransformer extends BaseValueTransformer
25+
{
26+
protected function configure()
27+
{
28+
$this->addRequiredOption('em');
29+
$this->addRequiredOption('className');
30+
}
31+
32+
/**
33+
* Reverse Transforming the selected id value to an Doctrine Entity.
34+
*
35+
* This handles NULL, the EntityManager#find method returns null if no entity was found.
36+
*
37+
* @param int|string $newId
38+
* @param object $oldEntity
39+
* @return object
40+
*/
41+
public function reverseTransform($newId, $oldEntity)
42+
{
43+
if (empty($newId)) {
44+
return null;
45+
}
46+
47+
return $this->getOption('em')->find($this->getOption('className'), $newId);
48+
}
49+
50+
/**
51+
* @param object $entity
52+
* @return int|string
53+
*/
54+
public function transform($entity)
55+
{
56+
if (empty($entity)) {
57+
return 0;
58+
}
59+
60+
return current( $this->getOption('em')->getUnitOfWork()->getEntityIdentifier($entity) );
61+
}
62+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\ORM\Tools\SchemaTool;
7+
use Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\EntityToIDTransformer;
8+
9+
class EntityToIDTransformerTest extends \Symfony\Bundle\DoctrineBundle\Tests\TestCase
10+
{
11+
/**
12+
* @var EntityManager
13+
*/
14+
private $em;
15+
16+
public function setUp()
17+
{
18+
parent::setUp();
19+
$this->em = $this->createTestEntityManager();
20+
21+
$schemaTool = new SchemaTool($this->em);
22+
$classes = array($this->em->getClassMetadata('Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag'));
23+
try {
24+
$schemaTool->dropSchema($classes);
25+
} catch(\Exception $e) {
26+
27+
}
28+
try {
29+
$schemaTool->createSchema($classes);
30+
} catch(\Exception $e) {
31+
32+
}
33+
}
34+
35+
public function testRequiredEntityManager()
36+
{
37+
$this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
38+
$transformer = new EntityToIDTransformer(array('className' => 'Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag'));
39+
}
40+
41+
public function testRequiredClassName()
42+
{
43+
$this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
44+
$transformer = new EntityToIDTransformer(array('em' => $this->em));
45+
}
46+
47+
public function createTransformer()
48+
{
49+
$transformer = new EntityToIDTransformer(array(
50+
'em' => $this->em,
51+
'className' => 'Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag'
52+
));
53+
return $transformer;
54+
}
55+
56+
public function testTranformEmptyValueReturnsNull()
57+
{
58+
$transformer = $this->createTransformer();
59+
$this->assertEquals(0, $transformer->transform(null));
60+
$this->assertEquals(0, $transformer->transform(""));
61+
$this->assertEquals(0, $transformer->transform(0));
62+
}
63+
64+
public function testTransform()
65+
{
66+
$transformer = $this->createTransformer();
67+
68+
$tag = new Tag("name");
69+
$this->em->persist($tag);
70+
$this->em->flush();
71+
72+
$this->assertEquals(1, $transformer->transform($tag));
73+
}
74+
75+
public function testReverseTransformEmptyValue()
76+
{
77+
$transformer = $this->createTransformer();
78+
$this->assertNull($transformer->reverseTransform(0, null));
79+
}
80+
81+
public function testReverseTransform()
82+
{
83+
$transformer = $this->createTransformer();
84+
85+
$tag = new Tag("name");
86+
$this->em->persist($tag);
87+
$this->em->flush();
88+
89+
$this->assertSame($tag, $transformer->reverseTransform(1, null));
90+
}
91+
}

0 commit comments

Comments
 (0)