Skip to content

Commit

Permalink
Merge pull request #2816 from l0gicgate/FixRedirectBug
Browse files Browse the repository at this point in the history
4.x - Fix RouteCollectorProxy::redirect() Bug
  • Loading branch information
l0gicgate committed Aug 20, 2019
2 parents 806adf2 + 5128833 commit 89f8076
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
6 changes: 4 additions & 2 deletions Slim/Routing/RouteCollectorProxy.php
Expand Up @@ -195,8 +195,10 @@ public function group(string $pattern, $callable): RouteGroupInterface
*/
public function redirect(string $from, $to, int $status = 302): RouteInterface
{
$handler = function () use ($to, $status) {
$response = $this->responseFactory->createResponse($status);
$responseFactory = $this->responseFactory;

$handler = function () use ($to, $status, $responseFactory) {
$response = $responseFactory->createResponse($status);
return $response->withHeader('Location', (string) $to);
};

Expand Down
26 changes: 16 additions & 10 deletions tests/Routing/RouteCollectorProxyTest.php
Expand Up @@ -12,6 +12,7 @@
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\CallableResolver;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\RouteCollectorInterface;
use Slim\Interfaces\RouteGroupInterface;
Expand Down Expand Up @@ -384,9 +385,8 @@ public function testMap()

public function testRedirect()
{

$callableResolverProphecy = $this->prophesize(CallableResolverInterface::class);
$containerProphecy = $this->prophesize(ContainerInterface::class);
$callableResolver = new CallableResolver($containerProphecy->reveal());

$from = '/from';
$to = '/to';
Expand All @@ -399,27 +399,33 @@ public function testRedirect()

$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$responseFactoryProphecy
->createResponse(302)
->willReturn($responseProphecy->reveal())
->createResponse()
->will(function () use ($responseProphecy) {
$this
->createResponse(302)
->willReturn($responseProphecy)
->shouldBeCalledOnce();
return $responseProphecy->reveal();
})
->shouldBeCalledOnce();

$routeCollector = new RouteCollector(
$responseFactoryProphecy->reveal(),
$callableResolverProphecy->reveal()
$callableResolver
);

$routeCollectorProxy = new RouteCollectorProxy(
$responseFactoryProphecy->reveal(),
$callableResolverProphecy->reveal(),
null,
$callableResolver,
$containerProphecy->reveal(),
$routeCollector
);

$route = $routeCollectorProxy->redirect($from, $to);
$routeCallable = $route->getCallable();
$routeCallable();
$request = $this->createServerRequest('/');
$response = $route->run($request);

$this->assertEquals($from, $route->getPattern());
$this->assertSame($responseProphecy->reveal(), $response);
}

public function testGroup()
Expand Down

0 comments on commit 89f8076

Please sign in to comment.