Git Product home page Git Product logo

Comments (25)

kenjis avatar kenjis commented on May 24, 2024

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.

RodolfoSilva avatar RodolfoSilva commented on May 24, 2024

The Output library not resolve that?

from ci-phpunit-test.

kenjis avatar kenjis commented on May 24, 2024

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.

RodolfoSilva avatar RodolfoSilva commented on May 24, 2024

All right.

from ci-phpunit-test.

kenjis avatar kenjis commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

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.

RodolfoSilva avatar RodolfoSilva commented on May 24, 2024

All right.

from ci-phpunit-test.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

@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.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

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.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

Thanks. I reproduced the error.

from ci-phpunit-test.

kenjis avatar kenjis commented on May 24, 2024

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.

lingfengchencn avatar lingfengchencn commented on May 24, 2024

aha....
thinks,

from ci-phpunit-test.

esetnik avatar esetnik commented on May 24, 2024

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.

esetnik avatar esetnik commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

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.

  1. replace the Input object with a mock
  2. patch Input::set_cookie() using monkey patching
  3. 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-test dev-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.

esetnik avatar esetnik commented on May 24, 2024

@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.

kenjis avatar kenjis commented on May 24, 2024

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.

esetnik avatar esetnik commented on May 24, 2024

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.

kenjis avatar kenjis commented on May 24, 2024

@esetnik Yes, I opened #86

from ci-phpunit-test.

esetnik avatar esetnik commented on May 24, 2024

Thanks!

from ci-phpunit-test.

lf-uraku-yuki avatar lf-uraku-yuki commented on May 24, 2024

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.

MarcioQuimbundo avatar MarcioQuimbundo commented on May 24, 2024

error_reporting(0) will not solve the problem, but it will clean the output error

from ci-phpunit-test.

kenjis avatar kenjis commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.