Skip to content

Commit

Permalink
Merge pull request #337 from spatie/support-context
Browse files Browse the repository at this point in the history
Support context
  • Loading branch information
freekmurze authored Mar 29, 2024
2 parents 22d8320 + e2faf99 commit f15936b
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ docs
phpunit.xml
psalm.xml
vendor
.phpunit.cache
48 changes: 19 additions & 29 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Spatie Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="APP_DEBUG" value="true" />
<env name="CACHE_DRIVER" value="array" />
<env name="CACHE_STORE" value="array" />
</php>
<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">
<testsuites>
<testsuite name="Spatie Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="APP_DEBUG" value="true"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="CACHE_STORE" value="array"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
53 changes: 53 additions & 0 deletions src/Ray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,6 +86,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
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
|--------------------------------------------------------------------------
*/

use Illuminate\Support\Facades\Context;
use Spatie\LaravelRay\Tests\TestCase;

uses(TestCase::class)->in('.');
Expand All @@ -32,3 +33,8 @@ function assertMatchesOsSafeSnapshot($data): void

test()->expect($json)->toMatchJsonSnapshot();
}

function contextSupported(): bool
{
return class_exists(Context::class);
}
86 changes: 86 additions & 0 deletions tests/Unit/ContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Spatie\LaravelRay\Tests\Unit;

use Illuminate\Support\Facades\Context;

it('can send all context', function () {
if (! contextSupported()) {
return;
}

Context::add('key', 'value');

ray()->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');
});


it('can send specific context keys variadic', function () {
if (! contextSupported()) {
return;
}

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');
});

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');

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');
});

it('can send all hidden context', function () {
if (! contextSupported()) {
return;
}

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');
});

0 comments on commit f15936b

Please sign in to comment.