0

So I am trying to setup a Behat scenario that lets me test when a soap calls fails authentication. I just need to be able to test this step and then move onto other testing steps. Well I am specifically testing a call to a SOAP service without authentication (the service requires HTTP Auth). When I do this it generates a PHP Fatal error and then kills the script.

So far I have tried using "@" to suppress the error and calling error_reporting(0) to turn off error reporting during that part of the test. But it still kills the script. Is there any way around this?

public function iConnectToASoapServiceAt($soapURL) {
    $this->soapURL = $soapURL;
    $errorReporting = error_reporting(); //Save the current error reporting level
    error_reporting(0); //Turn off error reporting before we try this

    try {
        $this->client = @new Zend\Soap\Client($soapURL, $this->options);
        $this->soapFunctions = $this->client->getFunctions();
    }
    catch (\Exception $e) {
        #For testing when it fails we cannot actually throw an error here.
        #throw new Exception("Error connecting to SOAP server.");
    }

    error_reporting($errorReporting);
}

Here is the error:

PHP Fatal error:  SOAP-ERROR: Parsing WSDL: Couldn't load from

Any way around this?

Patrick
  • 3,302
  • 4
  • 28
  • 47
  • A fatal error will kill your script, doesn't matter what you do. It is not an exception so that you can catch it. In your case you should follow the code at the line where it throws the fatal error and try to handle it from there. – Twisted1919 Jun 28 '13 at 18:54

1 Answers1

1

You can't recover from a fatal error, those are, well, fatal.

From this question SoapClient error fallback in PHP you can let PHP die gracefully.

To avoid the problem completely, check that the service is available, as noted here: PHP SOAP error catching and here SoapClient error fallback in PHP

Community
  • 1
  • 1
Ast Derek
  • 2,739
  • 1
  • 20
  • 28
  • Your links actually provide two workarounds. The first is to use curl (or some other http tool) to test the connection. The second is to change the try/catch a bit and turn off xdebug. I utilized the second trick first, but now I am also looking into the first one. – Patrick Jun 28 '13 at 19:49
  • Catching non-loading WSDL in the PHP SoapClient is kind of tricky. It completely does not work if XDebug is active - this is a known bug. In this case, `file_get_contents()` the WSDL and throw an exception yourself. In any other case, you have to suppress the errors and let the SoapClient throw exceptions. Without exceptions, it will also not work. – Sven Jun 28 '13 at 20:33