Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# HTTP Mock for PHP

[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/InterNations/http-mock?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/InterNations/http-mock.svg)](https://travis-ci.org/InterNations/http-mock) [![Dependency Status](https://www.versioneye.com/user/projects/53479c42fe0d0720b500006a/badge.png)](https://www.versioneye.com/user/projects/53479c42fe0d0720b500006a) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/InterNations/http-mock.svg)](http://isitmaintained.com/project/InterNations/http-mock "Average time to resolve an issue") [![Percentage of issues still open](http://isitmaintained.com/badge/open/InterNations/http-mock.svg)](http://isitmaintained.com/project/InterNations/http-mock "Percentage of issues still open")

Mock HTTP requests on the server side in your PHP unit tests.

HTTP Mock for PHP mocks the server side of an HTTP request to allow integration testing with the HTTP side.
It uses PHP’s builtin web server to start a second process that handles the mocking. The server allows
registering request matcher and responses from the client side.

*BIG FAT WARNING:* software like this is inherently insecure. Only use in trusted, controlled environments.
This is a fork of https://github.com/internations/http-mock

Its been updated to use PSR/7 Http methods, and Slim on the server side.
The API has been kept the same where possible, but any direct use of Request or Response objects
will be different.

## Usage

`composer require --dev pagely/http-mock`

Read the [docs](doc/index.md)
33 changes: 23 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "internations/http-mock",
"description": "Mock HTTP requests on the server side in your PHP unit tests",
"name": "pagely/http-mock",
"description": "Mock HTTP requests on the server side in your PHP unit tests, PSR/7 Fork of internations version",
"license": "MIT",
"authors": [
{
Expand All @@ -10,25 +10,38 @@
{
"name": "Max Beutel",
"email": "max.beutel@internations.org"
},
{
"name": "Joshua Eichorn",
"email": "joshua.eichorn@pagely.com"
}
],
"require": {
"php": "~7.1",
"silex/silex": "~2.0",
"guzzle/guzzle": ">=3.8",
"symfony/process": "~3|~4",
"php": "~7.1|~8",
"symfony/process": "~3|~4|~5",
"jeremeamia/superclosure": "~2",
"lstrojny/hmmmath": ">=0.5.0"
"lstrojny/hmmmath": ">=0.5.0",
"guzzlehttp/guzzle": "^6.3",
"slim/slim": "^3.12"
},
"require-dev": {
"internations/kodierungsregelwerksammlung": "~0.23.0",
"internations/testing-component": "1.0.1",
"phpunit/phpunit": "^7"
},
"autoload": {
"psr-4": {"InterNations\\Component\\HttpMock\\": "src/"}
"psr-4": {"Pagely\\Component\\HttpMock\\": "src/"}
},
"autoload-dev": {
"psr-4": {"InterNations\\Component\\HttpMock\\Tests\\": "tests/"}
}
"psr-4": {"Pagely\\Component\\HttpMock\\Tests\\": "tests/"}
},
"repositories": [
{
"type": "composer",
"url": "https://gdartifactory1.jfrog.io/artifactory/api/composer/composer-virt/"
},
{
"packagist.org": false
}
]
}
2 changes: 1 addition & 1 deletion doc/recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Once a SUT (system under test) has fired HTTP requests, we often want to validate that our assumption about the nature
of those requests are valid. For that purpose HTTP mock stores every request for later inspection. The recorded requests
are presented as an instance of `InterNations\Component\HttpMock\Request\UnifiedRequest`.
are presented as an instance of `Slim\Http\Response`.

```php
$this->http->mock
Expand Down
3 changes: 2 additions & 1 deletion doc/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Overview of the internal server functionality
```
POST /_expectation
{
response (required): serialized Symfony response
response (required): stringified http respopnse
responseCallback (optional): serialized closure that is passed in the response, and can return a new Response
matcher (optional): serialized list of closures
limiter (optional): serialized closure that limits the validity of the expectation
}
Expand Down
8 changes: 4 additions & 4 deletions doc/start.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Getting started with HTTP mock

