there4 / slim-test-helpers

Integration testing helpers for the Slim Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Repeated GET responses return empty body, 200 when they should 404

justinph opened this issue · comments

I'm using this library to do testing on my SLIM-based site. I have noticed that under some circumstances, the responses coming back from a GET request will be incorrect when run repeatedly.

For instance, the first response from this will function as expected:

 $this->client->get('/topic/appetites/rss');
 $body = $this->client->response->getBody();

And $body contains the expected information.

However, if you run this:

 $this->client->get('/topic/education/rss');
 $body = $this->client->response->getBody();

$body will be empty.

A similar thing happens when testing the status of responses. If the preceding responses were a 200, a subsequent response that you'd expect to be a 404 will return 200. But, if you put that 404 before the 200s, it will properly return a 404.

I think this has something to do with the output buffering (oh fun!), but I haven't been able to figure it out yet. I'm posting this in the hopes that someone else has seen the issue and has some thoughts. I'm not expecting anyone to fix this for me and I hope to be able to dig into it further in the near future.

Hi @justinph - Thanks for creating an issue for this. I haven't seen this behavior before, but certainly believe that it could be happening.

If you have a chance, can you try to create a simple test case that demonstrates this? I'd be more than happy to pull it into the repo and use it to help hunt down this issue.

I have made a test case and it's here: https://github.com/justinph/slim-test-case

Have done some cursory testing with both php5.4 and 5.5 on OS X and linux and both have the issue. Again, not expecting you to hunt this down for me, but I certainly won't turn down the help!

Hi @craig-davis and @justinph
Unfortunately, I have run into the same issue.
I'm on Windows (XAMPP) and PHP 5.6.8 (cli) (built: Apr 15 2015 15:07:09).
Have you found any solution/workaround yet?

Martin

I think I have found a workaround. It seems that it gets confused by multiple slim instances.
I created a Pull Request @justinph repository but you can also have a look at my Fork

Martin

I've the same problem, and it turns out (at least in Slim 2.6) that the request (and the response) is a singleton in the Slim app. Thus even when mocking a new \Slim\Environment, you'll always get the previous request that points to the previous environment (and thus the previous endpoint).

The only solution I found was to create a new Slim app for each request in the test. But that's not enough, because the mock happens after the singleton request is created, and thus you also end up with the previous request.

I'll try to send a PR fixing this problem ASAP.

Hi all, I've pushed PR #17 fixing this issue.
Please merge at your convenience.

Thanks!

Hi,

I think I'm still having issues regarding this bug on one of my projects.

I forked justinph's https://github.com/justinph/slim-test-case and made sure composer downloaded the latest version of slim-test-helpers which includes the above mentioned pull request by masterzen. I then ran justinph's tests and my test still fails. I was expecting them to pass? Could someone please double check if I was doing something wrong or if this issue might still be unresolved?

Thanks!

Solved my problem regarding justinph's fork. Masterzen's commit requires a change to the getSlimInstance() method used in the bootstrap file to set the slim router as a NoCacheRouter.

One suggestion would be to update the slim-unit-testing-example repository and use a NoCacheRouter in the example. Whilst it doesn't appear necessary for the tests which run in the example, it might be a good way to make people aware. I'm happy to add this and send a pull request if you think this would be useful.

This is still a issue. Consider the following code inside a test class:
`
/**
* @dataProvider stuffParamentersProvider
* @param string $language
* @param string $site
* @param integer $status
*/
public function testSearchStuff($language, $site, $status)
{
$d1 = [
'language' => $language,
'site' => $site
];
$this->client->get('/client/resource/v1/search/stuff', $d1);
$this->app->router = new NoCacheRouter($this->app->router);
$this->assertEquals($status, $this->client->response->status());
}

public function stuffParamentersProvider()
{
    return array(
    array('es','ARG',200),
    array('es','COL',200),
    array('pt','BRA',200),
    array('pt','USA',400),
    array('en','MEX',400),
    array('es','MEX',200)
  );
}

You get the first result asserted as 200 as expected, then:Failed asserting that 404 matches expected 200.`
PHP 5.5.9
Slim 2.6.3
PHPUnit 4.8.36

The only solution I have found so far is to declare the test routes on each test method and only one request per test method. Hope this helps anyone.