-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Rougin\Slytherin\Forward; | ||
|
||
use Rougin\Slytherin\Forward\Fixture\Builder; | ||
use Rougin\Slytherin\Testcase; | ||
|
||
/** | ||
* @package Slytherin | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Rougin\Slytherin\Forward\Fixture; | ||
|
||
use Rougin\Slytherin\Application; | ||
use Rougin\Slytherin\Configuration; | ||
use Rougin\Slytherin\Container\Container; | ||
|
||
/** | ||
* @package Slytherin | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
use Rougin\Slytherin\Forward\Fixture\Router; | ||
|
||
return | ||
[ | ||
'name' => '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', | ||
// ----------------------------------------------------- | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Rougin\Slytherin\Forward\Fixture\Packages; | ||
|
||
use Rougin\Slytherin\Http\HttpIntegration; | ||
|
||
/** | ||
* @package Slytherin | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
class HttpPackage extends HttpIntegration | ||
{ | ||
protected $preferred = 'slytherin'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Rougin\Slytherin\Forward\Fixture\Packages; | ||
|
||
use Rougin\Slytherin\Routing\RoutingIntegration; | ||
|
||
/** | ||
* @package Slytherin | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
class RoutingPackage extends RoutingIntegration | ||
{ | ||
protected $preferred = 'slytherin'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Rougin\Slytherin\Forward\Fixture; | ||
|
||
use Rougin\Slytherin\Routing\Router as Slytherin; | ||
|
||
/** | ||
* @package Slytherin | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Rougin\Slytherin\Forward\Fixture\Routes; | ||
|
||
/** | ||
* @package Slytherin | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
class Hello extends Route | ||
{ | ||
/** | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function index() | ||
{ | ||
$this->response->getBody()->write('Hello world!'); | ||
|
||
return $this->response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Rougin\Slytherin\Forward\Fixture\Routes; | ||
|
||
/** | ||
* @package Slytherin | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
class Home extends Route | ||
{ | ||
/** | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function index() | ||
{ | ||
return $this->response->withStatus(404); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Rougin\Slytherin\Forward\Fixture\Routes; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* @package Slytherin | ||
* @author Rougin Gutib <rougingutib@gmail.com> | ||
*/ | ||
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'); | ||
} | ||
} |