Skip to content

Commit

Permalink
Reset Route arguments for each call
Browse files Browse the repository at this point in the history
Fix #2430
  • Loading branch information
mathmarques committed Apr 27, 2018
1 parent e84150b commit 95585bd
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class Route extends Routable implements RouteInterface
*/
protected $arguments = [];

/**
* Route arguments parameters
*
* @var null|array
*/
protected $savedArguments;

/**
* The callable payload
*
Expand Down Expand Up @@ -286,7 +293,15 @@ public function getArgument($name, $default = null)
*/
public function prepare(ServerRequestInterface $request, array $arguments)
{
// Add the arguments
// Save the arguments added on routes
if (!isset($this->savedArguments)) {
$this->savedArguments = $this->getArguments();
}

// Reset the arguments
$this->setArguments($this->savedArguments);

// Add the route arguments
foreach ($arguments as $k => $v) {
$this->setArgument($k, $v);
}
Expand Down
96 changes: 96 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,102 @@ public function testExceptionOutputBufferingPrepend()
$this->assertStringStartsWith('output buffer test', $output);
}

public function testInvokeSequentialProccessToAPathWithOptionalArgsAndWithoutOptionalArgs()
{
$app = new App();
$app->get('/foo[/{bar}]', function ($req, $res, $args) {
return $res->write(count($args));
});

// Prepare request and response objects
$env = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'REQUEST_METHOD' => 'GET',
]);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();

// Invoke process with optional arg
$resOut = $app->process($req, $res);

$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $resOut);
$this->assertEquals('1', (string)$resOut->getBody());

// Prepare request and response objects
$env = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo',
'REQUEST_METHOD' => 'GET',
]);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();

// Invoke process with optional arg
$resOut2 = $app->process($req, $res);

$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $resOut2);
$this->assertEquals('0', (string)$resOut2->getBody());
}

public function testInvokeSequentialProccessToAPathWithOptionalArgsAndWithoutOptionalArgsAndKeepSetedArgs()
{
$app = new App();
$app->get('/foo[/{bar}]', function ($req, $res, $args) {
return $res->write(count($args));
})->setArgument('baz', 'quux');

// Prepare request and response objects
$env = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'REQUEST_METHOD' => 'GET',
]);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();

// Invoke process with optional arg
$resOut = $app->process($req, $res);

$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $resOut);
$this->assertEquals('2', (string)$resOut->getBody());

// Prepare request and response objects
$env = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo',
'REQUEST_METHOD' => 'GET',
]);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();

// Invoke process with optional arg
$resOut2 = $app->process($req, $res);

$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $resOut2);
$this->assertEquals('1', (string)$resOut2->getBody());
}

protected function skipIfPhp70()
{
if (version_compare(PHP_VERSION, '7.0', '>=')) {
Expand Down

0 comments on commit 95585bd

Please sign in to comment.