Download the PHP package jshayes/fake-requests without Composer
On this page you can find all versions of the php package jshayes/fake-requests. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jshayes/fake-requests
More information about jshayes/fake-requests
Files in jshayes/fake-requests
Package fake-requests
Short Description A package to make it easier to test with guzzle.
License MIT
Informations about the package fake-requests
fake-requests
A simple package to make testing with guzzle easier
Basic usage
You can register expected calls to the MockHandler
. The mock handler has methods for each http request type.
This simple example creates two expectations. The first is a GET
request with the URL path of /get-request
. The second is a POST
request with a URL path of /post-request
. The client is then resolved out of the factory, and GET
and POST
requests are made for each expectation.
The ClientFactory
can be used to resolve guzzle client instances. You can bind a handler to the factory so that when it resolves the guzzle client it will swap out the default handler with the one that you have specified. It will also keep the registered middleware intact in the case that you are using the HandlerStack
.
Once an expectation is met, it is removed from the handler. So if you make the same request twice you have to add two separate expectations.
You can also use the following alternative syntax
This example sets up the same expectations as the example above.
You can also specify hosts in the uri, rather than just the path. This can allow you to ensure the correct service is being hit in the case that you talk to more than one remote service.
Inspecting the request
If you need to make assertions on the request that created, or the options that are provided, you can use the inspectRequest
method. This method receives an instance of \Psr\Http\Message\RequestInterface
as the first parameter.
Alternatively, you can use the getRequest
method to get the request off the RequestHandler
after it has been handled. This request is an instance of \JSHayes\FakeRequests\Request
, which is a decorator around the \Psr\Http\Message\RequestInterface
. This decorator exposes a few assertion helper functions. For some examples, see the following
Note that the request is null until one has been handled by the handler.
Extending the request
If you ever want to add some custom helper methods to the request, you can extend the request using the extendRequest
method. This method accepts a string that is the class name of the request class you would like to use. This extended request class must extend JSHayes\FakeRequests\Request
. Since the JSHayes\FakeRequests\Request
class extends PHPUnit\Framework\Assert
, your extended request class will have access to PHPUnit's assertion methods.
For example, you can create an extended request similar to the following
This extended request can be user as follows
Alternatively, if you do not wish to extend every request that the mock handler handles, you can extend requests on the request handler itself.
Customizing the response
There are a few ways to create a custom response for each expectation. When you create a custom response, that response is what will be returned to the guzzle client when the request expectation is met. The three ways to customize the response are as follows.
The first method is by passing in the parameters for the request. The first parameter is the status code. The second is the body of the response. The third is the array of headers to add to the response.
The second method is by creating a request that implements \Psr\Http\Message\ResponseInterface
.
The third method is by passing a callback to respondWith
. This callback will receive an instance of \JSHayes\FakeRequests\ResponseBuilder
Controlling when a handler should be handled
If you would like more control over when a RequestHandler
handles a given request, you can use the when
method. This method receives an instance of \Psr\Http\Message\RequestInterface
as the first parameter.
The handler will only handle the request when the method and uri match, and when the when
callback returns true.
Allowing Unexpected Calls
Sometimes you might want the MockHandler
to not error when it receives calls to endpoint that it did not expect calls to. In this case you can use the allowUnexpectedCalls
method on the MockHandler
In this example the GET
request to /test
will respond with a generic 200 response.
Testing with Laravel
This package also comes with a trait to make testing with Laravel a bit easier.
In this example, the fakeRequests
method created the MockHandler
for you. It will also bind it to the ClientFactory
and bind the ClientFactory
instance to the IOC. If you resolve the ClientFactory
out of the IOC in you code, this trait will allow you to easily use the MockHandler
in all of you guzzle client instances.
All versions of fake-requests with dependencies
illuminate/support Version ^6.0
phpunit/phpunit Version ~8.0