-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #287 from rdohms/input-group-buttons
Input Group Button Support
- Loading branch information
Showing
4 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Braincrafted\Bundle\BootstrapBundle\Form\Extension; | ||
|
||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\ButtonBuilder; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
|
||
class InputGroupButtonExtension extends AbstractTypeExtension | ||
{ | ||
/** | ||
* @var ButtonBuilder | ||
*/ | ||
protected $prependedButtonBuilder; | ||
|
||
/** | ||
* @var ButtonBuilder | ||
*/ | ||
protected $appendedButtonBuilder; | ||
|
||
/** | ||
* Returns the name of the type being extended. | ||
* | ||
* @return string The name of the type being extended | ||
*/ | ||
public function getExtendedType() | ||
{ | ||
return 'text'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildView(FormView $view, FormInterface $form, array $options) | ||
{ | ||
if ($this->prependedButtonBuilder !== null) { | ||
$view->vars['input_group_button_prepend'] = $this->prependedButtonBuilder->getForm()->createView(); | ||
} | ||
|
||
if ($this->appendedButtonBuilder !== null) { | ||
$view->vars['input_group_button_append'] = $this->appendedButtonBuilder->getForm()->createView(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
if (!isset($options['attr']) || !isset($options['attr']['input_group'])) { | ||
return; | ||
} | ||
|
||
if (isset($options['attr']['input_group']['button_prepend'])) { | ||
$this->prependedButtonBuilder = $this->addButton( | ||
$builder, | ||
$options['attr']['input_group']['button_prepend'] | ||
); | ||
} | ||
|
||
|
||
if (isset($options['attr']['input_group']['button_append'])) { | ||
$this->appendedButtonBuilder = $this->addButton( | ||
$builder, | ||
$options['attr']['input_group']['button_append'] | ||
); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Adds a button | ||
* | ||
* @param FormBuilderInterface $builder | ||
* @param array $config | ||
* @return ButtonBuilder | ||
*/ | ||
protected function addButton($builder, $config) | ||
{ | ||
$options = (isset($config['options']))? $config['options'] : array(); | ||
return $builder->create($config['name'], $config['type'], $options); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
|
||
namespace Braincrafted\Bundle\BootstrapBundle\Tests\Form\Extension; | ||
|
||
use \Mockery as m; | ||
use Braincrafted\Bundle\BootstrapBundle\Form\Extension\InputGroupButtonExtension; | ||
use Symfony\Component\Form\FormView; | ||
|
||
class InputGroupButtonExtensionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var InputGroupButtonExtension | ||
*/ | ||
private $extension; | ||
|
||
/** | ||
* | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->extension = new InputGroupButtonExtension(); | ||
} | ||
|
||
public function testBuildView() | ||
{ | ||
$optionsBoth = array( | ||
'attr' => array( | ||
'input_group' => array( | ||
'button_prepend' => array('name' => 'prepend', 'type' => 'submit', 'options' => array()), | ||
'button_append' => array('name' => 'append', 'type' => 'submit', 'options' => array()) | ||
) | ||
), | ||
); | ||
|
||
$buttonPrepend = m::mock('Symfony\Component\Form\Button'); | ||
$buttonPrepend->shouldReceive('createView')->andReturn('prepend'); | ||
|
||
$buttonAppend = m::mock('Symfony\Component\Form\Button'); | ||
$buttonAppend->shouldReceive('createView')->andReturn('append'); | ||
|
||
$buttonBuilderPrepend = m::mock('Symfony\Component\Form\ButtonBuilder'); | ||
$buttonBuilderPrepend->shouldReceive('getForm')->andReturn($buttonPrepend); | ||
|
||
$buttonBuilderAppend = m::mock('Symfony\Component\Form\ButtonBuilder'); | ||
$buttonBuilderAppend->shouldReceive('getForm')->andReturn($buttonAppend); | ||
|
||
$builder = m::mock('Symfony\Component\Form\FormBuilderInterface'); | ||
$builder->shouldReceive('create')->with('prepend', m::any(), m::any())->andReturn($buttonBuilderPrepend); | ||
$builder->shouldReceive('create')->with('append', m::any(), m::any())->andReturn($buttonBuilderAppend); | ||
|
||
$this->extension->buildForm($builder, $optionsBoth); | ||
|
||
$view = new FormView(); | ||
$type = m::mock('Symfony\Component\Form\FormInterface'); | ||
|
||
$this->extension->buildView($view, $type, array()); | ||
|
||
$this->assertArrayHasKey('input_group_button_prepend', $view->vars); | ||
$this->assertArrayHasKey('input_group_button_append', $view->vars); | ||
$this->assertEquals('prepend', $view->vars['input_group_button_prepend']); | ||
$this->assertEquals('append', $view->vars['input_group_button_append']); | ||
} | ||
|
||
/** | ||
* @covers Braincrafted\Bundle\BootstrapBundle\Form\Extension\InputGroupBtnExtension::buildForm() | ||
* @dataProvider provideForform | ||
*/ | ||
public function testBuildForm($options) | ||
{ | ||
$builder = m::mock('Symfony\Component\Form\FormBuilderInterface'); | ||
|
||
if (isset($options['attr']['input_group']['button_prepend'])) { | ||
$builder->shouldReceive('create')->with('prepend', 'submit', array())->once(); | ||
} | ||
|
||
if (isset($options['attr']['input_group']['button_append'])) { | ||
$builder->shouldReceive('create')->with('append', 'submit', array())->once(); | ||
} | ||
|
||
$this->extension->buildForm($builder, $options); | ||
} | ||
|
||
public function provideForForm() | ||
{ | ||
$optionsBoth = array( | ||
'attr' => array( | ||
'input_group' => array( | ||
'button_prepend' => array('name' => 'prepend', 'type' => 'submit', 'options' => array()), | ||
'button_append' => array('name' => 'append', 'type' => 'submit', 'options' => array()) | ||
) | ||
), | ||
); | ||
|
||
$optionsOne = array( | ||
'attr' => array( | ||
'input_group' => array( | ||
'button_append' => array('name' => 'append', 'type' => 'submit', 'options' => array()) | ||
) | ||
), | ||
); | ||
|
||
$optionsNone = array( | ||
'attr' => array( | ||
'input_group' => array( | ||
) | ||
), | ||
); | ||
|
||
return array( | ||
array($optionsBoth), | ||
array($optionsOne), | ||
array($optionsNone), | ||
array(array()), | ||
); | ||
} | ||
|
||
/** | ||
* @covers Braincrafted\Bundle\BootstrapBundle\Form\Extension\InputGroupBtnExtension::getExtendedType() | ||
*/ | ||
public function testGetExtendedType() | ||
{ | ||
$this->assertEquals('text', $this->extension->getExtendedType()); | ||
} | ||
} |