Phpunit, mocking SoapClient is problematic (mock magic methods)

I usually don't work with the \SoapClient class directly, instead I use a Client class which uses the SoapClient. For example:

class Client
{
    /**
     * @var SoapClient 
     */
    protected $soapClient;

    public function __construct(SoapClient $soapClient)
    {
        $this->soapClient = $soapClient;
    }

    public function getAuthenticateServiceSettings()
    {
        return $this->soapClient->getAuthenticateServiceSettings();
    }
}

This way is easier to mock the Client class, than mocking the SoapClient.


PHPUnit allows you to stub a web service based on a wsdl file.

$soapClientMock = $this->getMockFromWsdl('soapApiDescription.wsdl');
$soapClientMock
    ->method('getAuthenticateServiceSettings')
    ->willReturn(true);

See example here:

https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubbing-and-mocking-web-services.examples.GoogleTest.php