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

composer.json

Lines changed: 1 addition & 0 deletions
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",

rector.php

Lines changed: 4 additions & 0 deletions
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
],

src/ExceptionNotifyServiceProvider.php

Lines changed: 0 additions & 4 deletions
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

tests/Channels/ChannelTest.php

Lines changed: 8 additions & 1 deletion
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);

tests/Channels/MailChannelTest.php

Lines changed: 4 additions & 3 deletions
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();

tests/Collectors/CollectorTest.php

Lines changed: 10 additions & 2 deletions
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__);

tests/ExceptionNotifyManagerTest.php

Lines changed: 1 addition & 1 deletion
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);

tests/FeatureTest.php

Lines changed: 1 addition & 11 deletions
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__);

tests/Pest.php

Lines changed: 3 additions & 0 deletions
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 {})

tests/Support/ExceptionContextTest.php

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)