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