Skip to content

Commit

Permalink
refactor(tests): refactor ExceptionNotifyManagerTest.php and Support/…
Browse files Browse the repository at this point in the history
…HeplersTest.php

- Refactored the ExceptionNotifyManagerTest.php and Support/HeplersTest.php files
- Simplified the usage of class namespaces
- Updated the usage of RuntimeException and InvalidArgumentException within the files
- Added new test cases for reporting exceptions
- Improved the handling of exceptions in the should not report test case
  • Loading branch information
guanguans committed May 9, 2024
1 parent f69e192 commit 43ab532
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
46 changes: 27 additions & 19 deletions tests/ExceptionNotifyManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

use Guanguans\LaravelExceptionNotify\Contracts\Channel;
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
use Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException;
use Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

it('can call', function (): void {
Expand All @@ -37,13 +40,13 @@

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

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

config()->set('exception-notify.enabled', true);
// $mockApplication = Mockery::spy(Illuminate\Foundation\Application::class);
Expand All @@ -53,34 +56,39 @@
$this->isRunningInConsole = false;
})->call(app());
expect(new ExceptionNotifyManager(app()))
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();
->report(new RuntimeException)->toBeNull();

config()->set('exception-notify.enabled', true);
$mockApplication = Mockery::mock(Application::class);
$mockApplication->allows('make')->with('config')->andReturn(config());
// $mockApplication->allows('runningInConsole')->andReturnFalse();
expect(new ExceptionNotifyManager($mockApplication))
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();
->report(new RuntimeException)->toBeNull();
})->group(__DIR__, __FILE__);

it('should report', function (): void {
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeTrue();
})->group(__DIR__, __FILE__);

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

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

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

config()->set('exception-notify.enabled', true);
config()->set('exception-notify.env', '*');
config()->set('exception-notify.except', []);
expect(app(ExceptionNotifyManager::class))->shouldReport(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeTrue();
})->group(__DIR__, __FILE__);
ExceptionNotifyManager::skipWhen(function (\Throwable $throwable) {
if (app()->environment('testing')) {
return true;
}

return Arr::first(
[
Exception::class,
],
static fn (string $type): bool => $throwable instanceof $type
);
});
expect(app(ExceptionNotifyManager::class))->shouldReport(new RuntimeException)->toBeFalse();
})->group(__DIR__, __FILE__)->depends('it should report');

it('can get default driver', function (): void {
expect(app(ExceptionNotifyManager::class))
Expand All @@ -98,7 +106,7 @@

it('will throw `InvalidArgumentException`', function (): void {
app(ExceptionNotifyManager::class)->driver('foo');
})->group(__DIR__, __FILE__)->throws(\Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException::class);
})->group(__DIR__, __FILE__)->throws(InvalidArgumentException::class);

it('can create custom driver', function (): void {
app(ExceptionNotifyManager::class)->extend('foo', static fn (): Channel => new class implements Channel {
Expand Down
7 changes: 5 additions & 2 deletions tests/Support/HeplersTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpInternalEntityUsedInspection */
/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection StaticClosureCanBeUsedInspection */
Expand All @@ -16,10 +17,12 @@
* @see https://github.com/guanguans/laravel-exception-notify
*/

use Guanguans\LaravelExceptionNotify\ReportUsingCreator;
use Guanguans\LaravelExceptionNotify\CollectorManager;
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;

it('can make object', function (): void {
expect(make(ReportUsingCreator::class))->toBeInstanceOf(ReportUsingCreator::class);
expect(make(ExceptionNotifyManager::class))->toBeInstanceOf(ExceptionNotifyManager::class)
->and(make(CollectorManager::class))->toBeInstanceOf(CollectorManager::class);
})->group(__DIR__, __FILE__);

it('can explode env', function (): void {
Expand Down

0 comments on commit 43ab532

Please sign in to comment.