Skip to content

Commit

Permalink
Fixed setting 'compound' option of sonata_type_model
Browse files Browse the repository at this point in the history
  • Loading branch information
pulzarraider committed Jan 11, 2014
1 parent e46820b commit d74b680
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Form/Type/ModelType.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,23 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'compound' => function (Options $options) {
return isset($options['multiple']) ? $options['multiple'] : false;
if (isset($options['multiple']) && $options['multiple']) {
if (isset($options['expanded']) && $options['expanded']) {
//checkboxes
return true;
}

//select tag (with multiple attribute)
return false;
}

if (isset($options['expanded']) && $options['expanded']) {
//radio buttons
return true;
}

//select tag
return false;
},

'template' => 'choice',
Expand Down
40 changes: 40 additions & 0 deletions Tests/Form/Type/ModelTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,44 @@ public function testGetDefaultOptions()
$this->assertEquals('SonataAdminBundle', $options['btn_catalogue']);
$this->assertInstanceOf('Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList', $options['choice_list']);
}

/**
* @dataProvider getCompundOptionTests
*/
public function testCompundOption($expectedCompound, $multiple, $expanded)
{
$type = new ModelType();
$modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
$optionResolver = new OptionsResolver();

$type->setDefaultOptions($optionResolver);

$options = $optionResolver->resolve(array('model_manager' => $modelManager, 'choices' => array(), 'multiple'=>$multiple, 'expanded'=>$expanded));

$this->assertEquals($expectedCompound, $options['compound']);
$this->assertEquals('choice', $options['template']);
$this->assertEquals($multiple, $options['multiple']);
$this->assertEquals($expanded, $options['expanded']);
$this->assertInstanceOf('Sonata\AdminBundle\Model\ModelManagerInterface', $options['model_manager']);
$this->assertNull($options['class']);
$this->assertNull($options['property']);
$this->assertNull($options['query']);
$this->assertEquals(0, count($options['choices']));
$this->assertEquals(0, count($options['preferred_choices']));
$this->assertEquals('link_add', $options['btn_add']);
$this->assertEquals('link_list', $options['btn_list']);
$this->assertEquals('link_delete', $options['btn_delete']);
$this->assertEquals('SonataAdminBundle', $options['btn_catalogue']);
$this->assertInstanceOf('Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList', $options['choice_list']);
}

public function getCompundOptionTests()
{
return array(
array(true, true, true), //checkboxes
array(false, true, false), //select tag (with multiple attribute)
array(true, false, true), //radio buttons
array(false, false, false), //select tag
);
}
}

0 comments on commit d74b680

Please sign in to comment.