Skip to content

Commit 59b5ded

Browse files
committed
Fix tests
1 parent 645b8c3 commit 59b5ded

File tree

8 files changed

+140
-289
lines changed

8 files changed

+140
-289
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"require-dev": {
1010
"mockery/mockery": "^1.3.1",
1111
"nunomaduro/collision": "^8.1",
12-
"phpunit/phpunit": "^11.0 || ^12",
1312
"orchestra/testbench": "^9.0 || ^10",
14-
"spatie/laravel-ray": "^1.24"
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>
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+
}

tests/PreventSavingStacheItemsToDisk.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/TestCase.php

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,24 @@
22

33
namespace TransformStudios\Front\Tests;
44

5-
use Orchestra\Testbench\TestCase as OrchestraTestCase;
6-
use Statamic\Extend\Manifest;
7-
use Statamic\Providers\StatamicServiceProvider;
8-
use Statamic\Statamic;
5+
use Statamic\Testing\AddonTestCase;
6+
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
97
use TransformStudios\Front\ServiceProvider;
108

11-
abstract class TestCase extends OrchestraTestCase
9+
abstract class TestCase extends AddonTestCase
1210
{
13-
use PreventSavingStacheItemsToDisk;
11+
use PreventsSavingStacheItemsToDisk;
1412

15-
protected function setUp(): void
16-
{
17-
parent::setUp();
18-
}
19-
20-
public function tearDown(): void
21-
{
22-
$this->deleteFakeStacheDirectory();
13+
protected string $addonServiceProvider = ServiceProvider::class;
2314

24-
parent::tearDown();
25-
}
26-
27-
protected function getPackageProviders($app)
28-
{
29-
return [
30-
StatamicServiceProvider::class,
31-
ServiceProvider::class,
32-
];
33-
}
15+
protected string $fakeStacheDirectory = __DIR__.'/__fixtures__/dev-null';
3416

35-
protected function getPackageAliases($app)
36-
{
37-
return [
38-
'Statamic' => Statamic::class,
39-
];
40-
}
41-
42-
protected function getEnvironmentSetUp($app)
43-
{
44-
parent::getEnvironmentSetUp($app);
45-
46-
$app->make(Manifest::class)->manifest = [
47-
'transformstudios/front' => [
48-
'id' => 'transformstudios/front',
49-
'namespace' => 'TransformStudios\\Front',
50-
],
51-
];
52-
}
53-
54-
protected function resolveApplicationConfiguration($app)
17+
protected function setUp(): void
5518
{
56-
parent::resolveApplicationConfiguration($app);
57-
58-
$configs = ['assets', 'cp', 'forms', 'routes', 'static_caching', 'sites', 'stache', 'system', 'users'];
19+
parent::setUp();
5920

60-
foreach ($configs as $config) {
61-
$app['config']->set("statamic.$config", require __DIR__."/../vendor/statamic/cms/config/{$config}.php");
21+
if (! file_exists($this->fakeStacheDirectory)) {
22+
mkdir($this->fakeStacheDirectory, 0777, true);
6223
}
63-
64-
// Setting the user repository to the default flat file system
65-
$app['config']->set('statamic.users.repository', 'file');
66-
67-
// Assume the pro edition within tests
68-
$app['config']->set('statamic.editions.pro', true);
69-
70-
$app['config']->set('front', require __DIR__ . '/../config/front.php');
7124
}
7225
}

0 commit comments

Comments
 (0)