kenjis / ci-phpunit-test

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

How to stop PHP code execution when exit() in unit test? #397

Closed i-am-nikola closed 2 years ago

i-am-nikola commented 2 years ago

ci-phpunit-test has functionality that makes all exit() and die() in your code throw CIPHPUnitTestExitException

When I use "try cache" in my code, both response and exception are returned. ex: try { $data = [123]; exit(); } catch(Exception $e) { $data = [456]; exit(); } => return "[123][456]" in my test case. I don't want this, My Expected result is return "[123]" Thanks!!!

HoangLongTrinh commented 2 years ago

@kenjis i have same issues, could you help me check it please? thank you so much!

kenjis commented 2 years ago

It seems the code does return nothing.

try {
$data = [123];
exit();
} catch(Exception $e) {
$data = [456];
exit();
}

Can you show the minimul code to reproduce your problem?

i-am-nikola commented 2 years ago

@kenjis

try {
  $this->response('Success')
} catch(Exception $e) {
   $this->response('Failed');
}

My response

function response($message) {
   // mycode
   exit();
}

It return both $this->response('Success') and $this->response('Failed'), I want return only $this->response('Success')

kenjis commented 2 years ago

See https://github.com/kenjis/ci-phpunit-test/blob/3.x/docs/HowToWriteTests.md#codeigniter-rest-server

In your case:

        try {
            echo 'Success';
            exit();
        } catch(CIPHPUnitTestExitException $e) {
            throw $e;
        } catch(Exception $e) {
            echo 'Failed';
            exit();
        }
i-am-nikola commented 2 years ago

@kenjis Good worked, My problem is solved. Thank you so much!!!