Skip to content

Commit

Permalink
Merge pull request #4 from nathanjdunn/feature/number-element
Browse files Browse the repository at this point in the history
Number element
  • Loading branch information
sdebacker authored Feb 19, 2019
2 parents 7b8e78f + 4425e81 commit 9341a25
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/TypiCMS/BootForms/BasicFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ public function inputGroup($label, $name, $value = null)
return $this->formGroup($label, $name, $control);
}

public function number($label, $name, $value = null)
{
$control = $this->builder->number($name)->value($value);

return $this->formGroup($label, $name, $control);
}

public function __call($method, $parameters)
{
return call_user_func_array([$this->builder, $method], $parameters);
Expand Down
92 changes: 92 additions & 0 deletions tests/BasicFormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,98 @@ public function testModifyingDifferentElementsOfAFormGroup()
$this->assertEquals($expected, $result);
}

public function testRenderNumberGroup()
{
$expected = '<div class="form-group"><label for="number">Number</label><input type="number" name="number" id="number" class="form-control"></div>';
$result = $this->form->number('Number', 'number')->render();
$this->assertEquals($expected, $result);
}

public function testRenderNumberGroupWithValue()
{
$expected = '<div class="form-group"><label for="number">Number</label><input type="number" name="number" id="number" class="form-control" value="15"></div>';
$result = $this->form->number('Number', 'number')->value('15')->render();
$this->assertEquals($expected, $result);
}

public function testRenderNumberGroupWithError()
{
$errorStore = Mockery::mock('TypiCMS\Form\ErrorStore\ErrorStoreInterface');
$errorStore->shouldReceive('hasError')->andReturn(true);
$errorStore->shouldReceive('getError')->andReturn('Number is required.');

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

$expected = '<div class="form-group"><label for="number">Number</label><input type="number" name="number" id="number" class="form-control is-invalid"><div class="invalid-feedback">Number is required.</div></div>';
$result = $this->form->number('Number', 'number')->render();
$this->assertEquals($expected, $result);
}

public function testRenderNumberGroupWithErrorAndCustomFormText()
{
$errorStore = Mockery::mock('TypiCMS\Form\ErrorStore\ErrorStoreInterface');
$errorStore->shouldReceive('hasError')->andReturn(true);
$errorStore->shouldReceive('getError')->andReturn('Number is required.');

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

$expected = '<div class="form-group"><label for="number">Number</label><input type="number" name="number" id="number" class="form-control is-invalid"><div class="invalid-feedback">Number is required.</div><small class="form-text">some custom text</small></div>';
$result = $this->form->number('Number', 'number')->formText('some custom text')->render();
$this->assertEquals($expected, $result);
}

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

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

$expected = '<div class="form-group"><label for="number">Number</label><input type="number" name="number" value="15" id="number" class="form-control"></div>';
$result = $this->form->number('Number', 'number')->render();
$this->assertEquals($expected, $result);
}

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

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

$expected = '<div class="form-group"><label for="number">Number</label><input type="number" name="number" value="15" id="number" class="form-control"></div>';
$result = $this->form->number('Number', 'number')->defaultValue('22')->render();
$this->assertEquals($expected, $result);
}

public function testRenderNumberGroupWithDefaultValue()
{
$expected = '<div class="form-group"><label for="number">Number</label><input type="number" name="number" id="number" class="form-control" value="15"></div>';
$result = $this->form->number('Number', 'number')->defaultValue('15')->render();
$this->assertEquals($expected, $result);
}

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

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

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

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

$expected = '<div class="form-group"><label for="number">Number</label><input type="number" name="number" value="18" id="number" class="form-control is-invalid"><div class="invalid-feedback">Number is required.</div></div>';
$result = $this->form->number('Number', 'number')->render();
$this->assertEquals($expected, $result);
}

private function getStubObject()
{
$obj = new stdClass();
Expand Down

0 comments on commit 9341a25

Please sign in to comment.