Comments (25)
If I turn on ExitPatcher
only, no error occurs.
3 patchers:
$ ../../vendor/bin/phpunit controllers/Redirect_test.php
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
Warning: The Xdebug extension is not loaded
No code coverage will be generated.
.PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 87
.PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 84
.PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 84
.
Time: 4.27 seconds, Memory: 15.75Mb
OK (4 tests, 8 assertions)
ExitPatcher
only:
$ ../../vendor/bin/phpunit controllers/Redirect_test.php
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
Warning: The Xdebug extension is not loaded
No code coverage will be generated.
....
Time: 288 ms, Memory: 7.25Mb
OK (4 tests, 8 assertions)
And I found that when I turn on FunctionPatcher
or MethodPatcher
, the error occurs.
from ci-phpunit-test.
The Output library not resolve that?
from ci-phpunit-test.
I found the cause, but I don't know why it happens.
From the beginning, when header()
was called in redirect()
, CI's error handler catched it and discarded. Therefore the error (warning) was not outputted.
But when the error is outputted, CI's error handler does not catch it.
At least, I found workaround. So I'm going to commit it tomorrow.
from ci-phpunit-test.
All right.
from ci-phpunit-test.
I added workaround: b2ec257
And added fix to redirect()
: d394540
Because caling header()
in redirect()
is meaningless for testing.
- in php-cli,
header()
does nothing, but it causes "Cannot modify header information", because PHPUnit outputs progress info on testing already. - ci-phpunit-test has special logic for testing redirect headers.
from ci-phpunit-test.
The Output library not resolve that?
Unfortunately no. Because Output library does not manage all response headers.
In this case, redirect()
helper directly calls header()
and exit()
.
from ci-phpunit-test.
All right.
from ci-phpunit-test.
once we run PHPUnit test, PHPUnit..phar will output :
PHPUnit 4.8.9 by Sebastian Bergmann and contributors.
So ,we can't change header();
I adds "ob_start();" at "CIPHPUnitTet.php" line 2; and it works.
<?php
ob_start();
/**
* Part of CI PHPUnit Test
*
* @author Kenji Suzuki <https://github.com/kenjis>
* @license MIT License
* @copyright 2015 Kenji Suzuki
* @link https://github.com/kenjis/ci-phpunit-test
*/
class CIPHPUnitTest
is there any problem?
from ci-phpunit-test.
@lingfengchencn The error happened in your app?
In my understanding I added workround for the error, so we never see the error.
from ci-phpunit-test.
Yes, en....
wait a monent to build a demo, We need them maybe:
ob_start();
ob_end_flush();
NO,does not work...
from ci-phpunit-test.
application\controllers\Welcome.php codes:
public function index()
{
$encoding = strtolower($this->config->item('charset'));
header("Content-type: application/json; charset=$encoding", true, 200 );
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");// HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
echo '';
}
Run test\controllers\Welcome_test.php :
$output
A PHP Error was encountered
Severity: WarningMessage: Cannot modify header information - headers already sent by (output started at D:\Codes\test\system\core\Exceptions.php:272)Filename: D:\Codes\test\application\controllers\Welcome.phpLine Number: 35
Backtrace:
File: D:\Codes\test\application\controllers\Welcome.php Line: 35 Function: header
File: D:\Codes\test\application\tests\_ci_phpunit_test\CIPHPUnitTestRequest.php Line: 312 Function: call_user_func_array
File: D:\Codes\test\application\tests\_ci_phpunit_test\CIPHPUnitTestRequest.php Line: 215 Function: createAndCallController
File: D:\Codes\test\application\tests\_ci_phpunit_test\CIPHPUnitTestRequest.php Line: 126 Function: callControllerMethod
File: D:\Codes\test\application\tests\_ci_phpunit_test\CIPHPUnitTestCase.php Line: 106 Function: request
File: D:\Codes\test\application\tests\controllers\Welcome_test.php Line: 16 Function: request
from ci-phpunit-test.
Thanks. I reproduced the error.
from ci-phpunit-test.
Is there any good way to test header()
in PHPUnit?
I don't know except for @runInSeparateProcess
.
See sebastianbergmann/phpunit#720
@lingfengchencn I added test case for header()
: kenjis/ci-app-for-ci-phpunit-test@e5609c3
In your case, it is not a bug of ci-phpunit-test, but the specification of PHPUnit.
I think your code is bad coding in CodeIgniter. I recomennd you use $this->output
.
Because if you use it, you don't have to use @runInSeparateProcess
and you can test headers using $this->assertResponseHeader()
: https://github.com/kenjis/ci-phpunit-test/blob/master/docs/FunctionAndClassReference.md#testcaseassertresponseheadername-value
from ci-phpunit-test.
aha....
thinks,
from ci-phpunit-test.
I have the same error output during each test and I am not using monkey patching. Is there anything I should check?
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at phar:///usr/local/Cellar/phpunit/5.1.0/libexec/phpunit-5.1.0.phar/phpunit/Util/Printer.php:134)
Filename: /system/core/Input.php
Line Number: 410
from ci-phpunit-test.
To add a bit more context to my previous message. I have narrowed it down to usage of $this->resetInstance()
in combination with a library that calls set_cookie in it's constructor. It looks like the $this->resetInstance()
starts the output at which point no headers can be set by codeigniter in the test case. Anyone have any suggestions on how to work around this?
from ci-phpunit-test.
with a library that calls set_cookie in it's constructor
Basically, you can't call any functions which send headers during your test execution.
See sebastianbergmann/phpunit#720 (comment)
PHPUnit assumes that neither the test code nor the tested code emit output or send headers.
So the solution is not to call the setcookie()
during the test execution.
- replace the Input object with a mock
- patch
Input::set_cookie()
using monkey patching - patch
setcookie()
using monkey patching: https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/master/application/tests/controllers/Patching_on_function_test.php#L223-L233 (You need the latest ci-phpunit-testdev-master
, or configure function patcher)
But most easy workround is to use $this->warningOff()
: https://github.com/kenjis/ci-phpunit-test/blob/master/docs/FunctionAndClassReference.md#testcasewarningoff
from ci-phpunit-test.
@kenjis thanks for the feedback. I was hoping your previous statement would make it possible to use set_cookie() during test execution:
I think your code is bad coding in CodeIgniter. I recomennd you use $this->output.
Because if you use it, you don't have to use @runInSeparateProcess and you can test headers using $this->assertResponseHeader()
Note that I am using CI set_cookie not setcookie and I am using it with $this->load->view()
which I assume uses $this->output
. With those in place I should be able to avoid @runInSeparateProcess right?
Can you clarify how I would $this->assertResponseHeader()
for set_cookie per the example you provided above?
from ci-phpunit-test.
Unfortunately, you can't write tests like in the $this->output->set_header()
case, because CI_Input::set_cookie()
calls setcookie()
. That is, whichever you use CI_Input::set_cookie()
or setcookie()
, there is no difference after all. So the answer to your question is no.
To do what you want, we have to provide a special CI_Input class for testing.
from ci-phpunit-test.
Maybe this is something we should investigate. I think it would be a pretty common use case to want to test whether cookies were set by the controller under test. I'm happy to take a stab at implementing it if you can give me some direction as to what should be tweaked. It would be also nice to implement $this->assertResponseSetCookie()
or something similar.
from ci-phpunit-test.
from ci-phpunit-test.
Thanks!
from ci-phpunit-test.
I confirmed the same message without using monkey-patch.
Changed ENVIRONMENT definition from 'original-string' to 'testing' and solved it (Bootstrap.php).
from ci-phpunit-test.
error_reporting(0) will not solve the problem, but it will clean the output error
from ci-phpunit-test.
This is an old issue. If you have the same error PHP Warning: Cannot modify header information - headers already sent by (output started at ...)
, open an new issue.
from ci-phpunit-test.
Related Issues (20)
- 3.0.2 release date? HOT 2
- Coverage report is acting weird HOT 2
- Cannot create mock whose methods throw exceptions HOT 2
- How to extend the test runner for post operations? HOT 4
- 3.x new version with Updates for PHP 8.1 HOT 3
- How to stop PHP code execution when exit() in unit test? HOT 5
- Argument 1 passed to PHPUnit\Runner\TestSuiteSorter::reorderTestsInSuite() must implement interface PHPUnit\Framework\Test, instance of PHPUnit\Framework\TestSuite given HOT 3
- PHPUnit 9.5.24 + CodeIgniter 3.1.13 + PHP 8.1 HOT 2
- Session problem HOT 8
- Mocking DB object can't use getDouble? HOT 1
- How to clear RAM memory after run testcase in testsuite? HOT 3
- How to mock DB in CI? HOT 1
- PHP Fatal error: Uncaught Error: Class "TestCase" not found HOT 2
- Replacement of helper function redirect() does not work HOT 3
- cant set session $this->session->userdata HOT 1
- Upgrading to CodeIgniter4 is strongly recommended
- How to test external API with json response? HOT 1
- 1.0.2 fails with phpunit <7 when installed with "--from-composer" HOT 2
- issue during code coverage when referencing $this->uri in a view HOT 5
- Add support for php 8.2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ci-phpunit-test.