kenjis / ci-phpunit-test

An easier way to use PHPUnit with CodeIgniter 3.x.
http://kenjis.github.io/ci-phpunit-test/
MIT License
586 stars 195 forks source link

Risky was occurred. #14

Closed rochefort closed 9 years ago

rochefort commented 9 years ago

I called show_404 in a controller, but risky was occurred. I should close object in show_404. Any good ideas?

message:

$ phpunit -v --debug controllers/Welcome_test.php
R

Time: 416 ms, Memory: 8.50Mb

There was 1 risky test:

1) Welcome_test::test_method_404
Test code or tested code did not (only) close its own output buffers

/Users/trsw/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:153
/Users/trsw/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:105

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 2, Risky: 1.

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done

application code:

# application/config/routes.php
$route['foo/(.*)'] = "welcome/index/$1";

# application/controllers/Welcome.php
    public function index()
    {
        $rsegment_array = $this->uri->rsegment_array();
        $method = $rsegment_array[3];
        if ($method == 'bar') {
            show_404();
        }

        $this->load->view('welcome_message');
    }

testing code:

    /**
     * @expectedException       PHPUnit_Framework_Exception
     * @expectedExceptionCode   404
     */
    public function test_method_404()
    {
        $this->request('GET', 'foo/bar');
    }
kenjis commented 9 years ago

How about this? https://github.com/kenjis/ci-phpunit-test/commit/7e256d9ba49ca77546ef73795d694c8d70f8043b

rochefort commented 9 years ago

You are great! I should have use ob_get_level like this. Thank you for your commit.

kenjis commented 9 years ago

Output buffering of PHP is stackable.

Thank you for your report!

trungdq88 commented 8 years ago

@kenjis I am having a same problem here, but my code is not simple as show_404 but really complicated. Do you have any advice on how to trace down the output buffering issue?

My test target method is using form_validation, ReCaptcha (third-party library) and bunch of models.

kenjis commented 8 years ago

The reason PHPUnit reports risky is you have output buffer which is not cleared. $this->request() calls ob_start().

You have to search ob_start() and make sure all of them are cleared and buffering is turned off.

trungdq88 commented 8 years ago

@kenjis thank you, I found the reason.

There are severals call to ob_start() in core/Exceptions.php. When there is PHP Error in the application, CI render the error template and somehow mess around with the output buffering. I fixed that by fixing the error in my application code, not sure if this is a CodeIgniter's issue or should we fix it in ci-phpunit-test.

Anyway, the risky test helped me to know if there is unexpected PHP Errors in the output, so I find it helpful in an unexpected way.