diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml deleted file mode 100644 index e93bb0d..0000000 --- a/.github/workflows/php.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: PHP Composer - -on: - push: - branches: [ main, develop ] - pull_request: - branches: [ main, develop ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - name: Validate composer.json and composer.lock - run: composer validate --strict - - - name: Cache Composer packages - id: composer-cache - uses: actions/cache@v2 - with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-php- - - - name: Install dependencies - run: composer install --prefer-dist --no-progress - - - name: Execute tests (Unit and Feature tests) via PHPUnit with coverage report generated - run: vendor/bin/phpunit --coverage-clover coverage.xml - - - name: Upload code coverage to Codecov - uses: codecov/codecov-action@v1 diff --git a/.gitignore b/.gitignore index db2e1a0..6d1c5b4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /vendor composer.lock .phpunit.result.cache -testbench/ \ No newline at end of file +testbench/ +/coverage \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 22d840e..1a28e31 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: php php: - - '7.2' - '7.3' - '7.4' + - '8.0' sudo: false @@ -16,4 +16,4 @@ before_script: - travis_retry composer update --no-interaction --prefer-dist script: - - vendor/bin/phpunit \ No newline at end of file + - vendor/bin/phpunit diff --git a/changelog.md b/changelog.md index 543e16a..a7168d4 100644 --- a/changelog.md +++ b/changelog.md @@ -4,7 +4,16 @@ All notable changes to `laravel Ussd` will be documented in this file. ## [Unreleased] +## [v2.3.0] - 2021-06-27 +### Added +- Add missen test to improve coverage +### Change +- Minor bug fixes +- Upgrade test dependencies + + ## [v2.2.0] - 2020-09-11 +### Added - Add Support for laravel 8 ## [v2.1.0] - 2020-05-26 @@ -35,7 +44,7 @@ All notable changes to `laravel Ussd` will be documented in this file. - Updated Readme - Updated changelog -## [v0.1.0] - 2020-05-02 +## [v0.1.0] - 2020-05-02 ### Added - Ussd Package Project with README, contributing, changelog, license, etc. - State class to define what should occur at various stages when user navigates @@ -50,7 +59,8 @@ All notable changes to `laravel Ussd` will be documented in this file. - Ussd config to allow developers customize behaviour - Ussd service Provider class to allow laravel know how to integrate the package -[Unreleased]: ../../compare/v2.2.0...HEAD +[Unreleased]: ../../compare/v2.3.0...HEAD +[v2.3.0]: ../../compare/v2.2.0...v2.3.0 [v2.2.0]: ../../compare/v2.1.0...v2.2.0 [v2.1.0]: ../../compare/v2.0.0...v2.1.0 [v2.0.0]: ../../compare/v1.0.0...v2.0.0 diff --git a/composer.json b/composer.json index 35d9ffa..d7008f3 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,9 @@ "illuminate/support": "~5|~6|~7|~8" }, "require-dev": { - "phpunit/phpunit": "^8.0", + "phpunit/phpunit": "^9.5", "mockery/mockery": "^1.1", - "orchestra/testbench": "~3|~4|~5", + "orchestra/testbench": "~3|~4|~5|~6", "sempro/phpunit-pretty-print": "^1.0" }, "autoload": { diff --git a/phpunit.xml b/phpunit.xml index ce34605..2d770b8 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,22 +1,25 @@ - - - - ./tests/ - - - - - src/ - - + + + + src/ + + + + + ./tests/ + + diff --git a/readme.md b/readme.md index b08b7e3..39cb8cf 100644 --- a/readme.md +++ b/readme.md @@ -113,11 +113,11 @@ class MakePayment extends Action $response = Http::post('/payment', [ 'phone_number' => $this->record->phoneNumber ]); - + if ($response->ok()) { return PaymentSuccess::class; } - + return PaymentError::class; } } diff --git a/src/Decision.php b/src/Decision.php index 479c4a3..d780e52 100644 --- a/src/Decision.php +++ b/src/Decision.php @@ -9,7 +9,7 @@ class Decision /** @var mixed */ protected $argument; - + /** @var string|null */ protected $output; @@ -127,7 +127,7 @@ public function in(array $array, string $output, bool $strict = false): self { return $this->setOutputForCondition( function () use ($array, $strict) { - return in_array($array, $this->argument, $strict); + return in_array($this->argument, $array, $strict); }, $output ); @@ -135,10 +135,10 @@ function () use ($array, $strict) { public function custom(callable $function, string $output): self { - $func = function () use ($function) { + $func = function () use ($function) { return $function($this->argument); }; - + return $this->setOutputForCondition($func, $output); } diff --git a/src/Menu.php b/src/Menu.php index 6676f1e..adb288d 100644 --- a/src/Menu.php +++ b/src/Menu.php @@ -5,7 +5,7 @@ class Menu { const NUMBERING_ALPHABETIC_LOWER = 'alphabetic_lower'; - const NUMBERING_ALPHABETIC_UPPER = 'alphabetic_lower'; + const NUMBERING_ALPHABETIC_UPPER = 'alphabetic_upper'; const NUMBERING_EMPTY = 'empty'; const NUMBERING_NUMERIC = 'numeric'; diff --git a/src/Record.php b/src/Record.php index 0ef6703..bffb352 100644 --- a/src/Record.php +++ b/src/Record.php @@ -36,6 +36,15 @@ protected function getTtl($ttl) return $ttl ?? config('ussd.cache_ttl'); } + /** + * @param string $default + * @return mixed + */ + protected function getDefault($default) + { + return $default ?? config('ussd.cache_default'); + } + /** * @param array $keys * @return array @@ -83,7 +92,7 @@ public function has($key) */ public function set($key, $value, $ttl = null) { - return $this->cache->set($this->getKey($key), $value, $ttl); + return $this->cache->set($this->getKey($key), $value, $this->getTtl($ttl)); } /** @@ -95,7 +104,7 @@ public function set($key, $value, $ttl = null) */ public function setMultiple($values, $ttl = null) { - return $this->cache->setMultiple($this->getValues($values), $ttl); + return $this->cache->setMultiple($this->getValues($values), $this->getTtl($ttl)); } /** @@ -107,7 +116,7 @@ public function setMultiple($values, $ttl = null) */ public function get($key, $default = null) { - return $this->cache->get($this->getKey($key), $default); + return $this->cache->get($this->getKey($key), $this->getDefault($default)); } /** @@ -122,7 +131,7 @@ public function get($key, $default = null) public function getMultiple($keys, $default = null) { return array_values( - (array)$this->cache->getMultiple($this->getKeys($keys), $default) + (array)$this->cache->getMultiple($this->getKeys($keys), $this->getDefault($default)) ); } @@ -150,7 +159,7 @@ public function deleteMultiple($keys) /** * Increment the value of an item in the cache. - * + * * @since v2.0.0 * @param string $key * @param mixed $value @@ -209,7 +218,7 @@ public function __invoke($argument) if (is_string($argument)) { return $this->get($argument, config('ussd.cache_default')); } - + if (is_array($argument)) { $this->setMultiple($argument, config('ussd.cache_ttl')); } diff --git a/src/Ussd.php b/src/Ussd.php index cc4816e..15d215e 100644 --- a/src/Ussd.php +++ b/src/Ussd.php @@ -8,7 +8,7 @@ class Ussd { /** * An instance on Application - * + * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; @@ -18,25 +18,6 @@ public function __construct($app) $this->app = $app; } - /** - * @param string $id; - * @param string $store - * @return Record - */ - public function record(string $id, string $store = null) - { - return new Record(Cache::store($store), $id); - } - - /** - * @param string $menu - * @return Menu - */ - public function menu(string $menu = '') - { - return new Menu($menu); - } - /** * @return Machine */ diff --git a/tests/ActionTest.php b/tests/ActionTest.php index f325c27..7c0547c 100644 --- a/tests/ActionTest.php +++ b/tests/ActionTest.php @@ -2,13 +2,12 @@ namespace Sparors\Ussd\Tests; -use Orchestra\Testbench\TestCase; -use Sparors\Ussd\State; +use PHPUnit\Framework\TestCase; + -/** @internal */ class ActionTest extends TestCase { - public function testAction() + public function test_it_runs_successfully() { $action = new RunAction(); $this->assertEquals(ByeState::class, $action->run()); diff --git a/tests/Commands/TestActionCommand.php b/tests/Commands/ActionCommandTest.php similarity index 55% rename from tests/Commands/TestActionCommand.php rename to tests/Commands/ActionCommandTest.php index 80ae62d..3eaab33 100644 --- a/tests/Commands/TestActionCommand.php +++ b/tests/Commands/ActionCommandTest.php @@ -2,25 +2,12 @@ namespace Sparors\Ussd\Tests\Commands; +use Sparors\Ussd\Tests\TestCase; use Illuminate\Support\Facades\File; -use Orchestra\Testbench\TestCase; -use Sparors\Ussd\UssdServiceProvider; -class TestActionCommand extends TestCase +class ActionCommandTest extends TestCase { - /** - * Tell Testbench to use this package. - * - * @param $app - * - * @return array - */ - public function getPackageProviders($app) - { - return [UssdServiceProvider::class]; - } - - public function testItPrintOutSuccessWhenClassDoesNotExists() + public function test_it_print_out_success_when_class_does_not_exists() { File::shouldReceive('exists')->once()->andReturn(false); File::shouldReceive('isDirectory')->once(); @@ -31,10 +18,10 @@ public function testItPrintOutSuccessWhenClassDoesNotExists() ->assertExitCode(0); } - public function testItPrintOutErrorWhenClassExists() + public function test_it_print_out_error_when_class_exists() { File::shouldReceive('exists')->once()->andReturn(true); - $this->artisan('ussd:state', ['name' => 'save']) + $this->artisan('ussd:action', ['name' => 'save']) ->expectsOutput('File Already exists !') ->assertExitCode(0); } diff --git a/tests/Commands/TestStateCommand.php b/tests/Commands/StateCommandTest.php similarity index 61% rename from tests/Commands/TestStateCommand.php rename to tests/Commands/StateCommandTest.php index 26bb176..a261cbe 100644 --- a/tests/Commands/TestStateCommand.php +++ b/tests/Commands/StateCommandTest.php @@ -2,25 +2,12 @@ namespace Sparors\Ussd\Tests\Commands; +use Sparors\Ussd\Tests\TestCase; use Illuminate\Support\Facades\File; -use Orchestra\Testbench\TestCase; -use Sparors\Ussd\UssdServiceProvider; -class TestStateCommand extends TestCase +class StateCommandTest extends TestCase { - /** - * Tell Testbench to use this package. - * - * @param $app - * - * @return array - */ - public function getPackageProviders($app) - { - return [UssdServiceProvider::class]; - } - - public function testItPrintOutSuccessWhenClassDoesNotExists() + public function test_it_print_out_success_when_class_does_not_exists() { File::shouldReceive('exists')->once()->andReturn(false); File::shouldReceive('isDirectory')->once(); @@ -31,7 +18,7 @@ public function testItPrintOutSuccessWhenClassDoesNotExists() ->assertExitCode(0); } - public function testItPrintOutErrorWhenClassExists() + public function test_it_print_out_error_when_class_exists() { File::shouldReceive('exists')->once()->andReturn(true); $this->artisan('ussd:state', ['name' => 'welcome']) diff --git a/tests/DecisionTest.php b/tests/DecisionTest.php index a4b090a..81be085 100644 --- a/tests/DecisionTest.php +++ b/tests/DecisionTest.php @@ -2,64 +2,64 @@ namespace Sparors\Ussd\Tests; -use Orchestra\Testbench\TestCase; use Sparors\Ussd\Decision; +use PHPUnit\Framework\TestCase; class DecisionTest extends TestCase { - public function testReturnsTheOutcome() + public function test_it_returns_the_outcome() { $decision = new Decision('argument'); $this->assertNull($decision->outcome()); } - public function testCanCompareArgumentUsingEqual() + public function test_it_can_compare_argument_using_equal() { $decision = new Decision(1); $this->assertEquals('True', $decision->equal('1', 'True') ->outcome()); } - public function testCanCompareArgumentUsingStrictEqual() + public function test_it_can_compare_argument_using_strict_equal() { $decision = new Decision(1); $this->assertNull($decision ->equal('1', 'True', true)->outcome()); } - public function testCanCompareArgumentUsingNumeric() + public function test_it_can_compare_argument_using_numeric() { $decision = new Decision('1'); $this->assertEquals('True', $decision->numeric('True')->outcome()); } - public function testCanCompareArgumentUsingInteger() + public function test_it_can_compare_argument_using_integer() { $decision = new Decision(1); $this->assertEquals('True', $decision->integer('True')->outcome()); } - public function testCanCompareArgumentUsingAmount() + public function test_it_can_compare_argument_using_amount() { $decision = new Decision(11.2); $this->assertEquals('True', $decision->amount('True')->outcome()); } - public function testCanCompareArgumentUsingLength() + public function test_it_can_compare_argument_using_length() { $decision = new Decision('one'); $this->assertEquals('True', $decision->length(3, 'True') ->outcome()); } - public function testCanCompareArgumentUsingPhoneNumber() + public function test_it_can_compare_argument_using_phone_number() { $decision = new Decision('0241212123'); $this->assertEquals('True', $decision->phoneNumber('True') ->outcome()); } - public function testCanUseYourCustomCondition() + public function test_it_can_use_your_custom_conditional_logic() { $decision = new Decision(['active' => true]); $this->assertEquals('Custom', $decision @@ -68,33 +68,45 @@ public function testCanUseYourCustomCondition() }, 'Custom')->outcome()); } - public function testCanCompareArgumentBetweenTwoNumbers() + public function test_it_can_compare_argument_between_two_numbers() { $decision = new Decision(3); $this->assertEquals('True', $decision->between(1, 10, 'True')->outcome()); } - public function testCanUseAnyWildCards() + public function test_it_can_compare_argument_with_values_in_an_array() + { + $decision = new Decision('second'); + $this->assertEquals('True', $decision->in(['first', 'second', 'third'], 'True')->outcome()); + } + + public function test_it_can_compare_argument_with_values_in_an_array_strictly() + { + $decision = new Decision(2); + $this->assertEquals('True', $decision->in(['1', 2, '3'], 'True', true)->outcome()); + } + + public function test_it_can_use_any_wild_cards() { $decision = new Decision('5'); $this->assertEquals('True', $decision->any('True')->outcome()); } - public function testDecisionCanBeChain() + public function test_decision_can_be_chain() { $decision = new Decision('45'); $this->assertEquals('True', $decision ->phoneNumber('Phone')->any('True')->outcome()); } - public function testItIgnoresFollowingDecisionWhenConditionIsMet() + public function test_it_ignores_following_decision_when_condition_is_met() { $decision = new Decision('1234'); $this->assertEquals('First', $decision ->numeric('First')->any('Second')->outcome()); } - public function testItReturnsNullWhenNoConditionIsMet() + public function test_it_returns_null_when_no_condition_is_met() { $decision = new Decision('super'); $this->assertNull($decision->numeric('Numeric') diff --git a/tests/Facades/UssdTest.php b/tests/Facades/UssdTest.php new file mode 100644 index 0000000..329a876 --- /dev/null +++ b/tests/Facades/UssdTest.php @@ -0,0 +1,21 @@ +assertInstanceOf(Machine::class, UssdFacade::machine()); + } + + public function test_it_injects_machine_using_the_service_container() + { + $this->assertInstanceOf(Ussd::class, app('ussd')); + } +} diff --git a/tests/HasManipulatorsTest.php b/tests/HasManipulatorsTest.php index f3b30f9..fc7b5ce 100644 --- a/tests/HasManipulatorsTest.php +++ b/tests/HasManipulatorsTest.php @@ -2,7 +2,6 @@ namespace Sparors\Ussd\Tests; -use Illuminate\Support\Facades\Request; use Orchestra\Testbench\TestCase; use Sparors\Ussd\Decision; use Sparors\Ussd\HasManipulators; @@ -10,7 +9,7 @@ class HasManipulatorsTest extends TestCase { - public function testSetSessionId() + public function test_it_sets_session_id() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); @@ -18,7 +17,7 @@ public function testSetSessionId() $this->assertEquals('1234', $manipulator->sessionId); } - public function testSetSessionIdFromRequest() + public function test_it_sets_session_id_from_request() { request()->merge(['session_id' => '1234']); /** @var \Sparors\Ussd\HasManipulators */ @@ -27,7 +26,7 @@ public function testSetSessionIdFromRequest() $this->assertEquals('1234', $manipulator->sessionId); } - public function testSetPhoneNumber() + public function test_it_sets_phone_number() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); @@ -35,7 +34,7 @@ public function testSetPhoneNumber() $this->assertEquals('0545112466', $manipulator->phoneNumber); } - public function testSetPhoneNumberFromRequest() + public function test_it_sets_phone_number_from_request() { request()->merge(['phone_number' => '0545112466']); /** @var \Sparors\Ussd\HasManipulators */ @@ -44,7 +43,7 @@ public function testSetPhoneNumberFromRequest() $this->assertEquals('0545112466', $manipulator->phoneNumber); } - public function testSetNetwork() + public function test_it_sets_network() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); @@ -52,7 +51,7 @@ public function testSetNetwork() $this->assertEquals('MTN', $manipulator->network); } - public function testSetNetworkFromRequest() + public function test_it_sets_network_from_request() { request()->merge(['network' => 'MTN']); /** @var \Sparors\Ussd\HasManipulators */ @@ -61,7 +60,7 @@ public function testSetNetworkFromRequest() $this->assertEquals('MTN', $manipulator->network); } - public function testSetInput() + public function test_it_sets_input() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); @@ -69,16 +68,16 @@ public function testSetInput() $this->assertEquals('1', $manipulator->input); } - public function testSetInputFromRequest() + public function test_it_sets_input_from_request() { request()->merge(['input' => '1']); /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); - $manipulator->setSessionIdFromRequest('input'); - $this->assertEquals('1', $manipulator->sessionId); + $manipulator->setInputFromRequest('input'); + $this->assertEquals('1', $manipulator->input); } - public function testSetStore() + public function test_it_sets_store() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); @@ -86,7 +85,7 @@ public function testSetStore() $this->assertEquals('redis', $manipulator->store); } - public function testSet() + public function test_set_multiple_values_at_once() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); @@ -104,7 +103,7 @@ public function testSet() $this->assertEquals('0545112466', $manipulator->phoneNumber); } - public function testSetFromRequest() + public function test_set_multiple_values_at_once_from_request() { request()->merge([ 'op_network' => 'MTN', @@ -127,7 +126,7 @@ public function testSetFromRequest() $this->assertEquals('0545112466', $manipulator->phoneNumber); } - public function testSetInitialStateCanTakeString() + public function test_it_sets_initial_state_with_string_of_classname() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); @@ -135,7 +134,7 @@ public function testSetInitialStateCanTakeString() $this->assertEquals('Sparors\Ussd\Record', $manipulator->initialState); } - public function testSetInitialStateCanTakeClassInstance() + public function test_it_sets_initial_state_with_class_instance() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); @@ -144,7 +143,15 @@ public function testSetInitialStateCanTakeClassInstance() $this->assertEquals('Sparors\Ussd\Decision', $manipulator->initialState); } - public function testSetResponse() + public function test_it_sets_initial_state_to_null_if_state_is_invalid() + { + /** @var \Sparors\Ussd\HasManipulators */ + $manipulator = $this->getMockForTrait(HasManipulators::class); + $manipulator->setInitialState(1); + $this->assertNull($manipulator->initialState); + } + + public function test_it_set_response_formatting() { /** @var \Sparors\Ussd\HasManipulators */ $manipulator = $this->getMockForTrait(HasManipulators::class); diff --git a/tests/MachineTest.php b/tests/MachineTest.php index d28b3c3..1e43b18 100644 --- a/tests/MachineTest.php +++ b/tests/MachineTest.php @@ -2,19 +2,18 @@ namespace Sparors\Ussd\Tests; -use Sparors\Ussd\State; use Sparors\Ussd\Machine; use Orchestra\Testbench\TestCase; class MachineTest extends TestCase { - public function testRun() + public function test_it_runs_successfully() { $machine = (new Machine())->setSessionId('1234') ->setInput('1') ->setInitialState(HelloState::class) ->setStore('array'); - + $this->assertEquals( [ 'message' => 'Hello World', @@ -34,7 +33,7 @@ public function testRun() ); } - public function testInitialStateCanBeACallable() + public function test_initial_state_can_be_a_callable() { $machine = (new Machine())->setSessionId('1234') ->setInput('1') @@ -42,7 +41,7 @@ public function testInitialStateCanBeACallable() return HelloState::class; }) ->setStore('array'); - + $this->assertEquals( [ 'message' => 'Hello World', diff --git a/tests/MenuTest.php b/tests/MenuTest.php index cade137..a43bdc7 100644 --- a/tests/MenuTest.php +++ b/tests/MenuTest.php @@ -2,62 +2,101 @@ namespace Sparors\Ussd\Tests; -use Orchestra\Testbench\TestCase; use Sparors\Ussd\Menu; +use PHPUnit\Framework\TestCase; class MenuTest extends TestCase { - public function testCanBeUsedAsString() + public function test_it_can_be_used_as_string() { $menu = new Menu('Hello Ussd'); $this->assertEquals('Hello Ussd', $menu); } - public function testCanBeConvertedToStringExplicitly() + public function test_it_can_be_converted_to_string_explicitly() { $menu = new Menu('Hello Ussd'); $this->assertEquals('Hello Ussd', $menu->toString()); } - public function testCanHaveLineBreak() + public function test_it_can_have_line_break() { $menu = new Menu; $this->assertEquals("\n", $menu->lineBreak()); } - public function testCanHaveDoubleLineBreak() + public function test_it_can_have_double_line_break() { $menu = new Menu; $this->assertEquals("\n\n", $menu->lineBreak(2)); } - public function testCanHaveTextWithLineBreak() + public function test_it_can_have_text_with_line_break() { $menu = new Menu; $this->assertEquals("Hello Ussd\n", $menu->line("Hello Ussd")); } - public function testCanHaveTextWithNoLineBreak() + public function test_it_can_have_text_with_no_line_break() { $menu = new Menu; $this->assertEquals("Hello Ussd", $menu->text("Hello Ussd")); } - public function testCanParseAListToString() + public function test_it_can_parse_a_list_to_string() { $menu = new Menu; $this->assertEquals("1.New Gen\n2.Old Gen", $menu->listing(['New Gen', 'Old Gen'])); } - public function testCanPaginateAndParseAListToString() + public function test_it_can_paginate_and_parse_a_list_to_string() { $menu = new Menu; $this->assertEquals("3.Extra", $menu->paginateListing(['New Gen', 'Old Gen', 'Extra'], 2, 2)); } - public function testMethodCanBeChained() + public function test_it_can_parse_a_list_to_string_with_alphabets_lower_for_numbering() + { + $menu = new Menu; + $this->assertEquals("a.New Gen\nb.Old Gen", + $menu->listing( + ['New Gen', 'Old Gen'], + Menu::NUMBERING_SEPARATOR_DOT, + Menu::ITEMS_SEPARATOR_LINE_BREAK, + Menu::NUMBERING_ALPHABETIC_LOWER + ) + ); + } + + public function test_it_can_parse_a_list_to_string_with_alphabets_upper_for_numbering() + { + $menu = new Menu; + $this->assertEquals("A.New Gen\nB.Old Gen", + $menu->listing( + ['New Gen', 'Old Gen'], + Menu::NUMBERING_SEPARATOR_DOT, + Menu::ITEMS_SEPARATOR_LINE_BREAK, + Menu::NUMBERING_ALPHABETIC_UPPER + ) + ); + } + + public function test_it_can_parse_a_list_to_string_with_empty_string_for_numbering() + { + $menu = new Menu; + $this->assertEquals(".New Gen\n.Old Gen", + $menu->listing( + ['New Gen', 'Old Gen'], + Menu::NUMBERING_SEPARATOR_DOT, + Menu::ITEMS_SEPARATOR_LINE_BREAK, + Menu::NUMBERING_EMPTY + ) + ); + } + + public function test_method_can_be_chained() { $menu = new Menu; $this->assertEquals("Hello Ussd\n1.Ok\n2.Fine\nBye", diff --git a/tests/RecordTest.php b/tests/RecordTest.php index 7ec6c7c..4b006eb 100644 --- a/tests/RecordTest.php +++ b/tests/RecordTest.php @@ -3,47 +3,40 @@ namespace Sparors\Ussd\Tests; use Sparors\Ussd\Record; -use Orchestra\Testbench\TestCase; +use Sparors\Ussd\Tests\TestCase; use Illuminate\Support\Facades\Cache; -use Sparors\Ussd\UssdServiceProvider; class RecordTest extends TestCase { - /** - * Tell Testbench to use this package. - * - * @param $app - * - * @return array - */ - public function getPackageProviders($app) - { - return [UssdServiceProvider::class]; - } - - public function testSetAndGetSingleValue() + public function test_it_can_set_and_get_a_single_value() { $record = new Record(Cache::store('array'), '1'); $record->set('name', 'ussd'); - $this->assertEquals('ussd', $record->get('name')); + $record->set('author', 'Isaac Sai'); + $record->version = '1.0'; + $this->assertEquals('ussd', $record->name); + $this->assertEquals('Isaac Sai', $record('author')); + $this->assertEquals('1.0', $record->get('version')); } - public function testsetAndGetMultipleValues() + public function test_it_can_set_and_get_multiple_values() { $record = new Record(Cache::store('array'), '1'); $record->setMultiple(['name' => 'ussd', 'version' => '1.0']); - $this->assertEquals(['ussd', '1.0'], $record->getMultiple(['name', 'version'])); + $record(['author' => 'Isaac Sai',]); + $this->assertEquals(['ussd', '1.0', 'Isaac Sai'], $record->getMultiple(['name', 'version', 'author'])); } - public function testDeleteValue() + public function test_it_can_delete_a_value() { $record = new Record(Cache::store('array'), '1'); - $record->setMultiple(['name' => 'ussd', 'version' => '1.0']); + $record->setMultiple(['name' => 'ussd', 'version' => '1.0', 'author' => 'Isaac Sai']); $record->delete('name'); - $this->assertEquals([null, '1.0'], $record->getMultiple(['name', 'version'])); + unset($record->author); + $this->assertEquals([null, '1.0', null], $record->getMultiple(['name', 'version', 'author'])); } - public function testDeleteMultipleValues() + public function test_it_can_delete_multiple_values() { $record = new Record(Cache::store('array'), '1'); $record->setMultiple(['name' => 'ussd', 'version' => '1.0']); @@ -51,11 +44,30 @@ public function testDeleteMultipleValues() $this->assertEquals([null, null], $record->getMultiple(['name', 'version'])); } - public function testHasValue() + public function test_it_can_verify_if_cache_has_value() { $record = new Record(Cache::store('array'), '1'); $record->set('name', 'ussd'); + $record->set('author', 'Isaac Sai'); $this->assertTrue($record->has('name')); $this->assertFalse($record->has('version')); + $this->assertTrue(isset($record->name)); + } + + public function test_it_can_increment_or_decrement_a_numeric_value() + { + $record = new Record(Cache::store('array'), '1'); + $record->set('age', 17); + $this->assertEquals(18, $record->increment('age')); + $this->assertEquals(17, $record->decrement('age')); + } + + public function test_it_can_delete_all_values() + { + $record = new Record(Cache::store('array'), '1'); + $record->setMultiple(['name' => 'ussd', 'version' => '1.0']); + $record->flush(); + $this->assertNull($record->get('name')); + $this->assertNull($record->get('version')); } } \ No newline at end of file diff --git a/tests/RunAction.php b/tests/RunAction.php index 4012114..0f027e3 100644 --- a/tests/RunAction.php +++ b/tests/RunAction.php @@ -4,6 +4,7 @@ use Sparors\Ussd\Action; +/** @internal */ class RunAction extends Action { public function run(): string diff --git a/tests/StateTest.php b/tests/StateTest.php index 1c21064..d773301 100644 --- a/tests/StateTest.php +++ b/tests/StateTest.php @@ -6,7 +6,7 @@ class StateTest extends TestCase { - public function testState() + public function test_state_works_correctly() { $hello = new HelloState(); $this->assertEquals('Hello World', $hello->render()); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..1110b1f --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,21 @@ +