|
|
@@ -674,23 +674,132 @@ class ControllerFactoryTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Test that required int (non-string)
|
|
|
+ * Test that coercing string to float, int and bool params
|
|
|
*/
|
|
|
- public function testInvokeRequiredIntParam(): void
|
|
|
+ public function testInvokePassedParametersCoercion(): void
|
|
|
{
|
|
|
$request = new ServerRequest([
|
|
|
- 'url' => 'test_plugin_three/dependencies/requiredInt',
|
|
|
+ 'url' => 'test_plugin_three/dependencies/requiredTyped',
|
|
|
'params' => [
|
|
|
'plugin' => null,
|
|
|
'controller' => 'Dependencies',
|
|
|
- 'action' => 'requiredInt',
|
|
|
- 'pass' => ['one'],
|
|
|
+ 'action' => 'requiredTyped',
|
|
|
+ 'pass' => ['1.0', '2', '0'],
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+ $controller = $this->factory->create($request);
|
|
|
+
|
|
|
+ $result = $this->factory->invoke($controller);
|
|
|
+ $data = json_decode((string)$result->getBody(), true);
|
|
|
+
|
|
|
+ $this->assertNotNull($data);
|
|
|
+ $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false], $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that default values work for typed parameters
|
|
|
+ */
|
|
|
+ public function testInvokeOptionalTypedParam(): void
|
|
|
+ {
|
|
|
+ $request = new ServerRequest([
|
|
|
+ 'url' => 'test_plugin_three/dependencies/optionalTyped',
|
|
|
+ 'params' => [
|
|
|
+ 'plugin' => null,
|
|
|
+ 'controller' => 'Dependencies',
|
|
|
+ 'action' => 'optionalTyped',
|
|
|
+ 'pass' => ['1.0'],
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+ $controller = $this->factory->create($request);
|
|
|
+
|
|
|
+ $result = $this->factory->invoke($controller);
|
|
|
+ $data = json_decode((string)$result->getBody(), true);
|
|
|
+
|
|
|
+ $this->assertNotNull($data);
|
|
|
+ $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => true], $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test using invalid value for supported type
|
|
|
+ */
|
|
|
+ public function testInvokePassedParametersUnsupportedFloatCoercion(): void
|
|
|
+ {
|
|
|
+ $request = new ServerRequest([
|
|
|
+ 'url' => 'test_plugin_three/dependencies/requiredTyped',
|
|
|
+ 'params' => [
|
|
|
+ 'plugin' => null,
|
|
|
+ 'controller' => 'Dependencies',
|
|
|
+ 'action' => 'requiredTyped',
|
|
|
+ 'pass' => ['true', '2', '1'],
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+ $controller = $this->factory->create($request);
|
|
|
+
|
|
|
+ $this->expectException(MissingActionException::class);
|
|
|
+ $this->expectExceptionMessage('Action Dependencies::requiredTyped() cannot coerce "true" to `float` for parameter `one`');
|
|
|
+ $this->factory->invoke($controller);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test using invalid value for supported type
|
|
|
+ */
|
|
|
+ public function testInvokePassedParametersUnsupportedIntCoercion(): void
|
|
|
+ {
|
|
|
+ $request = new ServerRequest([
|
|
|
+ 'url' => 'test_plugin_three/dependencies/requiredTyped',
|
|
|
+ 'params' => [
|
|
|
+ 'plugin' => null,
|
|
|
+ 'controller' => 'Dependencies',
|
|
|
+ 'action' => 'requiredTyped',
|
|
|
+ 'pass' => ['1', '2.0', '1'],
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+ $controller = $this->factory->create($request);
|
|
|
+
|
|
|
+ $this->expectException(MissingActionException::class);
|
|
|
+ $this->expectExceptionMessage('Action Dependencies::requiredTyped() cannot coerce "2.0" to `int` for parameter `two`');
|
|
|
+ $this->factory->invoke($controller);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test using invalid value for supported type
|
|
|
+ */
|
|
|
+ public function testInvokePassedParametersUnsupportedBoolCoercion(): void
|
|
|
+ {
|
|
|
+ $request = new ServerRequest([
|
|
|
+ 'url' => 'test_plugin_three/dependencies/requiredTyped',
|
|
|
+ 'params' => [
|
|
|
+ 'plugin' => null,
|
|
|
+ 'controller' => 'Dependencies',
|
|
|
+ 'action' => 'requiredTyped',
|
|
|
+ 'pass' => ['1', '1', 'true'],
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+ $controller = $this->factory->create($request);
|
|
|
+
|
|
|
+ $this->expectException(MissingActionException::class);
|
|
|
+ $this->expectExceptionMessage('Action Dependencies::requiredTyped() cannot coerce "true" to `bool` for parameter `three`');
|
|
|
+ $this->factory->invoke($controller);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test using an unsupported type.
|
|
|
+ */
|
|
|
+ public function testInvokePassedParamUnsupportedType(): void
|
|
|
+ {
|
|
|
+ $request = new ServerRequest([
|
|
|
+ 'url' => 'test_plugin_three/dependencies/unsupportedTyped',
|
|
|
+ 'params' => [
|
|
|
+ 'plugin' => null,
|
|
|
+ 'controller' => 'Dependencies',
|
|
|
+ 'action' => 'unsupportedTyped',
|
|
|
+ 'pass' => ['test'],
|
|
|
],
|
|
|
]);
|
|
|
$controller = $this->factory->create($request);
|
|
|
|
|
|
$this->expectException(MissingActionException::class);
|
|
|
- $this->expectExceptionMessage('Action Dependencies::requiredInt() cannot coerce "one" to `int` for parameter `one`');
|
|
|
+ $this->expectExceptionMessage('Action Dependencies::unsupportedTyped() cannot coerce "test" to `array` for parameter `one`');
|
|
|
$this->factory->invoke($controller);
|
|
|
}
|
|
|
|