Download the PHP package fansipan/mock-client without Composer
On this page you can find all versions of the php package fansipan/mock-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fansipan/mock-client
More information about fansipan/mock-client
Files in fansipan/mock-client
Package mock-client
Short Description PSR-18 Mock HTTP Client
License MIT
Homepage https://github.com/phanxipang/mock-client
Informations about the package mock-client
Fansipan Mock HTTP Client
Fansipan mock HTTP client is PSR-18 Client implementation that provides ability send test requests with fake responses.
The MockClient
accepts Psr\Http\Message\ResponseInterface
which when used on a request, will respond with a fake response without actually sending a real request to the web. This helps speed up tests massively and can help you test your application for different API response scenarios, like a 404 error or 500 error.
Installation
You can install the package via composer:
Usage
Creating Mock Client
By default MockClient
will always return 200 - OK
status code with empty body. If you want to return a different response, create a Psr\Http\Message\ResponseInterface
instance and pass it as constructor argument. You can use MockResponse
to quickly create a fake response.
Faking Response
The MockResponse
class is used to create fake responses. It can accept a body, status, and headers. These properties will be populated in the fake response. The response body accepts an array for a JSON body or plain strings to simulate other responses, like XML.
You don't have to add
['Content-Type' => 'application/json']
header if your body is array.
If you have fixture data and don't want to create response manually, you can also use fixture
method to create a response.
If your fixture is a JSON or XML file, there's no need to add the
Content-Type
header manually.
Faking Response Sequences
Sequence faking allows you to define a number of fake responses in a specific order. It will pull out the next response in the sequence, removing it from the sequence. Each response can only be consumed once. When all the responses in a response sequence have been consumed, any further requests will cause the response sequence to throw an exception.
Faking Specific URLs
Alternatively, you may use ScopingMockClient
and pass an array to the constructor argument. The array's keys should represent URL patterns that you wish to fake and their associated responses. The *
character may be used as a wildcard character. Any requests made to URLs that have not been faked will actually be executed.
Sequence Faking also works with ScopingMockClient
Adding Expectations
When using faking responses, it's important to be able to check that a specific make request was sent and with the correct data, and headers. MockClient
& ScopingMockClient
provide you with various ways to add expectations to your tests.
Available Expectations
assertSent
assetNotSend
assertNothingSent
assertSentCount
The assertSent
/ assertNotSent
are the two most powerful expectation methods. They can accept a URL pattern or even a closure where you define if a request/response is what you expect.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Lynh
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of mock-client with dependencies
php-http/discovery Version ^1.14
psr/http-client Version ^1.0
psr/http-factory Version ^1.0