Unit test exception-messages formatted with sprintf - php

How can I test if an exception throws the expected error message, when I format my error messages like this:
throw new \Exception(sprintf('Random string: "%s".', 'blablabla'));
Obviously testing with $this->assertEquals() won't work, since I don't know what %s is going to be. Is there a straight forward way to test Exception messages that use sprintf?

I believe that you should know what '%s' is going to be by triggering the part of your code you want an exception in, in a controlled way.
You could trigger the exceptions you are expecting, then you can use assertEquals with the error message you are expecting to be triggered

Related

PHP - Is this code guaranteed to stop if the condition isn't met?

I am only learning Exceptions and error reporting, and I am trying to understand behaviours of try/catch and perhaps other methods that I don't know yet. I have code like this:
function nameless(....) {
if(!$condition) {
throw new Exception('Condition not met');
}
[someCode ...]
return $result;
}
I want to make sure that someCode only executes if the condition is met. Would a structure like this guarantee that it?
Before talking about putting the rest of the code in an else block, or other methods, I want to know if there is some way to execute nameless() in a way that would continue its execution after it throws the exception.
Once I know that, I would like to know if there are better/worse ways of doing things and if this particular example is poor or it's one of a hundred equally valid ways to do this. Thank you.
Yes, it will stop. The behavior is defined here:
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." message, unless a handler has been defined with set_exception_handler().
Based on the definition and purpose of exceptions, the function should not continue after an exception, and you shouldn't look for a way to force that to happen.
If you have some code that must be executed even after an exception is thrown, you should not include it in the function, but instead enclose the function in a try/catch, with the necessary code in a finally block.

Throwing custom exceptions without messages

I haven't uset custom exceptions before, and now it appears that I might need them but I'm not sure I understood correctly how are they supposed to be used.
Say I have a custom exception like this:
class not_found_exception extends exception{ }
Then I have a bunch of request handler functions (I think they are called controllers in the wild). And each function can throw different types of exceptions. If the URL doesn't match it throws my custom exception, the script will catch that type of exception and skip to next request handler if it's caught. But other types of exception are not supposed to be caught because they mean that the url matches but there's some other error that should be displayed.
The thing is that my custom exception doesn't need an error message because it would not show up anywhere because its supposed to be caught, but other exceptions do. So is it ok to just throw an empty not_found_exception exception?
The short answer is simply: yes. That would be ok.
For further information about best-practices regarding Exception I'll refer you to this blog. It may be written for .NET but the theories can still be applied to PHP.
https://blogs.msdn.microsoft.com/kcwalina/2007/01/30/how-to-design-exception-hierarchies/

phpunit get exception message

I've just started with php unit.
In my test cases UsersController should return:
public function UsersController() {
....
throw new \Cake\Network\Exception\NotFoundException();
}
phpunit code
$this->setExpectedException('Cake\Network\Exception\NotFoundException');
returns an assertion failed that looks like:
App\Test\TestCase\Controller\UsersControllerTest::testActivate
Failed asserting that exception of type "Cake\Network\Exception\NotFoundException" is thrown.
Meanwhile browser return a 404 PageNotFound and $this->assertResponseOk() returns:
App\Test\TestCase\Controller\UsersControllerTest::testActivate
exception 'Cake\Network\Exception\NotFoundException' with message 'Not Found' in /vagrant/ifmx.local/src/Controller/UsersController.php:215
Does somebody know why it's happened? And is there any way to get exception message in unit test.
You seem to have misunderstood how the integration test case works.
It doesn't just wrap a call to a controller method, like $controller->action(), it simulates a whole request, and as such, exceptions are being catched, and result in error pages being rendered, just like it happens when you are visiting a URL in your browser.
So PHPUnit will never know about the exception, and thus you cannot use the expected exception feature as you would when directly testing specific code parts where exceptions bubble up to PHPUnit. If that would happen, then it wouldn't be possible to assert responses, which one might want to do, even when an exception has been thrown.
Possible exceptions are being buffered in the IntegrationTestCase::$_exception property, which you can for example use to test if, what kind of exception has been thrown, etc, like
$this->assertInstanceOf('\Cake\Network\Exception\NotFoundException', $this->_exception);
$this->assertEquals('Not Found', $this->_exception->getMessage());

