Skip to content

:fontawesome-solid-journal-whills: Testing


Many Laravel services provide functionality to help you easily and expressively write tests, and this SOAP wrapper is no exception.


alt text


Faking

Include use CodeDredd\Soap\Facades\Soap in your testing class. The Soap facade's fake method allows you to instruct the SOAP client to return stubbed / dummy responses when requests are made.

fake

Intercepts request with possible given responses

  • Method : function fake($callback = null)
  • Param : callable|array $callback
  • Return : \CodeDredd\Soap\SoapFactory

Examples with Soap::response

For returning empty 200 status code responses for every request, you may call the fake method with no arguments

Soap::fake();

You may pass an array to the fake method. The array's keys should represent ACTION patterns that you wish to fake and their associated responses. The * character may be used as a wildcard character. You may use the response method to construct stub / fake responses for these endpoints

Soap::fake([
    // Stub a JSON response for all Get_ actions...
    'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']),

    // Stub a string response for Submit_User action
    'Submit_User' => Soap::response('Hello World', 200, ['Headers']),
]);

Difference between Laravel Http

The difference between Laravels HTTP wrapper is the fact that actions which are not defined in fake are also faked with a default 200 response!

Soap::fake([
    // Stub a JSON response for all Get_ actions...
    'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']),

    // Stub a string response for all other actions
    '*' => Soap::response('Hello World', 200, ['Headers']),
]);

If you require more complicated logic to determine what responses to return for certain endpoints, you may pass a callback to the fake method. This callback will receive an instance of CodeDredd\Soap\Client\Request and should return a response instance:

Soap::fake(function ($request) {
    return Soap::response('Hello World', 200);
});

Examples with Soap::sequence

Sometimes you may need to specify that a single ACTION should return a series of fake responses in a specific order. You may accomplish this by using the Soap::sequence method to build the responses:

Soap::fake([
    // Stub a series of responses for Get_* actions...
    'Get_*' => Soap::sequence()
        ->push('Hello World')
        ->push(['foo' => 'bar'])
        ->pushStatus(404)
]);

Throws exception if empty

When all of the responses in a response sequence have been consumed, any further requests will cause the response sequence to throw an exception!

If you would like to specify a default response that should be returned when a sequence is empty, you may use the whenEmpty method

Soap::fake([
    // Stub a series of responses for Get_* actions...
    'Get_*' => Soap::sequence()
        ->push('Hello World')
        ->push(['foo' => 'bar'])
        ->whenEmpty(Soap::response())
]);

response

Create a new response instance for use during stubbing (for fake responses)

  • Method : static function response($body = null, $status = 200, $headers = [])
  • Param : array|string|null $body
  • Param : int $status
  • Param : array $headers
  • Return : \GuzzleHttp\Promise\PromiseInterface

When $body is string

One important notice. Because a SOAP API doesn't return a single string value every response with only a string in the body is wrapped in an array with key response.

[
    'response' => 'Hello World'
]

sequence

Get an invokable object that returns a sequence of responses in order for use during stubbing

  • Method : function sequence(array $responses = [])
  • Return : \CodeDredd\Soap\Client\ResponseSequence

push

Push a response to the sequence.

  • Method : function push($body = '', int $status = 200, array $headers = [])
  • Return : \CodeDredd\Soap\Client\ResponseSequence

pushResponse

Push a response to the sequence.

  • Method : function pushResponse($response)
  • Param : \GuzzleHttp\Promise\PromiseInterface|\Closure $response
  • Return : \CodeDredd\Soap\Client\ResponseSequence

pushStatus

Push a response with the given status code to the sequence.

  • Method : function pushStatus(string $filePath, int $status = 200, array $headers = [])
  • Return : \CodeDredd\Soap\Client\ResponseSequence

dontFailWhenEmpty

Make the sequence return a default response when it is empty.

  • Method : function dontFailWhenEmpty()
  • Return : \CodeDredd\Soap\Client\ResponseSequence

whenEmpty

Make the sequence return a custom default response when it is empty.

  • Method : function whenEmpty($response)
  • Param : \GuzzleHttp\Promise\PromiseInterface|\Closure $response
  • Return : \CodeDredd\Soap\Client\ResponseSequence

pushFile

Push response with the contents of a file as the body to the sequence.

  • Method : function pushFile(int $status = 200, array $headers = [])
  • Return : \CodeDredd\Soap\Client\ResponseSequence

fakeSequence

If you would like to fake a sequence of responses but do not need to specify a specific ACTION pattern that should be faked, you may use the Soap::fakeSequence method.

Register a response sequence for the given URL pattern.

  • Method : function fakeSequence(string $url = '*')
  • Return : \CodeDredd\Soap\Client\ResponseSequence

Example

Soap::fakeSequence()
    ->push('Hello World')
    ->whenEmpty(Soap::response());

Tip

fakeSequence has the same methods as Soap::response. So in most cases fakeSequence will be the better choice to fake response because its an easier and shorter way to define fake responses.


Asserts

When faking responses, you may occasionally wish to inspect the requests the client receives in order to make sure your application is sending the correct data or headers.

assertSent

Assert that a request / response pair was recorded matching a given truth test.

  • Method : function assertSent(callable $callback)
  • Return : void

Examples

Soap::assertSent(function($request){
    return $request->action() === 'YourAction'
});
Soap::assertSent(function($request){
    return $request->action() === 'YourAction' &&
        $request->arguments() === ['argument' => 'value']
});
Soap::fake();

Soap::baseWsdl('https://test/v1?wsdl')->call('YourAction', ['argument' => 'value']);

Soap::assertSent(function($request){
    return $request->action() === 'YourAction' &&
        $request->arguments() === ['argument' => 'value']
});

assertNotSent

Assert that a request / response pair was not recorded matching a given truth test.

  • Method : function assertNotSent(callable $callback)
  • Return : void

Examples

Soap::assertNotSent(function($request){
    return $request->action() === 'YourAction'
});
Soap::assertNotSent(function($request){
    return $request->action() === 'YourAction' &&
        $request->arguments() === ['argument' => 'value']
});
Soap::fake();

Soap::baseWsdl('https://test/v1?wsdl')->call('YourAction', ['argument' => 'value']);

Soap::assertNotSent(function($request){
    return $request->action() === 'YourAction' &&
        $request->arguments() === ['argument' => 'NotThisValue']
});

assertActionCalled

Assert that a given soap action is called with optional arguments.

  • Method : function assertActionCalled(string $action)
  • Return : void

Examples

Soap::assertActionCalled('YourAction');
Soap::fake();

Soap::baseWsdl('https://test/v1?wsdl')->call('YourAction');

Soap::assertActionCalled('YourAction');

assertNothingSent

Assert that no request / response pair was recorded.

  • Method : function assertNothingSent()
  • Return : void

Examples

Soap::assertNothingSent();
Soap::fake();

Soap::assertNothingSent();

assertSequencesAreEmpty

Assert that every created response sequence is empty.

  • Method : function assertSequencesAreEmpty()
  • Return : void

Examples

Soap::assertSequencesAreEmpty();
Soap::fake();

Soap::assertSequencesAreEmpty();

assertSentCount

Assert how many requests have been recorded.

  • Method : function assertSentCount(int $count)
  • Return : void

Examples

Soap::assertSentCount(3);
Soap::fake();

$client = Soap::buildClient('laravlel_soap');
$response = $client->call('YourAction');
$response2 = $client->call('YourOtherAction');

Soap::assertSentCount(2);

assertSentInOrder

Assert that the given request was sent in the given order.

  • Method : function assertSentInOrder($callbacks)
  • Return : void