Skip to content

Commit a72963c

Browse files
authored
Support L12 & S6 (#19)
1 parent 44bdb6d commit a72963c

14 files changed

Lines changed: 209 additions & 325 deletions

File tree

.github/workflows/test.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Run Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
php-tests:
9+
runs-on: ${{ matrix.os }}
10+
strategy:
11+
matrix:
12+
os: [ubuntu-latest]
13+
php: [8.3, 8.4]
14+
laravel: [11.*, 12.*]
15+
dependency-version: [prefer-stable]
16+
exclude:
17+
- php: 8.1
18+
laravel: 11.*
19+
- php: 8.1
20+
laravel: 12.*
21+
- php: 8.4
22+
laravel: 10.*
23+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ubuntu-latest
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@v3
27+
28+
- name: Cache dependencies
29+
uses: actions/cache@v4
30+
with:
31+
path: ~/.composer/cache/files
32+
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
33+
34+
- name: Setup PHP
35+
uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: ${{ matrix.php }}
38+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
39+
coverage: none
40+
41+
- name: Install dependencies
42+
run: |
43+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
44+
45+
- name: Execute tests
46+
run: vendor/bin/pest

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
"name": "transformstudios/front",
33
"require": {
44
"php": "^8.3",
5-
"laravel/framework": "^11.0",
5+
"illuminate/contracts": "^11.0 || ^12.0",
66
"pixelfear/composer-dist-plugin": "^0.1.4",
7-
"statamic/cms": "^5.0"
7+
"statamic/cms": "^5.0 || ^6.0"
88
},
99
"require-dev": {
1010
"mockery/mockery": "^1.3.1",
1111
"nunomaduro/collision": "^8.1",
12-
"phpunit/phpunit": "^11.0",
13-
"orchestra/testbench": "^9.0",
14-
"spatie/laravel-ray": "^1.24"
12+
"orchestra/testbench": "^9.0 || ^10",
13+
"spatie/laravel-ray": "^1.24",
14+
"pestphp/pest": "^4.1"
1515
},
1616
"autoload": {
1717
"psr-4": {
@@ -43,7 +43,8 @@
4343
},
4444
"config": {
4545
"allow-plugins": {
46-
"pixelfear/composer-dist-plugin": true
46+
"pixelfear/composer-dist-plugin": true,
47+
"pestphp/pest-plugin": true
4748
}
4849
}
4950
}

phpunit.xml

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +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-
printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"
11-
stopOnFailure="false"
12-
verbose="true">
13-
<testsuites>
14-
<testsuite name="Unit">
15-
<directory suffix="Test.php">tests/Unit/</directory>
16-
</testsuite>
17-
<testsuite name="Test Suite">
18-
<directory suffix="Test.php">./tests</directory>
19-
</testsuite>
20-
</testsuites>
21-
<filter>
22-
<whitelist processUncoveredFilesFromWhitelist="true">
23-
<directory suffix=".php">./app</directory>
24-
</whitelist>
25-
</filter>
26-
<php>
27-
<env name="APP_ENV" value="testing"/>
28-
<env name="APP_KEY" value="base64:xRIcDp1ReW8Y8rd9V9D7hOVV4TI7ThCF3FKxRg01Rm8="/>
29-
<env name="APP_URL" value="http://front.test"/>
30-
<env name="CACHE_DRIVER" value="array"/>
31-
<env name="SESSION_DRIVER" value="array"/>
32-
<env name="QUEUE_DRIVER" value="sync"/>
33-
<env name="MAIL_DRIVER" value="array"/>
34-
<env name="DB_CONNECTION" value="sqlite"/>
35-
<env name="DB_DATABASE" value=":memory:"/>
36-
</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+
<coverage/>
4+
<testsuites>
5+
<testsuite name="Test Suite">
6+
<directory suffix="Test.php">./tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
<php>
10+
<env name="APP_ENV" value="testing"/>
11+
<env name="APP_KEY" value="base64:xRIcDp1ReW8Y8rd9V9D7hOVV4TI7ThCF3FKxRg01Rm8="/>
12+
<env name="APP_URL" value="http://front.test"/>
13+
<env name="DB_CONNECTION" value="sqlite"/>
14+
<env name="DB_DATABASE" value=":memory:"/>
15+
<env name="CACHE_DRIVER" value="array"/>
16+
<env name="SESSION_DRIVER" value="array"/>
17+
<env name="QUEUE_DRIVER" value="sync"/>
18+
<env name="MAIL_DRIVER" value="array"/>
19+
</php>
20+
<source/>
3721
</phpunit>

src/Logging/LogHandler.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Collection;
66
use Monolog\Handler\AbstractProcessingHandler;
7+
use Monolog\Level;
78
use Monolog\Logger as Monolog;
89
use Monolog\LogRecord;
910
use Statamic\Support\Arr;
@@ -15,7 +16,7 @@ class LogHandler extends AbstractProcessingHandler
1516
/** @throws \Illuminate\Contracts\Container\BindingResolutionException */
1617
public function __construct(array $channelConfig)
1718
{
18-
parent::__construct(Monolog::toMonologLevel($channelConfig['level'] ?? Monolog::DEBUG));
19+
parent::__construct(Monolog::toMonologLevel($channelConfig['level'] ?? Level::Debug));
1920
}
2021

2122
public function write(array|LogRecord $record): void
@@ -29,13 +30,11 @@ public function write(array|LogRecord $record): void
2930
}
3031

