-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchesRequestSnapshots.php
48 lines (36 loc) · 1.42 KB
/
MatchesRequestSnapshots.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php declare(strict_types=1);
namespace ericnorris\GCPAuthContrib\Tests;
use GuzzleHttp\ClientInterface;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Spatie\Snapshots\MatchesSnapshots;
trait MatchesRequestSnapshots {
use MatchesSnapshots;
/** @var Request[] */
private $requestHistory = [];
/**
* @var Response[] $recordedResponses An array of responses that will be replayed to the caller
*
* @return ClientInterface
*/
protected function makeSnapshotClient(array $recordedResponses): ClientInterface {
$historyMiddleware = \GuzzleHttp\Middleware::history($this->requestHistory);
$fakeHandler = new \GuzzleHttp\Handler\MockHandler($recordedResponses);
$fakeStack = \GuzzleHttp\HandlerStack::create($fakeHandler);
$fakeStack->push($historyMiddleware);
return new \GuzzleHttp\Client([
"handler" => $fakeStack,
"headers" => [
"user-agent" => "ericnorris/gcp-auth-contrib unit test"
],
]);
}
protected function assertRequestHistoryMatchesSnapshot() {
foreach (array_column($this->requestHistory, "request") as $request) {
$this->assertMatchesSnapshot(\GuzzleHttp\Psr7\str($request));
}
}
protected function getSnapshotDirectory(): string {
return __DIR__ . "/../snapshots";
}
}