Created RedirectActionTest

This commit is contained in:
Alejandro Celaya 2016-07-30 20:02:48 +02:00
parent fcdcfde04f
commit ebeaa3c64a
2 changed files with 118 additions and 3 deletions

View file

@ -70,10 +70,10 @@ class RedirectAction implements MiddlewareInterface
// If provided shortCode does not belong to a valid long URL, dispatch next middleware, which will trigger
// a not-found error
if (! isset($longUrl)) {
return $out($request, $response->withStatus(404), 'Not found');
return $this->notFoundResponse($request, $response, $out);
}
// Track visit to this shortcode
// Track visit to this short code
$this->visitTracker->track($shortCode);
// Return a redirect response to the long URL.
@ -81,7 +81,18 @@ class RedirectAction implements MiddlewareInterface
return new RedirectResponse($longUrl);
} catch (\Exception $e) {
// In case of error, dispatch 404 error
return $out($request, $response);
return $this->notFoundResponse($request, $response, $out);
}
}
/**
* @param Request $request
* @param Response $response
* @param callable $out
* @return Response
*/
protected function notFoundResponse(Request $request, Response $response, callable $out)
{
return $out($request, $response->withStatus(404), 'Not Found');
}
}

View file

@ -0,0 +1,104 @@
<?php
namespace ShlinkioTest\Shlink\Core\Action;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Core\Service\VisitsTracker;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
class RedirectActionTest extends TestCase
{
/**
* @var RedirectAction
*/
protected $action;
/**
* @var ObjectProphecy
*/
protected $urlShortener;
public function setUp()
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
$visitTracker = $this->prophesize(VisitsTracker::class);
$visitTracker->track(Argument::any());
$this->action = new RedirectAction($this->urlShortener->reveal(), $visitTracker->reveal());
}
/**
* @test
*/
public function redirectionIsPerformedToLongUrl()
{
$shortCode = 'abc123';
$expectedUrl = 'http://domain.com/foo/bar';
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($expectedUrl)
->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$response = $this->action->__invoke($request, new Response());
$this->assertInstanceOf(Response\RedirectResponse::class, $response);
$this->assertEquals(302, $response->getStatusCode());
$this->assertTrue($response->hasHeader('Location'));
$this->assertEquals($expectedUrl, $response->getHeaderLine('Location'));
}
/**
* @test
*/
public function nextErrorMiddlewareIsInvokedIfLongUrlIsNotFound()
{
$shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)
->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$originalResponse = new Response();
$test = $this;
$this->action->__invoke($request, $originalResponse, function (
ServerRequestInterface $req,
ResponseInterface $resp,
$error
) use (
$test,
$request
) {
$test->assertSame($request, $req);
$test->assertEquals(404, $resp->getStatusCode());
$test->assertEquals('Not Found', $error);
});
}
/**
* @test
*/
public function nextErrorMiddlewareIsInvokedIfAnExceptionIsThrown()
{
$shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class)
->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$originalResponse = new Response();
$test = $this;
$this->action->__invoke($request, $originalResponse, function (
ServerRequestInterface $req,
ResponseInterface $resp,
$error
) use (
$test,
$request
) {
$test->assertSame($request, $req);
$test->assertEquals(404, $resp->getStatusCode());
$test->assertEquals('Not Found', $error);
});
}
}