From 571f06ad8f75bcc6f7854cbc5694b415c1ae3e8d Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Fri, 29 Mar 2024 09:46:00 +0100 Subject: [PATCH] support context --- .gitignore | 1 + phpunit.xml.dist | 48 +++++++++++--------------- src/Ray.php | 54 +++++++++++++++++++++++++++++ tests/Pest.php | 9 +++++ tests/Unit/ContextTest.php | 70 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 29 deletions(-) create mode 100644 tests/Unit/ContextTest.php diff --git a/.gitignore b/.gitignore index 3c6e908..3e217f1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ docs phpunit.xml psalm.xml vendor +.phpunit.cache diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 56f8617..b84cb97 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,31 +1,21 @@ - - - - tests - - - - - ./src - - - - - - - - - - + + + + tests + + + + + + + + + + + + + ./src + + diff --git a/src/Ray.php b/src/Ray.php index 75cdc81..0c4a19c 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -10,6 +10,7 @@ use Illuminate\Mail\Mailable; use Illuminate\Mail\MailManager; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Context; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Testing\Fakes\MailFake; @@ -37,6 +38,7 @@ use Spatie\LaravelRay\Watchers\Watcher; use Spatie\Ray\Client; use Spatie\Ray\Payloads\ExceptionPayload; +use Spatie\Ray\Payloads\LogPayload; use Spatie\Ray\Ray as BaseRay; use Spatie\Ray\Settings\Settings; use Throwable; @@ -85,6 +87,58 @@ public function mailable(Mailable ...$mailables): self return $this; } + /** + * @param array|string ...$keys + * + * @return $this + */ + public function context(...$keys): self + { + if (! class_exists(Context::class)) { + return $this; + } + + if (isset($keys[0]) && is_array($keys[0])) { + $keys = $keys[0]; + } + + $context = count($keys) + ? Context::only($keys) + : Context::all(); + + $this + ->send($context) + ->label('Context'); + + return $this; + } + + /** + * @param array|string ...$keys + * + * @return $this + */ + public function hiddenContext(...$keys): self + { + if (! class_exists(Context::class)) { + return $this; + } + + if (isset($keys[0]) && is_array($keys[0])) { + $keys = $keys[0]; + } + + $hiddenContext = count($keys) + ? Context::onlyHidden($keys) + : Context::allHidden(); + + $this + ->send($hiddenContext) + ->label('Hidden Context'); + + return $this; + } + /** * @param Model|iterable ...$model * diff --git a/tests/Pest.php b/tests/Pest.php index 4d5dfd6..c4697db 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -6,6 +6,7 @@ |-------------------------------------------------------------------------- */ +use Illuminate\Support\Facades\Context; use Spatie\LaravelRay\Tests\TestCase; uses(TestCase::class)->in('.'); @@ -32,3 +33,11 @@ function assertMatchesOsSafeSnapshot($data): void test()->expect($json)->toMatchJsonSnapshot(); } + +function onlyIfContextSupported() +{ + + if (!class_exists(Context::class)) { + test()->skip('Context is not supported for this Laravel version'); + } +} diff --git a/tests/Unit/ContextTest.php b/tests/Unit/ContextTest.php new file mode 100644 index 0000000..b724cd3 --- /dev/null +++ b/tests/Unit/ContextTest.php @@ -0,0 +1,70 @@ +context(); + + expect($this->client->sentRequests())->toHaveCount(2); + + $requests = $this->client->sentRequests(); + + $clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data']; + + expect($clipboardData)->toContain('key', 'value'); +})->onlyIfContextSupported(); + + +it('can send specific context keys variadic', function () { + Context::add('key1', 'value1'); + Context::add('key2', 'value2'); + Context::add('key3', 'value3'); + + ray()->context('key1', 'key3'); + + expect($this->client->sentRequests())->toHaveCount(2); + + $requests = $this->client->sentRequests(); + + $clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data']; + + expect($clipboardData)->toContain('key1', 'key3'); + expect($clipboardData)->not()->toContain('key2'); +})->onlyIfContextSupported(); + +it('can send specific context keys using an array', function () { + Context::add('key1', 'value1'); + Context::add('key2', 'value2'); + Context::add('key3', 'value3'); + + ray()->context(['key1', 'key3']); + + expect($this->client->sentRequests())->toHaveCount(2); + + $requests = $this->client->sentRequests(); + + $clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data']; + + expect($clipboardData)->toContain('key1', 'key3'); + expect($clipboardData)->not()->toContain('key2'); +})->onlyIfContextSupported(); + +it('can send all hidden context', function () { + Context::addHidden('hidden-key', 'hidden-value'); + Context::add('visible-key', 'visible-value'); + + ray()->hiddenContext(); + + expect($this->client->sentRequests())->toHaveCount(2); + + $requests = $this->client->sentRequests(); + + $clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data']; + + expect($clipboardData)->toContain('hidden-key'); + expect($clipboardData)->not()->toContain('visible-key'); +})->onlyIfContextSupported();