Skip to content

Commit 1dcd71a

Browse files
authored
added new assert function assertSentCount (#15)
* feat(v1): added new assert function `assertSentCount` - Updated testing docs and reworked asserts presentation - fixed in `Response` that arguments body could be null * Apply fixes from StyleCI (#16)
1 parent ae580f6 commit 1dcd71a

File tree

9 files changed

+420
-74
lines changed

9 files changed

+420
-74
lines changed

docs/commands/overview.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Overview of Commands
22

3+
... better docs will come ;-)
4+
35
## Requirements
46

57
You need to install following package to use the code generation feature

docs/testing.md

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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+
```

docs/testing/asserts.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
1+
# Asserts
2+
13
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. You may accomplish this by calling the `Soap::assertSent` method after calling `Soap::fake`.
24

5+
## Overview
6+
7+
### assertSent
8+
9+
!!! info ""
10+
- **`Method`** : `#!php-inline function assertSent(callable $callback)`
11+
- **`Return`** : `#!php-inline void`
12+
13+
!!! example "Examples"
14+
=== "simple"
15+
``` php-inline
16+
Soap::assertSent(function($request){
17+
return $request->action() === 'YourAction'
18+
});
19+
```
20+
=== "with arguments"
21+
``` php-inline
22+
Soap::assertSent(function($request){
23+
return $request->action() === 'YourAction' &&
24+
$request->arguments() === ['argument' => 'value']
25+
});
26+
```
27+
28+
29+
## Examples
330
The `assertSent` method accepts a callback which will be given an `CodeDredd\Soap\Client\Request` instance and should return a boolean value indicating if the request matches your expectations. In order for the test to pass, at least one request must have been issued matching the given expectations:
431

532
Soap::fake();

mkdocs.yml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ site_author: 'Gregor Becker'
44
docs_dir: docs/
55
repo_name: 'codedredd/laravel-soap'
66
repo_url: 'https://github.com/codedredd/laravel-soap'
7+
8+
# Copyright
9+
copyright: Copyright © 2020 - 2020 Gregor Becker
10+
711
nav:
812
- Introduction: 'index.md'
913
- Installation: 'installation.md'
@@ -16,8 +20,26 @@ nav:
1620
- Overview: 'commands/overview.md'
1721
- 'Make Client': 'commands/make-client.md'
1822
- 'Make Validation': 'commands/make-validation.md'
19-
- Testing:
20-
- Faking: 'testing/faking.md'
21-
- Asserts: 'testing/asserts.md'
23+
- Testing: 'testing.md'
2224
theme:
2325
name: 'material'
26+
markdown_extensions:
27+
- admonition
28+
- codehilite:
29+
guess_lang: false
30+
- toc:
31+
permalink: true
32+
- pymdownx.betterem:
33+
smart_enable: all
34+
- pymdownx.superfences
35+
- pymdownx.inlinehilite
36+
- pymdownx.tabbed
37+
- pymdownx.emoji:
38+
emoji_index: !!python/name:materialx.emoji.twemoji
39+
emoji_generator: !!python/name:materialx.emoji.to_svg
40+
- pymdownx.highlight:
41+
extend_pygments_lang:
42+
- name: php-inline
43+
lang: php
44+
options:
45+
startinline: true

src/Client/Request.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function xmlContent()
6363
public function arguments(): array
6464
{
6565
$xml = SoapXml::fromString($this->xmlContent());
66+
$arguments = Arr::first(XMLSerializer::domNodeToArray($xml->getBody()));
6667

67-
return Arr::first(XMLSerializer::domNodeToArray($xml->getBody()));
68+
return $arguments ?? [];
6869
}
6970
}

src/Facades/Soap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @method static assertActionCalled(string $action)
2929
* @method static assertNothingSent()
3030
* @method static assertSequencesAreEmpty()
31+
* @method static assertSentCount($count)
3132
*/
3233
class Soap extends Facade
3334
{

0 commit comments

Comments
 (0)