lundi 11 juillet 2016

Unit Test with Slim V3

I Have to create unit Test for my application

My Action is:

public function getHello(Request $request, Response $response, $args) {
    return $response->withJson(['bar' => 'foo']);
}

And my test is:

class OrdineTest extends PHPUnit_Framework_TestCase {

  public function testGetHello() {
    $req = $this->createRquestObject('GET', '/v1/hello');
    $req->getBody()->rewind();
    $req = $req->withHeader('Content-Type', 'application/json');
    $req = $req->withMethod('GET');

    $app = new \Slim\App();
    $path = '/v1/hello';
    $callable = function ($req, $res) {
      return $res;
    };
    $app->get($path, $callable);
    $resOut = $app($req, new \Slim\Http\Response());
    $resOut->getBody()->rewind();
    $this->assertEquals('hello', $resOut->getBody()->getContents());
  }

  private function createRquestObject($method, $url) {
    $env = Environment::mock([
            'REQUEST_URI' => $url,
            'REQUEST_METHOD' => $method,

    ]);
    $uri = Uri::createFromEnvironment($env);
    $headers = Headers::createFromEnvironment($env);
    $cookies = [];
    $serverParams = $env->all();
    $body = new RequestBody();
    $req = new \Slim\Http\Request($method, $uri, $headers, $cookies, $serverParams, $body);

    return $req;
  }


}

but $resOut->getBody()->getContents(); is Empty

Aucun commentaire:

Enregistrer un commentaire