Skip to content

Commit e0c9238

Browse files
committed
test: Remove useless files, change exception class names, adjust configuration settings, and refactor notification methods.
1 parent 2825e1e commit e0c9238

File tree

14 files changed

+46
-119
lines changed

14 files changed

+46
-119
lines changed

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<directory suffix=".php">src/</directory>
1414
</include>
1515
<exclude>
16-
<!--<directory>src/Support/</directory>-->
17-
<!--<file>src/Channels/DdChannel.php</file>-->
16+
<directory>src/Rectors/</directory>
17+
<file>src/Support/helpers.php</file>
1818
</exclude>
1919
</coverage>
2020
<testsuites>

src/Channels/MailChannel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class MailChannel extends Channel
2424
public function report(string $report): void
2525
{
2626
tap(Mail::driver($this->config->get('mailer')), function (Mailer $mailer): void {
27-
collect($this->config)
27+
collect($this->config->all())
2828
->except(['mailer'])
2929
->each(static function ($value, string $key) use ($mailer): void {
30-
$mailer->{Str::studly($key)}($value);
30+
$mailer->{Str::camel($key)}($value);
3131
});
3232
})->send($this->createMail($report));
3333
}

src/ExceptionNotifyManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ protected function attempt(string $key, int $maxAttempts, int $decaySeconds = 60
151151
});
152152
}
153153

154-
protected function createDriver($driver)
154+
protected function createDriver($driver): Channel
155155
{
156156
if (isset($this->customCreators[$driver])) {
157157
return $this->callCustomCreator($driver);

src/Exceptions/Exception.php

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

src/Support/helpers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
if (!\function_exists('make')) {
1717
/**
18+
* @codeCoverageIgnore
19+
*
1820
* @psalm-param string|array<string, mixed> $abstract
1921
*
2022
* @param mixed $abstract

tests/Channels/DumpChannelTest.php renamed to tests/Channels/MailChannelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
1515

1616
it('can report', function (): void {
17-
expect($this->app->make(ExceptionNotifyManager::class)->driver('dump'))
17+
expect($this->app->make(ExceptionNotifyManager::class)->driver('mail'))
1818
->report('report')
19-
->toBe('report');
19+
->toBeNull();
2020
})->group(__DIR__, __FILE__);

tests/Channels/NotifyChannelTest.php

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,9 @@
1111
* @see https://github.com/guanguans/laravel-exception-notify
1212
*/
1313

14-
use Guanguans\LaravelExceptionNotify\Channels\BarkChannel;
15-
use Guanguans\LaravelExceptionNotify\Channels\ChanifyChannel;
16-
use Guanguans\LaravelExceptionNotify\Channels\DingTalkChannel;
17-
use Guanguans\LaravelExceptionNotify\Channels\DiscordChannel;
18-
use Guanguans\LaravelExceptionNotify\Channels\FeiShuChannel;
19-
use Guanguans\LaravelExceptionNotify\Channels\PushDeerChannel;
20-
use Guanguans\LaravelExceptionNotify\Channels\QqChannelBotChannel;
21-
use Guanguans\LaravelExceptionNotify\Channels\ServerChanChannel;
22-
use Guanguans\LaravelExceptionNotify\Channels\SlackChannel;
23-
use Guanguans\LaravelExceptionNotify\Channels\TelegramChannel;
24-
use Guanguans\LaravelExceptionNotify\Channels\WeWorkChannel;
25-
use Guanguans\LaravelExceptionNotify\Channels\XiZhiChannel;
26-
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
27-
use Guanguans\Notify\Clients\Client;
28-
use Guanguans\Notify\Contracts\MessageInterface;
29-
use Illuminate\Notifications\Channels\MailChannel;
30-
use Illuminate\Support\Str;
14+
use Guanguans\LaravelExceptionNotify\Channels\NotifyChannel;
15+
use Illuminate\Config\Repository;
3116

32-
it('can report', function (): void {
33-
expect(new BarkChannel(
34-
Mockery::spy(Client::class)->allows('send')->once()->getMock()
35-
))->report('report')->toBeNull();
36-
})->group(__DIR__, __FILE__)->skip();
37-
38-
it('can create message', function (): void {
39-
$collection = collect([
40-
BarkChannel::class,
41-
ChanifyChannel::class,
42-
DingTalkChannel::class,
43-
DiscordChannel::class,
44-
FeiShuChannel::class,
45-
MailChannel::class,
46-
PushDeerChannel::class,
47-
QqChannelBotChannel::class,
48-
ServerChanChannel::class,
49-
SlackChannel::class,
50-
TelegramChannel::class,
51-
WeWorkChannel::class,
52-
XiZhiChannel::class,
53-
])->transform(function (string $channelClass): MessageInterface {
54-
$channelName = (string) Str::of(class_basename($channelClass))
55-
->beforeLast('Channel')
56-
->lcfirst();
57-
58-
$channel = $this
59-
->app
60-
->make(ExceptionNotifyManager::class)
61-
->driver($channelName);
62-
63-
return (fn () => $this->createMessage('report'))->call($channel);
64-
});
65-
66-
expect($collection)->each->toBeInstanceOf(MessageInterface::class);
67-
})->group(__DIR__, __FILE__)->skip();
17+
it('will throw `InvalidArgumentException`', function (): void {
18+
new NotifyChannel(new Repository([]));
19+
})->group(__DIR__, __FILE__)->throws(\Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException::class);

tests/CollectorManagerTest.php

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

tests/ExceptionNotifyManagerTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @see https://github.com/guanguans/laravel-exception-notify
1212
*/
1313

14+
use Guanguans\LaravelExceptionNotify\Contracts\Channel;
1415
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
1516
use Illuminate\Contracts\Foundation\Application;
1617
use Illuminate\Support\Str;
@@ -32,13 +33,13 @@
3233

3334
it('can report if', function (): void {
3435
expect(app(ExceptionNotifyManager::class))
35-
->reportIf(true, new \Guanguans\LaravelExceptionNotify\Exceptions\Exception)->toBeNull();
36+
->reportIf(true, new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();
3637
})->group(__DIR__, __FILE__);
3738

3839
it('can report', function (): void {
3940
config()->set('exception-notify.enabled', false);
4041
expect(app(ExceptionNotifyManager::class))
41-
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\Exception)->toBeNull();
42+
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();
4243

4344
config()->set('exception-notify.enabled', true);
4445
$mockApplication = Mockery::spy(Illuminate\Foundation\Application::class);
@@ -47,7 +48,7 @@
4748

4849
/** @noinspection PhpVoidFunctionResultUsedInspection */
4950
expect(new ExceptionNotifyManager($mockApplication))
50-
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\Exception)->toBeNull();
51+
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();
5152

5253
config()->set('exception-notify.enabled', true);
5354
$mockApplication = Mockery::mock(Application::class);
@@ -56,26 +57,26 @@
5657

5758
/** @noinspection PhpVoidFunctionResultUsedInspection */
5859
expect(new ExceptionNotifyManager($mockApplication))
59-
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\Exception)->toBeNull();
60+
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();
6061
})->group(__DIR__, __FILE__);
6162

6263
it('should not report', function (): void {
6364
config()->set('exception-notify.enabled', false);
64-
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\Exception)->toBeFalse();
65+
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeFalse();
6566

6667
config()->set('exception-notify.enabled', true);
6768
config()->set('exception-notify.env', ['production', 'local']);
68-
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\Exception)->toBeFalse();
69+
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeFalse();
6970

7071
config()->set('exception-notify.enabled', true);
7172
config()->set('exception-notify.env', '*');
7273
config()->set('exception-notify.except', [Exception::class]);
73-
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\Exception)->toBeFalse();
74+
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeFalse();
7475

