|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Doctrine\Tests\Form\EventListener; |
| 13 | + |
| 14 | +use Doctrine\Common\Collections\ArrayCollection; |
| 15 | +use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener; |
| 16 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 17 | +use Symfony\Component\Form\FormBuilder; |
| 18 | +use Symfony\Component\Form\FormEvent; |
| 19 | +use Symfony\Component\Form\FormEvents; |
| 20 | + |
| 21 | +class MergeDoctrineCollectionTest extends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | + /** @var \Doctrine\Common\Collections\ArrayCollection */ |
| 24 | + private $collection; |
| 25 | + /** @var \Symfony\Component\EventDispatcher\EventDispatcher */ |
| 26 | + private $dispatcher; |
| 27 | + private $factory; |
| 28 | + private $form; |
| 29 | + |
| 30 | + protected function setUp() |
| 31 | + { |
| 32 | + $this->collection = new ArrayCollection(array('test')); |
| 33 | + $this->dispatcher = new EventDispatcher(); |
| 34 | + $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface'); |
| 35 | + $this->form = $this->getBuilder() |
| 36 | + ->getForm(); |
| 37 | + } |
| 38 | + |
| 39 | + protected function tearDown() |
| 40 | + { |
| 41 | + $this->collection = null; |
| 42 | + $this->dispatcher = null; |
| 43 | + $this->factory = null; |
| 44 | + $this->form = null; |
| 45 | + } |
| 46 | + |
| 47 | + protected function getBuilder($name = 'name') |
| 48 | + { |
| 49 | + return new FormBuilder($name, null, $this->dispatcher, $this->factory); |
| 50 | + } |
| 51 | + |
| 52 | + protected function getForm($name = 'name') |
| 53 | + { |
| 54 | + return $this->getBuilder($name) |
| 55 | + ->setData($this->collection) |
| 56 | + ->addEventSubscriber(new MergeDoctrineCollectionListener()) |
| 57 | + ->getForm(); |
| 58 | + } |
| 59 | + |
| 60 | + public function testOnSubmitDoNothing() |
| 61 | + { |
| 62 | + $submittedData = array('test'); |
| 63 | + $event = new FormEvent($this->getForm(), $submittedData); |
| 64 | + |
| 65 | + $this->dispatcher->dispatch(FormEvents::SUBMIT, $event); |
| 66 | + |
| 67 | + $this->assertTrue($this->collection->contains('test')); |
| 68 | + $this->assertSame(1, $this->collection->count()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testOnSubmitNullClearCollection() |
| 72 | + { |
| 73 | + $submittedData = array(); |
| 74 | + $event = new FormEvent($this->getForm(), $submittedData); |
| 75 | + |
| 76 | + $this->dispatcher->dispatch(FormEvents::SUBMIT, $event); |
| 77 | + |
| 78 | + $this->assertTrue($this->collection->isEmpty()); |
| 79 | + } |
| 80 | +} |
0 commit comments