wellrested/test/Routes/RegexRouteTest.php

108 lines
3.1 KiB
PHP

<?php
namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Routes\RegexRoute;
use Prophecy\Argument;
/**
* @covers pjdietz\WellRESTed\Routes\RegexRoute
*/
class RegexRouteTest extends \PHPUnit_Framework_TestCase
{
private $handler;
private $request;
private $response;
/**
* @dataProvider matchingRouteProvider
*/
public function testMatchesPattern($pattern, $path)
{
$this->request->getPath()->willReturn($path);
$route = new RegexRoute($pattern, $this->handler->reveal());
$resp = $route->getResponse($this->request->reveal());
$this->assertNotNull($resp);
}
/**
* @dataProvider matchingRouteProvider
*/
public function testExtractsCaptures($pattern, $path, $captures)
{
$this->request->getPath()->willReturn($path);
$route = new RegexRoute($pattern, $this->handler->reveal());
$route->getResponse($this->request->reveal());
$this->handler->getResponse(Argument::any(), Argument::that(
function ($args) use ($captures) {
return $args = $captures;
}))->shouldHaveBeenCalled();
}
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 testFailsToMatchMismatchingPattern($pattern, $path)
{
$this->request->getPath()->willReturn($path);
$route = new RegexRoute($pattern, $this->handler->reveal());
$resp = $route->getResponse($this->request->reveal());
$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 testThrowsExceptionOnInvalidPattern($pattern)
{
$route = new RegexRoute($pattern, $this->handler->reveal());
$route->getResponse($this->request->reveal());
}
public function invalidRouteProvider()
{
return [
["~/unterminated"],
["/nope"]
];
}
public function setUp()
{
$this->request = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\RequestInterface");
$this->response = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\ResponseInterface");
$this->handler = $this->prophesize("\\pjdietz\\WellRESTed\\Interfaces\\HandlerInterface");
$this->handler->getResponse(Argument::cetera())->willReturn($this->response->reveal());
}
}