Sfoglia il codice sorgente

Add missing EntityToIDTransformer files

Benjamin Eberlei 14 anni fa
parent
commit
1fab031d4d

+ 62 - 0
src/Symfony/Bundle/DoctrineBundle/Form/ValueTransformer/EntityToIDTransformer.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace Symfony\Bundle\DoctrineBundle\Form\ValueTransformer;
+
+use Symfony\Component\Form\ValueTransformer\BaseValueTransformer;
+use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * Transforms a Doctrine Entity into its identifier value and back.
+ *
+ * This only works with single-field primary key fields.
+ *
+ * @author Benjamin Eberlei <kontakt@beberlei.de>
+ */
+class EntityToIDTransformer extends BaseValueTransformer
+{
+    protected function configure()
+    {
+        $this->addRequiredOption('em');
+        $this->addRequiredOption('className');
+    }
+
+    /**
+     * Reverse Transforming the selected id value to an Doctrine Entity.
+     *
+     * This handles NULL, the EntityManager#find method returns null if no entity was found.
+     * 
+     * @param  int|string $newId
+     * @param  object $oldEntity
+     * @return object
+     */
+    public function reverseTransform($newId, $oldEntity)
+    {
+        if (empty($newId)) {
+            return null;
+        }
+
+        return $this->getOption('em')->find($this->getOption('className'), $newId);
+    }
+
+    /**
+     * @param  object $entity
+     * @return int|string
+     */
+    public function transform($entity)
+    {
+        if (empty($entity)) {
+            return 0;
+        }
+
+        return current( $this->getOption('em')->getUnitOfWork()->getEntityIdentifier($entity) );
+    }
+}

+ 91 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/Form/ValueTransformer/EntityToIDTransformerTest.php

@@ -0,0 +1,91 @@
+<?php
+
+namespace Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer;
+
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\ORM\Tools\SchemaTool;
+use Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\EntityToIDTransformer;
+
+class EntityToIDTransformerTest extends \Symfony\Bundle\DoctrineBundle\Tests\TestCase
+{
+    /**
+     * @var EntityManager
+     */
+    private $em;
+
+    public function setUp()
+    {
+        parent::setUp();
+        $this->em = $this->createTestEntityManager();
+
+        $schemaTool = new SchemaTool($this->em);
+        $classes = array($this->em->getClassMetadata('Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag'));
+        try {
+            $schemaTool->dropSchema($classes);
+        } catch(\Exception $e) {
+
+        }
+        try {
+            $schemaTool->createSchema($classes);
+        } catch(\Exception $e) {
+            
+        }
+    }
+
+    public function testRequiredEntityManager()
+    {
+        $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
+        $transformer = new EntityToIDTransformer(array('className' => 'Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag'));
+    }
+
+    public function testRequiredClassName()
+    {
+        $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
+        $transformer = new EntityToIDTransformer(array('em' => $this->em));
+    }
+
+    public function createTransformer()
+    {
+        $transformer = new EntityToIDTransformer(array(
+            'em' => $this->em,
+            'className' => 'Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag'
+        ));
+        return $transformer;
+    }
+
+    public function testTranformEmptyValueReturnsNull()
+    {
+        $transformer = $this->createTransformer();
+        $this->assertEquals(0, $transformer->transform(null));
+        $this->assertEquals(0, $transformer->transform(""));
+        $this->assertEquals(0, $transformer->transform(0));
+    }
+
+    public function testTransform()
+    {
+        $transformer = $this->createTransformer();
+
+        $tag = new Tag("name");
+        $this->em->persist($tag);
+        $this->em->flush();
+
+        $this->assertEquals(1, $transformer->transform($tag));
+    }
+
+    public function testReverseTransformEmptyValue()
+    {
+        $transformer = $this->createTransformer();
+        $this->assertNull($transformer->reverseTransform(0, null));
+    }
+
+    public function testReverseTransform()
+    {
+        $transformer = $this->createTransformer();
+
+        $tag = new Tag("name");
+        $this->em->persist($tag);
+        $this->em->flush();
+
+        $this->assertSame($tag, $transformer->reverseTransform(1, null));
+    }
+}