Skip to content

Commit

Permalink
Add exception handler command and refactor command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cybersai committed Jan 20, 2024
1 parent fc0d1c1 commit 564db53
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/Commands/ActionMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ class ActionMakeCommand extends ActionCommand
{
protected $signature = 'make:ussd-action
{name : The name of the USSD Action}
{--init : Create the class as the initial USSD action}
{--force : Create the class even if USSD action already exists}';
}
24 changes: 24 additions & 0 deletions src/Commands/ExceptionHandlerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Sparors\Ussd\Commands;

class ExceptionHandlerCommand extends GeneratorCommand
{
protected $type = 'USSD Exception Handler';

protected $description = 'Create a new USSD exception handler class';

protected $signature = 'ussd:exception-handler
{name : The name of the USSD Exception Handler}
{--force : Create the class even if USSD exception handler already exists}';

protected function getStub()
{
return __DIR__.'/../../stubs/exception-handler.stub';
}

protected function getDefaultNamespace($rootNamespace)
{
return $this->extendNamespace('ExceptionHandlers');
}
}
10 changes: 10 additions & 0 deletions src/Commands/ExceptionHandlerMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sparors\Ussd\Commands;

class ExceptionHandlerMakeCommand extends ExceptionHandlerCommand
{
protected $signature = 'make:ussd-exception-handler
{name : The name of the USSD Exception Handler}
{--force : Create the class even if USSD exception handler already exists}';
}
2 changes: 2 additions & 0 deletions src/Commands/StateMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ class StateMakeCommand extends StateCommand
{
protected $signature = 'make:ussd-state
{name : The name of the USSD State}
{--init : Create the class as an initial USSD state}
{--cont : Create the class as a continuing USSD state}
{--force : Create the class even if USSD state already exists}';
}
4 changes: 4 additions & 0 deletions src/UssdServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Sparors\Ussd\Commands\ResponseMakeCommand;
use Illuminate\Foundation\Console\AboutCommand;
use Sparors\Ussd\Commands\ConfiguratorMakeCommand;
use Sparors\Ussd\Commands\ExceptionHandlerCommand;
use Sparors\Ussd\Commands\ExceptionHandlerMakeCommand;

class UssdServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -61,6 +63,8 @@ protected function bootForConsole()
DecisionMakeCommand::class,
ConfiguratorCommand::class,
ConfiguratorMakeCommand::class,
ExceptionHandlerCommand::class,
ExceptionHandlerMakeCommand::class,
]);

AboutCommand::add('USSD', [
Expand Down
14 changes: 14 additions & 0 deletions stubs/exception-handler.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace {{ namespace }};

use Exception;
use Sparors\Ussd\Contracts\ExceptionHandler;

class {{ class }} implements ExceptionHandler
{
public function handle(Exception $exception): string
{
return $exception->getMessage();
}
}
60 changes: 18 additions & 42 deletions tests/Integration/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,43 @@

final class CommandTest extends TestCase
{
public function test_action_command_print_out_success_when_class_does_not_exists()
/** @dataProvider data_available_make_commands */
public function test_action_command_print_out_success_when_class_does_not_exists($command, $class)
{
File::shouldReceive('exists')->once();
File::shouldReceive('isDirectory')->once();
File::shouldReceive('makeDirectory')->once();
File::shouldReceive('get')->once();
File::shouldReceive('put')->once();

$this->artisan('ussd:action', ['name' => 'Save'])
->expectsOutputToContain('USSD Action [app/Ussd/Actions/Save.php] created successfully.')
$this->artisan($command, ['name' => $class])
->expectsOutputToContain($class)
->assertExitCode(0);
}

public function test_action_command_print_out_error_when_class_exists()
/** @dataProvider data_available_make_commands */
public function test_action_command_print_out_error_when_class_exists($command, $class)
{
File::shouldReceive('exists')->once()->andReturn(true);

$this->artisan('ussd:action', ['name' => 'Save'])
->expectsOutputToContain('USSD Action already exists.')
$this->artisan($command, ['name' => $class])
->expectsOutputToContain('already exists.')
->assertExitCode(0);
}

/** @dataProvider data_available_make_commands */
public function test_make_commands_are_available($command)
{
$this->artisan($command, ['name' => 'NameLess'])->assertExitCode(0);
}

public static function data_available_make_commands()
{
return [
['ussd:state'],
['make:ussd-state'],
['ussd:action'],
['make:ussd-action'],
['ussd:decision'],
['make:ussd-decision'],
['ussd:configurator'],
['make:ussd-configurator'],
['ussd:state', 'WelcomeState'],
['make:ussd-state', 'WelcomeState'],
['ussd:action', 'MenuAction'],
['make:ussd-action', 'MenuAction'],
['ussd:decision', 'StrictEqual'],
['make:ussd-decision', 'StrictEqual'],
['ussd:configurator', 'DynamicConfigurator'],
['make:ussd-configurator', 'DynamicConfigurator'],
['ussd:exception-handler', 'CatchExceptionHandler'],
['make:ussd-exception-handler', 'CatchExceptionHandler'],
];
}

public function test_state_command_print_out_success_when_class_does_not_exists()
{
File::shouldReceive('exists')->once();
File::shouldReceive('isDirectory')->once();
File::shouldReceive('makeDirectory')->once();
File::shouldReceive('get')->once();
File::shouldReceive('put')->once();

$this->artisan('ussd:state', ['name' => 'Welcome'])
->expectsOutputToContain('USSD State [app/Ussd/States/Welcome.php] created successfully.')
->assertExitCode(0);
}

public function test_state_command_print_out_error_when_class_exists()
{
File::shouldReceive('exists')->once()->andReturn(true);

$this->artisan('ussd:state', ['name' => 'Welcome'])
->expectsOutputToContain('USSD State already exists.')
->assertExitCode(0);
}
}

0 comments on commit 564db53

Please sign in to comment.