Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 24, 2015
Merged

Conversation

kenjis
Copy link
Owner

@kenjis kenjis commented Jul 23, 2015

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

None yet

2 participants