diff --git a/tests/Forward/ApplicationTest.php b/tests/Forward/ApplicationTest.php new file mode 100644 index 00000000..7d270901 --- /dev/null +++ b/tests/Forward/ApplicationTest.php @@ -0,0 +1,55 @@ + + */ +class ApplicationTest extends Testcase +{ + /** + * @var \Rougin\Slytherin\Forward\Fixture\Builder + */ + protected $builder; + + protected function doSetUp() + { + $this->builder = new Builder; + } + + /** + * @runInSeparateProcess + * + * @return void + */ + public function test_run_with_404_error() + { + $this->builder->setUrl('GET', '/'); + + $this->builder->make()->run(); + + $response = http_response_code(); + + $this->assertEquals(404, $response); + } + + /** + * @runInSeparateProcess + * + * @return void + */ + public function test_run_with_sample_text() + { + $builder = new Builder; + + $builder->setUrl('GET', '/v1/hello'); + + $this->expectOutputString('Hello world!'); + + $builder->make()->run(); + } +} \ No newline at end of file diff --git a/tests/Forward/Fixture/Builder.php b/tests/Forward/Fixture/Builder.php new file mode 100644 index 00000000..c1877c3f --- /dev/null +++ b/tests/Forward/Fixture/Builder.php @@ -0,0 +1,93 @@ + + */ +class Builder +{ + protected $cookies = array(); + + protected $files = array(); + + protected $query = array(); + + protected $parsed = array(); + + protected $server = array(); + + public function make() + { + $config = new Configuration; + + $config->load(__DIR__ . '/Config'); + + $config->set('app.http.cookies', $this->cookies); + $config->set('app.http.files', $this->files); + $config->set('app.http.get', $this->query); + $config->set('app.http.post', $this->parsed); + $config->set('app.http.server', $this->server); + + $container = new Container; + + $app = new Application($container, $config); + + $items = $config->get('app.packages'); + + return $app->integrate($items); + } + + public function setCookies($cookies) + { + $this->cookies = $cookies; + + return $this; + } + + public function setFiles($files) + { + $this->files = $files; + + return $this; + } + + public function setQuery($query) + { + $this->query = $query; + + return $this; + } + + public function setParsed($parsed) + { + $this->parsed = $parsed; + + return $this; + } + + public function setServer($server) + { + $this->server = $server; + + return $this; + } + + public function setUrl($method, $uri) + { + $this->server['REQUEST_METHOD'] = $method; + + $this->server['REQUEST_URI'] = $uri; + + $this->server['SERVER_NAME'] = 'localhost'; + + $this->server['SERVER_PORT'] = '8000'; + + return $this; + } +} \ No newline at end of file diff --git a/tests/Forward/Fixture/Config/app.php b/tests/Forward/Fixture/Config/app.php new file mode 100644 index 00000000..70b2edc1 --- /dev/null +++ b/tests/Forward/Fixture/Config/app.php @@ -0,0 +1,44 @@ + 'Slytherin', + + 'base_url' => 'http://localhost:8000', + + 'environment' => 'development', + + 'timezone' => 'Asia/Manila', + + 'http' => + [ + 'cookies' => array(), + + 'files' => array(), + + 'get' => array(), + + 'post' => array(), + + 'server' => array(), + ], + + 'router' => new Router, + + 'middlewares' => [], + + 'packages' => + [ + // Testing Packages --------------------------------------- + 'Rougin\Slytherin\Forward\Fixture\Packages\HttpPackage', + 'Rougin\Slytherin\Forward\Fixture\Packages\RoutingPackage', + // -------------------------------------------------------- + + // Slytherin Integrations ------------------------------ + 'Rougin\Slytherin\Integration\ConfigurationIntegration', + 'Rougin\Slytherin\Middleware\MiddlewareIntegration', + // ----------------------------------------------------- + ], +]; \ No newline at end of file diff --git a/tests/Forward/Fixture/Packages/HttpPackage.php b/tests/Forward/Fixture/Packages/HttpPackage.php new file mode 100644 index 00000000..eb18b08b --- /dev/null +++ b/tests/Forward/Fixture/Packages/HttpPackage.php @@ -0,0 +1,14 @@ + + */ +class HttpPackage extends HttpIntegration +{ + protected $preferred = 'slytherin'; +} \ No newline at end of file diff --git a/tests/Forward/Fixture/Packages/RoutingPackage.php b/tests/Forward/Fixture/Packages/RoutingPackage.php new file mode 100644 index 00000000..50ac9003 --- /dev/null +++ b/tests/Forward/Fixture/Packages/RoutingPackage.php @@ -0,0 +1,14 @@ + + */ +class RoutingPackage extends RoutingIntegration +{ + protected $preferred = 'slytherin'; +} \ No newline at end of file diff --git a/tests/Forward/Fixture/Router.php b/tests/Forward/Fixture/Router.php new file mode 100644 index 00000000..9fe62396 --- /dev/null +++ b/tests/Forward/Fixture/Router.php @@ -0,0 +1,26 @@ + + */ +class Router extends Slytherin +{ + protected $namespace = 'Rougin\Slytherin\Forward\Fixture\Routes\\'; + + protected $prefix = '/'; + + public function routes($parsed = false) + { + // Default routes ------------------ + $this->get('/v1/hello', 'Hello@index'); + $this->get('/', 'Home@index'); + // --------------------------------- + + return parent::routes($parsed); + } +} \ No newline at end of file diff --git a/tests/Forward/Fixture/Routes/Hello.php b/tests/Forward/Fixture/Routes/Hello.php new file mode 100644 index 00000000..9e30a8a2 --- /dev/null +++ b/tests/Forward/Fixture/Routes/Hello.php @@ -0,0 +1,20 @@ + + */ +class Hello extends Route +{ + /** + * @return \Psr\Http\Message\ResponseInterface + */ + public function index() + { + $this->response->getBody()->write('Hello world!'); + + return $this->response; + } +} \ No newline at end of file diff --git a/tests/Forward/Fixture/Routes/Home.php b/tests/Forward/Fixture/Routes/Home.php new file mode 100644 index 00000000..6dd961c5 --- /dev/null +++ b/tests/Forward/Fixture/Routes/Home.php @@ -0,0 +1,18 @@ + + */ +class Home extends Route +{ + /** + * @return \Psr\Http\Message\ResponseInterface + */ + public function index() + { + return $this->response->withStatus(404); + } +} \ No newline at end of file diff --git a/tests/Forward/Fixture/Routes/Route.php b/tests/Forward/Fixture/Routes/Route.php new file mode 100644 index 00000000..2b3f7f3c --- /dev/null +++ b/tests/Forward/Fixture/Routes/Route.php @@ -0,0 +1,47 @@ + + */ +class Route +{ + /** + * @var \Psr\Http\Message\ServerRequestInterface + */ + protected $request; + + /** + * @var \Psr\Http\Message\ResponseInterface + */ + protected $response; + + /** + * Initializes the controller instance. + * + * @param \Psr\Http\Message\ServerRequestInterface $request + * @param \Psr\Http\Message\ResponseInterface $response + */ + public function __construct(ServerRequestInterface $request, ResponseInterface $response) + { + $this->request = $request; + + $this->response = $response; + } + + public function json($data, $code = 200, $options = 0) + { + $response = $this->response->withStatus($code); + + $stream = json_encode($data); + + $response->getBody()->write($stream); + + return $response->withHeader('Content-Type', 'application/json'); + } +} \ No newline at end of file