mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
Added unit-tests for input->all() method.
This commit is contained in:
@@ -6,23 +6,29 @@ require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
|
||||
class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected $names = [
|
||||
'Lester',
|
||||
'Michael',
|
||||
'Franklin',
|
||||
'Trevor',
|
||||
];
|
||||
|
||||
protected $brands = [
|
||||
'Samsung',
|
||||
'Apple',
|
||||
'HP',
|
||||
'Canon',
|
||||
];
|
||||
|
||||
protected $day = 'monday';
|
||||
|
||||
public function testPost()
|
||||
{
|
||||
global $_POST;
|
||||
|
||||
$names = [
|
||||
'Lester',
|
||||
'Michael',
|
||||
'Franklin',
|
||||
'Trevor',
|
||||
];
|
||||
|
||||
$day = 'monday';
|
||||
|
||||
$_POST = [
|
||||
'names' => $names,
|
||||
'day' => $day,
|
||||
'names' => $this->names,
|
||||
'day' => $this->day,
|
||||
];
|
||||
|
||||
$router = TestRouter::router();
|
||||
@@ -31,11 +37,12 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$handler = TestRouter::request()->getInputHandler();
|
||||
|
||||
$this->assertEquals($names, $handler->value('names'));
|
||||
$this->assertEquals($names, $handler->all(['names'])['names']);
|
||||
$this->assertEquals($day, $handler->value('day'));
|
||||
$this->assertEquals($this->names, $handler->value('names'));
|
||||
$this->assertEquals($this->names, $handler->all(['names'])['names']);
|
||||
$this->assertEquals($this->day, $handler->value('day'));
|
||||
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $handler->find('day'));
|
||||
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $handler->post('day'));
|
||||
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $handler->find('day', 'post'));
|
||||
|
||||
// Check non-existing and wrong request-type
|
||||
$this->assertCount(1, $handler->all(['non-existing']));
|
||||
@@ -53,9 +60,10 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
/* @var $object \Pecee\Http\Input\InputItem */
|
||||
foreach($objects as $i => $object) {
|
||||
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $object);
|
||||
$this->assertEquals($names[$i], $object->getValue());
|
||||
$this->assertEquals($this->names[$i], $object->getValue());
|
||||
}
|
||||
|
||||
// Reset
|
||||
$_POST = [];
|
||||
}
|
||||
|
||||
@@ -63,18 +71,9 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
global $_GET;
|
||||
|
||||
$names = [
|
||||
'Lester',
|
||||
'Michael',
|
||||
'Franklin',
|
||||
'Trevor',
|
||||
];
|
||||
|
||||
$day = 'monday';
|
||||
|
||||
$_GET = [
|
||||
'names' => $names,
|
||||
'day' => $day,
|
||||
'names' => $this->names,
|
||||
'day' => $this->day,
|
||||
];
|
||||
|
||||
$router = TestRouter::router();
|
||||
@@ -83,9 +82,9 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
$handler = TestRouter::request()->getInputHandler();
|
||||
|
||||
$this->assertEquals($names, $handler->value('names'));
|
||||
$this->assertEquals($names, $handler->all(['names'])['names']);
|
||||
$this->assertEquals($day, $handler->value('day'));
|
||||
$this->assertEquals($this->names, $handler->value('names'));
|
||||
$this->assertEquals($this->names, $handler->all(['names'])['names']);
|
||||
$this->assertEquals($this->day, $handler->value('day'));
|
||||
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $handler->find('day'));
|
||||
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $handler->get('day'));
|
||||
|
||||
@@ -105,25 +104,73 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
||||
/* @var $object \Pecee\Http\Input\InputItem */
|
||||
foreach($objects as $i => $object) {
|
||||
$this->assertInstanceOf(\Pecee\Http\Input\InputItem::class, $object);
|
||||
$this->assertEquals($names[$i], $object->getValue());
|
||||
$this->assertEquals($this->names[$i], $object->getValue());
|
||||
}
|
||||
|
||||
// Reset
|
||||
$_GET = [];
|
||||
}
|
||||
|
||||
public function testFile()
|
||||
{
|
||||
// TODO: implement test-file
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
public function testFiles()
|
||||
{
|
||||
// TODO: implement test-files
|
||||
$this->assertEquals(true, true);
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
$this->assertEquals(true, true);
|
||||
global $_POST;
|
||||
global $_GET;
|
||||
|
||||
$_POST = [
|
||||
'names' => $this->names,
|
||||
'is_sad' => true,
|
||||
];
|
||||
|
||||
$_GET = [
|
||||
'brands' => $this->brands,
|
||||
'is_happy' => true,
|
||||
];
|
||||
|
||||
$router = TestRouter::router();
|
||||
$router->reset();
|
||||
$router->getRequest()->setMethod('post');
|
||||
|
||||
$handler = TestRouter::request()->getInputHandler();
|
||||
|
||||
// GET
|
||||
$brandsFound = $handler->all(['brands', 'nothing']);
|
||||
|
||||
$this->assertArrayHasKey('brands', $brandsFound);
|
||||
$this->assertArrayHasKey('nothing', $brandsFound);
|
||||
$this->assertEquals($this->brands, $brandsFound['brands']);
|
||||
$this->assertNull($brandsFound['nothing']);
|
||||
|
||||
// POST
|
||||
$namesFound = $handler->all(['names', 'nothing']);
|
||||
|
||||
$this->assertArrayHasKey('names', $namesFound);
|
||||
$this->assertArrayHasKey('nothing', $namesFound);
|
||||
$this->assertEquals($this->names, $namesFound['names']);
|
||||
$this->assertNull($namesFound['nothing']);
|
||||
|
||||
// DEFAULT VALUE
|
||||
$nonExisting = $handler->all([
|
||||
'non-existing'
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('non-existing', $nonExisting);
|
||||
$this->assertNull($nonExisting['non-existing']);
|
||||
|
||||
// Reset
|
||||
$_GET = [];
|
||||
$_POST = [];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user