Skip to content

Apply fixes from StyleCI #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/LaravelNotionApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace FiveamCode\LaravelNotionApi;

use Illuminate\Support\ServiceProvider;
use FiveamCode\LaravelNotionApi\Tests\Plugins\PestPluginHttpRecorder;
use Illuminate\Support\ServiceProvider;

/**
* Class LaravelNotionApiServiceProvider.
Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
5 changes: 1 addition & 4 deletions tests/RecordedEndpointCommentsTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?php

use Carbon\Carbon;
use Dotenv\Dotenv;
use FiveamCode\LaravelNotionApi\Entities\Collections\CommentCollection;
use FiveamCode\LaravelNotionApi\Entities\Comment;
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use YourVendor\YourPackage\Middleware\InterceptRequestesForRecording;

beforeEach(function () {
Http::recordAndFakeLater('https://api.notion.com/v1/comments*')
Expand Down Expand Up @@ -49,4 +46,4 @@
$this->expectExceptionCode(404);

\Notion::comments()->ofBlock('cbf6b0af-6eaa-45ca-9715-9fa147ef6b17')->list();
});
});
27 changes: 15 additions & 12 deletions tests/plugins/PestPluginHttpRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand All @@ -26,12 +25,12 @@ public static function register()
},
]);
}

return $recorder;
});
}
}


class HttpRecorder
{
private $stubsFolder = '__recorded_stubs__';
Expand All @@ -41,12 +40,14 @@ class HttpRecorder
public function storeIn($directory)
{
$this->stubsFolder = $directory;

return $this;
}

public function minifyJson()
{
$this->usePrettyJson = false;

return $this;
}

Expand All @@ -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']);
}
}