3132
if (! Arr::get($record, 'context.exception')) {
32-
$errors = collect(
33-
[
34-
'Request URL: '.request()->fullUrl(),
35-
'Request data: '.json_encode(request()->input()),
36-
'Error: '.json_encode($record),
37-
]
38-
);
33+
$errors = collect([
34+
'Request URL: '.request()->fullUrl(),
35+
'Request data: '.json_encode(request()->input()),
36+
'Error: '.json_encode($record),
37+
]);
3938

4039
front()
4140
->post(
@@ -60,14 +59,12 @@ private function convertErrorToFrontMessage(Throwable $error): array
6059

6160
private function formatErrorLines(Throwable $error): Collection
6261
{
63-
return collect(
64-
[
65-
'Request URL: '.request()->fullUrl(),
66-
'Request data: '.json_encode(request()->input()),
67-
'**'.$error->getMessage().'**',
68-
'* '.$error->getFile().' ('.$error->getLine().')',
69-
]
70-
)->merge($this->formatStackTrace($error));
62+
return collect([
63+
'Request URL: '.request()->fullUrl(),
64+
'Request data: '.json_encode(request()->input()),
65+
'**'.$error->getMessage().'**',
66+
'* '.$error->getFile().' ('.$error->getLine().')',
67+
])->merge($this->formatStackTrace($error));
7168
}
7269

7370
private function formatStackTrace(Throwable $error): Collection

src/Logging/Logger.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ class Logger
99
/** @throws \Illuminate\Contracts\Container\BindingResolutionException */
1010
public function __invoke(array $config)
1111
{
12-
return new Monolog(
13-
config('app.name'),
14-
[new LogHandler($config)]
15-
);
12+
return new Monolog(config('app.name'), [new LogHandler($config)]);
1613
}
1714
}

src/Notifications/BaseNotification.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ public function __construct(
1515
public string $key,
1616
public string $subject,
1717
public string $renderedView,
18-
public Collection $users)
19-
{
20-
}
18+
public Collection $users
19+
) {}
2120

2221
public function via($notifiable)
2322
{

src/Notifications/Channel.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Http\Client\Response;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Facades\Cache;
8-
use Illuminate\Support\Facades\Http;
98
use Statamic\Auth\User;
109

1110
class Channel
@@ -39,17 +38,13 @@ private function data(BaseNotification $notification): array
3938

4039
private function post(string $segment, string $id, array $data): Response
4140
{
42-
return Http::withToken(config('front.api_token'))
43-
->baseUrl('https://api2.frontapp.com')
41+
return front()
4442
->post("/$segment/$id/messages", $data)
4543
->throw();
4644
}
4745

4846
private function getConversationId(Response $response): string
4947
{
50-
return last(explode(
51-
'/',
52-
Arr::get($response, '_links.related.conversation')
53-
));
48+
return last(explode('/', Arr::get($response, '_links.related.conversation')));
5449
}
5550
}

