From 5d05a138672b25cfded2684bb7b9799f89ac2745 Mon Sep 17 00:00:00 2001 From: tom Date: Tue, 19 Feb 2013 20:39:47 +0000 Subject: [PATCH 1/2] Made the Annotation builder automatically pass the EntityManager to Form Elements which need it. --- .../Form/Annotation/AnnotationBuilder.php | 2 +- .../Annotation/ElementAnnotationsListener.php | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/DoctrineORMModule/Form/Annotation/AnnotationBuilder.php b/src/DoctrineORMModule/Form/Annotation/AnnotationBuilder.php index 1d7463e1..5dc914d4 100644 --- a/src/DoctrineORMModule/Form/Annotation/AnnotationBuilder.php +++ b/src/DoctrineORMModule/Form/Annotation/AnnotationBuilder.php @@ -54,7 +54,7 @@ public function setEventManager(EventManagerInterface $events) { parent::setEventManager($events); - $this->getEventManager()->attach(new ElementAnnotationsListener); + $this->getEventManager()->attach(new ElementAnnotationsListener($this->em)); return $this; } diff --git a/src/DoctrineORMModule/Form/Annotation/ElementAnnotationsListener.php b/src/DoctrineORMModule/Form/Annotation/ElementAnnotationsListener.php index 57ba69eb..ec0817a2 100644 --- a/src/DoctrineORMModule/Form/Annotation/ElementAnnotationsListener.php +++ b/src/DoctrineORMModule/Form/Annotation/ElementAnnotationsListener.php @@ -1,6 +1,7 @@ em = $em; + } + /** * Detach listeners * @@ -42,6 +58,7 @@ public function attach(EventManagerInterface $events) $this->listeners[] = $events->attach('configureElement', array($this, 'handleRequiredAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleTypeAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleValidatorAnnotation')); + $this->listeners[] = $events->attach('configureElement', array($this, 'handleLinkedFormElements')); $this->listeners[] = $events->attach('checkForExclude', array($this, 'handleExcludeAnnotation')); } @@ -234,4 +251,33 @@ public function handleValidatorAnnotation($e) break; } } + + /** + * Handle the form elements which require the doctrine entity manager to populate. + * + * @param \Zend\EventManager\EventInterface $e + * @return void + */ + public function handleLinkedFormElements($e) + { + $annotation = $e->getParam('annotation'); + if (!$annotation instanceof \Zend\Form\Annotation\Type) { + return; + } + + if (!in_array( + $annotation->getType(), + array( + 'DoctrineORMModule\Form\Element\EntityMultiCheckbox', + 'DoctrineORMModule\Form\Element\EntityRadio', + 'DoctrineORMModule\Form\Element\EntitySelect', + ) + )) { + return; + } + + $elementSpec = $e->getParam('elementSpec'); + + $elementSpec['spec']['options']['object_manager'] = $this->em; + } } From 50cde3b1d5ba9f5f29213eeb3d438b8e9d95478d Mon Sep 17 00:00:00 2001 From: tom Date: Tue, 19 Feb 2013 21:29:44 +0000 Subject: [PATCH 2/2] Added test for handleLinkedFormElements() --- .../Form/ElementAnnotationsListenerTest.php | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/tests/DoctrineORMModuleTest/Form/ElementAnnotationsListenerTest.php b/tests/DoctrineORMModuleTest/Form/ElementAnnotationsListenerTest.php index a42505d3..efd02bc9 100644 --- a/tests/DoctrineORMModuleTest/Form/ElementAnnotationsListenerTest.php +++ b/tests/DoctrineORMModuleTest/Form/ElementAnnotationsListenerTest.php @@ -9,13 +9,25 @@ */ class ElementAnnotationsListenerTest extends PHPUnit_Framework_TestCase { + protected $listener; + + protected $entityManager; + + protected function setUp() + { + $this->entityManager = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->listener = new ElementAnnotationsListener($this->entityManager); + } /** * @dataProvider eventProvider */ public function testHandleAnnotationType($type, $expectedType) { - $listener = new ElementAnnotationsListener(); + $listener = $this->listener; $event = new Zend\EventManager\Event(); $checkboxAnnotation = new Doctrine\ORM\Mapping\Column(); $checkboxAnnotation->type = $type; @@ -30,7 +42,7 @@ public function testHandleAnnotationType($type, $expectedType) public function testHandleAnnotationAttributesShallAppent() { - $listener = new ElementAnnotationsListener(); + $listener = $this->listener; $event = new Zend\EventManager\Event(); $annotation = new Doctrine\ORM\Mapping\Column(); @@ -56,4 +68,42 @@ public function eventProvider() array('string', 'Zend\Form\Element'), ); } + + /** + * @covers DoctrineORMModule\Form\Annotation\ElemenetAnnotationsListener::handleLinkedFormElements + * @dataProvider linkedElementTypeProvider + */ + public function testHandleLinkedFormElements($type, $requiresEntityManager) + { + $listener = $this->listener; + $event = new \Zend\EventManager\Event(); + $annotation = new \Zend\Form\Annotation\Type(array('value' => $type)); + + $event->setParam('annotation', $annotation); + $event->setParam('elementSpec', new ArrayObject(array( + 'spec' => array('options' => array()), + ))); + + $listener->handleLinkedFormElements($event); + $spec = $event->getParam('elementSpec'); + + if ($requiresEntityManager) { + $this->assertArrayHasKey('object_manager', $spec['spec']['options']); + $this->assertEquals($this->entityManager, $spec['spec']['options']['object_manager']); + return; + } + + $this->assertArrayNotHasKey('object_manager', $spec['spec']['options']); + + } + + public function linkedElementTypeProvider() + { + return array( + array('DoctrineORMModule\Form\Element\EntityMultiCheckbox', true), + array('DoctrineORMModule\Form\Element\EntityRadio', true), + array('DoctrineORMModule\Form\Element\EntitySelect', true), + array('Zend\Form\Element', false), + ); + } }