Skip to content

Commit

Permalink
add FakeHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
aaemnnosttv committed Nov 8, 2019
1 parent ce0299d commit d04397d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/phpunit/includes/FakeHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Fake HTTP Client
*
* @package Google\Site_Kit\Tests
* @copyright 2019 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Tests;

use Google\Site_Kit_Dependencies\GuzzleHttp\Client;
use Google\Site_Kit_Dependencies\GuzzleHttp\Message\RequestInterface;
use Google\Site_Kit_Dependencies\GuzzleHttp\Message\Response;

/**
* Class FakeHttpClient
*/
class FakeHttpClient extends Client {
/**
* Handler function for overriding requests.
*
* @var callable
*/
protected $request_handler;

/**
* Sets the handler for all requests.
*
* @param callable $handler
*/
public function set_request_handler( callable $handler ) {
$this->request_handler = $handler;
}

/**
* @param RequestInterface $request
*
* @return \Google\Site_Kit_Dependencies\GuzzleHttp\Message\ResponseInterface
*/
public function send( RequestInterface $request ) {
if ( $this->request_handler ) {
return call_user_func( $this->request_handler, $request );
}

return new Response( 200 );
}
}

0 comments on commit d04397d

Please sign in to comment.