slimphp / Slim-Skeleton

Slim Framework 4 Skeleton Application
http://www.slimframework.com
MIT License
1.59k stars 479 forks source link

GET test with Query Params #198

Closed PedroPMS closed 3 years ago

PedroPMS commented 3 years ago

I'm trying to test a route with query params but it's not working

$app = $this->getAppInstance();

        $request = $this->createRequest(
            'GET',
            "BuscaPreco?identificador=$identificador&comId=$estabelecimentoId&fuelId=$combustivelId&cliId=$estabelecimentoId",
            ['Authorization' => "Bearer $this->tokenAlexGottschalk"],
        );

This is returning Not Fund, how can I test this route?

Xachman commented 3 years ago

@PedroPMS Did you have a solution for this?

Xachman commented 3 years ago
    protected function createRequest(
        string $method,
        string $path,
        string $query = "",
        array $headers = ['HTTP_ACCEPT' => 'application/json'],
        array $cookies = [],
        array $serverParams = []
    ): Request {
        $uri = new Uri('', '', 80, $path, $query);
        $handle = fopen('php://temp', 'w+');
        $stream = (new StreamFactory())->createStreamFromResource($handle);

        $h = new Headers();
        foreach ($headers as $name => $value) {
            $h->addHeader($name, $value);
        }

        return new SlimRequest($method, $uri, $h, $cookies, $serverParams, $stream);
    }

I updated the TestCase class method createRequest to accept the query.