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. /** * Test fix for issue #512 - thanks for reporting Bernhard H. * OODBBean::__toString() implementation only works with C_ERR_IGNORE * * @return void */ public function testToStringIssue512() { R::setErrorHandlingFUSE(\RedBeanPHP\OODBBean::C_ERR_FATAL); $boxedBean = R::dispense('boxedbean'); $str = (string) $boxedBean; asrt($str, '{"id":0}'); //no fatal error R::setErrorHandlingFUSE(FALSE); }
/** * 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); }