|
| 1 | +# :fontawesome-solid-journal-whills: **Testing** |
| 2 | + |
| 3 | +Many Laravel services provide functionality to help you easily and expressively write tests, and this SOAP wrapper is no exception. |
| 4 | + |
| 5 | +## :fontawesome-brands-jedi-order: **Faking** |
| 6 | + |
| 7 | +The `Soap` facade's `fake` method allows you to instruct the SOAP client to return stubbed / dummy responses when requests are made. |
| 8 | + |
| 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: |
| 11 | + |
| 12 | + use CodeDredd\Soap\Facades\Soap; |
| 13 | + |
| 14 | + Soap::fake(); |
| 15 | + |
| 16 | + $response = Soap::baseWsdl(...)->call(...); |
| 17 | + |
| 18 | +### Faking Specific URLs |
| 19 | + |
| 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. |
| 23 | + |
| 24 | + Soap::fake([ |
| 25 | + // Stub a JSON response for all Get_ actions... |
| 26 | + 'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']), |
| 27 | + |
| 28 | + // Stub a string response for Submit_User action |
| 29 | + 'Submit_User' => Soap::response('Hello World', 200, ['Headers']), |
| 30 | + ]); |
| 31 | + |
| 32 | +If you would like to overwrite the fallback ACTION pattern that will stub all unmatched URLs, you may use a single `*` character: |
| 33 | + |
| 34 | + Soap::fake([ |
| 35 | + // Stub a JSON response for all Get_ actions... |
| 36 | + 'Get_*' => Soap::response(['foo' => 'bar'], 200, ['Headers']), |
| 37 | + |
| 38 | + // Stub a string response for all other actions |
| 39 | + '*' => Soap::response('Hello World', 200, ['Headers']), |
| 40 | + ]); |
| 41 | + |
| 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: |
| 43 | + |
| 44 | + //For above example |
| 45 | + [ |
| 46 | + 'response' => 'Hello World' |
| 47 | + ] |
| 48 | + |
| 49 | +### Faking Response Sequences |
| 50 | + |
| 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: |
| 52 | + |
| 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 | + ]); |
| 60 | + |
| 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: |
| 62 | + |
| 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 | + ]); |
| 70 | + |
| 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: |
| 72 | + |
| 73 | + Soap::fakeSequence() |
| 74 | + ->push('Hello World') |
| 75 | + ->whenEmpty(Soap::response()); |
| 76 | + |
| 77 | +### Fake Callback |
| 78 | + |
| 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: |
| 80 | + |
| 81 | + Soap::fake(function ($request) { |
| 82 | + return Soap::response('Hello World', 200); |
| 83 | + }); |
| 84 | + |
| 85 | +------------------------ |
| 86 | + |
| 87 | +## :fontawesome-brands-jedi-order: **Asserts** |
| 88 | + |
| 89 | +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. |
| 90 | + |
| 91 | +### :fontawesome-solid-jedi: ***assertSent*** |
| 92 | + |
| 93 | +> Assert that a request / response pair was recorded matching a given truth test. |
| 94 | +
|
| 95 | +!!! info "" |
| 96 | + - **`Method`** : `#!php-inline function assertSent(callable $callback)` |
| 97 | + - **`Return`** : `#!php-inline void` |
| 98 | + |
| 99 | +!!! example "Examples" |
| 100 | + === "simple" |
| 101 | + ``` php-inline |
| 102 | + Soap::assertSent(function($request){ |
| 103 | + return $request->action() === 'YourAction' |
| 104 | + }); |
| 105 | + ``` |
| 106 | + === "with arguments" |
| 107 | + ``` php-inline |
| 108 | + Soap::assertSent(function($request){ |
| 109 | + return $request->action() === 'YourAction' && |
| 110 | + $request->arguments() === ['argument' => 'value'] |
| 111 | + }); |
| 112 | + ``` |
| 113 | + === "full" |
| 114 | + ``` php-inline |
| 115 | + Soap::fake(); |
| 116 | + |
| 117 | + Soap::baseWsdl('https://test/v1?wsdl')->call('YourAction', ['argument' => 'value']); |
| 118 | + |
| 119 | + Soap::assertSent(function($request){ |
| 120 | + return $request->action() === 'YourAction' && |
| 121 | + $request->arguments() === ['argument' => 'value'] |
| 122 | + }); |
| 123 | + ``` |
| 124 | + |
| 125 | +### :fontawesome-solid-jedi: ***assertNotSent*** |
| 126 | + |
| 127 | +> Assert that a request / response pair was not recorded matching a given truth test. |
| 128 | +
|
| 129 | +!!! info "" |
| 130 | + - **`Method`** : `#!php-inline function assertNotSent(callable $callback)` |
| 131 | + - **`Return`** : `#!php-inline void` |
| 132 | + |
| 133 | +!!! example "Examples" |
| 134 | + === "simple" |
| 135 | + ``` php-inline |
| 136 | + Soap::assertNotSent(function($request){ |
| 137 | + return $request->action() === 'YourAction' |
| 138 | + }); |
| 139 | + ``` |
| 140 | + === "with arguments" |
| 141 | + ``` php-inline |
| 142 | + Soap::assertNotSent(function($request){ |
| 143 | + return $request->action() === 'YourAction' && |
| 144 | + $request->arguments() === ['argument' => 'value'] |
| 145 | + }); |
| 146 | + ``` |
| 147 | + === "full" |
| 148 | + ``` php-inline |
| 149 | + Soap::fake(); |
| 150 | + |
| 151 | + Soap::baseWsdl('https://test/v1?wsdl')->call('YourAction', ['argument' => 'value']); |
| 152 | + |
| 153 | + Soap::assertNotSent(function($request){ |
| 154 | + return $request->action() === 'YourAction' && |
| 155 | + $request->arguments() === ['argument' => 'NotThisValue'] |
| 156 | + }); |
| 157 | + ``` |
| 158 | + |
| 159 | +### :fontawesome-solid-jedi: ***assertActionCalled*** |
| 160 | + |
| 161 | +> Assert that a given soap action is called with optional arguments. |
| 162 | +
|
| 163 | +!!! info "" |
| 164 | + - **`Method`** : `#!php-inline function assertActionCalled(string $action)` |
| 165 | + - **`Return`** : `#!php-inline void` |
| 166 | + |
| 167 | +!!! example "Examples" |
| 168 | + === "simple" |
| 169 | + ``` php-inline |
| 170 | + Soap::assertActionCalled('YourAction'); |
| 171 | + ``` |
| 172 | + === "full" |
| 173 | + ``` php-inline |
| 174 | + Soap::fake(); |
| 175 | + |
| 176 | + Soap::baseWsdl('https://test/v1?wsdl')->call('YourAction'); |
| 177 | + |
| 178 | + Soap::assertActionCalled('YourAction'); |
| 179 | + ``` |
| 180 | +### :fontawesome-solid-jedi: ***assertNothingSent*** |
| 181 | + |
| 182 | +> Assert that no request / response pair was recorded. |
| 183 | +
|
| 184 | +!!! info "" |
| 185 | + - **`Method`** : `#!php-inline function assertNothingSent()` |
| 186 | + - **`Return`** : `#!php-inline void` |
| 187 | + |
| 188 | +!!! example "Examples" |
| 189 | + === "simple" |
| 190 | + ``` php-inline |
| 191 | + Soap::assertNothingSent(); |
| 192 | + ``` |
| 193 | + === "full" |
| 194 | + ``` php-inline |
| 195 | + Soap::fake(); |
| 196 | + |
| 197 | + Soap::assertNothingSent(); |
| 198 | + ``` |
| 199 | + |
| 200 | +### :fontawesome-solid-jedi: ***assertSequencesAreEmpty*** |
| 201 | + |
| 202 | +> Assert that every created response sequence is empty. |
| 203 | +
|
| 204 | +!!! info "" |
| 205 | + - **`Method`** : `#!php-inline function assertSequencesAreEmpty()` |
| 206 | + - **`Return`** : `#!php-inline void` |
| 207 | + |
| 208 | +!!! example "Examples" |
| 209 | + === "simple" |
| 210 | + ``` php-inline |
| 211 | + Soap::assertSequencesAreEmpty(); |
| 212 | + ``` |
| 213 | + === "full" |
| 214 | + ``` php-inline |
| 215 | + Soap::fake(); |
| 216 | + |
| 217 | + Soap::assertSequencesAreEmpty(); |
| 218 | + ``` |
| 219 | + |
| 220 | +### :fontawesome-solid-jedi: ***assertSentCount*** |
| 221 | + |
| 222 | +> Assert how many requests have been recorded. |
| 223 | +
|
| 224 | +!!! info "" |
| 225 | + - **`Method`** : `#!php-inline function assertSentCount(int $count)` |
| 226 | + - **`Return`** : `#!php-inline void` |
| 227 | + |
| 228 | +!!! example "Examples" |
| 229 | + === "simple" |
| 230 | + ``` php-inline |
| 231 | + Soap::assertSentCount(3); |
| 232 | + ``` |
| 233 | + === "full" |
| 234 | + ``` php-inline |
| 235 | + Soap::fake(); |
| 236 | + |
| 237 | + $client = Soap::buildClient('laravlel_soap'); |
| 238 | + $response = $client->call('YourAction'); |
| 239 | + $response2 = $client->call('YourOtherAction'); |
| 240 | + |
| 241 | + Soap::assertSentCount(2); |
| 242 | + ``` |
0 commit comments