Skip to content
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

Open
aoculi opened this issue Oct 7, 2017 · 4 comments
Open

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

aoculi opened this issue Oct 7, 2017 · 4 comments
Assignees
Labels
Milestone

Comments

@aoculi
Copy link

aoculi commented Oct 7, 2017

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:

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

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
Copy link
Author

aoculi commented Oct 27, 2017

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

@DavidePastore
Copy link
Owner

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

@aoculi
Copy link
Author

aoculi commented Nov 6, 2017

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants