-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with $request->getAttribute('routeInfo')[2] and PHPUnit #26
Comments
Hi @DavidePastore |
Hi @aoculi , I'm trying to find a way to write a test for it. Can you help me in this sense? |
Hi @DavidePastore , ok sure. For now I didn't really had the time to work on this. |
Hi @aoculi, I'm trying to find a way to reproduce the same issue but without success. <?php
namespace DavidePastore\Slim\Validation\Tests;
use Slim\App;
use Slim\Http\Body;
use Slim\Http\Environment;
use Slim\Http\Headers;
use Slim\Http\Request;
use Slim\Http\RequestBody;
use Slim\Http\Response;
use Slim\Http\Uri;
use DavidePastore\Slim\Validation\Validation;
use Respect\Validation\Validator as v;
class TrailingSlashTest extends \PHPUnit_Framework_TestCase
{
public function testNotExistingUrl()
{
//NEW
$app = new App([
'settings' => [
'displayErrorDetails' => true
],
]);
// Prepare request and response objects
$env = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/not-existing-url',
'REQUEST_METHOD' => 'GET',
]);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();
$app->getContainer()['request'] = $req;
$app->getContainer()['response'] = $res;
$mw = new Validation(array(
'username' => v::alnum()->noWhitespace()->length(1, 15),
));
$app->add($mw);
$app->get('/foo', function ($req, $res) {
return $res;
});
$resOut = $app->run(true);
$this->assertEquals(404, $resOut->getStatusCode());
//$this->assertRegExp('/.*middleware exception.*/', (string)$resOut);
}
} It just works, even in the last official release of Slim-Validation. Can you please create a simple minimal project where I can see the problem? If you can't do it, I suppose this is something not related to this middleware. |
With PHPUnit, if you try to test a not implemented route, Slim Validation will return an error (500), because $request->getAttribute('routeInfo')[2] is not defined here:
Slim-Validation/src/Validation.php
Line 102 in 161905b
It could be fixed with something like this:
$routeInfo = isset($request->getAttribute('routeInfo')[2]) ? $request->getAttribute('routeInfo')[2] : [];
$params = array_merge((array)$routeInfo, $params);
The text was updated successfully, but these errors were encountered: