mcannucci / phnock
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
Requires
- mcannucci/aspect-override: ^0.2.0
Requires (Dev)
- guzzle/guzzle: ^3.9
- phpstan/phpstan: ^0.12.63
- phpunit/phpunit: ^8.5
- rawr/cross-data-providers: ^2.3
This package is auto-updated.
Last update: 2021-04-29 01:13:04 UTC
README
Mock network request in your PHP tests
Getting Started
Installing
composer require --dev mcannucci/phnock
Bootstraping
Within the bootstrap file include Phnock::bootstrap() method with all the directories that need to be intercepted
<?php 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"; Phnock::bootstrap([ 'directories' => [ __DIR__.'/../src', __DIR__.'/../vendor/guzzle', ] ]);
Usage
Within your testcase include the trait "Phnock\Traits\HTTPMock" and call the method "$this->matchUriWithResponse"
<?php use Phnock\Traits\HTTPMock; use PHPUnit\Framework\TestCase; class MyTest extends TestCase { use HTTPMock public function test_my_test() { $this->matchUriWithResponse('example.com', 'Intercepted!'); // Network Request to 'example.com' 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\\example.com?paramter=1 // https\\example.com?paramter=2 $this->matchUriWithResponse('/https\\\\example\.com\?parameter=.+/','Intercepted');
Possible Response Bodies
// Use a file's contents as the body $this->matchUriWithResponse( 'example.com', Phnock\ResponseTypes\FileResponse::of('filepath') ); // Use an array that will be returned as json $this->matchUriWithResponse( 'example.com', [ 'a' => [ 'b' => 'c' ] ] ); // Use an iterable to change the contents of the response by how many times it's called $this->matchUriWithResponse( 'example.com', use Phnock\ResponseTypes\IterableResponse::from(['1','2','3']) );
Development
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
Versioning
SemVer for versioning. For the versions available, see the tags on this repository.
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Acknowledgments
- Shamelessly basing the name off nock
- CodeCeption/AspectMock was the starting point for the project (and a great library)