Skip to content

Commit 81a74c8

Browse files
committed
test(collector): Add php-mock-phpunit dependency for testing
- Add `php-mock/php-mock-phpunit` to `composer.json` for improved unit testing - Update `CollectorTest.php` to utilize Request facade for mocking request data - Enhance `TestCase.php` by including PHPMock for future test cases - Set up configurations for various channels in tests
1 parent 892afb4 commit 81a74c8

13 files changed

+84
-50
lines changed

Diff for: composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"pestphp/pest": "^1.23 || ^2.0 || ^3.0",
110110
"pestphp/pest-plugin-faker": "^1.0 || ^2.0 || ^3.0",
111111
"pestphp/pest-plugin-laravel": "^1.4 || ^2.0 || ^3.0",
112+
"php-mock/php-mock-phpunit": "^2.12",
112113
"phpstan/extension-installer": "^1.4",
113114
"phpstan/phpstan": "^2.1",
114115
"phpstan/phpstan-deprecation-rules": "^2.0",

Diff for: rector.php

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
5555
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
5656
use RectorLaravel\Rector\Class_\ModelCastsPropertyToCastsMethodRector;
57+
use RectorLaravel\Rector\Coalesce\ApplyDefaultInsteadOfNullCoalesceRector;
5758
use RectorLaravel\Rector\Empty_\EmptyToBlankAndFilledFuncRector;
5859
use RectorLaravel\Rector\FuncCall\HelperFuncCallToFacadeClassRector;
5960
use RectorLaravel\Rector\FuncCall\TypeHintTappableCallRector;
@@ -250,6 +251,9 @@ static function (array $carry, string $func): array {
250251
TypeHintTappableCallRector::class,
251252
])
252253
->withSkip([
254+
ApplyDefaultInsteadOfNullCoalesceRector::class => [
255+
__DIR__.'/src/Channels/AbstractChannel.php',
256+
],
253257
ScalarValueToConstFetchRector::class => [
254258
__DIR__.'/src/Template.php',
255259
],

Diff for: src/ExceptionNotifyServiceProvider.php

-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ public function register(): void
3737
->setupConfig()
3838
->registerAliases()
3939
->registerReportUsing();
40-
41-
$this->booting(function (): void {
42-
$this->registerReportUsing();
43-
});
4440
}
4541

4642
public function boot(): void

Diff for: tests/Channels/ChannelTest.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3+
/** @noinspection AnonymousFunctionStaticInspection */
34
/** @noinspection DebugFunctionUsageInspection */
45
/** @noinspection ForgottenDebugOutputInspection */
5-
/** @noinspection AnonymousFunctionStaticInspection */
66
/** @noinspection StaticClosureCanBeUsedInspection */
77

88
declare(strict_types=1);
@@ -23,6 +23,13 @@
2323
use Guanguans\LaravelExceptionNotify\Facades\ExceptionNotify;
2424
use Illuminate\Support\Facades\Log;
2525

26+
it('can not report', function (): void {
27+
config()->set('exception-notify.rate_limiter.max_attempts', 0);
28+
expect($this->app->make(ExceptionNotifyManager::class))
29+
->report(new RuntimeException('testing'))
30+
->toBeNull();
31+
})->group(__DIR__, __FILE__);
32+
2633
it('can listen reporting and reported event', function (): void {
2734
ExceptionNotify::reporting(function (ReportingEvent $reportingEvent): void {
2835
Log::info($reportingEvent::class);

Diff for: tests/Channels/MailChannelTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
use Illuminate\Support\Facades\Mail;
2222

2323
it('can report', function (): void {
24-
config(['exception-notify.channels.mail.render' => 'value']);
25-
config(['exception-notify.channels.mail.extender' => MailableExtender::class]);
24+
config([
25+
'exception-notify.channels.mail.render' => 'value',
26+
'exception-notify.channels.mail.extender' => MailableExtender::class,
27+
]);
2628

27-
Mail::fake();
2829
expect($this->app->make(ExceptionNotifyManager::class)->driver('mail'))
2930
->report(new RuntimeException('testing'))
3031
->toBeNull();

Diff for: tests/Collectors/CollectorTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
*/
1616

1717
use Guanguans\LaravelExceptionNotify\Collectors\RequestBasicCollector;
18+
use Illuminate\Support\Facades\Request;
1819

1920
it('can collect request basic', function (): void {
20-
\defined('LARAVEL_START') or \define('LARAVEL_START', microtime(true));
21+
// $defined = $this->getFunctionMock(class_namespace(RequestBasicCollector::class), 'defined');
22+
// $defined->expects($this->once())->willReturn(false);
23+
24+
Request::spy()
25+
->allows('server')
26+
->withArgs(['REQUEST_TIME_FLOAT'])
27+
->once()
28+
->andReturnNull();
2129

2230
expect(app(RequestBasicCollector::class))
2331
->collect()->toBeArray();
24-
})->group(__DIR__, __FILE__)->skip(\defined('LARAVEL_START'));
32+
})->group(__DIR__, __FILE__);

Diff for: tests/ExceptionNotifyManagerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3+
/** @noinspection AnonymousFunctionStaticInspection */
34
/** @noinspection NullPointerExceptionInspection */
45
/** @noinspection PhpVoidFunctionResultUsedInspection */
5-
/** @noinspection AnonymousFunctionStaticInspection */
66
/** @noinspection StaticClosureCanBeUsedInspection */
77

88
declare(strict_types=1);

Diff for: tests/FeatureTest.php

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3+
/** @noinspection AnonymousFunctionStaticInspection */
34
/** @noinspection NullPointerExceptionInspection */
45
/** @noinspection PhpVoidFunctionResultUsedInspection */
5-
/** @noinspection AnonymousFunctionStaticInspection */
66
/** @noinspection StaticClosureCanBeUsedInspection */
77

88
declare(strict_types=1);
@@ -16,8 +16,6 @@
1616
* @see https://github.com/guanguans/laravel-exception-notify
1717
*/
1818

19-
use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
20-
use Guanguans\LaravelExceptionNotify\Facades\ExceptionNotify;
2119
use Illuminate\Http\UploadedFile;
2220

2321
it('can proactive report exception', function (): void {
@@ -39,11 +37,3 @@
3937
])
4038
->assertStatus(500);
4139
})->group(__DIR__, __FILE__);
42-
43-
it('can all report exception', function (): void {
44-
collect(config('exception-notify.channels'))
45-
->keys()
46-
->each(function (string $channel): void {
47-
expect(ExceptionNotify::driver($channel))->report(new RuntimeException('testing'))->toBeNull();
48-
});
49-
})->group(__DIR__, __FILE__);

Diff for: tests/Pest.php

+3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
use Guanguans\LaravelExceptionNotifyTests\TestCase;
2222
use Illuminate\Support\Collection;
2323
use Illuminate\Support\Facades\File;
24+
use Illuminate\Support\Facades\Mail;
2425

2526
uses(TestCase::class)
2627
->beforeAll(function (): void {})
2728
->beforeEach(function (): void {
2829
Channel::flush();
30+
config()->set('exception-notify.rate_limiter.max_attempts', \PHP_INT_MAX);
2931
File::delete(glob(storage_path('logs/*.log')));
32+
Mail::fake();
3033
})
3134
->afterEach(function (): void {})
3235
->afterAll(function (): void {})

Diff for: tests/Support/ExceptionContextTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
/** @noinspection AnonymousFunctionStaticInspection */
4-
/** @noinspection StaticClosureCanBeUsedInspection */
54
/** @noinspection NullPointerExceptionInspection */
5+
/** @noinspection StaticClosureCanBeUsedInspection */
66

77
declare(strict_types=1);
88

Diff for: tests/Support/HeplersTest.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
/** @noinspection PhpUnhandledExceptionInspection */
4-
/** @noinspection PhpInternalEntityUsedInspection */
53
/** @noinspection AnonymousFunctionStaticInspection */
6-
/** @noinspection StaticClosureCanBeUsedInspection */
74
/** @noinspection NullPointerExceptionInspection */
5+
/** @noinspection PhpInternalEntityUsedInspection */
6+
/** @noinspection PhpUnhandledExceptionInspection */
7+
/** @noinspection StaticClosureCanBeUsedInspection */
88

99
declare(strict_types=1);
1010

@@ -18,16 +18,27 @@
1818
*/
1919

2020
use Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException;
21+
use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
2122
use Pest\Expectation;
2223
use function Guanguans\LaravelExceptionNotify\Support\env_explode;
2324
use function Guanguans\LaravelExceptionNotify\Support\human_bytes;
2425
use function Guanguans\LaravelExceptionNotify\Support\human_milliseconds;
2526
use function Guanguans\LaravelExceptionNotify\Support\make;
27+
use function Guanguans\LaravelExceptionNotify\Support\rescue;
2628

2729
it('will throw `InvalidArgumentException` when abstract is empty array', function (): void {
2830
make([]);
2931
})->group(__DIR__, __FILE__)->throws(InvalidArgumentException::class);
3032

33+
it('can catch a potential exception and return a default value', function (): void {
34+
expect(rescue(
35+
function (): void {
36+
throw new RuntimeException('testing');
37+
},
38+
fn (Throwable $throwable): Throwable => $throwable
39+
))->toBeInstanceOf(RuntimeException::class);
40+
})->group(__DIR__, __FILE__);
41+
3142
it('can explode env', function (): void {
3243
expect([
3344
env_explode('ENV_EXPLODE_STRING'),

Diff for: tests/Support/JsonFixerTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

33
/** @noinspection AnonymousFunctionStaticInspection */
4-
/** @noinspection StaticClosureCanBeUsedInspection */
54
/** @noinspection NullPointerExceptionInspection */
5+
/** @noinspection PhpUnhandledExceptionInspection */
6+
/** @noinspection StaticClosureCanBeUsedInspection */
67

78
declare(strict_types=1);
89

Diff for: tests/TestCase.php

+35-23
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@
3636
use Guanguans\LaravelExceptionNotify\Pipes\SprintfHtmlPipe;
3737
use Guanguans\LaravelExceptionNotify\Pipes\SprintfMarkdownPipe;
3838
use Guanguans\Notify\Foundation\Client;
39-
use Guanguans\Notify\Foundation\Response;
4039
use GuzzleHttp\MessageFormatter;
4140
use GuzzleHttp\Middleware;
41+
use GuzzleHttp\Psr7\Response;
4242
use Illuminate\Support\Facades\Log;
4343
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
44+
use phpmock\phpunit\PHPMock;
4445
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
4546

4647
class TestCase extends \Orchestra\Testbench\TestCase
4748
{
4849
use Faker;
4950
use MockeryPHPUnitIntegration;
51+
use PHPMock;
5052
use VarDumperTestTrait;
5153

5254
protected function setUp(): void
@@ -92,26 +94,52 @@ protected function defineEnvironment($app): void
9294
RequestQueryCollector::class,
9395
RequestRawFileCollector::class,
9496
]);
95-
config()->set('exception-notify.channels.log.pipes', [
97+
98+
config()->set('exception-notify.channels.mail.pipes', [
9699
AddKeywordChorePipe::with('keyword'),
97100
SprintfHtmlPipe::class,
98101
SprintfMarkdownPipe::class,
99102
FixPrettyJsonPipe::class,
100103
LimitLengthPipe::with(512),
101104
]);
102105

106+
config()->set('exception-notify.channels.stack.channels', [
107+
'dump',
108+
'log',
109+
'mail',
110+
'bark',
111+
'chanify',
112+
'dingTalk',
113+
'discord',
114+
'lark',
115+
'ntfy',
116+
'pushDeer',
117+
'slack',
118+
'telegram',
119+
'weWork',
120+
]);
121+
122+
config([
123+
'exception-notify.channels.bark.authenticator.token' => fake()->uuid(),
124+
'exception-notify.channels.chanify.authenticator.token' => fake()->uuid(),
125+
'exception-notify.channels.dingTalk.authenticator.token' => fake()->uuid(),
126+
'exception-notify.channels.discord.authenticator.webHook' => fake()->url(),
127+
'exception-notify.channels.lark.authenticator.token' => fake()->uuid(),
128+
'exception-notify.channels.ntfy.authenticator.usernameOrToken' => fake()->uuid(),
129+
'exception-notify.channels.pushDeer.authenticator.token' => fake()->uuid(),
130+
'exception-notify.channels.slack.authenticator.webHook' => fake()->url(),
131+
'exception-notify.channels.telegram.authenticator.token' => fake()->uuid(),
132+
'exception-notify.channels.weWork.authenticator.token' => fake()->uuid(),
133+
]);
134+
103135
collect(config('exception-notify.channels'))->each(static function (array $configuration, string $name): void {
104136
if ('notify' === ($configuration['driver'] ?? $name)) {
105137
config()->set(
106138
"exception-notify.channels.$name.client.extender",
107139
static fn (Client $client): Client => $client
108140
->mock([
109-
new \GuzzleHttp\Psr7\Response(body: $name),
141+
new Response(body: $name),
110142
])
111-
// ->before(
112-
// \Guanguans\Notify\Foundation\Middleware\Response::class,
113-
// Middleware::mapResponse(static fn (Response $response): Response => $response->dump()),
114-
// )
115143
->push(Middleware::log(Log::channel(), new MessageFormatter(MessageFormatter::DEBUG), 'debug'))
116144
);
117145
}
@@ -125,22 +153,6 @@ protected function defineRoutes($router): void
125153
static fn () => tap(
126154
response('proactive-report-exception'),
127155
static function (): void {
128-
config()->set('exception-notify.channels.stack.channels', [
129-
'dump',
130-
'log',
131-
'mail',
132-
'bark',
133-
'chanify',
134-
'dingTalk',
135-
'discord',
136-
'lark',
137-
'ntfy',
138-
'pushDeer',
139-
'slack',
140-
'telegram',
141-
'weWork',
142-
]);
143-
144156
ExceptionNotify::report(new RuntimeException('What happened?'));
145157
}
146158
)

0 commit comments

Comments
 (0)