Codeception / module-symfony

Codeception module for testing apps using Symfony framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

API tests stream response in terminal

lexdevelop opened this issue · comments

  • Codeception issue: My symfony controllers may return Symfony\Component\HttpFoundation\StreamedResponse which causes the problem when i run test in terminal, even without --debug or --steps i got weird characters in my terminal like:

`Codeception PHP Testing Framework v2.4.5
Powered by PHPUnit 7.3.4 by Sebastian Bergmann and contributors.

[1mApi Tests (12) [22m--------------------------------------------------------------------------------------------------------------------------------
Testing api

  • [35;1mInvoiceCest:[39;22m Test Try to get individual membership renewal link without authentication
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to get individual membership renewal link without authentication [32m(0.06s)[39m
  • [35;1mInvoiceCest:[39;22m Test membership renewal link by individual with auth
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to get individual membership renewal link [32m(14.70s)[39m
  • [35;1mInvoiceCest:[39;22m Test Try to get organization membership renewal link without authentication
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to get organization membership renewal link without authentication [32m(0.04s)[39m
  • [35;1mInvoiceCest:[39;22m Test membership renewal link by organization with auth
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to get organization membership renewal link [32m(14.35s)[39m
  • [35;1mInvoiceCest:[39;22m Test Try to check if organization invoice exists without authentication
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to check if organization invoice exists without authentication [32m(0.03s)[39m
  • [35;1mInvoiceCest:[39;22m Test does invoice exist by organization with auth
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to check if organization invoice exists [32m(14.25s)[39m
  • [35;1mInvoiceCest:[39;22m Test Try to check if individual invoice exists without authentication
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to check if individual invoice exists without authentication [32m(0.03s)[39m
  • [35;1mInvoiceCest:[39;22m Test does invoice exist by individual with auth
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to check if individual invoice exists [32m(15.56s)[39m
  • [35;1mInvoiceCest:[39;22m Test Try to get customer organization invoice without authentication
    [32;1m✔[39;22m [35;1mInvoiceCest:[39;22m Test try to get customer organization invoice without authentication [32m(0.03s)[39m
  • [35;1mInvoiceCest:[39;22m Test get member invoice by organization with auth
    %PDF-1.4
    1 0 obj
    <<
    /Title (��)
    /Creator (��wkhtmltopdf 0.12.5)
    /Producer (��Qt 4.8.7)
    /CreationDate (D:20180912115057+02'00')

endobj
3 0 obj
<<
/Type /ExtGState
/SA true
/SM 0.02
/ca 1.0
/CA 1.0
/AIS false
/SMask /None>>
endobj
4 0 obj
[/Pattern /DeviceRGB]
endobj
6 0 obj
<<
/Type /XObject`
The content is huge, this is just one part.

Test is always finished successfully, but this is really annoying output.

Can you do something with this type of response in Symfony Connector to prevent it from making an output?

@Naktibalda not sure since i'm using sendGet() from REST module.
$I->sendGET('url/i/want');
My api.suite.yml

actor: ApiTester
modules:
    enabled:
        - \Helper\Api
        - Symfony:
              app_path: 'app'
              environment: 'test'
              debug: true
        - REST:
              url: '/'
              depends: Symfony
              part: Json

REST module calls request method of Connector (indirectly).

I have try to play with doRequest() but didn't find the way to solve this.
Even when i try to run test with --silent flag i got this response, sounds like content from response populate output buffer of the tests.

Same issue, looking for resolution. Thanks,

ob_start();
$I->sendGET('link/to/streamed/response');
$content = ob_get_clean();

This way you will not get streamed response in your terminal.
Maybe we can check for such response and turn on output buffering.
Symfony connector extends \Symfony\Component\HttpKernel\Client which has protected function to support StreamedResponse.

protected function filterResponse($response)
    {
        // this is needed to support StreamedResponse
        ob_start();
        $response->sendContent();
        $content = ob_get_clean();

        return new DomResponse($content, $response->getStatusCode(), $response->headers->all());
    }

This was also asked in the Symfony repository, and in that thread the how and why is explained very well.

Some answers suggest decorating the Symfony\Component\HttpKernel\EventListener\StreamedResponseListener class.

So, 'Closing as everything is well described here, and that's how things work.'

The corresponding issue was closed in Symfony, but it should be fixed here. Please reopen this issue.

Corresponding issue was closed 2 years earlier than this issue.
Workaround was documented in the linked comment symfony/symfony#25005 (comment)

Yes, that is a workaround. But the issue is still there. And the issue is with codeception. Do you not want to fix this issue? Should the users of codeception just continue to be surprised that this does not work, search the internet for a soution, and then each have to apply this workaround? I would hope you instead would want to fix this in the codeception code.

It seems that workaroud requires configuration change in the application, so is it even possible to fix it in Codeception code?

Please raise pull request if you can get it working.