Skip to content

Commit 1eab9fe

Browse files
authored
Convert all tests to Pest (#79)
* feat: install Pest * refactor: FailedJobMonitorTest * refactor: NotifiableTest * replace test suite in Github action and `composer.json` * wip
1 parent b07a6d9 commit 1eab9fe

File tree

6 files changed

+52
-71
lines changed

6 files changed

+52
-71
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ jobs:
4848
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
4949
5050
- name: Execute tests
51-
run: vendor/bin/phpunit
51+
run: vendor/bin/pest

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":[],"times":{"\/Users\/alexandrumanase\/Documents\/repos\/laravel-failed-job-monitor\/tests\/FailedJobMonitorTest.php::it":0.004,"\/Users\/alexandrumanase\/Documents\/repos\/laravel-failed-job-monitor\/tests\/NotifiableTest.php::it":0.005}}

composer.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"spatie/laravel-package-tools": "^1.6"
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "^8.5|^9.5",
3029
"mockery/mockery": "^1.4",
31-
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0"
30+
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0",
31+
"pestphp/pest": "^1.22"
3232
},
3333
"autoload": {
3434
"psr-4": {
@@ -41,7 +41,7 @@
4141
}
4242
},
4343
"scripts": {
44-
"test": "vendor/bin/phpunit"
44+
"test": "vendor/bin/pest"
4545
},
4646
"suggest": {
4747
"guzzlehttp/guzzle": "Allows notifications to be sent to Slack",
@@ -55,5 +55,10 @@
5555
}
5656
},
5757
"minimum-stability": "dev",
58-
"prefer-stable": true
59-
}
58+
"prefer-stable": true,
59+
"config": {
60+
"allow-plugins": {
61+
"pestphp/pest-plugin": true
62+
}
63+
}
64+
}

tests/FailedJobMonitorTest.php

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?php
22

3-
namespace Spatie\FailedJobMonitor\Test;
4-
5-
use Illuminate\Foundation\Testing\DatabaseMigrations;
63
use Illuminate\Queue\Events\JobFailed;
74
use Illuminate\Support\Facades\Notification as NotificationFacade;
85
use Spatie\FailedJobMonitor\Notifiable;
@@ -11,67 +8,49 @@
118
use Spatie\FailedJobMonitor\Test\Dummy\AnotherNotification;
129
use Spatie\FailedJobMonitor\Test\Dummy\Job;
1310

14-
class FailedJobMonitorTest extends TestCase
15-
{
16-
use DatabaseMigrations;
17-
18-
/** @var \Spatie\FailedJobMonitor\Test\Dummy\TestQueueManager */
19-
protected $manager;
20-
21-
public function setUp(): void
11+
beforeAll(function () {
12+
function fireFailedEvent()
2213
{
23-
parent::setUp();
24-
25-
NotificationFacade::fake();
14+
return event(new JobFailed('test', new Job(), new \Exception()));
2615
}
27-
28-
/** @test */
29-
public function it_can_send_notification_when_a_job_failed()
16+
function returnsFalseWhenExceptionIsEmpty($notification)
3017
{
31-
$this->fireFailedEvent();
18+
$message = $notification->getEvent()->exception->getMessage();
3219

33-
NotificationFacade::assertSentTo(new Notifiable(), Notification::class);
20+
return !empty($message);
3421
}
22+
});
3523

36-
/** @test */
37-
public function it_can_send_notification_when_job_failed_to_different_notifiable()
38-
{
39-
$this->app['config']->set('failed-job-monitor.notifiable', AnotherNotifiable::class);
24+
beforeEach(function () {
25+
NotificationFacade::fake();
26+
});
4027

41-
$this->fireFailedEvent();
28+
it('can send notification when a job failed', function () {
29+
fireFailedEvent();
4230

43-
NotificationFacade::assertSentTo(new AnotherNotifiable(), Notification::class);
44-
}
31+
NotificationFacade::assertSentTo(new Notifiable(), Notification::class);
32+
});
4533

46-
/** @test */
47-
public function it_can_send_notification_when_job_failed_to_different_notification()
48-
{
49-
$this->app['config']->set('failed-job-monitor.notification', AnotherNotification::class);
34+
it('can send notification when job failed to different notifiable', function () {
35+
config()->set('failed-job-monitor.notifiable', AnotherNotifiable::class);
5036

51-
$this->fireFailedEvent();
37+
fireFailedEvent();
5238

53-
NotificationFacade::assertSentTo(new Notifiable(), AnotherNotification::class);
54-
}
39+
NotificationFacade::assertSentTo(new AnotherNotifiable(), Notification::class);
40+
});
5541

56-
/** @test */
57-
public function it_filters_out_notifications_when_the_notificationFilter_returns_false()
58-
{
59-
$this->app['config']->set('failed-job-monitor.callback', [$this, 'returnsFalseWhenExceptionIsEmpty']);
42+
it('can send notification when job failed to different notification', function () {
43+
config()->set('failed-job-monitor.notification', AnotherNotification::class);
6044

61-
$this->fireFailedEvent();
45+
fireFailedEvent();
6246

63-
NotificationFacade::assertNotSentTo(new Notifiable(), AnotherNotification::class);
64-
}
47+
NotificationFacade::assertSentTo(new Notifiable(), AnotherNotification::class);
48+
});
6549

66-
protected function fireFailedEvent()
67-
{
68-
return event(new JobFailed('test', new Job(), new \Exception()));
69-
}
50+
it('filters out notifications when the notificationFilter returns `false`', function () {
51+
config()->set('failed-job-monitor.callback', 'returnsFalseWhenExceptionIsEmpty');
7052

71-
public function returnsFalseWhenExceptionIsEmpty($notification)
72-
{
73-
$message = $notification->getEvent()->exception->getMessage();
53+
fireFailedEvent();
7454

75-
return ! empty($message);
76-
}
77-
}
55+
NotificationFacade::assertNotSentTo(new Notifiable(), AnotherNotification::class);
56+
});

tests/NotifiableTest.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
<?php
22

3-
namespace Spatie\FailedJobMonitor\Test;
4-
53
use Spatie\FailedJobMonitor\Notifiable;
64

7-
class NotifiableTest extends TestCase
8-
{
9-
/** @test */
10-
public function it_returns_always_list_of_emails()
11-
{
12-
$notifiable = new Notifiable();
5+
it('returns always list of emails', function () {
6+
$notifiable = new Notifiable();
137

14-
$this->app['config']->set('failed-job-monitor.mail.to', '[email protected]');
15-
$this->assertEquals(['[email protected]'], $notifiable->routeNotificationForMail());
8+
config()->set('failed-job-monitor.mail.to', '[email protected]');
9+
expect($notifiable->routeNotificationForMail())->toEqual(['[email protected]']);
1610

17-
$recipients = ['[email protected]', '[email protected]'];
18-
$this->app['config']->set('failed-job-monitor.mail.to', $recipients);
19-
$this->assertEquals($recipients, $notifiable->routeNotificationForMail());
20-
}
21-
}
11+
$recipients = ['[email protected]', '[email protected]'];
12+
config()->set('failed-job-monitor.mail.to', $recipients);
13+
expect($notifiable->routeNotificationForMail())->toEqual($recipients);
14+
});

tests/Pest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
uses(Spatie\FailedJobMonitor\Test\TestCase::class)->in('.');

0 commit comments

Comments
 (0)