Last active
September 19, 2019 17:38
-
-
Save joubertredrat/90314c9ca69b3153500aae34fcf556fd to your computer and use it in GitHub Desktop.
A bizarre mock that works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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