Download the PHP package aeris/guzzle-http-mock without Composer

On this page you can find all versions of the php package aeris/guzzle-http-mock. 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 guzzle-http-mock

GuzzleHttpMock

A mock library for verifying requests made with the Guzzle Http Client, and mocking responses.


Installation

Composer

You can install GuzzleHttpMock using composer:

Overview

GuzzleHttpMock allows you to setup Http request expectations, and mock responses.

How does it work?

When a GuzzleHttpMock object is attached to the Guzzle Http client, it will intercept all requests made by the client. Whenever a request is made, the mock checks the request against set expectations, and sends a response to matching requests.

Calling $httpMock->verify() checks that all expected requests have been made, and complains about any unexpected requests.

Usage

Attaching to a Guzzle Client

To start intercepting Http requests, the GuzzleHttpMock must be attached to a GuzzleClient:

Creating Request Expectations

The shouldReceiveRequest method returns a \Aeris\GuzzleHttpMock\Expectation\RequestExpectation object.

The RequestExpectation object uses withXyz methods to set expectations:

The expectation setters are chainable, allowing for a fluid interface:

Available Expectations

The following expectations are available on a \Aeris\GuzzleHttpMock\Expectation\RequestExpectation object.

Method Notes
withUrl($url:string) URL (full absolute path)
withMethod($url:string) Http method.
withQuery($query:\GuzzleHttp\Query)
withQueryParams($params:array)
withContentType($contentType:string)
withJsonContentType()
withBody($stream:StreamInterface)
withBodyParams($params:array)
withJsonBodyParams($params:array)
once() The request should be made a single time
times($callCount:number) The request should be made $callCount times.

Default Expectations

By default, a request is expected to be made one time, with an Http method of 'GET'.

Directly Setting an Expected Request

In addition to specifying request expectations individually, you can also directly set a \GuzzleHttp\Message\RequestInterface object as an expectation.

Custom Expectations

All expectation methods accept either a value or a callable as a parameter. By passing a callable, you can create custom expectations. For example:

In this case, the expectation will fail if the actual request body has a foo params which does not equal bar.

GuzzleHttpMock provides some built-in custom expectations, as well. For example:

Mocking Responses

When a request is made which matches an expectation, the GuzzleHttpMock will intercept the request, and respond with a mock response.

Available Responses

The following methods are available for mocking responses:

Method Notes
andRespondWith($response:\GuzzleHttp\Message\ResponseInterface) See Directly Setting a Mock Response
andRespondWithContent($data:array, $statusCode:string) Sets the response body
andRespondWithJson($data:array, $statCode:String) Sets a JSON response body

Directly Setting a Mock Response

You may mock a response directly using a response object:

Verifying Expectations

Expectations may be verified using the \Aeris\GuzzleHttpMock::verify() method.

With PHPUnit

When using GuzzleHttpMock with PHPUnit, make sure to add Mock::verify() to your teardown:

Gotchyas

We have used GuzzleHttpMock enough internally to feel comfortable using it on production projects, but also enough to know that there are a few "gotchyas". Hopefully, knowing these issues up-front will prevent much conflict between your forehead and your desk.

If you'd like to take a shot at resolving any of these issues, take a look at our contribution guidelines.

Unspecified expectations

In the current version of GuzzleHttpMock, any expectations which are not specified will result in a failed request.

You might argue that it would make more sense for the RequestExpectation to accept any value for unspecified expectations by default. And you might be right. Future versions of GuzzleHttpMock may do just that.

Where's my UnexpectedRequestException?

There are a couple of possible culprits here:

  1. Make sure you're calling Mock::verify(). If you're using a testing framework (eg PHPUnit), you can put verify() in the tearDown method.

  2. Another exception may be thrown before you had a chance to verify your request expectations.

Solving #2 can be a little tricky. If a RequestExpectation cannot be matched, GuzzleHttpClient will not respond with your mock response, which may cause other code to break before you have a chance to call verify().

If you're calling verify() in your test tearDown, you may want to try adding another verify() call immediately after the http request is made.

You can also try wrapping the offending code in a try...catch block, to give the UnexpectedRequestException priority.

That's more verbosity than you may want in all of your tests, but it can be helpful if you're debugging.

Why's it doing that thing I don't think it should do?

I don't know. That's really weird. Bummer...

Hey, why don't you open a new issue and tell us about it? Maybe we can help.

Contributing

For that warm fuzzy open-sourcey feeling, contribute to GuzzleHttpMock today!

We only ask that you include PHPUnit tests, and update documentation as needed. Also, if it's not an open issue or on our wish list, you might want to open an issue first, to make sure you're headed in the right direction.

Wish List

Take a look at the "Gotchyas" section for some things that could be fixed. Have another idea? Open an issue, and we'll talk.


All versions of guzzle-http-mock with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.3
guzzlehttp/guzzle Version ~5.0.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 aeris/guzzle-http-mock contains the following files

Loading the files please wait ....