123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Form;
- use Sonata\AdminBundle\Form\FormMapper;
- use Sonata\AdminBundle\Tests\Fixtures\Admin\CleanAdmin;
- use Symfony\Component\Form\FormBuilder;
- class FormMapperTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var \Sonata\AdminBundle\Builder\FormContractorInterface
- */
- protected $contractor;
- /**
- * @var \Sonata\AdminBundle\Admin\AdminInterface
- */
- protected $admin;
- /**
- * @var \Sonata\AdminBundle\Model\ModelManagerInterface
- */
- protected $modelManager;
- /**
- * @var FormMapper
- */
- protected $formMapper;
- public function setUp()
- {
- $this->contractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
- $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
- $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
- $formBuilder = new FormBuilder('test', 'stdClass', $eventDispatcher, $formFactory);
- $this->admin = new CleanAdmin('code', 'class', 'controller');
- $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- // php 5.3 BC
- $fieldDescription = $this->getFieldDescriptionMock();
- $this->modelManager->expects($this->any())
- ->method('getNewFieldDescriptionInstance')
- ->will($this->returnCallback(function($class, $name, array $options = array()) use ($fieldDescription) {
- $fieldDescriptionClone = clone $fieldDescription;
- $fieldDescriptionClone->setName($name);
- $fieldDescriptionClone->setOptions($options);
- return $fieldDescriptionClone;
- }));
- $this->admin->setModelManager($this->modelManager);
- $labelTranslatorStrategy = $this->getMock('Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface');
- $this->admin->setLabelTranslatorStrategy($labelTranslatorStrategy);
- $this->formMapper = new FormMapper(
- $this->contractor,
- $formBuilder,
- $this->admin
- );
- }
- public function testWithNoOptions()
- {
- $this->formMapper->with('foobar');
- $this->assertEquals(array('default' => array (
- 'collapsed' => false,
- 'class' => false,
- 'description' => false,
- 'translation_domain' => null,
- 'auto_created' => true,
- 'groups' => array('foobar'),
- 'tab' => true,
- 'name' => 'default',
- 'box_class' => 'box box-primary'
- )), $this->admin->getFormTabs());
- $this->assertEquals(array('foobar' => array(
- 'collapsed' => false,
- 'class' => false,
- 'description' => false,
- 'translation_domain' => null,
- 'fields' => array (),
- 'name' => 'foobar',
- 'box_class' => 'box box-primary'
- )), $this->admin->getFormGroups());
- }
- public function testWithOptions()
- {
- $this->formMapper->with('foobar', array(
- 'translation_domain' => 'Foobar',
- ));
- $this->assertEquals(array('foobar' => array(
- 'collapsed' => false,
- 'class' => false,
- 'description' => false,
- 'translation_domain' => 'Foobar',
- 'fields' => array (),
- 'name' => 'foobar',
- 'box_class' => 'box box-primary'
- )), $this->admin->getFormGroups());
- $this->assertEquals(array('default' => array (
- 'collapsed' => false,
- 'class' => false,
- 'description' => false,
- 'translation_domain' => 'Foobar',
- 'auto_created' => true,
- 'groups' => array('foobar'),
- 'tab' => true,
- 'name' => 'default',
- 'box_class' => 'box box-primary'
- )), $this->admin->getFormTabs());
- }
- public function testWithFieldsCascadeTranslationDomain()
- {
- $this->contractor->expects($this->once())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array()));
- $this->formMapper->with('foobar', array(
- 'translation_domain' => 'Foobar'
- ))
- ->add('foo', 'bar')
- ->end();
- $fieldDescription = $this->admin->getFormFieldDescription('foo');
- $this->assertEquals('foo', $fieldDescription->getName());
- $this->assertEquals('bar', $fieldDescription->getType());
- $this->assertEquals('Foobar', $fieldDescription->getTranslationDomain());
- $this->assertTrue($this->formMapper->has('foo'));
- $this->assertEquals(array('default' => array (
- 'collapsed' => false,
- 'class' => false,
- 'description' => false,
- 'translation_domain' => 'Foobar',
- 'auto_created' => true,
- 'groups' => array ('foobar'),
- 'tab' => true,
- 'name' => 'default',
- 'box_class' => 'box box-primary'
- )), $this->admin->getFormTabs());
- $this->assertEquals(array('foobar' => array(
- 'collapsed' => false,
- 'class' => false,
- 'description' => false,
- 'translation_domain' => 'Foobar',
- 'fields' => array(
- 'foo' => 'foo'
- ),
- 'name' => 'foobar',
- 'box_class' => 'box box-primary'
- )), $this->admin->getFormGroups());
- }
- public function testRemoveCascadeRemoveFieldFromFormGroup()
- {
- $this->formMapper->with('foo');
- $this->formMapper->remove('foo');
- }
- public function testIfTrueApply()
- {
- $this->contractor->expects($this->once())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array()));
- $this->formMapper
- ->ifTrue(true)
- ->add('foo', 'bar')
- ->ifEnd()
- ;
- $this->assertTrue($this->formMapper->has('foo'));
- }
- public function testIfTrueNotApply()
- {
- $this->formMapper
- ->ifTrue(false)
- ->add('foo', 'bar')
- ->ifEnd()
- ;
- $this->assertFalse($this->formMapper->has('foo'));
- }
- public function testIfTrueCombination()
- {
- $this->contractor->expects($this->once())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array()));
- $this->formMapper
- ->ifTrue(false)
- ->add('foo', 'bar')
- ->ifEnd()
- ->add('baz', 'foobaz')
- ;
- $this->assertFalse($this->formMapper->has('foo'));
- $this->assertTrue($this->formMapper->has('baz'));
- }
- public function testIfFalseApply()
- {
- $this->contractor->expects($this->once())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array()));
- $this->formMapper
- ->ifFalse(false)
- ->add('foo', 'bar')
- ->ifEnd()
- ;
- $this->assertTrue($this->formMapper->has('foo'));
- }
- public function testIfFalseNotApply()
- {
- $this->formMapper
- ->ifFalse(true)
- ->add('foo', 'bar')
- ->ifEnd()
- ;
- $this->assertFalse($this->formMapper->has('foo'));
- }
- public function testIfFalseCombination()
- {
- $this->contractor->expects($this->once())
- ->method('getDefaultOptions')
- ->will($this->returnValue(array()));
- $this->formMapper
- ->ifFalse(true)
- ->add('foo', 'bar')
- ->ifEnd()
- ->add('baz', 'foobaz')
- ;
- $this->assertFalse($this->formMapper->has('foo'));
- $this->assertTrue($this->formMapper->has('baz'));
- }
- /**
- * @expectedException RuntimeException
- * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
- */
- public function testIfTrueNested()
- {
- $this->formMapper->ifTrue(true);
- $this->formMapper->ifTrue(true);
- }
- /**
- * @expectedException RuntimeException
- * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
- */
- public function testIfFalseNested()
- {
- $this->formMapper->ifFalse(false);
- $this->formMapper->ifFalse(false);
- }
- /**
- * @expectedException RuntimeException
- * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
- */
- public function testIfCombinationNested()
- {
- $this->formMapper->ifTrue(true);
- $this->formMapper->ifFalse(false);
- }
- /**
- * @expectedException RuntimeException
- * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
- */
- public function testIfFalseCombinationNested2()
- {
- $this->formMapper->ifFalse(false);
- $this->formMapper->ifTrue(true);
- }
- /**
- * @expectedException RuntimeException
- * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
- */
- public function testIfFalseCombinationNested3()
- {
- $this->formMapper->ifFalse(true);
- $this->formMapper->ifTrue(false);
- }
- /**
- * @expectedException RuntimeException
- * @expectedExceptionMessage Cannot nest ifTrue or ifFalse call
- */
- public function testIfFalseCombinationNested4()
- {
- $this->formMapper->ifTrue(false);
- $this->formMapper->ifFalse(true);
- }
- private function getFieldDescriptionMock($name = null, $label = null, $translationDomain = null)
- {
- $fieldDescription = $this->getMockForAbstractClass('Sonata\AdminBundle\Admin\BaseFieldDescription');
- if ($name !== null) {
- $fieldDescription->setName($name);
- }
- if ($label !== null) {
- $fieldDescription->setOption('label', $label);
- }
- if ($translationDomain !== null) {
- $fieldDescription->setOption('translation_domain', $translationDomain);
- }
- return $fieldDescription;
- }
- }
|