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..306a1f3 100644 --- a/src/Context/RequestContextProvider.php +++ b/src/Context/RequestContextProvider.php @@ -4,6 +4,8 @@ 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; @@ -138,11 +140,26 @@ public function getRequestData(): array { return [ 'queryString' => $this->request->query->all(), - 'body' => $this->request->request->all(), + 'body' => $this->getInputBag()->all() + $this->request->query->all(), 'files' => $this->getFiles(), ]; } + protected function getInputBag(): InputBag|ParameterBag + { + $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..ccd0cbf 100644 --- a/tests/Concerns/MatchesCodeSnippetSnapshots.php +++ b/tests/Concerns/MatchesCodeSnippetSnapshots.php @@ -3,18 +3,16 @@ namespace Spatie\FlareClient\Tests\Concerns; 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..3f73f0a 100644 --- a/tests/Concerns/MatchesDumpSnapshots.php +++ b/tests/Concerns/MatchesDumpSnapshots.php @@ -3,14 +3,12 @@ namespace Spatie\FlareClient\Tests\Concerns; 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..303424c 100644 --- a/tests/Concerns/MatchesReportSnapshots.php +++ b/tests/Concerns/MatchesReportSnapshots.php @@ -3,14 +3,12 @@ namespace Spatie\FlareClient\Tests\Concerns; 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/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 new file mode 100644 index 0000000..1f93b27 --- /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: 7.3.2 +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..1f93b27 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: 7.3.2 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..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,10 +12,13 @@ stacktrace: { } context: arguments: - '[phpunit arguments removed]' + env: + php_version: 7.3.2 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_for_a_string_message__1.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report_for_a_string_message__1.yml index ad740a1..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,10 +12,13 @@ stacktrace: { } context: arguments: - '[phpunit arguments removed]' + env: + php_version: 7.3.2 stage: null message_level: null 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..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,10 +19,13 @@ stacktrace: { } context: arguments: - '[phpunit arguments removed]' + env: + php_version: 7.3.2 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__/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: { }