From 87e9c19edb3af5bfb76c65bc2450451e864291e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 22 Mar 2021 14:49:33 +0100 Subject: [PATCH] Added unit-tests for input->all() method. --- tests/Pecee/SimpleRouter/InputHandlerTest.php | 109 +++++++++++++----- 1 file changed, 78 insertions(+), 31 deletions(-) diff --git a/tests/Pecee/SimpleRouter/InputHandlerTest.php b/tests/Pecee/SimpleRouter/InputHandlerTest.php index 9732c19..7fcb87b 100644 --- a/tests/Pecee/SimpleRouter/InputHandlerTest.php +++ b/tests/Pecee/SimpleRouter/InputHandlerTest.php @@ -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 = []; } } \ No newline at end of file