Created
May 9, 2012 11:25
-
-
Save frodosghost/2643858 to your computer and use it in GitHub Desktop.
Unit Testing Symfony2 Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Manhattan\Bundle\GalleryBundle\Tests\Form; | |
use Symfony\Component\Form\FormFactory; | |
use Symfony\Component\Form\Extension\Core\CoreExtension; | |
use Manhattan\Bundle\GalleryBundle\Form\EventListener\EditFormSubscriber; | |
class EditFormSubscriberTest extends \PHPUnit_Framework_TestCase | |
{ | |
private $factory = null; | |
protected function setUp() | |
{ | |
$ext = new CoreExtension(); | |
$this->factory = new FormFactory(array( | |
$ext | |
)); | |
} | |
protected function tearDown() | |
{ | |
$this->factory = null; | |
} | |
public function testGetSubscribedEvents() | |
{ | |
$form_subscriber = new EditFormSubscriber($this->factory); | |
$subscribed_events = $form_subscriber->getSubscribedEvents(); | |
$this->assertTrue(array_key_exists('form.pre_set_data', $subscribed_events), | |
'->getSubscribedEvents() returns "form.pre_set_data" as event to run form on.'); | |
$this->assertEquals('preSetData', $subscribed_events['form.pre_set_data'], | |
'->getSubscribedEvents() sets function "preSetData" to the form listener "form.pre_set_data".'); | |
} | |
public function testPreSetData() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Manhattan\Bundle\GalleryBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilder; | |
use Manhattan\Bundle\GalleryBundle\Form\EventListener\EditFormSubscriber; | |
class GalleryType extends AbstractType | |
{ | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$builder | |
->add('title') | |
->add('description', 'textarea', array( | |
'attr' => array( | |
'class' => 'tinymce', | |
'data-theme' => 'body' | |
), 'required' => false | |
)) | |
->add('meta_description', 'text', array( | |
'required' => false, | |
'label' => 'Meta Description' | |
)) | |
->add('html_title', 'text', array( | |
'required' => false, | |
'label' => 'HTML Title' | |
)) | |
)); | |
$subscriber = new EditFormSubscriber($builder->getFormFactory()); | |
$builder->addEventSubscriber($subscriber); | |
} | |
public function getName() | |
{ | |
return 'gallery'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Manhattan\Bundle\GalleryBundle\Tests\Form; | |
use Symfony\Component\Form\FormFactory; | |
use Symfony\Component\Form\Extension\Core\CoreExtension; | |
use Manhattan\Bundle\GalleryBundle\Form\GalleryType; | |
class GalleryTypeTest extends \PHPUnit_Framework_TestCase | |
{ | |
private $factory; | |
protected function setUp() | |
{ | |
$ext = new CoreExtension(); | |
$this->factory = new FormFactory(array( | |
$ext | |
)); | |
} | |
protected function tearDown() | |
{ | |
$this->factory = null; | |
} | |
public function testFormNameIsGallery() | |
{ | |
$mock_gallery = $this->getMock('Manhattan\Bundle\GalleryBundle\Entity\Gallery'); | |
$mock_gallery->expects($this->any()) | |
->method('getId') | |
->will($this->returnValue(NULL)); | |
$form = $this->factory->create(new GalleryType(), $mock_gallery); | |
$this->assertEquals('gallery', $form->getName()); | |
} | |
public function testBuildFormHasCreateFields() | |
{ | |
$mock_gallery = $this->getMock('Manhattan\Bundle\GalleryBundle\Entity\Gallery'); | |
$mock_gallery->expects($this->any()) | |
->method('getId') | |
->will($this->returnValue(NULL)); | |
$form = $this->factory->create(new GalleryType(), $mock_gallery); | |
$this->assertTrue($form->has('title'), | |
'The GalleryType has a Title field.'); | |
$this->assertTrue($form->has('description'), | |
'The GalleryType has a Description field.'); | |
$this->assertTrue($form->has('meta_description'), | |
'The GalleryType has a Meta Description field.'); | |
$this->assertTrue($form->has('html_title'), | |
'The GalleryType has a HTML Title field.'); | |
$this->assertFalse($form->has('publish_date'), | |
'The GalleryType does NOT have a Publish Date field.'); | |
} | |
public function testBuildFormHasEditFields() | |
{ | |
$mock_gallery = $this->getMock('Manhattan\Bundle\GalleryBundle\Entity\Gallery'); | |
$mock_gallery->expects($this->any()) | |
->method('getId') | |
->will($this->returnValue('1')); | |
$form = $this->factory->create(new GalleryType(), $mock_gallery); | |
$this->assertTrue($form->has('publish_date'), | |
'The GalleryType has a Publish Date field.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment