Skip to content

Commit

Permalink
Merge pull request #14516 from eileenmcnaughton/except
Browse files Browse the repository at this point in the history
[test] Add PrematureExitException to support unit testing where the code would otherwise exit
  • Loading branch information
totten authored Jun 13, 2019
2 parents b9ba847 + 62e245e commit eff849b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
44 changes: 44 additions & 0 deletions CRM/Core/Exception/PrematureExitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Exception thrown during tests where live code would exit.
*
* This is when the code would exit in live mode.
*
* @param string $message
* The human friendly error message.
* @param string $error_code
* A computer friendly error code. By convention, no space (but underscore allowed).
* ex: mandatory_missing, duplicate, invalid_format
* @param array $data
* Extra params to return. eg an extra array of ids. It is not mandatory, but can help the computer using the api.
* Keep in mind the api consumer isn't to be trusted. eg. the database password is NOT a good extra data.
*/
class CRM_Core_Exception_PrematureExitException extends RuntimeException {

}
5 changes: 5 additions & 0 deletions CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -1395,9 +1395,14 @@ public static function formatDocUrl($url) {
*
* @param int $status
* (optional) Code with which to exit.
*
* @throws \CRM_Core_PrematureExit_Exception
*/
public static function civiExit($status = 0) {

if (CIVICRM_UF === 'UnitTests') {
throw new CRM_Core_Exception_PrematureExitException('civiExit called');
}
if ($status > 0) {
http_response_code(500);
}
Expand Down

0 comments on commit eff849b

Please sign in to comment.