Is it possible to make PHP exceptions non-fatal?

I wrote a class whose methods frequently throw a number of Exceptions--for example, a NoResultException if the given user ID doesn't correspond to a user, a BadTokenException if the token was wrong, an EmailTakenException if the e-mail address is already registered, etc.
I am trying to write a test program using it really quick. I was feeling lazy and I didn't want to write try/catch blocks. I probably will now, but I still have to ask: is it possible to change PHP's behavior and make it so that Exceptions don't produce fatal errors? In other words, can I make my script catch the Exceptions and output them using the default exception handler (since I'm using XDebug and the backtrace is nice), but allow the script to finish executing? Then I can just see the message of the error (like "The e-mail address is already taken") without stopping the rest of the page from being printed.
Thanks.
use trigger_error instead. it throws non-fatal errors
trigger_error("Custom Error", E_USER_ERROR);
display it via set_error_handler()
see
http://us1.php.net/trigger_error
and
http://us1.php.net/set_error_handler
You can only make exceptions non-fatal if you catch them through try/catch blocks and you've just said that you're too lazy to do that? This makes no sense. You either use trigger_error or you add try/catch blocks.
You can have uncaught exceptions be non-fatal by creating an set_exception_handler function to handle them. Most of the time I just show a HTTP 500 system error page. If there is a way to recover (Which I'm not sure why you wouldn't do it in a try/catch) you can do there by looking at the Exception class and the error code number in the exception.

Best possible PHP error class

I am looking for some coding ideas on the following task I am trying to achive.
I have a list of Error Numbers, Description, and User Friendly Description in a document.
Ex:
Error Number, Description, User Friendly Description
-----------------------------------------------------
1, Internal Error, "An Internal Error has occurred. Please try again later".
2, Delete Failed, "Unable to delete an Entry. Please try later".
I want to write a PHP class to store all the above in such a fashion that I can access them later with ease when an error occurs in the code..
Ex: If my code received an error 2, I want to check that error code with the list of error codes in the class, retrieve the description, user friendly description and display it to the user.
I want this to be of minimum overhead. So, don't want to store it in database.
I am using PHP5 with Zend MVC framework. Anybody can help me with the best possible sample code?
Thanks
Write an ini file with the error code and the user friendly text.
write an class which extends Exception which fetches your errorcodes from the ini file. add a method e.g.
public function getUserFriendlyMsg(){}
which returns the string from the ini file.
in your normal code when you have such an error you just need to throw the exception. e.g.
throw new My_Exception('Delete failed',2);
in your e.g. controller:
try{
// your code
}catch(My_Exception $e){
echo $e->getUserFriendlyMsg();
}
Note: you should consider extending your excpetion class to log the failures to a logfile, for this you can introduce servity levels. (see the manual - exception handling)
I like to use a simple custom error handler and custom exception handler that do the following:
If in development mode:
Show the detailed error message
If E_WARNING or worse, output error message into a log file (e.g. using Zend_Log)
If a fatal error, halt execution and show a nice error page with a full backtrace
If in production mode:
Only log error messages
On fatal errors, halt execution and show a nice "an error has occurred" page only.
I like working with errors, so any exception I catch I call a trigger_error() for to do the output and logging.
You can also extend the default Exception class to do the logging and display. You would want to turn any error that occurs into exceptions. Manual errors you would then trigger as exception using throw.
Inspiration:
Kohana's Error Handler (Screenshot here) is the nicest and greatest I've seen to date. It's open source, maybe you can even grab out that part (make sure you read the license first, though.)

Categories