From 70ea5de0c140d4147d1f510b003652842a95f4b6 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Tue, 30 Jul 2024 10:57:15 +0200 Subject: [PATCH 1/4] Fix body data contents --- composer.json | 2 +- src/Context/RequestContextProvider.php | 19 ++++++- .../Concerns/MatchesCodeSnippetSnapshots.php | 5 +- tests/Concerns/MatchesDumpSnapshots.php | 5 +- tests/Concerns/MatchesReportSnapshots.php | 5 +- tests/Context/RequestContextTest.php | 52 +++++++++++++++++++ tests/FlareTest.php | 2 + ...eport_a_initialised_report_instance__1.yml | 24 +++++++++ ...areTest__it_can_report_an_exception__1.yml | 3 ++ .../ReportTest__it_can_create_a_report__1.yml | 1 + ...reate_a_report_for_a_string_message__1.yml | 1 + ...__it_can_create_a_report_with_glows__1.yml | 1 + ...urn_the_request_context_as_an_array__1.yml | 24 +++++++++ 13 files changed, 133 insertions(+), 11 deletions(-) create mode 100644 tests/__snapshots__/FlareTest__it_can_report_a_initialised_report_instance__1.yml create mode 100644 tests/__snapshots__/RequestContextTest__it_can_return_the_request_context_as_an_array__1.yml diff --git a/composer.json b/composer.json index 5326cd3..adde555 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "phpstan/extension-installer": "^1.1", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.0", - "spatie/phpunit-snapshot-assertions": "^4.0|^5.0", + "spatie/pest-plugin-snapshots": "^1.0|^2.0", "pestphp/pest": "^1.20|^2.0" }, "autoload": { diff --git a/src/Context/RequestContextProvider.php b/src/Context/RequestContextProvider.php index 319b693..8c815ae 100644 --- a/src/Context/RequestContextProvider.php +++ b/src/Context/RequestContextProvider.php @@ -2,8 +2,10 @@ namespace Spatie\FlareClient\Context; +use Illuminate\Support\Arr; use RuntimeException; use Symfony\Component\HttpFoundation\File\UploadedFile; +use Symfony\Component\HttpFoundation\InputBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Mime\Exception\InvalidArgumentException; use Throwable; @@ -138,11 +140,26 @@ public function getRequestData(): array { return [ 'queryString' => $this->request->query->all(), - 'body' => $this->request->request->all(), + 'body' => $this->getInputSource()->all() + $this->request->query->all(), 'files' => $this->getFiles(), ]; } + protected function getInputSource(): InputBag + { + $contentType = $this->request->headers->get('CONTENT_TYPE', 'text/html'); + + $isJson = str_contains($contentType, '/json') || str_contains($contentType, '+json'); + + if ($isJson) { + return new InputBag((array) json_decode($this->request->getContent(), true)); + } + + return in_array($this->request->getMethod(), ['GET', 'HEAD']) + ? $this->request->query + : $this->request->request; + } + /** @return array */ public function toArray(): array { diff --git a/tests/Concerns/MatchesCodeSnippetSnapshots.php b/tests/Concerns/MatchesCodeSnippetSnapshots.php index a6140d4..4fbee53 100644 --- a/tests/Concerns/MatchesCodeSnippetSnapshots.php +++ b/tests/Concerns/MatchesCodeSnippetSnapshots.php @@ -4,17 +4,16 @@ use Spatie\FlareClient\Tests\TestClasses\CodeSnippetDriver; use Spatie\Snapshots\MatchesSnapshots; +use function Spatie\Snapshots\assertMatchesSnapshot; trait MatchesCodeSnippetSnapshots { - use MatchesSnapshots; - public function assertMatchesCodeSnippetSnapshot(array $codeSnippet) { $codeSnippet = $this->removeMicrotime($codeSnippet); $codeSnippet = $this->removeTime($codeSnippet); - $this->assertMatchesSnapshot($codeSnippet, new CodeSnippetDriver()); + assertMatchesSnapshot($codeSnippet, new CodeSnippetDriver()); } private function removeMicrotime(array $codeSnippet): array diff --git a/tests/Concerns/MatchesDumpSnapshots.php b/tests/Concerns/MatchesDumpSnapshots.php index 5e188f6..b1aabaa 100644 --- a/tests/Concerns/MatchesDumpSnapshots.php +++ b/tests/Concerns/MatchesDumpSnapshots.php @@ -4,13 +4,12 @@ use Spatie\FlareClient\Tests\TestClasses\DumpDriver; use Spatie\Snapshots\MatchesSnapshots; +use function Spatie\Snapshots\assertMatchesSnapshot; trait MatchesDumpSnapshots { - use MatchesSnapshots; - public function assertMatchesDumpSnapshot(array $codeSnippet) { - $this->assertMatchesSnapshot($codeSnippet, new DumpDriver()); + assertMatchesSnapshot($codeSnippet, new DumpDriver()); } } diff --git a/tests/Concerns/MatchesReportSnapshots.php b/tests/Concerns/MatchesReportSnapshots.php index 47c800b..f25048b 100644 --- a/tests/Concerns/MatchesReportSnapshots.php +++ b/tests/Concerns/MatchesReportSnapshots.php @@ -4,13 +4,12 @@ use Spatie\FlareClient\Tests\TestClasses\ReportDriver; use Spatie\Snapshots\MatchesSnapshots; +use function Spatie\Snapshots\assertMatchesSnapshot; trait MatchesReportSnapshots { - use MatchesSnapshots; - public function assertMatchesReportSnapshot(array $report) { - $this->assertMatchesSnapshot($report, new ReportDriver()); + assertMatchesSnapshot($report, new ReportDriver()); } } diff --git a/tests/Context/RequestContextTest.php b/tests/Context/RequestContextTest.php index 831a06d..d582469 100644 --- a/tests/Context/RequestContextTest.php +++ b/tests/Context/RequestContextTest.php @@ -48,3 +48,55 @@ $this->assertMatchesCodeSnippetSnapshot($contextArray); }); + +it('can retrieve the body contents of a json request', function () { + $content = '{"key": "value"}'; + + $server = [ + 'HTTP_CONTENT_TYPE' => 'application/json', + ]; + + $request = new Request(server: $server, content: $content); + + $context = new RequestContextProvider($request); + + expect($context->toArray()['request_data']['body'])->toBe(['key' => 'value']); +}); + +it('will not crash when a json body is invalid', function () { + $content = 'SOME INVALID JSON'; + + $server = [ + 'HTTP_CONTENT_TYPE' => 'application/json', + ]; + + $request = new Request(server: $server, content: $content); + + $context = new RequestContextProvider($request); + + expect($context->toArray()['request_data']['body'])->toBe([]); +}); + +it('can retrieve the body contents of a POST request', function () { + $post = ['key' => 'value']; + + $server['REQUEST_METHOD'] = 'POST'; + + $request = new Request(request: $post, server: $server); + + $context = new RequestContextProvider($request); + + expect($context->toArray()['request_data']['body'])->toBe(['key' => 'value']); +}); + +it('can retrieve the body contents of a GET request', function () { + $query = ['key' => 'value']; + + $server['REQUEST_METHOD'] = 'GET'; + + $request = new Request(query: $query, server: $server); + + $context = new RequestContextProvider($request); + + expect($context->toArray()['request_data']['body'])->toBe(['key' => 'value']); +}); diff --git a/tests/FlareTest.php b/tests/FlareTest.php index 20079e2..09fb08c 100644 --- a/tests/FlareTest.php +++ b/tests/FlareTest.php @@ -90,6 +90,8 @@ $_POST['user'] = 'john@example.com'; $_POST['password'] = 'secret'; + $_SERVER['REQUEST_METHOD'] = 'POST'; + $this->flare->censorRequestBodyFields(['user', 'password']); reportException(); diff --git a/tests/__snapshots__/FlareTest__it_can_report_a_initialised_report_instance__1.yml b/tests/__snapshots__/FlareTest__it_can_report_a_initialised_report_instance__1.yml new file mode 100644 index 0000000..e412387 --- /dev/null +++ b/tests/__snapshots__/FlareTest__it_can_report_a_initialised_report_instance__1.yml @@ -0,0 +1,24 @@ +notifier: 'Flare Client' +language: PHP +framework_version: null +language_version: 7.3.2 +exception_class: PHPUnit\Framework\Exception +seen_at: 1546346096 +message: 'This is a test' +glows: { } +solutions: { } +documentation_links: { } +stacktrace: { } +context: + arguments: + - '[phpunit arguments removed]' + env: + php_version: 8.3.7 +stage: null +message_level: null +open_frame_index: null +application_path: null +application_version: null +tracking_uuid: fake-uuid +handled: null +uuid: fake-uuid diff --git a/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml b/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml index 1e77fd9..e412387 100644 --- a/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml +++ b/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml @@ -12,10 +12,13 @@ stacktrace: { } context: arguments: - '[phpunit arguments removed]' + env: + php_version: 8.3.7 stage: null message_level: null open_frame_index: null application_path: null application_version: null tracking_uuid: fake-uuid +handled: null uuid: fake-uuid diff --git a/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml index 1b133a6..ef4649f 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml @@ -18,4 +18,5 @@ open_frame_index: null application_path: null application_version: null tracking_uuid: fake-uuid +handled: null uuid: fake-uuid diff --git a/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml index ad740a1..4f2f5f4 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml @@ -18,4 +18,5 @@ open_frame_index: 0 application_path: null application_version: null tracking_uuid: fake-uuid +handled: null uuid: fake-uuid diff --git a/tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml index 625daaf..10de769 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml @@ -25,4 +25,5 @@ open_frame_index: null application_path: null application_version: null tracking_uuid: fake-uuid +handled: null uuid: fake-uuid diff --git a/tests/__snapshots__/RequestContextTest__it_can_return_the_request_context_as_an_array__1.yml b/tests/__snapshots__/RequestContextTest__it_can_return_the_request_context_as_an_array__1.yml new file mode 100644 index 0000000..cf1d122 --- /dev/null +++ b/tests/__snapshots__/RequestContextTest__it_can_return_the_request_context_as_an_array__1.yml @@ -0,0 +1,24 @@ +request: + url: 'http://example.com/test' + ip: 1.2.3.4 + method: GET + useragent: null +request_data: + queryString: + get-key-1: get-value-1 + body: + get-key-1: get-value-1 + files: + file-one: + pathname: /tests/stubs/file.txt + size: 4 + mimeType: text/plain + file-two: + pathname: /tests/stubs/file.txt + size: 4 + mimeType: text/plain +headers: + host: example.com +cookies: + cookie-key-1: cookie-value-1 +session: { } From d77be76018a9455dcaaf480a147365f65cb71b49 Mon Sep 17 00:00:00 2001 From: rubenvanassche Date: Tue, 30 Jul 2024 08:57:48 +0000 Subject: [PATCH 2/4] Fix styling --- src/Context/RequestContextProvider.php | 1 - tests/Concerns/MatchesCodeSnippetSnapshots.php | 1 - tests/Concerns/MatchesDumpSnapshots.php | 1 - tests/Concerns/MatchesReportSnapshots.php | 1 - 4 files changed, 4 deletions(-) diff --git a/src/Context/RequestContextProvider.php b/src/Context/RequestContextProvider.php index 8c815ae..9352409 100644 --- a/src/Context/RequestContextProvider.php +++ b/src/Context/RequestContextProvider.php @@ -2,7 +2,6 @@ namespace Spatie\FlareClient\Context; -use Illuminate\Support\Arr; use RuntimeException; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\InputBag; diff --git a/tests/Concerns/MatchesCodeSnippetSnapshots.php b/tests/Concerns/MatchesCodeSnippetSnapshots.php index 4fbee53..ccd0cbf 100644 --- a/tests/Concerns/MatchesCodeSnippetSnapshots.php +++ b/tests/Concerns/MatchesCodeSnippetSnapshots.php @@ -3,7 +3,6 @@ namespace Spatie\FlareClient\Tests\Concerns; use Spatie\FlareClient\Tests\TestClasses\CodeSnippetDriver; -use Spatie\Snapshots\MatchesSnapshots; use function Spatie\Snapshots\assertMatchesSnapshot; trait MatchesCodeSnippetSnapshots diff --git a/tests/Concerns/MatchesDumpSnapshots.php b/tests/Concerns/MatchesDumpSnapshots.php index b1aabaa..3f73f0a 100644 --- a/tests/Concerns/MatchesDumpSnapshots.php +++ b/tests/Concerns/MatchesDumpSnapshots.php @@ -3,7 +3,6 @@ namespace Spatie\FlareClient\Tests\Concerns; use Spatie\FlareClient\Tests\TestClasses\DumpDriver; -use Spatie\Snapshots\MatchesSnapshots; use function Spatie\Snapshots\assertMatchesSnapshot; trait MatchesDumpSnapshots diff --git a/tests/Concerns/MatchesReportSnapshots.php b/tests/Concerns/MatchesReportSnapshots.php index f25048b..303424c 100644 --- a/tests/Concerns/MatchesReportSnapshots.php +++ b/tests/Concerns/MatchesReportSnapshots.php @@ -3,7 +3,6 @@ namespace Spatie\FlareClient\Tests\Concerns; use Spatie\FlareClient\Tests\TestClasses\ReportDriver; -use Spatie\Snapshots\MatchesSnapshots; use function Spatie\Snapshots\assertMatchesSnapshot; trait MatchesReportSnapshots From 67854086039c36096a84c23ae2cac1d5bca5ccdc Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Tue, 30 Jul 2024 11:04:40 +0200 Subject: [PATCH 3/4] Fix tests --- src/Context/RequestContextProvider.php | 2 +- tests/TestClasses/ReportDriver.php | 1 + ...lareTest__it_can_report_a_initialised_report_instance__1.yml | 2 +- .../__snapshots__/FlareTest__it_can_report_an_exception__1.yml | 2 +- tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml | 2 ++ ...portTest__it_can_create_a_report_for_a_string_message__1.yml | 2 ++ .../ReportTest__it_can_create_a_report_with_glows__1.yml | 2 ++ 7 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Context/RequestContextProvider.php b/src/Context/RequestContextProvider.php index 9352409..16a6fd8 100644 --- a/src/Context/RequestContextProvider.php +++ b/src/Context/RequestContextProvider.php @@ -144,7 +144,7 @@ public function getRequestData(): array ]; } - protected function getInputSource(): InputBag + protected function getInputSource(): InputBag|ParameterBag { $contentType = $this->request->headers->get('CONTENT_TYPE', 'text/html'); diff --git a/tests/TestClasses/ReportDriver.php b/tests/TestClasses/ReportDriver.php index 0d233e3..0519877 100644 --- a/tests/TestClasses/ReportDriver.php +++ b/tests/TestClasses/ReportDriver.php @@ -67,6 +67,7 @@ protected function removePhpunitArguments(array $data): array protected function freezeLanguageVersion(array $data): array { data_set($data, 'language_version', '7.3.2', true); + data_set($data, 'context.env.php_version', '7.3.2', true); return $data; } diff --git a/tests/__snapshots__/FlareTest__it_can_report_a_initialised_report_instance__1.yml b/tests/__snapshots__/FlareTest__it_can_report_a_initialised_report_instance__1.yml index e412387..1f93b27 100644 --- a/tests/__snapshots__/FlareTest__it_can_report_a_initialised_report_instance__1.yml +++ b/tests/__snapshots__/FlareTest__it_can_report_a_initialised_report_instance__1.yml @@ -13,7 +13,7 @@ context: arguments: - '[phpunit arguments removed]' env: - php_version: 8.3.7 + php_version: 7.3.2 stage: null message_level: null open_frame_index: null diff --git a/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml b/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml index e412387..1f93b27 100644 --- a/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml +++ b/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml @@ -13,7 +13,7 @@ context: arguments: - '[phpunit arguments removed]' env: - php_version: 8.3.7 + php_version: 7.3.2 stage: null message_level: null open_frame_index: null diff --git a/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml index ef4649f..9f4756c 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml @@ -12,6 +12,8 @@ stacktrace: { } context: arguments: - '[phpunit arguments removed]' + env: + php_version: 7.3.2 stage: null message_level: null open_frame_index: null diff --git a/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml index 4f2f5f4..68537be 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml @@ -12,6 +12,8 @@ stacktrace: { } context: arguments: - '[phpunit arguments removed]' + env: + php_version: 7.3.2 stage: null message_level: null open_frame_index: 0 diff --git a/tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml index 10de769..76a3dc3 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_a_report_with_glows__1.yml @@ -19,6 +19,8 @@ stacktrace: { } context: arguments: - '[phpunit arguments removed]' + env: + php_version: 7.3.2 stage: null message_level: null open_frame_index: null From 1b9de03e493c7de6be07dc2a0b12224712f71d64 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Tue, 30 Jul 2024 11:07:13 +0200 Subject: [PATCH 4/4] Fix tests --- src/Context/RequestContextProvider.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Context/RequestContextProvider.php b/src/Context/RequestContextProvider.php index 16a6fd8..306a1f3 100644 --- a/src/Context/RequestContextProvider.php +++ b/src/Context/RequestContextProvider.php @@ -5,6 +5,7 @@ use RuntimeException; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\InputBag; +use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Mime\Exception\InvalidArgumentException; use Throwable; @@ -139,12 +140,12 @@ public function getRequestData(): array { return [ 'queryString' => $this->request->query->all(), - 'body' => $this->getInputSource()->all() + $this->request->query->all(), + 'body' => $this->getInputBag()->all() + $this->request->query->all(), 'files' => $this->getFiles(), ]; } - protected function getInputSource(): InputBag|ParameterBag + protected function getInputBag(): InputBag|ParameterBag { $contentType = $this->request->headers->get('CONTENT_TYPE', 'text/html');