Skip to content

Instantly share code, notes, and snippets.

@joubertredrat
Last active September 19, 2019 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joubertredrat/90314c9ca69b3153500aae34fcf556fd to your computer and use it in GitHub Desktop.
Save joubertredrat/90314c9ca69b3153500aae34fcf556fd to your computer and use it in GitHub Desktop.
A bizarre mock that works
<?php
namespace Tests;
use GuzzleHttp\Client;
use Tests\TestCase;
/**
* Any Test
*
* @package Tests
*/
class AnyTest extends TestCase
{
/**
* @return void
*/
public fuction testPleaseINeedToBeHappy(): void
{
$client = self::getHttpClientMock();
$response = $client->post('', [
'json' => [
'query' => '...',
'variables' => [
'foo' => 'bar',
],
],
]);
$contentData = $response
->getBody()
->getContents()
;
/* ... */
}
/**
* @return Client
*/
public static function getHttpClientMock(): Client
{
$client = \Mockery::spy(Client::class);
$client
->shouldReceive('post')
->with(
'',
[
'json' => [
'query' => '...',
'variables' => [
'foo' => 'bar',
],
],
]
)
->andReturn(self::getResponseMock())
;
return $client;
}
/**
* @return object
*/
public static function getResponseMock()
{
return new class
{
/**
* @return object
*/
public function getBody()
{
return new class
{
/**
* @return string
*/
public function getContents(): string
{
return \json_encode([
'baz' => 'qux',
]);
}
};
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment