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 1/3] 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(); From 70761b25548361fa860d462589a8153333218ca1 Mon Sep 17 00:00:00 2001 From: freekmurze Date: Fri, 29 Mar 2024 08:48:12 +0000 Subject: [PATCH 2/3] Fix styling --- src/Ray.php | 1 - tests/Pest.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Ray.php b/src/Ray.php index 0c4a19c..b1331e2 100644 --- a/src/Ray.php +++ b/src/Ray.php @@ -38,7 +38,6 @@ 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; diff --git a/tests/Pest.php b/tests/Pest.php index c4697db..efa4299 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -37,7 +37,7 @@ function assertMatchesOsSafeSnapshot($data): void function onlyIfContextSupported() { - if (!class_exists(Context::class)) { + if (! class_exists(Context::class)) { test()->skip('Context is not supported for this Laravel version'); } } From eddcb604ae6bba88d94e576f57e14fe393285f51 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Fri, 29 Mar 2024 09:51:47 +0100 Subject: [PATCH 3/3] fix tests --- tests/Pest.php | 7 ++----- tests/Unit/ContextTest.php | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/tests/Pest.php b/tests/Pest.php index c4697db..1687b9f 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -34,10 +34,7 @@ function assertMatchesOsSafeSnapshot($data): void test()->expect($json)->toMatchJsonSnapshot(); } -function onlyIfContextSupported() +function contextSupported(): bool { - - if (!class_exists(Context::class)) { - test()->skip('Context is not supported for this Laravel version'); - } + return class_exists(Context::class); } diff --git a/tests/Unit/ContextTest.php b/tests/Unit/ContextTest.php index b724cd3..7c61878 100644 --- a/tests/Unit/ContextTest.php +++ b/tests/Unit/ContextTest.php @@ -5,6 +5,10 @@ use Illuminate\Support\Facades\Context; it('can send all context', function () { + if (! contextSupported()) { + return; + } + Context::add('key', 'value'); ray()->context(); @@ -16,10 +20,14 @@ $clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data']; expect($clipboardData)->toContain('key', 'value'); -})->onlyIfContextSupported(); +}); it('can send specific context keys variadic', function () { + if (! contextSupported()) { + return; + } + Context::add('key1', 'value1'); Context::add('key2', 'value2'); Context::add('key3', 'value3'); @@ -34,9 +42,13 @@ expect($clipboardData)->toContain('key1', 'key3'); expect($clipboardData)->not()->toContain('key2'); -})->onlyIfContextSupported(); +}); it('can send specific context keys using an array', function () { + if (! contextSupported()) { + return; + } + Context::add('key1', 'value1'); Context::add('key2', 'value2'); Context::add('key3', 'value3'); @@ -51,9 +63,13 @@ expect($clipboardData)->toContain('key1', 'key3'); expect($clipboardData)->not()->toContain('key2'); -})->onlyIfContextSupported(); +}); it('can send all hidden context', function () { + if (! contextSupported()) { + return; + } + Context::addHidden('hidden-key', 'hidden-value'); Context::add('visible-key', 'visible-value'); @@ -67,4 +83,4 @@ expect($clipboardData)->toContain('hidden-key'); expect($clipboardData)->not()->toContain('visible-key'); -})->onlyIfContextSupported(); +});