Zend Test - "Test code or tested code did not (only) close its own output buffers" is thrown
anatolykhelmer opened this issue · 6 comments
What ZF application I'm using when issue happen ?
- ^ZF2.5
- [ * ] ^ZF3
- Expressive 1
- Expressive 2
- Expressive 3
What PHP version you're using?
- PHP 5.6
- PHP 7.0
- [ * ] PHP 7.1
- PHP 7.2
What ErrorHeroModule version you're using?
- ^1.0
- [ * ] ^2.0
What Database you're using?
- [ * ] MySQL version ...
- PostgreSQL version ...
- Other (please write DB name and version)
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.
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.
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?
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();
}
Thank you!
@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.
Thanks!