Skip to content

Commit

Permalink
Add forward-based unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 25, 2023
1 parent d6987f4 commit 4cd46b4
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/Forward/ApplicationTest.php
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();
}
}
93 changes: 93 additions & 0 deletions tests/Forward/Fixture/Builder.php
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;
}
}
44 changes: 44 additions & 0 deletions tests/Forward/Fixture/Config/app.php
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',
// -----------------------------------------------------
],
];
14 changes: 14 additions & 0 deletions tests/Forward/Fixture/Packages/HttpPackage.php
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';
}
14 changes: 14 additions & 0 deletions tests/Forward/Fixture/Packages/RoutingPackage.php
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';
}
26 changes: 26 additions & 0 deletions tests/Forward/Fixture/Router.php
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);
}
}
20 changes: 20 additions & 0 deletions tests/Forward/Fixture/Routes/Hello.php
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;
}
}
18 changes: 18 additions & 0 deletions tests/Forward/Fixture/Routes/Home.php
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);
}
}
47 changes: 47 additions & 0 deletions tests/Forward/Fixture/Routes/Route.php
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');
}
}

0 comments on commit 4cd46b4

Please sign in to comment.