Skip to content

Add functionality to patch exit() on the fly - #32

Merged
kenjis merged 1 commit into
masterfrom
add-exit-patch
Jul 24, 2015
Merged

Add functionality to patch exit() on the fly#32
kenjis merged 1 commit into
masterfrom
add-exit-patch

Conversation

@kenjis

@kenjis kenjis commented Jul 23, 2015

Copy link
Copy Markdown
Owner

When a unit test exercises code that contains exit() or die() statement, the execution of the whole test suite is aborted.

If you set TestCase::$enable_patcher true, you can make all exit() and die() throw a CIPHPUnitTestExitException instead of quitting phpunit.

If a controller is like below:

    public function index()
    {
        $this->output
            ->set_status_header(200)
            ->set_content_type('application/json', 'utf-8')
            ->set_output(json_encode(['foo' => 'bar']))
            ->_display();
        exit;
    }

A test could be:

    public function test_index()
    {
        try {
            $this->request('GET', 'welcome/index');
        } catch (CIPHPUnitTestExitException $e) {
            $output = ob_get_clean();
        }
        $this->assertContains('{"foo":"bar"}', $output);
    }

This functionality is only to rescue legacy codes. I recommend not to use exit() in your code at all.

@RodolfoSilva

Copy link
Copy Markdown
Contributor

Good. 😀

@kenjis
kenjis force-pushed the add-exit-patch branch 2 times, most recently from fcbdbd9 to d708fd6 Compare July 24, 2015 00:00
kenjis added a commit that referenced this pull request Jul 24, 2015
Add functionality to patch exit() on the fly
@kenjis
kenjis merged commit 184608f into master Jul 24, 2015
@kenjis
kenjis deleted the add-exit-patch branch July 24, 2015 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants