Skip to content

Commit

Permalink
test: Update tests and refactor code
Browse files Browse the repository at this point in the history
- Removed unnecessary code related to GuzzleHttp in NotifyChannelTest.php
- Added a new test file DefaultNotifyClientExtenderTest.php
- Refactored ExceptionNotifyManagerTest.php
- Removed ExceptionNotify usage in FeatureTest.php
- Updated configuration settings in TestCase.php
  • Loading branch information
guanguans committed May 8, 2024
1 parent 029ab4f commit 0596277
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/ExceptionNotifyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ protected function createDriver($driver): Channel
return $this->{$method}($configRepository);
}

if (class_exists($class = "Guanguans\\LaravelExceptionNotify\\Channels\\{$studlyName}Channel")) {
if (class_exists($class = "\\Guanguans\\LaravelExceptionNotify\\Channels\\{$studlyName}Channel")) {
return new $class($configRepository);
}

throw new InvalidArgumentException("Driver [$driver] not supported.");
throw new InvalidArgumentException("Driver [$studlyName] not supported.");
}

/**
Expand Down
9 changes: 1 addition & 8 deletions tests/Channels/NotifyChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

use Guanguans\LaravelExceptionNotify\Channels\NotifyChannel;
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
use Guanguans\Notify\Foundation\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Illuminate\Config\Repository;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -30,12 +28,7 @@
})->group(__DIR__, __FILE__)->throws(\Guanguans\LaravelExceptionNotify\Exceptions\InvalidArgumentException::class);

it('can report', function (): void {
config()->set('exception-notify.channels.bark.authenticator.token', 'token');
config()->set('exception-notify.channels.bark.client.extender', static fn (Client $client): Client => $client->mock([
(new HttpFactory)->createResponse(200, '{"code":200,"message":"success","timestamp":1708331409}'),
]));

expect($this->app->make(ExceptionNotifyManager::class)->driver('bark'))
expect(app(ExceptionNotifyManager::class)->driver('bark'))
->report('report')
->toBeInstanceOf(ResponseInterface::class);
})->group(__DIR__, __FILE__);
3 changes: 2 additions & 1 deletion tests/Commands/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/

use Guanguans\LaravelExceptionNotify\Commands\TestCommand;
use Symfony\Component\Console\Command\Command;
use function Pest\Laravel\artisan;

it('can test for exception-notify', function (): void {
config()->set('exception-notify.enabled', false);
artisan(TestCommand::class)->assertSuccessful();
artisan(TestCommand::class)->assertExitCode(Command::SUCCESS);
})->group(__DIR__, __FILE__);

it('will throws RuntimeException', function (): void {
Expand Down
24 changes: 24 additions & 0 deletions tests/DefaultNotifyClientExtenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 guanguans<[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/guanguans/laravel-exception-notify
*/

namespace Guanguans\LaravelExceptionNotify\Tests;

use Guanguans\LaravelExceptionNotify\DefaultNotifyClientExtender;
use Guanguans\Notify\Foundation\Client;

it('can extend notify client', function (): void {
expect(app(DefaultNotifyClientExtender::class)(new Client))->toBeInstanceOf(Client::class);
})->group(__DIR__, __FILE__);
15 changes: 7 additions & 8 deletions tests/ExceptionNotifyManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection NullPointerExceptionInspection */
/** @noinspection PhpVoidFunctionResultUsedInspection */
/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

Expand All @@ -18,7 +20,6 @@
use Guanguans\LaravelExceptionNotify\ExceptionNotifyManager;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Str;
use function Pest\Faker\faker;

it('can call', function (): void {
ExceptionNotifyManager::macro('foo', fn ($param) => $param);
Expand All @@ -45,20 +46,19 @@
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();

config()->set('exception-notify.enabled', true);
$mockApplication = Mockery::spy(Illuminate\Foundation\Application::class);
// $mockApplication = Mockery::spy(Illuminate\Foundation\Application::class);
// $mockApplication->allows()->make('config')->atLeast()->once()->andReturn(config());
// $mockApplication->allows()->runningInConsole()->atLeast()->once()->andReturnFalse();

/** @noinspection PhpVoidFunctionResultUsedInspection */
expect(new ExceptionNotifyManager($mockApplication))
(function (): void {
$this->isRunningInConsole = false;
})->call(app());
expect(new ExceptionNotifyManager(app()))
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();

config()->set('exception-notify.enabled', true);
$mockApplication = Mockery::mock(Application::class);
$mockApplication->allows('make')->with('config')->andReturn(config());
// $mockApplication->allows('runningInConsole')->andReturnFalse();

/** @noinspection PhpVoidFunctionResultUsedInspection */
expect(new ExceptionNotifyManager($mockApplication))
->report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException)->toBeNull();
})->group(__DIR__, __FILE__);
Expand Down Expand Up @@ -88,7 +88,6 @@
})->group(__DIR__, __FILE__);

it('can attempt key', function (): void {
// $uuid = faker()->uuid();
$uuid = Str::uuid()->toString();
expect(fn () => $this->attempt($uuid, 3))
->call(app(ExceptionNotifyManager::class))->toBeTrue()
Expand Down
38 changes: 38 additions & 0 deletions tests/Faker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 guanguans<[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/guanguans/laravel-exception-notify
*/

namespace Guanguans\LaravelExceptionNotify\Tests;

use Faker\Factory;
use Faker\Generator;

trait Faker
{
final protected function faker(string $locale = 'en_US'): Generator
{
/**
* @var array<string, Generator> $fakers
*/
static $fakers = [];

if (!\array_key_exists($locale, $fakers)) {
$faker = Factory::create($locale);

$faker->seed(9001);

$fakers[$locale] = $faker;
}

return $fakers[$locale];
}
}
2 changes: 0 additions & 2 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* @see https://github.com/guanguans/laravel-exception-notify
*/

use Guanguans\LaravelExceptionNotify\Facades\ExceptionNotify;
use Illuminate\Http\UploadedFile;

it('can report exception', function (): void {
ExceptionNotify::report(new \Guanguans\LaravelExceptionNotify\Exceptions\RuntimeException('What happened?'), ['lark']);
$this
->post('report-exception?foo=bar', [
'bar' => 'baz',
Expand Down
14 changes: 9 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
use Guanguans\LaravelExceptionNotify\Pipes\ReplaceStrPipe;
use Guanguans\LaravelExceptionNotify\Pipes\SprintfHtmlPipe;
use Guanguans\LaravelExceptionNotify\Pipes\SprintfMarkdownPipe;
use Guanguans\Notify\Foundation\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

class TestCase extends \Orchestra\Testbench\TestCase
{
use MockeryPHPUnitIntegration;
use VarDumperTestTrait;
use Faker;

protected function setUp(): void
{
Expand All @@ -68,9 +71,10 @@ protected function getPackageProviders($app): array
protected function defineEnvironment($app): void
{
config()->set('exception-notify.job.queue', 'exception-notify');
// config()->set('exception-notify.channels.mail.dsn', 'smtp://[email protected]:[email protected]:465?verify_peer=0');
// config()->set('exception-notify.channels.mail.from', '[email protected]');
// config()->set('exception-notify.channels.mail.to', '[email protected]');
config()->set('exception-notify.channels.bark.authenticator.token', $this->faker()->uuid());
config()->set('exception-notify.channels.bark.client.extender', static fn (Client $client): Client => $client->mock([
(new HttpFactory)->createResponse(200, '{"code":200,"message":"success","timestamp":1708331409}'),
]));
config()->set('exception-notify.collectors', [
ApplicationCollector::class,
ChoreCollector::class,
Expand All @@ -80,12 +84,12 @@ protected function defineEnvironment($app): void
PhpInfoCollector::class,
RequestBasicCollector::class,
RequestCookieCollector::class,
RequestRawFileCollector::class,
RequestFileCollector::class,
RequestHeaderCollector::class,
RequestMiddlewareCollector::class,
RequestPostCollector::class,
RequestQueryCollector::class,
RequestRawFileCollector::class,
RequestServerCollector::class,
RequestSessionCollector::class,
]);
Expand All @@ -102,7 +106,7 @@ protected function defineEnvironment($app): void
protected function defineRoutes($router): void
{
$router->any('report-exception', static fn () => tap(response('report-exception'), static function (): void {
ExceptionNotify::report(new RuntimeException('What happened?'), ['lark', 'dump']);
ExceptionNotify::report(new RuntimeException('What happened?'), ['dump', 'log', 'bark', 'lark']);
}));

$router->any('exception', static fn () => tap(response('exception'), static function (): void {
Expand Down

0 comments on commit 0596277

Please sign in to comment.