PHP code example of mcannucci / phnock

1. Go to this page and download the library: Download mcannucci/phnock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mcannucci / phnock example snippets




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



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!');
 }
}

// 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');

// 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'])
);