src/helpers.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
function front(): PendingRequest
77
{
8-
return Http::withToken(config('front.api_token'))->baseUrl(
9-
'https://api2.frontapp.com'
10-
);
8+
return Http::withToken(config('front.api_token'))
9+
->baseUrl('https://api2.frontapp.com');
1110
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use Illuminate\Notifications\AnonymousNotifiable;
4+
use Illuminate\Support\Carbon;
5+
use Illuminate\Support\Facades\Cache;
6+
use Illuminate\Support\Facades\Http;
7+
use Statamic\Facades\User;
8+
use TransformStudios\Front\Notifications\BaseNotification;
9+
use TransformStudios\Front\Notifications\Channel;
10+
11+
beforeEach(function () {
12+
config()->set('front.notifications.channel', 'test-channel');
13+
Http::preventStrayRequests();
14+
Http::fake([
15+
'https://api2.frontapp.com/channels/test-channel/messages' => Http::response([
16+
'_links' => [
17+
'related' => [
18+
'conversation' => 'https://transform-studios.api.frontapp.com/conversations/cnv_id',
19+
],
20+
],
21+
], 200),
22+
'https://api2.frontapp.com/conversations/cnv_id/messages' => Http::response([], 200),
23+
]);
24+
});
25+
26+
test('can send front message', function () {
27+
$users = collect([makeUser('erin@transformstudios.com'), makeUser('erin@silentz.co')]);
28+
$notification = new TestNotification('some-key', 'Monitor Alert: Error Detected', '', $users);
29+
30+
expect((new Channel)->send(new AnonymousNotifiable, $notification))->toBeTrue();
31+
});
32+
33+
it('stores the conversation id', function () {
34+
$notification = new TestNotification('some-key', 'Monitor Alert: Error Detected', '', collect([makeUser('foo@bar.com')]));
35+
36+
expect(Cache::get('some-key'))->toBeNull();
37+
expect((new Channel)->send(new AnonymousNotifiable, $notification))->toBeTrue();
38+
expect(Cache::get('some-key'))->toEqual('cnv_id');
39+
});
40+
41+
it('removes the conversation id when alert cleared', function () {
42+
$notification = new TestNotification('some-key', 'Monitor Alert: Error Cleared', '', collect([makeUser('foo@bar.com')]));
43+
44+
Cache::put('some-key', 'cnv_id');
45+
expect(Cache::get('some-key'))->not->toBeNull();
46+
expect((new Channel)->send(new AnonymousNotifiable, $notification))->toBeTrue();
47+
expect(Cache::get('some-key'))->toBeNull();
48+
});
49+
50+
it('adds to the conversation when conversation id exists', function () {
51+
$user = makeUser('foo@bar.com');
52+
$notification = new TestNotification('some-key', 'Monitor Alert: Error Detected', '', collect([$user]));
53+
$anotherNotification = new TestNotification('some-key', 'Monitor Alert: Error Cleared', '', collect([$user]));
54+
$channel = new Channel;
55+
56+
expect($channel->send(new AnonymousNotifiable, $notification))->toBeTrue();
57+
expect($channel->send(new AnonymousNotifiable, $anotherNotification))->toBeTrue();
58+
});
59+
60+
class TestNotification extends BaseNotification {}

tests/Pest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use Statamic\Auth\User;
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Test Case
8+
|--------------------------------------------------------------------------
9+
|
10+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
11+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
12+
| need to change it using the "pest()" function to bind a different classes or traits.
13+
|
14+
*/
15+
16+
pest()->extend(TransformStudios\Front\Tests\TestCase::class)->in(__DIR__);
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Expectations
21+
|--------------------------------------------------------------------------
22+
|
23+
| When you're writing tests, you often need to check that values meet certain conditions. The
24+
| "expect()" function gives you access to a set of "expectations" methods that you can use
25+
| to assert different things. Of course, you may extend the Expectation API at any time.
26+
|
27+
*/
28+
29+
expect()->extend('toBeOne', function () {
30+
return $this->toBe(1);
31+
});
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Functions
36+
|--------------------------------------------------------------------------
37+
|
38+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
39+
| project that you don't want to repeat in every file. Here you can also expose helpers as
40+
| global functions to help you to reduce the number of lines of code in your test files.
41+
|
42+
*/
43+
44+
function makeUser(string $email): User
45+
{
46+
return tap(User::make()->email($email))->save();
47+
}

0 commit comments

Comments
 (0)