Anti-Catch

So i’m trying to do some testing of a SOAP interface. Specifically at the moment, negative testing.

So my SUCCESS condition is that a SoapFault Exception is thrown.
try-catch works for catching the fault. But is there a mechanism for catching the opposite - that NO exception was thrown?

You’re testing it with what?

PHP and SoapClient. Basically i fire a request in with bad data, and expect a SoapFault to be thrown.

OK, but you’re not using PHPUnit or something similar? You’re writing custom code in a PHP file to do your ‘unit’ tests?

If so, well, you just can use a boolean:

$worked = false;

try {
  ... code ...
} catch (Exception ex) {
  $workd = true;
}

if (!$worked) {
  echo "didn't work";
}

Unless I misunderstood something…

Yeah, i’d considered that but it just seems a bit less readable that way. I’ll probably have to implement somewhere along those lines, though.

This sort of thing is why I haven’t missed exceptions when I moved to Go.

$expected = false;
try {
     // do your test case
} catch(SoapFault $f) {
    if($f) {// check that your fault meets your expected criteria in the if statement
        $expected = true
    }
} finally {
     if (!$expected) {
          // throw a new exception or do whatever to let your test case know it failed
     }
}

BTW, you can’t use PHP Unit?
They have exception annotations: https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions

And also a method to the same effect, void setExpectedException(string $exceptionName[, string $exceptionMessage = '', integer $exceptionCode = NULL])

See https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions.tables.api

Unfortunately for that particular project i have no control over the environment.

Even so, setExpectedException I dont think would work for the depth of error message (The message to be checked would be at a depth of like… $ex->details->Fault->PGFault->details->errormessage)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.