7576
config()->set('exception-notify.enabled', true);
7677
config()->set('exception-notify.env', '*');
7778
config()->set('exception-notify.except', []);
78-
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\Exception)->toBeTrue();
79+
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeTrue();
7980
})->group(__DIR__, __FILE__);
8081

8182
it('can get default driver', function (): void {
@@ -92,3 +93,17 @@
9293
->call(app(ExceptionNotifyManager::class))->toBeTrue()
9394
->call(app(ExceptionNotifyManager::class))->toBeFalse();
9495
})->group(__DIR__, __FILE__);
96+
97+
it('will throw `InvalidArgumentException`', function (): void {
98+
app(ExceptionNotifyManager::class)->driver('foo');
99+
})->group(__DIR__, __FILE__)->throws(\Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException::class);
100+
101+
it('can create custom driver', function (): void {
102+
app(ExceptionNotifyManager::class)->extend('foo', function () {
103+
return new class implements Channel {
104+
public function report(string $report): void {}
105+
};
106+
});
107+
108+
expect(app(ExceptionNotifyManager::class))->driver('foo')->toBeInstanceOf(Channel::class);
109+
})->group(__DIR__, __FILE__);

tests/FeatureTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Illuminate\Http\UploadedFile;
1616

1717
it('can report exception', function (): void {
18-
ExceptionNotify::report(new \Guanguans\LaravelExceptionNotify\Exceptions\Exception('What happened?'), ['lark']);
18+
ExceptionNotify::report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException('What happened?'), ['lark']);
1919
$this
2020
->post('report-exception?foo=bar', [
2121
'bar' => 'baz',

0 commit comments

Comments
 (0)