diff --git a/src/LaravelNotionApiServiceProvider.php b/src/LaravelNotionApiServiceProvider.php index e73af7c..20ab8ad 100644 --- a/src/LaravelNotionApiServiceProvider.php +++ b/src/LaravelNotionApiServiceProvider.php @@ -2,8 +2,8 @@ namespace FiveamCode\LaravelNotionApi; -use Illuminate\Support\ServiceProvider; use FiveamCode\LaravelNotionApi\Tests\Plugins\PestPluginHttpRecorder; +use Illuminate\Support\ServiceProvider; /** * Class LaravelNotionApiServiceProvider. diff --git a/tests/Pest.php b/tests/Pest.php index 01ce43a..f155ff0 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\Config; uses(NotionApiTest::class)->beforeEach(function () { - $dotenv = Dotenv::createImmutable(__DIR__ . '/..', '.env.testing'); + $dotenv = Dotenv::createImmutable(__DIR__.'/..', '.env.testing'); $dotenv->load(); Config::set('laravel-notion-api.notion-api-token', env('NOTION_API_TOKEN')); })->in(__DIR__); diff --git a/tests/RecordedEndpointCommentsTest.php b/tests/RecordedEndpointCommentsTest.php index 195c634..fc91707 100644 --- a/tests/RecordedEndpointCommentsTest.php +++ b/tests/RecordedEndpointCommentsTest.php @@ -1,14 +1,11 @@ expectExceptionCode(404); \Notion::comments()->ofBlock('cbf6b0af-6eaa-45ca-9715-9fa147ef6b17')->list(); -}); \ No newline at end of file +}); diff --git a/tests/plugins/PestPluginHttpRecorder.php b/tests/plugins/PestPluginHttpRecorder.php index c194183..9ca5123 100644 --- a/tests/plugins/PestPluginHttpRecorder.php +++ b/tests/plugins/PestPluginHttpRecorder.php @@ -3,18 +3,17 @@ namespace FiveamCode\LaravelNotionApi\Tests\Plugins; use GuzzleHttp\Client; -use Illuminate\Support\Str; use Illuminate\Http\Client\Request; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Str; class PestPluginHttpRecorder { public static function register() { Http::macro('recordAndFakeLater', function (array|string $urls = ['*']) { - - if (!is_array($urls)) { + if (! is_array($urls)) { $urls = [$urls]; } @@ -26,12 +25,12 @@ public static function register() }, ]); } + return $recorder; }); } } - class HttpRecorder { private $stubsFolder = '__recorded_stubs__'; @@ -41,12 +40,14 @@ class HttpRecorder public function storeIn($directory) { $this->stubsFolder = $directory; + return $this; } public function minifyJson() { $this->usePrettyJson = false; + return $this; } @@ -57,34 +58,36 @@ public function handle(Request $request) $urlInfo = parse_url($request->url()); //create specific filename for storing stubs - $filename = Str::lower($request->method()) . '_'; + $filename = Str::lower($request->method()).'_'; $filename .= Str::slug(Str::replace('/', '-', $urlInfo['path'])); - $filename .= '_' . Str::slug(Str::replace('&', '_', Str::replace('=', '-', $urlInfo['query']))); + $filename .= '_'.Str::slug(Str::replace('&', '_', Str::replace('=', '-', $urlInfo['query']))); $filename .= '.json'; - if ($forceRecording || !File::exists('tests/' . $this->stubsFolder . '/' . $filename)) { - File::makeDirectory('tests/' . $this->stubsFolder, 0777, true, true); + if ($forceRecording || ! File::exists('tests/'.$this->stubsFolder.'/'.$filename)) { + File::makeDirectory('tests/'.$this->stubsFolder, 0777, true, true); $client = new Client(); $response = $client->request($request->method(), $request->url(), [ 'headers' => $request->headers(), 'body' => $request->body(), - 'http_errors' => false + 'http_errors' => false, ]); $recordedResponse = [ 'status' => $response->getStatusCode(), - 'data' => json_decode($response->getBody()->getContents(), true) + 'data' => json_decode($response->getBody()->getContents(), true), ]; file_put_contents( - 'tests/' . $this->stubsFolder . '/' . $filename, + 'tests/'.$this->stubsFolder.'/'.$filename, json_encode($recordedResponse, $this->usePrettyJson ? JSON_PRETTY_PRINT : 0) ); + return Http::response($recordedResponse['data'], $response->getStatusCode()); } - $preRecordedData = json_decode(file_get_contents('tests/' . $this->stubsFolder . '/' . $filename), true); + $preRecordedData = json_decode(file_get_contents('tests/'.$this->stubsFolder.'/'.$filename), true); + return Http::response($preRecordedData['data'], $preRecordedData['status']); } }