Skip to content

Commit 571f06a

Browse files
committed
support context
1 parent 8888778 commit 571f06a

File tree

5 files changed

+153
-29
lines changed

5 files changed

+153
-29
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ docs
99
phpunit.xml
1010
psalm.xml
1111
vendor
12+
.phpunit.cache

phpunit.xml.dist

+19-29
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
verbose="true"
12-
>
13-
<testsuites>
14-
<testsuite name="Spatie Test Suite">
15-
<directory>tests</directory>
16-
</testsuite>
17-
</testsuites>
18-
<coverage>
19-
<include>
20-
<directory suffix=".php">./src</directory>
21-
</include>
22-
</coverage>
23-
<logging>
24-
<junit outputFile="build/report.junit.xml"/>
25-
</logging>
26-
<php>
27-
<env name="APP_DEBUG" value="true" />
28-
<env name="CACHE_DRIVER" value="array" />
29-
<env name="CACHE_STORE" value="array" />
30-
</php>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<testsuites>
4+
<testsuite name="Spatie Test Suite">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<logging>
9+
<junit outputFile="build/report.junit.xml"/>
10+
</logging>
11+
<php>
12+
<env name="APP_DEBUG" value="true"/>
13+
<env name="CACHE_DRIVER" value="array"/>
14+
<env name="CACHE_STORE" value="array"/>
15+
</php>
16+
<source>
17+
<include>
18+
<directory suffix=".php">./src</directory>
19+
</include>
20+
</source>
3121
</phpunit>

src/Ray.php

+54
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Mail\Mailable;
1111
use Illuminate\Mail\MailManager;
1212
use Illuminate\Support\Collection;
13+
use Illuminate\Support\Facades\Context;
1314
use Illuminate\Support\Facades\DB;
1415
use Illuminate\Support\Facades\Mail;
1516
use Illuminate\Support\Testing\Fakes\MailFake;
@@ -37,6 +38,7 @@
3738
use Spatie\LaravelRay\Watchers\Watcher;
3839
use Spatie\Ray\Client;
3940
use Spatie\Ray\Payloads\ExceptionPayload;
41+
use Spatie\Ray\Payloads\LogPayload;
4042
use Spatie\Ray\Ray as BaseRay;
4143
use Spatie\Ray\Settings\Settings;
4244
use Throwable;
@@ -85,6 +87,58 @@ public function mailable(Mailable ...$mailables): self
8587
return $this;
8688
}
8789

90+
/**
91+
* @param array|string ...$keys
92+
*
93+
* @return $this
94+
*/
95+
public function context(...$keys): self
96+
{
97+
if (! class_exists(Context::class)) {
98+
return $this;
99+
}
100+
101+
if (isset($keys[0]) && is_array($keys[0])) {
102+
$keys = $keys[0];
103+
}
104+
105+
$context = count($keys)
106+
? Context::only($keys)
107+
: Context::all();
108+
109+
$this
110+
->send($context)
111+
->label('Context');
112+
113+
return $this;
114+
}
115+
116+
/**
117+
* @param array|string ...$keys
118+
*
119+
* @return $this
120+
*/
121+
public function hiddenContext(...$keys): self
122+
{
123+
if (! class_exists(Context::class)) {
124+
return $this;
125+
}
126+
127+
if (isset($keys[0]) && is_array($keys[0])) {
128+
$keys = $keys[0];
129+
}
130+
131+
$hiddenContext = count($keys)
132+
? Context::onlyHidden($keys)
133+
: Context::allHidden();
134+
135+
$this
136+
->send($hiddenContext)
137+
->label('Hidden Context');
138+
139+
return $this;
140+
}
141+
88142
/**
89143
* @param Model|iterable ...$model
90144
*

tests/Pest.php

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|--------------------------------------------------------------------------
77
*/
88

9+
use Illuminate\Support\Facades\Context;
910
use Spatie\LaravelRay\Tests\TestCase;
1011

1112
uses(TestCase::class)->in('.');
@@ -32,3 +33,11 @@ function assertMatchesOsSafeSnapshot($data): void
3233

3334
test()->expect($json)->toMatchJsonSnapshot();
3435
}
36+
37+
function onlyIfContextSupported()
38+
{
39+
40+
if (!class_exists(Context::class)) {
41+
test()->skip('Context is not supported for this Laravel version');
42+
}
43+
}

tests/Unit/ContextTest.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Spatie\LaravelRay\Tests\Unit;
4+
5+
use Illuminate\Support\Facades\Context;
6+
7+
it('can send all context', function () {
8+
Context::add('key', 'value');
9+
10+
ray()->context();
11+
12+
expect($this->client->sentRequests())->toHaveCount(2);
13+
14+
$requests = $this->client->sentRequests();
15+
16+
$clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data'];
17+
18+
expect($clipboardData)->toContain('key', 'value');
19+
})->onlyIfContextSupported();
20+
21+
22+
it('can send specific context keys variadic', function () {
23+
Context::add('key1', 'value1');
24+
Context::add('key2', 'value2');
25+
Context::add('key3', 'value3');
26+
27+
ray()->context('key1', 'key3');
28+
29+
expect($this->client->sentRequests())->toHaveCount(2);
30+
31+
$requests = $this->client->sentRequests();
32+
33+
$clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data'];
34+
35+
expect($clipboardData)->toContain('key1', 'key3');
36+
expect($clipboardData)->not()->toContain('key2');
37+
})->onlyIfContextSupported();
38+
39+
it('can send specific context keys using an array', function () {
40+
Context::add('key1', 'value1');
41+
Context::add('key2', 'value2');
42+
Context::add('key3', 'value3');
43+
44+
ray()->context(['key1', 'key3']);
45+
46+
expect($this->client->sentRequests())->toHaveCount(2);
47+
48+
$requests = $this->client->sentRequests();
49+
50+
$clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data'];
51+
52+
expect($clipboardData)->toContain('key1', 'key3');
53+
expect($clipboardData)->not()->toContain('key2');
54+
})->onlyIfContextSupported();
55+
56+
it('can send all hidden context', function () {
57+
Context::addHidden('hidden-key', 'hidden-value');
58+
Context::add('visible-key', 'visible-value');
59+
60+
ray()->hiddenContext();
61+
62+
expect($this->client->sentRequests())->toHaveCount(2);
63+
64+
$requests = $this->client->sentRequests();
65+
66+
$clipboardData = $requests[0]['payloads'][0]['content']['meta']['0']['clipboard_data'];
67+
68+
expect($clipboardData)->toContain('hidden-key');
69+
expect($clipboardData)->not()->toContain('visible-key');
70+
})->onlyIfContextSupported();

0 commit comments

Comments
 (0)