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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package fake-requests

Build Status Coverage Status

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

PHP Build Version
Package Version
Requires guzzlehttp/guzzle Version ^6.3
illuminate/support Version ^6.0
phpunit/phpunit Version ~8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jshayes/fake-requests contains the following files

Loading the files please wait ....