Split tests into unit and integration test suites

This commit is contained in:
PJ Dietz 2015-02-22 14:05:05 -05:00
parent 812012bdbf
commit 1a5712a417
21 changed files with 94 additions and 28 deletions

View File

@ -1,3 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4" /> <project version="4">
<component name="PhpUnit">
<phpunit_settings>
<PhpUnitSettings load_method="CUSTOM_LOADER" configuration_file_path="$PROJECT_DIR$/phpunit.xml.dist" custom_loader_path="$PROJECT_DIR$/vendor/autoload.php" phpunit_phar_path="$PROJECT_DIR$/vendor/bin/phpunit" use_configuration_file="true" />
</phpunit_settings>
</component>
</project>

View File

@ -20,8 +20,11 @@
<env name="FAIL_PORT" value="9001-9100" /> <env name="FAIL_PORT" value="9001-9100" />
</php> </php>
<testsuites> <testsuites>
<testsuite name="WellRESTed Test Suite"> <testsuite name="unit">
<directory>./test/</directory> <directory>./test/tests/unit/</directory>
</testsuite>
<testsuite name="integration">
<directory>./test/tests/integration/</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter> <filter>

View File

@ -136,7 +136,7 @@ class Router implements HandlerInterface
public function getResponse(RequestInterface $request, array $args = null) public function getResponse(RequestInterface $request, array $args = null)
{ {
$response = $this->tryResponse($this->routeTable, $request, $args); $response = $this->tryResponse($this->routeTable, $request, $args);
if ($response) { if ($response && $response instanceof ResponseInterface) {
// Check if the router has an error handler for this status code. // Check if the router has an error handler for this status code.
$status = $response->getStatusCode(); $status = $response->getStatusCode();
$errorResponse = $this->getErrorResponse($status, $request, $args, $response); $errorResponse = $this->getErrorResponse($status, $request, $args, $response);

View File

@ -1,17 +0,0 @@
<?php
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Response;
/**
* Mini Handler class that allways returns a 200 status code Response.
*/
class MockHandler implements HandlerInterface
{
public function getResponse(\pjdietz\WellRESTed\Interfaces\RequestInterface $request, array $args = null)
{
$resp = new Response();
$resp->setStatusCode(200);
return $resp;
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace pjdietz\WellRESTed\Test\Integration;
use pjdietz\WellRESTed\Router;
use Prophecy\Argument;
class RouterTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
public function setUp()
{
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$this->request->getPath()->willReturn("/");
$this->request->getMethod()->willReturn("GET");
$this->response = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface");
$this->response->getStatusCode()->willReturn(200);
$this->response->getBody()->willReturn("Hello, world!");
}
public function testDispatchesCallable()
{
$response = $this->response;
$router = new Router();
$router->add("/", function () use ($response) {
return $response->reveal();
});
$result = $router->getResponse($this->request->reveal());
$this->assertSame($response->reveal(), $result);
}
public function testDispatchesCallableWithArguments()
{
$response = $this->response;
$args = ["cat" => "molly"];
$router = new Router();
$router->add("/", function ($rqst, $args) use ($response) {
$response->getBody()->willReturn($args["cat"]);
return $response->reveal();
});
$result = $router->getResponse($this->request->reveal(), $args);
$this->assertEquals("molly", $result->getBody());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testStopsDispatchingCallablesAfterFirstNonNull()
{
$router = new Router();
$router->add("/cats/{cat}", function () {
echo "Hello, cat!";
return true;
});
$router->add("/cats/{cat}", function () {
echo "Hello, cat!";
});
$this->request->getPath()->willReturn("/cats/molly");
ob_start();
$router->getResponse($this->request->reveal());
$captured = ob_get_contents();
ob_end_clean();
$this->assertEquals("Hello, cat!", $captured);
}
}

View File

@ -19,7 +19,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
{ {
$host = "localhost"; $host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT")); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/method.php"); $script = realpath(__DIR__ . "/../../sham-routers/method.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
@ -57,7 +57,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
{ {
$host = "localhost"; $host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT")); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/headers.php"); $script = realpath(__DIR__ . "/../../sham-routers/headers.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
@ -92,7 +92,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
{ {
$host = "localhost"; $host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT")); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/body.php"); $script = realpath(__DIR__ . "/../../sham-routers/body.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); $rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
@ -125,7 +125,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
{ {
$host = "localhost"; $host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT")); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/formFields.php"); $script = realpath(__DIR__ . "/../../sham-routers/formFields.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
$rqst = new Request("http://$host:$port"); $rqst = new Request("http://$host:$port");
@ -158,7 +158,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
{ {
$host = "localhost"; $host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT")); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/headers.php"); $script = realpath(__DIR__ . "/../../sham-routers/headers.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); $rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
@ -181,7 +181,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
{ {
$host = "localhost"; $host = "localhost";
$port = $this->getRandomNumberInRange(getenv("PORT")); $port = $this->getRandomNumberInRange(getenv("PORT"));
$script = realpath(__DIR__ . "/sham-routers/headers.php"); $script = realpath(__DIR__ . "/../../sham-routers/headers.php");
$server = new ShamServer($host, $port, $script); $server = new ShamServer($host, $port, $script);
$rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface"); $rqst = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");