Skip to content

Commit

Permalink
add input test
Browse files Browse the repository at this point in the history
  • Loading branch information
ibelar committed May 31, 2023
1 parent fcd5c85 commit fde0e0e
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
148 changes: 148 additions & 0 deletions tests/Views/InputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);
/**
* Test Input.
*/

namespace Fohn\Ui\Tests\Views;

use Fohn\Ui\Component\Form\Control\Calendar;
use Fohn\Ui\Component\Form\Control\Checkbox;
use Fohn\Ui\Component\Form\Control\Input;
use Fohn\Ui\Component\Form\Control\Number;
use Fohn\Ui\Component\Form\Control\Radio;
use Fohn\Ui\Core\Exception;

class InputTest extends \PHPUnit\Framework\TestCase
{
protected function getInput(array $default = []): Input
{
$input = new MockInput(array_merge(['controlName' => 'inputCtrl'], $default));
$input->invokeInitRenderTree();

return $input;
}

public function testInput(): void
{
$input = $this->getInput();

$this->assertFalse($input->isReadonly());
$this->assertFalse($input->isDisabled());
$this->assertFalse($input->isRequired());

$input->readonly();
$input->disabled();
$input->required();

$this->assertTrue($input->isReadonly());
$this->assertTrue($input->isDisabled());
$this->assertTrue($input->isRequired());

$this->assertSame(['readonly' => 'true', 'disabled' => 'true', 'required' => 'true'], $input->getHtmlAttrs());
}

public function testInputAttribute(): void
{
$input = $this->getInput();

$input->appendInputHtmlAttribute('placeholder', 'add value');
$this->assertSame(['placeholder' => 'add value'], $input->getHtmlAttrs());

$input->removeInputHtmlAttribute('placeholder');
$this->assertSame([], $input->getHtmlAttrs());
}

public function testInputPlaceholder(): void
{
// test backward compatibility
$input = $this->getInput(['placeholder' => 'add value']);
$this->assertSame(['placeholder' => 'add value'], $input->getHtmlAttrs());
}

public function testHintCaption(): void
{
$input = $this->getInput();

$input->setCaption('Input');
$input->setHint('my hint');

$this->assertSame('Input', $input->getCaption());
$this->assertSame('my hint', $input->getHint());
}

public function testGetValue(): void
{
$input = $this->getInput();

$input->setWithPostValue('a');
$this->assertSame('a', $input->getValue());

$input->setValue('b');
$this->assertSame('b', $input->getInputValue());
}

public function testCalendarValue(): void
{
$dateInput = new Calendar(['controlName' => 'calendar']);

$dateInput->setWithPostValue('2023-05-22');
$date = \DateTime::createFromFormat('Y-m-d', '2023-05-22', new \DateTimeZone('UTC'));
$this->assertSame($date, $dateInput->getValue());

$dateInput->setValue(\DateTime::createFromFormat('Y-m-d', '2023-06-22', new \DateTimeZone('UTC')));
$this->assertSame('2023-06-22', $dateInput->getInputValue());

$dateTimeInput = new Calendar(['type' => 'datetime', 'format' => 'Y-m-d H:i']);
$datetime = \DateTime::createFromFormat('Y-m-d', '2023-05-22 12:05', new \DateTimeZone('UTC'));
$this->assertSame($datetime, $dateTimeInput->getValue());

$dateTimeInput->setValue(\DateTime::createFromFormat('Y-m-d H:i', '2023-06-22 12:05', new \DateTimeZone('UTC')));
$this->assertSame('2023-06-22 12:05', $dateTimeInput->getInputValue());
}

public function testGetRadioValue(): void
{
$radio = new Radio(['controlName' => 'radio']);
$radio->setItems(['a' => 'A', 'b' => 'B']);

$this->assertSame('a', $radio->getValue());

$radio->setValue('b');
$this->assertSame('b', $radio->getValue());
$this->assertSame('b', $radio->getInputValue());

$this->expectException(Exception::class);
$radio->setValue('c');
}

public function testGetNumberValue(): void
{
$int = new Number(['controlName' => 'int']);
$int->setValue(12);
$this->assertSame('12', $int->getInputValue());

$int->setWithPostValue('12');
$this->assertSame(12, $int->getValue());

$float = new Number(['controlName' => 'float', 'precision' => 2]);
$float->setValue(12.34);
$this->assertSame(12.34, $float->getInputValue());

$float->setWithPostValue('22.34');
$this->assertSame(22.34, $float->getValue());
}

public function testCheckboxValue(): void
{
$box = new Checkbox(['controlName' => 'box']);
$box->setValue(true);
$this->assertTrue($box->getValue());
$box->setValue(false);
$this->assertFalse($box->getValue());

$box->setWithPostValue('1');
$this->assertTrue($box->getValue());
}
}
18 changes: 18 additions & 0 deletions tests/Views/MockInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);
/**
* Input test purpose.
*/

namespace Fohn\Ui\Tests\Views;

use Fohn\Ui\Component\Form\Control\Input;

class MockInput extends Input
{
public function getHtmlAttrs(): array
{
return $this->inputAttrs;
}
}

0 comments on commit fde0e0e

Please sign in to comment.