Skip to content

Commit

Permalink
Merge pull request #2 from nathanjdunn/feature/number-type
Browse files Browse the repository at this point in the history
Number Element
  • Loading branch information
sdebacker authored Feb 17, 2019
2 parents e099385 + f5d2f12 commit 8eecae3
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/TypiCMS/Form/Elements/Number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace TypiCMS\Form\Elements;

class Number extends Input
{
protected $attributes = [
'type' => 'number',
];

public function placeholder($placeholder)
{
$this->setAttribute('placeholder', $placeholder);

return $this;
}

public function max($max)
{
$this->setAttribute('max', $max);

return $this;
}

public function min($min)
{
$this->setAttribute('min', $min);

return $this;
}

public function step($step)
{
$this->setAttribute('step', $step);

return $this;
}

public function defaultValue($value)
{
if (!$this->hasValue()) {
$this->setValue($value);
}

return $this;
}

protected function hasValue()
{
return isset($this->attributes['value']);
}
}
64 changes: 64 additions & 0 deletions tests/NumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use PHPUnit\Framework\TestCase;
use TypiCMS\Form\Elements\Number;

class NumberTest extends TestCase
{
use TextSubclassContractTest;

protected function newTestSubjectInstance($name)
{
return new Number($name);
}

protected function getTestSubjectType()
{
return 'number';
}

public function testDefaultValue()
{
$number = new Number('number');
$number->defaultValue(0);

$expected = '<input type="number" name="number" value="0">';
$this->assertSame($expected, $number->render());
}

public function testMinValue()
{
$number = new Number('number');
$number->min(5);

$expected = '<input type="number" name="number" min="5">';
$this->assertSame($expected, $number->render());
}

public function testMaxValue()
{
$number = new Number('number');
$number->max(10);

$expected = '<input type="number" name="number" max="10">';
$this->assertSame($expected, $number->render());
}

public function testStepValue()
{
$number = new Number('number');
$number->step(1);

$expected = '<input type="number" name="number" step="1">';
$this->assertSame($expected, $number->render());
}

public function testPlaceholderValue()
{
$number = new Number('number');
$number->placeholder('Number');

$expected = '<input type="number" name="number" placeholder="Number">';
$this->assertSame($expected, $number->render());
}
}

0 comments on commit 8eecae3

Please sign in to comment.