DavidePastore / Slim-Validation

A validation library for the Slim Framework. It internally uses Respect/Validation.
170 stars 30 forks source link

Issue with $request->getAttribute('routeInfo')[2] and PHPUnit #26

Open aoculi opened 7 years ago

aoculi commented 7 years ago

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: https://github.com/DavidePastore/Slim-Validation/blob/161905b9d942a848e170a15d58dfc17826a08cc6/src/Validation.php#L102

It could be fixed with something like this:

$routeInfo = isset($request->getAttribute('routeInfo')[2]) ? $request->getAttribute('routeInfo')[2] : []; $params = array_merge((array)$routeInfo, $params);

aoculi commented 7 years ago

Hi @DavidePastore Can you merge the branch route-info-2-not-defined?

DavidePastore commented 7 years ago

Hi @aoculi , I'm trying to find a way to write a test for it. Can you help me in this sense?

aoculi commented 7 years ago

Hi @DavidePastore , ok sure. For now I didn't really had the time to work on this. For now, my test return a 500 error on my project: https://github.com/aoculi/slim-api/blob/validation-pagination/tests/app/Middlewares/TrailingSlashTest.php I've also tried to create a test on a slim-validation fork but I didn't succeed to reproduce the issue yet.

DavidePastore commented 6 years ago

Hi @aoculi, I'm trying to find a way to reproduce the same issue but without success. This is the partial result:

<?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.