getTest(); } /** * @param TestDbo $test * @ensure42 * @return string */ public function testTypedParameterDoc(TestDbo $test) { return $test->getTest(); } /** * @param TestDbo $test * @nullable * @return string */ public function testNullable(TestDbo $test = null) { return is_null($test); } public function testBeforeMiddleware(LuStringDbo $_user) { return $_user->getString(); } public function testAfterMiddleware() { return "A result"; } } class BeforeMiddleware implements LuAbstractMiddleware { public function onBefore($_parameters) { $_parameters["_user"] = "\"my user\""; return $_parameters; } } class AfterMiddleware implements LuAbstractMiddleware { public function onAfter() { return "AfterMiddleware"; } } class AuthenticationMiddleware implements LuAbstractMiddleware { public function onBefore($_parameters, $permissions = []) { if ($permissions[0] != "ALLOWED") { throw new LuBusinessException("Unauthorized", 401); } return $_parameters; } } class LuRouteTest extends \PHPUnit_Framework_TestCase { public function testBasic() { $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testBasic"); $router->setup(); $res = $router->dispatch("GET", "/test", []); $this->assertSame("Test. succeeded", $res); } public function testParameter() { $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testParameter"); $router->setup(); $res = $router->dispatch("GET", "/test", ["test" => "A value"]); $this->assertSame("A value", $res); } public function testTypedParameter() { $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameter"); $router->setup(); $res = $router->dispatch("GET", "/test", ["test" => '{"test": "Some value"}']); $this->assertSame("Some value", $res); } public function testTypedParameterDoc() { $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc"); $router->setup(); $res = $router->dispatch("GET", "/test", ["test" => '{"test": "42"}']); $this->assertSame("42", $res); } public function testNullable() { $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testNullable"); $router->setup(); $res = $router->dispatch("GET", "/test", ["test" => 'null']); $this->assertSame(true, $res); } public function testBeforeMiddleware() { $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testBeforeMiddleware", [], ['BeforeMiddleware']); $router->setup(); $res = $router->dispatch("GET", "/test", []); $this->assertSame("my user", $res); } public function testAfterMiddleware() { $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testAfterMiddleware", [], ['AfterMiddleware']); $router->setup(); $res = $router->dispatch("GET", "/test", []); $this->assertSame("AfterMiddleware", $res); } public function testAuthenticationMiddlewareAllowed() { $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testBasic", ["permissions" => ["ALLOWED"]], ['AuthenticationMiddleware']); $router->setup(); $res = $router->dispatch("GET", "/test", []); $this->assertSame("Test. succeeded", $res); } public function testAuthenticationMiddlewareNotAllowed() { $this->expectException(LuBusinessException::class); $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testBasic", ["permissions" => ["NOT_ALLOWED"]], ['AuthenticationMiddleware']); $router->setup(); $router->dispatch("GET", "/test", []); } public function testTypedParameterDocConstraint() { $this->expectException(LuBusinessException::class); $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc"); $router->setup(); $router->dispatch("GET", "/test", ["test" => '{"Test": "Test."}']); } public function testMethodNotAllowed() { $this->expectException(LuBusinessException::class); $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc"); $router->setup(); $router->dispatch("POST", "/test", ["test" => '{"Test": "Test."}']); } public function testRouteNotFound() { $this->expectException(LuBusinessException::class); $router = new LuRoute(); $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc"); $router->setup(); $router->dispatch("GET", "/notfound", ["test" => '{"Test": "Test."}']); } }