Skip to content

Commit e13d965

Browse files
committed
Improve example
1 parent cc63706 commit e13d965

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,66 @@ class ExampleTest extends PHPUnit_Framework_TestCase {
218218
}
219219
```
220220

221+
### Delayed Response Usage
222+
223+
By default responses will happen instantly. If you're looking to test timeouts, the DelayedResponse response wrapper may be useful.
224+
225+
```php
226+
<?php
227+
228+
use donatj\MockWebServer\DelayedResponse;
229+
use donatj\MockWebServer\MockWebServer;
230+
use donatj\MockWebServer\Response;
231+
232+
require __DIR__ . '/../vendor/autoload.php';
233+
234+
$server = new MockWebServer;
235+
$server->start();
236+
237+
$response = new Response(
238+
'This is our http body response',
239+
[ 'Cache-Control' => 'no-cache' ],
240+
200
241+
);
242+
243+
// Wrap the response in a DelayedResponse object, which will delay the response
244+
$delayedResponse = new DelayedResponse(
245+
$response,
246+
100000 // sets a delay of 100000 microseconds (.1 seconds) before returning the response
247+
);
248+
249+
$realtimeUrl = $server->setResponseOfPath('/realtime', $response);
250+
$delayedUrl = $server->setResponseOfPath('/delayed', $delayedResponse);
251+
252+
echo "Requesting: $realtimeUrl\n\n";
253+
254+
// This request will run as quickly as possible
255+
$start = microtime(true);
256+
file_get_contents($realtimeUrl);
257+
echo "Realtime Request took: " . (microtime(true) - $start) . " seconds\n\n";
258+
259+
echo "Requesting: $delayedUrl\n\n";
260+
261+
// The request will take the delayed time + the time it takes to make and transfer the request
262+
$start = microtime(true);
263+
file_get_contents($delayedUrl);
264+
echo "Delayed Request took: " . (microtime(true) - $start) . " seconds\n\n";
265+
266+
```
267+
268+
Outputs:
269+
270+
```
271+
Requesting: http://127.0.0.1:61355/realtime
272+
273+
Realtime Request took: 0.015669107437134 seconds
274+
275+
Requesting: http://127.0.0.1:61355/delayed
276+
277+
Delayed Request took: 0.10729098320007 seconds
278+
279+
```
280+
221281
## Multiple Responses from the Same Endpoint
222282

223283
### Response Stack

example/delayed.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@
1616
);
1717

1818
// Wrap the response in a DelayedResponse object, which will delay the response
19-
$delay = new DelayedResponse(
19+
$delayedResponse = new DelayedResponse(
2020
$response,
2121
100000 // sets a delay of 100000 microseconds (.1 seconds) before returning the response
2222
);
2323

24-
$url = $server->setResponseOfPath('/delayedPath', $delay);
24+
$realtimeUrl = $server->setResponseOfPath('/realtime', $response);
25+
$delayedUrl = $server->setResponseOfPath('/delayed', $delayedResponse);
2526

26-
echo "Requesting: $url\n\n";
27+
echo "Requesting: $realtimeUrl\n\n";
28+
29+
// This request will run as quickly as possible
30+
$start = microtime(true);
31+
file_get_contents($realtimeUrl);
32+
echo "Realtime Request took: " . (microtime(true) - $start) . " seconds\n\n";
33+
34+
echo "Requesting: $delayedUrl\n\n";
2735

2836
// The request will take the delayed time + the time it takes to make and transfer the request
29-
$content = file_get_contents($url);
37+
$start = microtime(true);
38+
file_get_contents($delayedUrl);
39+
echo "Delayed Request took: " . (microtime(true) - $start) . " seconds\n\n";

mddoc.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ There has been work [started to implement this](https://github.com/donatj/mock-w
6565
<section title="PHPUnit">
6666
<source name="example/phpunit.php" lang="php"/>
6767
</section>
68+
<section title="Delayed Response Usage">
69+
<text><![CDATA[By default responses will happen instantly. If you're looking to test timeouts, the DelayedResponse response wrapper may be useful.]]></text>
70+
<source name="example/delayed.php" lang="php"/>
71+
<text>Outputs:</text>
72+
<exec cmd="php example/delayed.php" format="code-block" />
73+
</section>
6874
</section>
6975
<section title="Multiple Responses from the Same Endpoint">
7076
<section title="Response Stack">

0 commit comments

Comments
 (0)