HTTP mock comes out of the box with an integration with [PHPUnit](https://phpunit.de) in the shape of
`InterNations\Component\HttpMock\PHPUnit\HttpMockTrait`. In order to use it, we start and stop the background HTTP
`Pagely\Component\HttpMock\PHPUnit\HttpMockTrait`. In order to use it, we start and stop the background HTTP
server in `setUpBeforeClass()` and `tearDownAfterClass()` respectively.

```php
namespace Acme\Tests;

use InterNations\Component\HttpMock\PHPUnit\HttpMockTrait;
use Pagely\Component\HttpMock\PHPUnit\HttpMockTrait;

class ExampleTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -58,10 +58,10 @@ class ExampleTest extends PHPUnit_Framework_TestCase
->end();
$this->http->setUp();

$this->assertSame('mocked body', $this->http->client->post('http://localhost:8082/foo')->send()->getBody(true));
$this->assertSame('mocked body', (string)$this->http->client->post('http://localhost:8082/foo')->getBody());

$this->assertSame('POST', $this->http->requests->latest()->getMethod());
$this->assertSame('/foo', $this->http->requests->latest()->getPath());
$this->assertSame('/foo', $this->http->requests->latest()->getUri()->getPath());
}
}
```
13 changes: 6 additions & 7 deletions doc/stubbing.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ The example above say: when we see a `GET` request asking for `/resource` respon
What we see here is internally syntactic sugar for the following, more verbose, example using plain callbacks.

```php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

$this->http->mock
->when()
Expand All @@ -43,21 +43,20 @@ $this->http->mock
->end();
```

What we can see above is that we use standard Symfony HTTP foundation `Request` and `Response` objects. If you want to
learn more about it, look at
[Symfony’s documentation](https://symfony.com/doc/current/components/http_foundation/introduction.html).
What we can see above is that we use standard PSR/7 Request Response, we use the Guzzle implementation on the Client and the Slim implementation on the server side, hurrah standards

Let’s have a look what we can do with matching and response building shortcuts:

```php
use Symfony\Component\HttpFoundation\Response;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Http\StatusCode;

$this->http->mock
->when()
->methodIs('GET')
->pathIs('/resource')
->then()
->statusCode(Response::HTTP_NOT_FOUND)
->statusCode(StatusCode::HTTP_NOT_FOUND)
->header('X-Custom-Header', 'Header Value')
->body('response')
->end();`
Expand Down
4 changes: 4 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
if (empty($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = '/_me';
$_SERVER['REQUEST_METHOD'] = 'GET';
}
$filename = __DIR__ . preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
if (php_sapi_name() === 'cli-server' && is_file($filename)) {
return false;
Expand Down
13 changes: 9 additions & 4 deletions src/Expectation.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace InterNations\Component\HttpMock;
namespace Pagely\Component\HttpMock;

use InterNations\Component\HttpMock\Matcher\ExtractorFactory;
use InterNations\Component\HttpMock\Matcher\MatcherFactory;
use InterNations\Component\HttpMock\Matcher\MatcherInterface;
use Pagely\Component\HttpMock\Matcher\ExtractorFactory;
use Pagely\Component\HttpMock\Matcher\MatcherFactory;
use Pagely\Component\HttpMock\Matcher\MatcherInterface;
use SuperClosure\SerializableClosure;
use Closure;

Expand Down Expand Up @@ -142,6 +142,11 @@ public function getResponse()
return $this->responseBuilder->getResponse();
}

public function getResponseCallback()
{
return $this->responseBuilder->getResponseCallback();
}

public function getLimiter()
{
return new SerializableClosure($this->limiter);
Expand Down
4 changes: 2 additions & 2 deletions src/Matcher/AbstractMatcher.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace InterNations\Component\HttpMock\Matcher;
namespace Pagely\Component\HttpMock\Matcher;

use Closure;
use SuperClosure\SerializableClosure;
use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\RequestInterface as Request;

abstract class AbstractMatcher implements MatcherInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/ClosureMatcher.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace InterNations\Component\HttpMock\Matcher;
namespace Pagely\Component\HttpMock\Matcher;

use Closure;

Expand Down
18 changes: 11 additions & 7 deletions src/Matcher/ExtractorFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace InterNations\Component\HttpMock\Matcher;
namespace Pagely\Component\HttpMock\Matcher;

use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\RequestInterface as Request;

class ExtractorFactory
{
Expand All @@ -17,7 +17,7 @@ public function createPathExtractor()
$basePath = $this->basePath;

return static function (Request $request) use ($basePath) {
return substr_replace($request->getPathInfo(), '', 0, strlen($basePath));
return substr_replace($request->getUri()->getPath(), '', 0, strlen($basePath));
};
}

Expand All @@ -31,28 +31,32 @@ public function createMethodExtractor()
public function createParamExtractor($param)
{
return static function (Request $request) use ($param) {
return $request->query->get($param);
return $request->getParam($param);
};
}

public function createParamExistsExtractor($param)
{
return static function (Request $request) use ($param) {
return $request->query->has($param);
return $request->getParam($param, false) !== false;
};
}

public function createHeaderExtractor($header)
{
return static function (Request $request) use ($header) {
return $request->headers->get($header);
$r = $request->getHeaderLine($header);
if (empty($r)) {
return null;
}
return $r;
};
}

public function createHeaderExistsExtractor($header)
{
return static function (Request $request) use ($header) {
return $request->headers->has($header);
return $request->hasHeader($header);
};
}
}
2 changes: 1 addition & 1 deletion src/Matcher/MatcherFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace InterNations\Component\HttpMock\Matcher;
namespace Pagely\Component\HttpMock\Matcher;

use Closure;

Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/MatcherInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace InterNations\Component\HttpMock\Matcher;
namespace Pagely\Component\HttpMock\Matcher;

use SuperClosure\SerializableClosure;
use Closure;
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/RegexMatcher.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace InterNations\Component\HttpMock\Matcher;
namespace Pagely\Component\HttpMock\Matcher;

class RegexMatcher extends AbstractMatcher
{
Expand Down
2 changes: 1 addition & 1 deletion src/Matcher/StringMatcher.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace InterNations\Component\HttpMock\Matcher;
namespace Pagely\Component\HttpMock\Matcher;

class StringMatcher extends AbstractMatcher
{
Expand Down
6 changes: 3 additions & 3 deletions src/MockBuilder.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace InterNations\Component\HttpMock;
namespace Pagely\Component\HttpMock;

use Closure;
use InterNations\Component\HttpMock\Matcher\ExtractorFactory;
use InterNations\Component\HttpMock\Matcher\MatcherFactory;
use Pagely\Component\HttpMock\Matcher\ExtractorFactory;
use Pagely\Component\HttpMock\Matcher\MatcherFactory;

class MockBuilder
{
Expand Down
14 changes: 7 additions & 7 deletions src/PHPUnit/HttpMockFacade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace InterNations\Component\HttpMock\PHPUnit;
namespace Pagely\Component\HttpMock\PHPUnit;

use Guzzle\Http\Client;
use InterNations\Component\HttpMock\Matcher\ExtractorFactory;
use InterNations\Component\HttpMock\Matcher\MatcherFactory;
use InterNations\Component\HttpMock\MockBuilder;
use InterNations\Component\HttpMock\RequestCollectionFacade;
use InterNations\Component\HttpMock\Server;
use GuzzleHttp\Client;
use Pagely\Component\HttpMock\Matcher\ExtractorFactory;
use Pagely\Component\HttpMock\Matcher\MatcherFactory;
use Pagely\Component\HttpMock\MockBuilder;
use Pagely\Component\HttpMock\RequestCollectionFacade;
use Pagely\Component\HttpMock\Server;
use RuntimeException;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/HttpMockFacadeMap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace InterNations\Component\HttpMock\PHPUnit;
namespace Pagely\Component\HttpMock\PHPUnit;

use ArrayAccess;
use BadMethodCallException;
Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/HttpMockTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace InterNations\Component\HttpMock\PHPUnit;
namespace Pagely\Component\HttpMock\PHPUnit;

trait HttpMockTrait
{
Expand Down
4 changes: 2 additions & 2 deletions src/PHPUnit/ServerManager.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace InterNations\Component\HttpMock\PHPUnit;
namespace Pagely\Component\HttpMock\PHPUnit;

use InterNations\Component\HttpMock\Server;
use Pagely\Component\HttpMock\Server;
use SplObjectStorage;

// @codingStandardsIgnoreStart
Expand Down
Loading