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 #72 from JesseLeite/fix-checkbox-binding-precedence
Browse files Browse the repository at this point in the history
Fix checkbox/radio binding precedence
  • Loading branch information
adamwathan committed Feb 16, 2016
2 parents d436cfa + 7d53670 commit 4a26473
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/AdamWathan/Form/Elements/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function setOldValue($oldValue)
$this->oldValue = $oldValue;
}

public function unsetOldValue()
{
$this->oldValue = null;
}

public function defaultToChecked()
{
if (! isset($this->checked)) {
Expand All @@ -47,12 +52,14 @@ public function defaultCheckedState($state)

public function check()
{
$this->unsetOldValue();
$this->setChecked(true);
return $this;
}

public function uncheck()
{
$this->unsetOldValue();
$this->setChecked(false);
return $this;
}
Expand Down
89 changes: 89 additions & 0 deletions tests/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,58 @@ public function testRenderRadioWithOldInput()
$this->assertEquals($expected, $result);
}

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

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

$expected = '<input type="checkbox" name="terms" value="agree">';
$result = (string)$this->form->checkbox('terms', 'agree')->uncheck();
$this->assertEquals($expected, $result);
}

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

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

$expected = '<input type="radio" name="color" value="green">';
$result = (string)$this->form->radio('color', 'green')->uncheck();
$this->assertEquals($expected, $result);
}

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

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

$expected = '<input type="checkbox" name="terms" value="agree" checked="checked">';
$result = (string)$this->form->checkbox('terms', 'agree')->check();
$this->assertEquals($expected, $result);
}

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

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

$expected = '<input type="radio" name="color" value="green" checked="checked">';
$result = (string)$this->form->radio('color', 'green')->check();
$this->assertEquals($expected, $result);
}

public function testRenderCheckboxAgainstBinaryZero()
{
$expected = '<input type="checkbox" name="boolean" value="0">';
Expand Down Expand Up @@ -521,6 +573,42 @@ public function testValueTakesPrecedenceOverBinding()
$this->assertEquals($expected, $result);
}

public function testExplicitUncheckOnCheckboxTakesPrecedenceOverBinding()
{
$object = $this->getStubObject();
$this->form->bind($object);
$expected = '<input type="radio" name="terms" value="agree">';
$result = (string)$this->form->radio('terms', 'agree')->uncheck();
$this->assertEquals($expected, $result);
}

public function testExplicitUncheckOnRadioTakesPrecedenceOverBinding()
{
$object = $this->getStubObject();
$this->form->bind($object);
$expected = '<input type="radio" name="color" value="green">';
$result = (string)$this->form->radio('color', 'green')->uncheck();
$this->assertEquals($expected, $result);
}

public function testExplicitCheckOnCheckboxTakesPrecedenceOverBinding()
{
$object = $this->getStubObject();
$this->form->bind($object);
$expected = '<input type="radio" name="terms" value="agree" checked="checked">';
$result = (string)$this->form->radio('terms', 'agree')->check();
$this->assertEquals($expected, $result);
}

public function testExplicitCheckOnRadioTakesPrecedenceOverBinding()
{
$object = $this->getStubObject();
$this->form->bind($object);
$expected = '<input type="radio" name="color" value="green" checked="checked">';
$result = (string)$this->form->radio('color', 'green')->check();
$this->assertEquals($expected, $result);
}

public function testBindUnsetProperty()
{
$object = $this->getStubObject();
Expand Down Expand Up @@ -591,6 +679,7 @@ private function getStubObject()
$obj->date_of_birth = new \DateTime('1985-05-06');
$obj->gender = 'male';
$obj->terms = 'agree';
$obj->color = 'green';
$obj->number = '0';
return $obj;
}
Expand Down

0 comments on commit 4a26473

Please sign in to comment.