Skip to content

Commit

Permalink
Add MakeExceptionCommand for API exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasss93 committed Sep 23, 2023
1 parent b2d2768 commit 86e113f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Console/MakeExceptionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Nutgram\Laravel\Console;

class MakeExceptionCommand extends BaseMakeCommand
{
protected $signature = 'nutgram:make:exception {name : Exception name}';

protected $description = 'Create a new Nutgram API Exception';

/**
* Return the sub directory name
* @return string
*/
protected function getSubDirName(): string
{
return 'Exceptions';
}

/**
* Return the stub file path
* @return string
*/
protected function getStubPath(): string
{
return __DIR__.'/../Stubs/Exception.stub';
}
}
1 change: 1 addition & 0 deletions src/NutgramServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function boot(): void
Console\HookSetCommand::class,
Console\ListCommand::class,
Console\MakeCommandCommand::class,
Console\MakeExceptionCommand::class,
Console\MakeConversationCommand::class,
Console\MakeHandlerCommand::class,
Console\MakeMiddlewareCommand::class,
Expand Down
12 changes: 12 additions & 0 deletions src/Stubs/Exception.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Telegram\{{ namespace }};

use SergiX44\Nutgram\Exception\ApiException;
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Exceptions\TelegramException;

class {{ name }} extends ApiException
{
public static ?string $pattern = '.*enter a string contained in the api error message.*';
}
19 changes: 19 additions & 0 deletions tests/Commands/MakeExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Support\Facades\File;
use Nutgram\Laravel\Console\MakeExceptionCommand;

beforeEach(function () {
File::deleteDirectory(config('nutgram.namespace'));
});

test('nutgram:make:exception makes an exception', function () {
$this->artisan(MakeExceptionCommand::class, ['name' => 'MyException'])
->expectsOutput('Nutgram Exception created successfully.')
->assertSuccessful();

expect(config('nutgram.namespace').'/Exceptions/MyException.php')
->toBeFile()
->getFileContent()
->toContain('class MyException');
});

0 comments on commit 86e113f

Please sign in to comment.