Esempio n. 1
0
 /**
  * Sets the error mode for FUSE.
  * What to do if a FUSE model method does not exist?
  * You can set the following options:
  *
  * OODBBean::C_ERR_IGNORE (default), ignores the call, returns NULL
  * OODBBean::C_ERR_LOG, logs the incident using error_log
  * OODBBean::C_ERR_NOTICE, triggers a E_USER_NOTICE
  * OODBBean::C_ERR_WARN, triggers a E_USER_WARNING
  * OODBBean::C_ERR_EXCEPTION, throws an exception
  * OODBBean::C_ERR_FUNC, allows you to specify a custom handler (function)
  * OODBBean::C_ERR_FATAL, triggers a E_USER_ERROR
  *
  * Custom handler method signature: handler( array (
  * 	'message' => string
  * 	'bean' => OODBBean
  * 	'method' => string
  * ) )
  *
  * This method returns the old mode and handler as an array.
  *
  * @param integer       $mode mode, determines how to handle errors
  * @param callable|NULL $func custom handler (if applicable)
  *
  * @return array
  */
 public static function setErrorHandlingFUSE($mode, $func = NULL)
 {
     return OODBBean::setErrorHandlingFUSE($mode, $func);
 }
Esempio n. 2
0
 /**
  * Test error handling options FUSE.
  */
 public function testModelErrorHandling()
 {
     $test = R::dispense('feed');
     $test->nonExistantMethod();
     pass();
     $old = R::setErrorHandlingFUSE(OODBBean::C_ERR_LOG);
     asrt(is_array($old), TRUE);
     asrt(count($old), 2);
     asrt($old[0], FALSE);
     asrt($old[1], NULL);
     $test->nonExistantMethod();
     //we cant really test this... :(
     pass();
     $old = R::setErrorHandlingFUSE(OODBBean::C_ERR_NOTICE);
     asrt(is_array($old), TRUE);
     asrt(count($old), 2);
     asrt($old[0], OODBBean::C_ERR_LOG);
     asrt($old[1], NULL);
     set_error_handler(function ($error, $str) {
         asrt($str, 'FUSE: method does not exist in model: nonExistantMethod');
     }, E_USER_NOTICE);
     $test->nonExistantMethod();
     restore_error_handler();
     $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_WARN);
     asrt(is_array($old), TRUE);
     asrt(count($old), 2);
     asrt($old[0], OODBBean::C_ERR_NOTICE);
     asrt($old[1], NULL);
     set_error_handler(function ($error, $str) {
         asrt($str, 'FUSE: method does not exist in model: nonExistantMethod');
     }, E_USER_WARNING);
     $test->nonExistantMethod();
     restore_error_handler();
     $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_FATAL);
     asrt(is_array($old), TRUE);
     asrt(count($old), 2);
     asrt($old[0], OODBBean::C_ERR_WARN);
     asrt($old[1], NULL);
     set_error_handler(function ($error, $str) {
         asrt($str, 'FUSE: method does not exist in model: nonExistantMethod');
     }, E_USER_ERROR);
     $test->nonExistantMethod();
     restore_error_handler();
     $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_EXCEPTION);
     asrt(is_array($old), TRUE);
     asrt(count($old), 2);
     asrt($old[0], OODBBean::C_ERR_FATAL);
     asrt($old[1], NULL);
     try {
         $test->nonExistantMethod();
         fail();
     } catch (\Exception $e) {
         pass();
     }
     global $test_bean;
     $test_bean = $test;
     global $has_executed_error_func_fuse;
     $has_executed_error_func_fuse = FALSE;
     $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_FUNC, function ($info) {
         global $has_executed_error_func_fuse;
         global $test_bean;
         $has_executed_error_func_fuse = TRUE;
         asrt(is_array($info), TRUE);
         asrt($info['method'], 'nonExistantMethod');
         asrt(json_encode($info['bean']->export()), json_encode($test_bean->export()));
         asrt($info['message'], 'FUSE: method does not exist in model: nonExistantMethod');
     });
     asrt(is_array($old), TRUE);
     asrt(count($old), 2);
     asrt($old[0], OODBBean::C_ERR_EXCEPTION);
     asrt($old[1], NULL);
     $test->nonExistantMethod();
     asrt($has_executed_error_func_fuse, TRUE);
     $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_IGNORE);
     asrt(is_array($old), TRUE);
     asrt(count($old), 2);
     asrt($old[0], OODBBean::C_ERR_FUNC);
     asrt(is_callable($old[1]), TRUE);
     $old = OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_IGNORE);
     asrt(is_array($old), TRUE);
     asrt(count($old), 2);
     asrt($old[0], OODBBean::C_ERR_IGNORE);
     asrt($old[1], NULL);
     try {
         OODBBean::setErrorHandlingFUSE(900);
         fail();
     } catch (\Exception $e) {
         pass();
         asrt($e->getMessage(), 'Invalid error mode selected');
     }
     try {
         OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_FUNC, 'hello');
         fail();
     } catch (\Exception $e) {
         pass();
         asrt($e->getMessage(), 'Invalid error handler');
     }
     OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_EXCEPTION);
     //make sure ignore FUSE events
     $test = R::dispense('feed');
     R::store($test);
     $test = $test->fresh();
     R::trash($test);
     pass();
     OODBBean::setErrorHandlingFUSE(OODBBean::C_ERR_IGNORE);
 }