Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MakeExceptionCommand for API exceptions #14

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});