|
1 | 1 | # :fontawesome-solid-journal-whills: **Testing**
|
2 | 2 |
|
3 |
| -Many Laravel services provide functionality to help you easily and expressively write tests, and this SOAP wrapper is no exception. |
| 3 | +--- |
| 4 | +Many Laravel services provide functionality to help you easily and expressively write tests, |
| 5 | +and this SOAP wrapper is no exception. |
4 | 6 |
|
| 7 | +--- |
| 8 | +{: style="height:auto;width:100%"} |
| 9 | + |
| 10 | +--- |
5 | 11 | ## :fontawesome-brands-jedi-order: **Faking**
|
6 | 12 |
|
7 |
| -The `Soap` facade's `fake` method allows you to instruct the SOAP client to return stubbed / dummy responses when requests are made. |
| 13 | +Include `#!php-inline use CodeDredd\Soap\Facades\Soap` in your testing class. |
| 14 | +The `Soap` facade's `fake` method allows you to instruct the SOAP client to return stubbed / dummy responses |
| 15 | +when requests are made. |
| 16 | + |
| 17 | +### :fontawesome-solid-jedi: ***fake*** |
| 18 | + |
| 19 | +> Intercepts request with possible given responses |
| 20 | +
|
| 21 | +!!! info "" |
| 22 | + - **`Method`** : `#!php-inline function fake($callback = null)` |
| 23 | + - **`Param`** : `#!php-inline callable|array $callback` |
| 24 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\SoapFactory` |
| 25 | + |
| 26 | +!!! example "Examples with Soap::response" |
| 27 | + === "simple" |
| 28 | + For returning empty `200` status code responses for every request, you may call the `fake` method with no arguments |
| 29 | + ``` php-inline |
| 30 | + Soap::fake(); |
| 31 | + ``` |
| 32 | + === "with arguments" |
| 33 | + 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 |
| 34 | + ```` php-inline |
| 35 | + Soap::fake([ |
| 36 | + // Stub a JSON response for all Get_ actions... |
| 37 | + 'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']), |
| 38 | + |
| 39 | + // Stub a string response for Submit_User action |
| 40 | + 'Submit_User' => Soap::response('Hello World', 200, ['Headers']), |
| 41 | + ]); |
| 42 | + ```` |
| 43 | + |
| 44 | + !!! warning "Difference between Laravel Http" |
| 45 | + 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! |
| 46 | + |
| 47 | + === "overwrite default response" |
| 48 | + ``` php-inline |
| 49 | + Soap::fake([ |
| 50 | + // Stub a JSON response for all Get_ actions... |
| 51 | + 'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']), |
| 52 | + |
| 53 | + // Stub a string response for all other actions |
| 54 | + '*' => Soap::response('Hello World', 200, ['Headers']), |
| 55 | + ]); |
| 56 | + ``` |
| 57 | + === "with callback" |
| 58 | + 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: |
| 59 | + ``` php-inline |
| 60 | + Soap::fake(function ($request) { |
| 61 | + return Soap::response('Hello World', 200); |
| 62 | + }); |
| 63 | + ``` |
| 64 | + |
| 65 | +!!! example "Examples with Soap::sequence" |
| 66 | + === "simple" |
| 67 | + 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: |
| 68 | + ```` php-inline |
| 69 | + Soap::fake([ |
| 70 | + // Stub a series of responses for Get_* actions... |
| 71 | + 'Get_*' => Soap::sequence() |
| 72 | + ->push('Hello World') |
| 73 | + ->push(['foo' => 'bar']) |
| 74 | + ->pushStatus(404) |
| 75 | + ]); |
| 76 | + ```` |
| 77 | + |
| 78 | + !!! warning "Throws exception if empty" |
| 79 | + When all of the responses in a response sequence have been consumed, any further requests will cause the response sequence to throw an exception! |
| 80 | + |
| 81 | + === "wtih whenEmpty" |
| 82 | + If you would like to specify a default response that should be returned when a sequence is empty, you may use the `whenEmpty` method |
| 83 | + ``` php-inline |
| 84 | + Soap::fake([ |
| 85 | + // Stub a series of responses for Get_* actions... |
| 86 | + 'Get_*' => Soap::sequence() |
| 87 | + ->push('Hello World') |
| 88 | + ->push(['foo' => 'bar']) |
| 89 | + ->whenEmpty(Soap::response()) |
| 90 | + ]); |
| 91 | + ``` |
| 92 | + |
| 93 | +### :fontawesome-solid-jedi: ***response*** |
8 | 94 |
|
9 |
| -### Simple Fake |
10 |
| -For example, to instruct the SOAP client to return empty, `200` status code responses for every request, you may call the `fake` method with no arguments: |
| 95 | +> Create a new response instance for use during stubbing (for fake responses) |
11 | 96 |
|
12 |
| - use CodeDredd\Soap\Facades\Soap; |
| 97 | +!!! info "" |
13 | 98 |
|
14 |
| - Soap::fake(); |
| 99 | + - **`Method`** : `#!php-inline static function response($body = null, $status = 200, $headers = [])` |
| 100 | + - **`Param`** : `#!php-inline array|string|null $body` |
| 101 | + - **`Param`** : `#!php-inline int $status` |
| 102 | + - **`Param`** : `#!php-inline array $headers` |
| 103 | + - **`Return`** : `#!php-inline \GuzzleHttp\Promise\PromiseInterface` |
15 | 104 |
|
16 |
| - $response = Soap::baseWsdl(...)->call(...); |
| 105 | +!!! warning "When `$body` is string" |
| 106 | + 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`. |
| 107 | + |
| 108 | + [ |
| 109 | + 'response' => 'Hello World' |
| 110 | + ] |
17 | 111 |
|
18 |
| -### Faking Specific URLs |
| 112 | +### :fontawesome-solid-jedi: ***sequence*** |
19 | 113 |
|
20 |
| -Alternatively, 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: |
21 |
| -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! |
22 |
| -Also a faked response status code is always 200 if you define it in the range between 200 and 400. Status codes 400 and greater are correct responded. |
| 114 | +> Get an invokable object that returns a sequence of responses in order for use during stubbing |
23 | 115 |
|
24 |
| - Soap::fake([ |
25 |
| - // Stub a JSON response for all Get_ actions... |
26 |
| - 'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']), |
| 116 | +!!! info "" |
| 117 | + - **`Method`** : `#!php-inline function sequence(array $responses = [])` |
| 118 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\Client\ResponseSequence` |
| 119 | + |
| 120 | +#### :fontawesome-brands-galactic-republic: ***push*** |
27 | 121 |
|
28 |
| - // Stub a string response for Submit_User action |
29 |
| - 'Submit_User' => Soap::response('Hello World', 200, ['Headers']), |
30 |
| - ]); |
| 122 | +> Push a response to the sequence. |
31 | 123 |
|
32 |
| -If you would like to overwrite the fallback ACTION pattern that will stub all unmatched URLs, you may use a single `*` character: |
| 124 | +!!! info "" |
| 125 | + - **`Method`** : `#!php-inline function push($body = '', int $status = 200, array $headers = [])` |
| 126 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\Client\ResponseSequence` |
33 | 127 |
|
34 |
| - Soap::fake([ |
35 |
| - // Stub a JSON response for all Get_ actions... |
36 |
| - 'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']), |
| 128 | +#### :fontawesome-brands-galactic-republic: ***pushResponse*** |
37 | 129 |
|
38 |
| - // Stub a string response for all other actions |
39 |
| - '*' => Soap::response('Hello World', 200, ['Headers']), |
40 |
| - ]); |
| 130 | +> Push a response to the sequence. |
41 | 131 |
|
42 |
| -One important notice. Because a SOAP API doesn't return only string every response with only a string in the body will be formatted to an array: |
| 132 | +!!! info "" |
| 133 | + - **`Method`** : `#!php-inline function pushResponse($response)` |
| 134 | + - **`Param`** : `#!php-inline \GuzzleHttp\Promise\PromiseInterface|\Closure $response` |
| 135 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\Client\ResponseSequence` |
| 136 | + |
| 137 | +#### :fontawesome-brands-galactic-republic: ***pushStatus*** |
43 | 138 |
|
44 |
| - //For above example |
45 |
| - [ |
46 |
| - 'response' => 'Hello World' |
47 |
| - ] |
| 139 | +> Push a response with the given status code to the sequence. |
48 | 140 |
|
49 |
| -### Faking Response Sequences |
| 141 | +!!! info "" |
| 142 | + - **`Method`** : `#!php-inline function pushStatus(string $filePath, int $status = 200, array $headers = [])` |
| 143 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\Client\ResponseSequence` |
| 144 | + |
| 145 | +#### :fontawesome-brands-galactic-republic: ***dontFailWhenEmpty*** |
50 | 146 |
|
51 |
| -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: |
| 147 | +> Make the sequence return a default response when it is empty. |
52 | 148 |
|
53 |
| - Soap::fake([ |
54 |
| - // Stub a series of responses for Get_* actions... |
55 |
| - 'Get_*' => Soap::sequence() |
56 |
| - ->push('Hello World') |
57 |
| - ->push(['foo' => 'bar']) |
58 |
| - ->pushStatus(404) |
59 |
| - ]); |
| 149 | +!!! info "" |
| 150 | + - **`Method`** : `#!php-inline function dontFailWhenEmpty()` |
| 151 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\Client\ResponseSequence` |
| 152 | + |
| 153 | +#### :fontawesome-brands-galactic-republic: ***whenEmpty*** |
60 | 154 |
|
61 |
| -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: |
| 155 | +> Make the sequence return a custom default response when it is empty. |
62 | 156 |
|
63 |
| - Soap::fake([ |
64 |
| - // Stub a series of responses for Get_* actions... |
65 |
| - 'Get_*' => Soap::sequence() |
66 |
| - ->push('Hello World') |
67 |
| - ->push(['foo' => 'bar']) |
68 |
| - ->whenEmpty(Soap::response()) |
69 |
| - ]); |
| 157 | +!!! info "" |
| 158 | + - **`Method`** : `#!php-inline function whenEmpty($response)` |
| 159 | + - **`Param`** : `#!php-inline \GuzzleHttp\Promise\PromiseInterface|\Closure $response` |
| 160 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\Client\ResponseSequence` |
| 161 | + |
| 162 | +#### :fontawesome-brands-galactic-republic: ***pushFile*** |
70 | 163 |
|
71 |
| -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: |
| 164 | +> Push response with the contents of a file as the body to the sequence. |
72 | 165 |
|
73 |
| - Soap::fakeSequence() |
74 |
| - ->push('Hello World') |
75 |
| - ->whenEmpty(Soap::response()); |
| 166 | +!!! info "" |
| 167 | + - **`Method`** : `#!php-inline function pushFile(int $status = 200, array $headers = [])` |
| 168 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\Client\ResponseSequence` |
76 | 169 |
|
77 |
| -### Fake Callback |
| 170 | +### :fontawesome-solid-jedi: ***fakeSequence*** |
78 | 171 |
|
79 |
| -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: |
| 172 | +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. |
80 | 173 |
|
81 |
| - Soap::fake(function ($request) { |
82 |
| - return Soap::response('Hello World', 200); |
83 |
| - }); |
| 174 | +> Register a response sequence for the given URL pattern. |
84 | 175 |
|
85 |
| ------------------------- |
| 176 | +!!! info "" |
| 177 | + - **`Method`** : `#!php-inline function fakeSequence(string $url = '*')` |
| 178 | + - **`Return`** : `#!php-inline \CodeDredd\Soap\Client\ResponseSequence` |
| 179 | + |
| 180 | +!!! example "Example" |
| 181 | + ``` php-inline |
| 182 | + Soap::fakeSequence() |
| 183 | + ->push('Hello World') |
| 184 | + ->whenEmpty(Soap::response()); |
| 185 | + ``` |
| 186 | + |
| 187 | +!!! tip "Tip" |
| 188 | + ``fakeSequence`` has the same methods as [`Soap::response`](#response). |
| 189 | + So in most cases `fakeSequence` will be the better choice to fake response because its an easier and shorter |
| 190 | + way to define fake responses. |
| 191 | + |
| 192 | +--- |
86 | 193 |
|
87 | 194 | ## :fontawesome-brands-jedi-order: **Asserts**
|
88 | 195 |
|
|
0 commit comments