there4 / slim-test-helpers

Integration testing helpers for the Slim Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

POST variables missing

jagandecapri opened this issue · comments

I am facing this weird problem.

I have wrote the following test.

 public function testGetTaskGroup()
 {
   $this->client->get('/task/group');
   $this->assertEquals(200, $this->client->response->status());
   $results = json_decode($this->client->response->body(), true);
 }

 public function testCreateTaskGroupItem()
 {
   $data = array(
      "name" => "first_task"
   );
   $this->client->post('/task/group', $data);
   $this->assertEquals(200, $this->client->response->status());
   $results = json_decode($this->client->response->body(), true);
 }

The POST variables in '/task/group' endpoint is missing when I retrieve it through $app->request->post() or $app->request->params() but if I comment out testGetTaskGroup, the variables for POST are appearing inside '/task/group' endpoint.

Any idea why this is happening? Thanks.

Found out the cause of the problem I was facing.

In each endpoint, I was getting the $app instance by $app = \Slim\Slim::getInstance();

E.g:

$app->requestMethod( 'endpoint', function (){
    $app = \Slim\Slim::getInstance();
    .......
});

This seems to be giving a singleton whereby the request variables set in the first API call remains in the following API calls.

Changed the function to the form

$app->requestMethod( 'endpoint', function () use ($app){
     .......
});

makes it work as desired.

Hi, I have the same problem when trying to test post services accepting json data. However, I have the definitions of the endpoints on different files, how could I pass the $app object in this scenario?

In the example, when I try to access the body of the request, I only manage to do it if it is the first request sent within the test.

app.php

$app->post('/user', 'newUser' );

user.php

function newUser() 
{
$app = \Slim\Slim::getInstance();    
$request = $app->request();

Any idea how i adapt your solution?

Regards

Hi @agarciagan

Not really sure whether this works or not though. Give it a try.

//app.php
$app->post('/user', $newUser);

//user.php
$newUser = function () use ($app){
   $request = $app->request();
};

Thanks for your help.
If I put both the function and the endpoint definition on the same file this works. However, If I keep them in two files I had to do a small change:

app.php

$app->post('/user',function () use ($app) { newUser($app); } );

user.php

function newUser($app)
{
$request = $app->request();

Finally, I forgot to mention thatr in order to be able to receive the json data from the body of the request, I had to update WebTestClient.php:

WebTestClient.php

$jsonData = false;
    if ((isset($optionalHeaders["CONTENT_TYPE"])) && ($optionalHeaders["CONTENT_TYPE"] == 'application/json'))
        $jsonData = true;
if ($jsonData) {            
$options['slim.input']   = json_encode($data);
} 

Can you think of a better way to manage to send json data within the body of the test request?

I did not modify the WebTestClient.php. In my test file I call the api endpoints like this

  .....
   $data = array( .... );
   $data = json_encode($data);
   $this->client->post('endpoint, $data);
  ....

and in the file containing the endpoints, I retrieve the data like this

    $data = $app->request->getBody();
    $data = json_decode($data, true);

Thanks a lot. I have undo my changes, recheck the code and now it is working without modifying the WebTestClient.php.

Regards

Glad to know that. 👍