I just tested the following idea:
ExitException.php:
<?php class DieException extends Exception {}
entrypoint.php:
<?php require_once 'ExitException.php'; try { require 'main_code.php'; run_main_code(); } catch (DieException $e) { return $e->getMessage(); } catch (ExitException $e) { return $e->getMessage(); }
A few include later:
deepcode.php
<?php throw new ExitException('Exiting normally');
This models exit or die , which falls to the highest level. Your program seems to come out as before, and now you can test it without killing the entire test suite. The only thing you got is that you will also catch another regular Exception in your existing code, in which case you will have to change your code to rebuild the DieException / ExitException tags until they reach the top level.
Another alternative is to return purely, which is probably due to rewriting a lot of your code. If you do not exit at this moment, why will your program generate additional results? He should check as early as possible if an "early exit" is required, and then just call this code and go to the natural end of the program.
This problem becomes much more complicated if you have to deal with third-party libraries or other code that you should not or cannot change, but if they do not come with your own unit tests, there are no tests for writing values for because ideally you should not Change third-party code to avoid future compatibility issues. A well-written third-party library should never die . It should always return control to the caller or throw an Exception that can be caught.
source share