120 lines
3.5 KiB
PHP
120 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace pjdietz\WellRESTed\Test;
|
|
|
|
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
|
|
use pjdietz\WellRESTed\Interfaces\RequestInterface;
|
|
use pjdietz\WellRESTed\Response;
|
|
use pjdietz\WellRESTed\Routes\RegexRoute;
|
|
|
|
class RegexRouteTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* @dataProvider matchingRouteProvider
|
|
*/
|
|
public function testMatchPatternForRoute($pattern, $path, $captures)
|
|
{
|
|
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
|
$mockRequest->expects($this->any())
|
|
->method('getPath')
|
|
->will($this->returnValue($path));
|
|
|
|
$route = new RegexRoute($pattern, __NAMESPACE__ . '\RegexRouteTestHandler');
|
|
$resp = $route->getResponse($mockRequest);
|
|
$this->assertNotNull($resp);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider matchingRouteProvider
|
|
*/
|
|
public function testExctractCapturesForRoute($pattern, $path, $captures)
|
|
{
|
|
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
|
$mockRequest->expects($this->any())
|
|
->method('getPath')
|
|
->will($this->returnValue($path));
|
|
|
|
$route = new RegexRoute($pattern, __NAMESPACE__ . '\RegexRouteTestHandler');
|
|
$resp = $route->getResponse($mockRequest);
|
|
$body = json_decode($resp->getBody(), true);
|
|
$this->assertEquals($captures, $body);
|
|
}
|
|
|
|
public function matchingRouteProvider()
|
|
{
|
|
return [
|
|
["~/cat/[0-9]+~", "/cat/2", [0 => "/cat/2"]],
|
|
["#/dog/.*#", "/dog/his-name-is-bear", [0 => "/dog/his-name-is-bear"]],
|
|
["~/cat/([0-9+])~", "/cat/2", [
|
|
0 => "/cat/2",
|
|
1 => "2"
|
|
]],
|
|
["~/dog/(?<id>[0-9+])~", "/dog/2", [
|
|
0 => "/dog/2",
|
|
1 => "2",
|
|
"id" => "2"
|
|
]]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider mismatchingRouteProvider
|
|
*/
|
|
public function testSkipMismatchingPattern($pattern, $path)
|
|
{
|
|
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
|
$mockRequest->expects($this->any())
|
|
->method('getPath')
|
|
->will($this->returnValue($path));
|
|
|
|
$route = new RegexRoute($pattern, 'NoClass');
|
|
$resp = $route->getResponse($mockRequest);
|
|
$this->assertNull($resp);
|
|
}
|
|
|
|
public function mismatchingRouteProvider()
|
|
{
|
|
return [
|
|
["~/cat/[0-9]+~", "/cat/molly"],
|
|
["~/cat/[0-9]+~", "/dog/bear"],
|
|
["#/dog/.*#", "/dog"]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider invalidRouteProvider
|
|
* @expectedException \pjdietz\WellRESTed\Exceptions\ParseException
|
|
*/
|
|
public function testFailOnInvalidPattern($pattern)
|
|
{
|
|
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
|
|
|
$route = new RegexRoute($pattern, 'NoClass');
|
|
$resp = $route->getResponse($mockRequest);
|
|
$this->assertNull($resp);
|
|
}
|
|
|
|
public function invalidRouteProvider()
|
|
{
|
|
return [
|
|
["~/unterminated"],
|
|
["/nope"]
|
|
];
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Mini Handler class that allways returns a 200 status code Response.
|
|
*/
|
|
class RegexRouteTestHandler implements HandlerInterface
|
|
{
|
|
public function getResponse(RequestInterface $request, array $args = null)
|
|
{
|
|
$resp = new Response();
|
|
$resp->setStatusCode(200);
|
|
$resp->setBody(json_encode($args));
|
|
return $resp;
|
|
}
|
|
}
|