This package is abandoned and no longer maintained. No replacement package was suggested.
There is no license information available for the latest version (0.1.0) of this package.

Mock HTTP requests

0.1.0 2020-12-29 15:12 UTC

This package is auto-updated.

Last update: 2021-04-29 01:13:04 UTC


Mock network request in your PHP tests

Getting Started


composer require --dev mcannucci/phnock


Within the bootstrap file include Phnock::bootstrap() method with all the directories that need to be intercepted


use Phnock\Phnock;

// options are:
// directories => which directories to mock
// temporaryFilesDir => where to save the temporary files, by default it is /tmp/
// disableCaching => whether aspect classes should use their previously creating files if there are no changes
require __DIR__ . "/../vendor/autoload.php";
  'directories' => [


Within your testcase include the trait "Phnock\Traits\HTTPMock" and call the method "$this->matchUriWithResponse"


use Phnock\Traits\HTTPMock;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
 use HTTPMock

 public function test_my_test()
   $this->matchUriWithResponse('', 'Intercepted!');
   // Network Request to '' from either curl or guzzle
   $this->assertEquals($response, 'Intercepted!');

Possible Return Types

Using a regex pattern to match urls:

// This will return 'Intercepted!' for any url that matches this regex
// Ex:
//  https\\
//  https\\

Possible Response Bodies

// Use a file's contents as the body
// Use an array that will be returned as json
  'a' => [
   'b' => 'c'
// Use an iterable to change the contents of the response by how many times it's called
 use Phnock\ResponseTypes\IterableResponse::from(['1','2','3'])


The provided docker compose setup can be use to build a development environment

docker-compose up -d
# Creating a shell inside the container
docker-compose run -rm php bash
# running phpstan and tests
composer all
# running only the tests
composer test
# running phpstan
composer analyse

Built With


SemVer for versioning. For the versions available, see the tags on this repository.


This project is licensed under the MIT License - see the file for details