Skip to content

Commit

Permalink
Updates docs
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Aug 30, 2021
1 parent 5714893 commit a40ab97
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Latest Stable Version](https://poser.pugx.org/donatj/mock-webserver/version)](https://packagist.org/packages/donatj/mock-webserver)
[![License](https://poser.pugx.org/donatj/mock-webserver/license)](https://packagist.org/packages/donatj/mock-webserver)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/donatj/mock-webserver/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/donatj/mock-webserver)
[![Build Status](https://github.com/donatj/mock-webserver/workflows/CI/badge.svg?)](https://github.com/donatj/mock-webserver/actions?query=workflow%3ACI)
[![CI](https://github.com/donatj/mock-webserver/workflows/CI/badge.svg?)](https://github.com/donatj/mock-webserver/actions?query=workflow%3ACI)
[![Build Status](https://travis-ci.org/donatj/mock-webserver.svg?branch=master)](https://travis-ci.org/donatj/mock-webserver)


Expand Down Expand Up @@ -101,7 +101,7 @@ require __DIR__ . '/../vendor/autoload.php';
$server = new MockWebServer;
$server->start();

// We define the servers response to requests of the /definedPath endpoint
// We define the server's response to requests of the /definedPath endpoint
$url = $server->setResponseOfPath(
'/definedPath',
new Response(
Expand Down Expand Up @@ -137,6 +137,53 @@ Content-type: text/html; charset=UTF-8
This is our http body response
```

### Change Default Response

```php
<?php

use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Responses\NotFoundResponse;

require __DIR__ . '/../vendor/autoload.php';

$server = new MockWebServer;
$server->start();

// The default response is donatj\MockWebServer\Responses\DefaultResponse
// which returns an HTTP 200 and a descriptive JSON payload.
//
// Change the default response to donatj\MockWebServer\Responses\NotFoundResponse
// to get a standard 404.
//
// Any other response may be specified as default as well.
$server->setDefaultResponse(new NotFoundResponse);

$content = file_get_contents($server->getServerRoot() . '/PageDoesNotExist', false, stream_context_create([
'http' => [ 'ignore_errors' => true ] // allow reading 404s
]));

// $http_response_header is a little known variable magically defined
// in the current scope by file_get_contents with the response headers
echo implode("\n", $http_response_header) . "\n\n";
echo $content . "\n";

```

Outputs:

```
HTTP/1.0 404 Not Found
Host: 127.0.0.1:61355
Date: Mon, 30 Aug 2021 20:02:58 GMT
Connection: close
X-Powered-By: PHP/7.3.29
Content-type: text/html; charset=UTF-8
VND.DonatStudios.MockWebServer: Resource '/PageDoesNotExist' not found!
```

### PHPUnit

```php
Expand Down
18 changes: 18 additions & 0 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ Set a specified path to provide a specific response

---

### Method: MockWebServer->setDefaultResponse

```php
function setDefaultResponse(\donatj\MockWebServer\ResponseInterface $response)
```

Override the default server response, e.g. Fallback or 404

#### Parameters:

- ***\donatj\MockWebServer\ResponseInterface*** `$response`

#### Returns:

- ***void***

---

### Method: MockWebServer->getLastRequest

```php
Expand Down
5 changes: 5 additions & 0 deletions mddoc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ I would be happy to accept pull requests that correct this.
<text>Outputs:</text>
<exec cmd="php example/simple.php" format="code-block" />
</section>
<section title="Change Default Response">
<source name="example/notfound.php" lang="php"/>
<text>Outputs:</text>
<exec cmd="php example/notfound.php" format="code-block" />
</section>
<section title="PHPUnit">
<source name="example/phpunit.php" lang="php"/>
</section>
Expand Down

0 comments on commit a40ab97

Please sign in to comment.