samsonasik / ErrorHeroModule

:gem: A Hero for your Zend Framework/Laminas, and Expressive/Mezzio application to log ( DB and Mail ) and handle php errors & exceptions during Mvc process/between request and response
MIT License
50 stars 6 forks source link

Zend Test - "Test code or tested code did not (only) close its own output buffers" is thrown #63

Closed anatolykhelmer closed 5 years ago

anatolykhelmer commented 5 years ago

What ZF application I'm using when issue happen ?

What PHP version you're using?

What ErrorHeroModule version you're using?

What Database you're using?

Expected behavior

When I test the application with zend-test (controller integration tests) I expect all the tests to run without any exceptions.

Actual behavior

It throws "Test code or tested code did not (only) close its own output buffers" by PHPUnit.

Steps/Codes to reproduce the behavior

Create a simple controller integration test using zend-test.

samsonasik commented 5 years ago

In your tests setUp() function, you can exclude ErrorHeroModule from "modules" config, with assumption, your setUp() will be as follow:

    public function setUp()
    {
        // The module configuration should still be applicable for tests.
        // You can override configuration here with test case specific values,
        // such as sample view templates, path stacks, module_listener_options,
        // etc.
        $configOverrides = [];

        $appConfig = include __DIR__ . '/../../../../config/application.config.php';
        $appConfig['modules'] = \array_filter($appConfig['modules'], function($v, $k) {
            return $v !== 'ErrorHeroModule';
        }, ARRAY_FILTER_USE_BOTH);

        $this->setApplicationConfig(ArrayUtils::merge(
            $appConfig,
            $configOverrides
        ));

        parent::setUp();
    }

In above code, the $appConfig variable that passed to setApplicationConfig() already exclude the ErrorHeroModule that filtered via array_filter.

anatolykhelmer commented 5 years ago

Ok! Thanks for the fast reply! I'll use it for most of the tests! But what if I want to actually test the integration with ErrorHeroModule?

samsonasik commented 5 years ago

I think you can add \ob_get_flush(); in early in the setUp() method, eg:

    public function setUp()
    {
        \ob_get_flush();

        // The module configuration should still be applicable for tests.
        // You can override configuration here with test case specific values,
        // such as sample view templates, path stacks, module_listener_options,
        // etc.
        $configOverrides = [];

        $this->setApplicationConfig(ArrayUtils::merge(
            include __DIR__ . '/../../../../config/application.config.php',
            $configOverrides
        ));

        parent::setUp();
    }
anatolykhelmer commented 5 years ago

Thank you!

samsonasik commented 5 years ago

@anatolykhelmer I've tagged new release 2.19.0 to ensure \ob_end_flush() executed while ob_get_level() > 0 before \ob_start() to flush and turn off existing active output buffering if any.

That's should handle that automatically.

anatolykhelmer commented 5 years ago

Thanks!