|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace FiveamCode\LaravelNotionApi\Macros; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use Illuminate\Http\Client\Request; |
| 7 | +use Illuminate\Support\Facades\File; |
| 8 | +use Illuminate\Support\Facades\Http; |
| 9 | +use Illuminate\Support\Str; |
| 10 | + |
| 11 | +class PestHttpRecorder |
| 12 | +{ |
| 13 | + public static function register() |
| 14 | + { |
| 15 | + Http::macro('recordAndFakeLater', function (array|string $urls = ['*']) { |
| 16 | + if (! is_array($urls)) { |
| 17 | + $urls = [$urls]; |
| 18 | + } |
| 19 | + |
| 20 | + $recorder = new HttpRecorder(); |
| 21 | + $httpFakeCallbacks = []; |
| 22 | + |
| 23 | + foreach ($urls as $url) { |
| 24 | + $httpFakeCallbacks[$url] = fn (Request $request) => $recorder->handle($request); |
| 25 | + } |
| 26 | + |
| 27 | + Http::fake($httpFakeCallbacks); |
| 28 | + |
| 29 | + return $recorder; |
| 30 | + }); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +class HttpRecorder |
| 35 | +{ |
| 36 | + private $snapshotDirectory = 'snapshots'; |
| 37 | + |
| 38 | + private $usePrettyJson = true; |
| 39 | + |
| 40 | + public function storeIn($directory) |
| 41 | + { |
| 42 | + $this->snapshotDirectory = $directory; |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + public function minifyJson() |
| 48 | + { |
| 49 | + $this->usePrettyJson = false; |
| 50 | + |
| 51 | + return $this; |
| 52 | + } |
| 53 | + |
| 54 | + public function handle(Request $request) |
| 55 | + { |
| 56 | + $forceRecording = in_array('--force-recording', $_SERVER['argv']); |
| 57 | + |
| 58 | + $urlInfo = parse_url($request->url()); |
| 59 | + |
| 60 | + // create specific filename for storing snapshots |
| 61 | + $method = Str::lower($request->method()); |
| 62 | + $name = Str::slug(Str::replace('/', '-', $urlInfo['path'])); |
| 63 | + $query = Str::slug(Str::replace('&', '_', Str::replace('=', '-', $urlInfo['query']))); |
| 64 | + |
| 65 | + $fileName = "{$method}_{$name}_{$query}.json"; |
| 66 | + $directoryPath = "tests/{$this->snapshotDirectory}"; |
| 67 | + $filePath = "{$directoryPath}/{$fileName}"; |
| 68 | + |
| 69 | + if ($forceRecording || ! File::exists($filePath)) { |
| 70 | + File::makeDirectory($directoryPath, 0744, true, true); |
| 71 | + |
| 72 | + $client = new Client(); |
| 73 | + $response = $client->request($request->method(), $request->url(), [ |
| 74 | + 'headers' => $request->headers(), |
| 75 | + 'body' => $request->body(), |
| 76 | + 'http_errors' => false, |
| 77 | + ]); |
| 78 | + |
| 79 | + $recordedResponse = [ |
| 80 | + 'status' => $response->getStatusCode(), |
| 81 | + 'data' => json_decode($response->getBody()->getContents(), true), |
| 82 | + ]; |
| 83 | + |
| 84 | + file_put_contents( |
| 85 | + $filePath, |
| 86 | + json_encode($recordedResponse, $this->usePrettyJson ? JSON_PRETTY_PRINT : 0) |
| 87 | + ); |
| 88 | + |
| 89 | + return Http::response($recordedResponse['data'], $response->getStatusCode()); |
| 90 | + } |
| 91 | + |
| 92 | + $preRecordedData = json_decode(file_get_contents($filePath), true); |
| 93 | + |
| 94 | + return Http::response($preRecordedData['data'], $preRecordedData['status']); |
| 95 | + } |
| 96 | +} |
0 commit comments