summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Controller/Input.php105
-rw-r--r--tests/Controller/InputTest.php224
-rw-r--r--tests/Controller/OutputTest.php3
3 files changed, 329 insertions, 3 deletions
diff --git a/src/Controller/Input.php b/src/Controller/Input.php
index 7f5ce35..a5f3904 100644
--- a/src/Controller/Input.php
+++ b/src/Controller/Input.php
@@ -9,5 +9,110 @@ use Psr\Http\Message\ServerRequestInterface;
*/
trait Input
{
+ /**
+ * Get request, set for controller
+ *
+ * @return ServerRequestInterface
+ */
+ abstract public function getRequest();
+
+ /**
+ * Get the request query parameters.
+ *
+ * <code>
+ * // Get all parameters
+ * $params = $this->getQueryParams();
+ *
+ * // Get specific parameters, specifying defaults for 'bar' and 'zoo'
+ * list($foo, $bar, $zoo) = $this->getQueryParams(['foo', 'bar' => 10, 'zoo' => 'monkey']);
+ * </code>
+ *
+ * @param array $list
+ * @return array
+ */
+ public function getQueryParams(array $list = null)
+ {
+ return isset($list)
+ ? $this->listQueryParams($list)
+ : (array)$this->getRequest()->getQueryParams();
+ }
+
+ /**
+ * Apply list to query params
+ *
+ * @param array $list
+ * @return array
+ */
+ protected function listQueryParams(array $list)
+ {
+ $result = [];
+ $params = $this->getRequest()->getQueryParams();
+
+ foreach ($list as $key => $value) {
+ if (is_int($key)) {
+ $key = $value;
+ $value = null;
+ }
+
+ $result[] = isset($params[$key]) ? $params[$key] : $value;
+ }
+
+ return $result;
+ }
+
+ /**
+ * Check if the request has a query parameter
+ *
+ * @param array $param
+ * @return boolean
+ */
+ public function hasQueryParam($param)
+ {
+ $params = $this->getQueryParams();
+
+ return isset($params[$param]);
+ }
+
+ /**
+ * Get a query parameter.
+ *
+ * Optionally apply filtering to the value.
+ * @link http://php.net/manual/en/filter.filters.php
+ *
+ * @param array $param
+ * @param string $default
+ * @param int $filter
+ * @param mixed $filterOptions
+ * @return mixed
+ */
+ public function getQueryParam($param, $default = null, $filter = null, $filterOptions = null)
+ {
+ $params = $this->getQueryParams();
+ $value = isset($params[$param]) ? $params[$param] : $default;
+
+ if (isset($filter) && isset($value)) {
+ $value = filter_var($value, $filter, $filterOptions);
+ }
+
+ return $value;
+ }
+
+
+ /**
+ * Get parsed body and uploaded files as input
+ *
+ * @return array|mixed
+ */
+ public function getInput()
+ {
+ $data = $this->getRequest()->getParsedBody();
+
+ if (is_array($data)) {
+ $files = $this->getRequest()->getUploadedFiles();
+ $data = array_replace_recursive($data, (array)$files);
+ }
+
+ return $data;
+ }
}
diff --git a/tests/Controller/InputTest.php b/tests/Controller/InputTest.php
new file mode 100644
index 0000000..a76370a
--- /dev/null
+++ b/tests/Controller/InputTest.php
@@ -0,0 +1,224 @@
+<?php
+
+namespace Jasny\Controller;
+
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Message\UploadedFileInterface;
+use Jasny\Controller\TestHelper;
+
+/**
+ * @covers Jasny\Controller\Input
+ */
+class InputTest extends \PHPUnit_Framework_TestCase
+{
+ use TestHelper;
+
+ public function queryParamsProvider()
+ {
+ return [
+ [null, null, 'wall',[]],
+ [[], null, 'wall', []],
+ [['foo' => 10], 10, 'wall', []],
+ [
+ ['foo' => 10, 'bar' => '', 'zoo' => ['monkey', 'lion'], 'qux' => ['a' => 10, 'b' => 20]],
+ 10, '', ['a' => 10, 'b' => 20]
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider queryParamsProvider
+ *
+ * @param array|null $data
+ */
+ public function testGetQueryParams($data)
+ {
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->once())->method('getQueryParams')->willReturn($data);
+
+ $controller = $this->getController(['getRequest']);
+ $controller->method('getRequest')->willReturn($request);
+
+ $params = $controller->getQueryParams();
+ $this->assertSame((array)$data, $params);
+ }
+
+ /**
+ * @dataProvider queryParamsProvider
+ *
+ * @param array|null $data
+ * @param int $foo
+ * @param string $bar
+ * @param array $qux
+ */
+ public function testListQueryParams($data, $foo, $bar, $qux)
+ {
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->once())->method('getQueryParams')->willReturn($data);
+
+ $controller = $this->getController(['getRequest']);
+ $controller->method('getRequest')->willReturn($request);
+
+ $list = $controller->getQueryParams(['bar' => 'wall', 'foo', 'qux' => []]);
+ $this->assertEquals([$bar, $foo, $qux], $list);
+ }
+
+
+
+ public function testHasQueryParams()
+ {
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->any())->method('getQueryParams')
+ ->willReturn(['foo' => 10, 'bar' => '', 'buz' => null, 'qux' => ['a' => 10, 'b' => 20]]);
+
+ $controller = $this->getController(['getRequest']);
+ $controller->method('getRequest')->willReturn($request);
+
+ $this->assertTrue($controller->hasQueryParam('foo'), 'foo');
+ $this->assertTrue($controller->hasQueryParam('bar'), 'bar');
+ $this->assertTrue($controller->hasQueryParam('qux'), 'qux');
+
+ $this->assertFalse($controller->hasQueryParam('nop'), 'nop');
+ $this->assertFalse($controller->hasQueryParam('buz'), 'buz');
+ }
+
+ public function getQueryParamProvider()
+ {
+ return [
+ [10, 'foo'],
+ ['', 'bar'],
+ [null, 'buz'],
+ [['a' => 10], 'qux'],
+ [null, 'nop'],
+
+ [10, 'foo', 22],
+ ['', 'bar', 'woop'],
+ ['woop', 'buz', 'woop'],
+ ['woop', 'nop', 'woop'],
+
+ [10, 'foo', null, FILTER_SANITIZE_NUMBER_INT],
+ [10, 'fox', null, FILTER_SANITIZE_NUMBER_INT],
+ [22, 'nop', '22 pigs', FILTER_SANITIZE_NUMBER_INT],
+ [null, 'nop', 'woop', FILTER_SANITIZE_NUMBER_INT],
+
+ [10, 'foo', null, FILTER_VALIDATE_INT],
+ [false, 'fox', null, FILTER_VALIDATE_INT],
+ [false, 'nop', '22 pigs', FILTER_VALIDATE_INT],
+ [null, 'nop', null, FILTER_VALIDATE_INT],
+
+ [10, 'foo', null, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE],
+ [null, 'fox', null, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE],
+ [null, 'nop', '22 pigs', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE],
+ [null, 'nop', null, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE]
+ ];
+ }
+
+ /**
+ * @dataProvider getQueryParamProvider
+ *
+ * @param mixed $expect
+ * @param array $param
+ * @param string $default
+ * @param int $filter
+ * @param array $filterOptions
+ */
+ public function testGetQueryParam($expect, ...$args)
+ {
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->any())->method('getQueryParams')
+ ->willReturn(['foo' => 10, 'fox' => '10 foxes', 'bar' => '', 'buz' => null, 'qux' => ['a' => 10]]);
+
+ $controller = $this->getController(['getRequest']);
+ $controller->method('getRequest')->willReturn($request);
+
+ $value = $controller->getQueryParam(...(array)$args);
+
+ $this->assertEquals($expect, $value);
+ }
+
+
+ public function inputProvider()
+ {
+ $file = $this->createMock(UploadedFileInterface::class);
+ $qux = $this->createMock(UploadedFileInterface::class);
+
+ return [
+ [
+ [],
+ [],
+ [],
+ ],
+ [
+ ['foo' => 10],
+ [],
+ ['foo' => 10],
+ ],
+ [
+ ['foo' => 10],
+ null,
+ ['foo' => 10],
+ ],
+ [
+ ['foo' => 10, 'bar' => '', 'buz' => null, 'qux' => ['a' => 10, 'b' => 20]],
+ [],
+ ['foo' => 10, 'bar' => '', 'buz' => null, 'qux' => ['a' => 10, 'b' => 20]],
+ ],
+ [
+ ['foo' => 10, 'qux' => ['a' => 10, 'b' => 20]],
+ ['file' => $file],
+ ['foo' => 10, 'qux' => ['a' => 10, 'b' => 20], 'file' => $file],
+ ],
+ [
+ ['foo' => 10, 'qux' => ['a' => 10, 'b' => 20]],
+ ['foo' => $file],
+ ['foo' => $file, 'qux' => ['a' => 10, 'b' => 20]],
+ ],
+ [
+ ['foo' => 10, 'qux' => ['a' => 10, 'b' => 20]],
+ ['file' => $file, 'qux' => ['c' => $qux]],
+ ['foo' => 10, 'qux' => ['a' => 10, 'b' => 20, 'c' => $qux], 'file' => $file],
+ ],
+ [
+ ['foo' => 10, 'qux' => ['a' => 10, 'b' => 20]],
+ ['file' => $file, 'qux' => ['b' => $qux]],
+ ['foo' => 10, 'qux' => ['a' => 10, 'b' => $qux], 'file' => $file],
+ ],
+ [
+ [],
+ ['foo' => $file],
+ ['foo' => $file]
+ ],
+ [
+ null,
+ ['foo' => $file],
+ null
+ ],
+ [
+ new \stdClass(),
+ ['foo' => $file],
+ new \stdClass(),
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider inputProvider
+ *
+ * @param array|mixed $data
+ * @param array|null $files
+ * @param array|mixed $expect
+ */
+ public function testGetInput($data, $files, $expect)
+ {
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->any())->method('getParsedBody')->willReturn($data);
+ $request->expects($this->any())->method('getUploadedFiles')->willReturn($files);
+
+ $controller = $this->getController(['getRequest']);
+ $controller->method('getRequest')->willReturn($request);
+
+ $input = $controller->getInput();
+
+ $this->assertEquals($expect, $input);
+ }
+}
diff --git a/tests/Controller/OutputTest.php b/tests/Controller/OutputTest.php
index 2793a14..7ca0aa4 100644
--- a/tests/Controller/OutputTest.php
+++ b/tests/Controller/OutputTest.php
@@ -2,9 +2,6 @@
namespace Jasny\Controller;
-use Jasny\Controller;
-use Jasny\Flash;
-use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Jasny\Controller\TestHelper;