Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Merge pull request #56 from Propaganistas/master
Browse files Browse the repository at this point in the history
Fix for default value hydration of InputGroups
  • Loading branch information
adamwathan committed Jun 9, 2015
2 parents c4190c4 + c7df41c commit 469464a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/AdamWathan/BootForms/BasicFormBuilder.php
Expand Up @@ -173,7 +173,9 @@ public function file($label, $name, $value = null)
public function inputGroup($label, $name, $value = null)
{
$control = new InputGroup($name);
$control->value($value);
if (!is_null($value) || !is_null($value = $this->getValueFor($name))) {
$control->value($value);
}

return $this->formGroup($label, $name, $control);
}
Expand Down
59 changes: 59 additions & 0 deletions tests/BasicFormBuilderTest.php
Expand Up @@ -499,6 +499,65 @@ public function testRenderInputGroupChangeTypeWithBothAddon()
$this->assertEquals($expected, $result);
}

public function testRenderInputGroupWithValue()
{
$expected = '<div class="form-group"><label class="control-label" for="test">Test</label><div class="input-group"><input type="text" name="test" id="test" class="form-control" value="abc"></div></div>';
$result = $this->form->inputGroup('Test', 'test')->value('abc')->render();
$this->assertEquals($expected, $result);
}

public function testRenderInputGroupWithOldInput()
{
$oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface');
$oldInput->shouldReceive('hasOldInput')->andReturn(true);
$oldInput->shouldReceive('getOldInput')->andReturn('xyz');

$this->builder->setOldInputProvider($oldInput);

$expected = '<div class="form-group"><label class="control-label" for="test">Test</label><div class="input-group"><input type="text" name="test" value="xyz" id="test" class="form-control"></div></div>';
$result = $this->form->inputGroup('Test', 'test')->render();
$this->assertEquals($expected, $result);
}

public function testRenderInputGroupWithOldInputAndDefaultValue()
{
$oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface');
$oldInput->shouldReceive('hasOldInput')->andReturn(true);
$oldInput->shouldReceive('getOldInput')->andReturn('xyz');

$this->builder->setOldInputProvider($oldInput);

$expected = '<div class="form-group"><label class="control-label" for="test">Test</label><div class="input-group"><input type="text" name="test" value="xyz" id="test" class="form-control"></div></div>';
$result = $this->form->inputGroup('Test', 'test')->defaultValue('acb')->render();
$this->assertEquals($expected, $result);
}

public function testRenderInputGroupWithDefaultValue()
{
$expected = '<div class="form-group"><label class="control-label" for="test">Test</label><div class="input-group"><input type="text" name="test" id="test" class="form-control" value="acb"></div></div>';
$result = $this->form->inputGroup('Test', 'test')->defaultValue('acb')->render();
$this->assertEquals($expected, $result);
}

public function testRenderInputGroupWithOldInputAndError()
{
$oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface');
$oldInput->shouldReceive('hasOldInput')->andReturn(true);
$oldInput->shouldReceive('getOldInput')->andReturn('abc');

$this->builder->setOldInputProvider($oldInput);

$errorStore = Mockery::mock('AdamWathan\Form\ErrorStore\ErrorStoreInterface');
$errorStore->shouldReceive('hasError')->andReturn(true);
$errorStore->shouldReceive('getError')->andReturn('Test is required.');

$this->builder->setErrorStore($errorStore);

$expected = '<div class="form-group has-error"><label class="control-label" for="test">Test</label><div class="input-group"><input type="text" name="test" value="abc" id="test" class="form-control"></div><p class="help-block">Test is required.</p></div>';
$result = $this->form->inputGroup('Test', 'test')->render();
$this->assertEquals($expected, $result);
}

private function getStubObject()
{
$obj = new stdClass;
Expand Down
35 changes: 34 additions & 1 deletion tests/InputGroupTest.php
Expand Up @@ -35,4 +35,37 @@ public function testCanRenderAfterAddonAndType()
$result = $input->render();
$this->assertEquals($expected, $result);
}
}

public function testCanRenderWithValue()
{
$input = new InputGroup('test');
$input = $input->value('abc');
$expected = '<div class="input-group"><input type="text" name="test" value="abc"></div>';
$result = $input->render();
$this->assertEquals($expected, $result);

$input = new InputGroup('test');
$input = $input->value(null);
$expected = '<div class="input-group"><input type="text" name="test"></div>';
$result = $input->render();
$this->assertEquals($expected, $result);
}

public function testDefaultValue()
{
$input = new InputGroup('test');
$expected = '<div class="input-group"><input type="text" name="test" value="abc"></div>';
$result = $input->defaultValue('abc')->render();
$this->assertEquals($expected, $result);

$input = new InputGroup('test');
$expected = '<div class="input-group"><input type="text" name="test" value="xyz"></div>';
$result = $input->value('xyz')->defaultValue('abc')->render();
$this->assertEquals($expected, $result);

$input = new InputGroup('test');
$expected = '<div class="input-group"><input type="text" name="test" value="xyz"></div>';
$result = $input->defaultValue('abc')->value('xyz')->render();
$this->assertEquals($expected, $result);
}
}

0 comments on commit 469464a

Please sign in to comment.