diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..89225fc --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,2 @@ +## Code changes will send PR to following users. +* @veeqtoh diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..35b1fa9 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: veeqtoh \ No newline at end of file diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..1c0db95 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,12 @@ +### Motivation and Context ### +Why is this change required? What problem does it solve? If it fixes an open issue, please link to the issue here. + + + +### Dependencies ### +Note if this change has any dependencies, e.g. a library (write the command to install it here e.g composer install or update ) + + + +### Test Instructions ### +Provide a list of tests the reviewer must complete to appropriately QA this work. This may include automated or manual tests. Consider the complexity of the change when compiling this list, and be sure to include any build instructions e.g. composer install. diff --git a/.github/workflows/ci-phpstan.yml b/.github/workflows/ci-phpstan.yml new file mode 100644 index 0000000..a99ad8c --- /dev/null +++ b/.github/workflows/ci-phpstan.yml @@ -0,0 +1,48 @@ +name: run-phpstan + +on: + pull_request: + +jobs: + run-tests: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: [8.1, 8.2, 8.3] + laravel: [10.*, 11.*] + include: + - laravel: 11.* + testbench: 9.* + - laravel: 10.* + testbench: 8.* + exclude: + - laravel: 11.* + php: 8.1 + + name: PHP${{ matrix.php }} - Laravel ${{ matrix.laravel }} + + steps: + - name: Update apt + run: sudo apt-get update --fix-missing + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - name: Setup Problem Matches + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: | + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --prefer-dist --no-interaction --no-suggest + - name: Run Larastan + run: vendor/bin/phpstan analyse \ No newline at end of file diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml new file mode 100644 index 0000000..0b42a92 --- /dev/null +++ b/.github/workflows/ci-tests.yml @@ -0,0 +1,48 @@ +name: run-tests + +on: + pull_request: + +jobs: + run-tests: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: [8.1, 8.2, 8.3] + laravel: [10.*, 11.*] + include: + - laravel: 11.* + testbench: 9.* + - laravel: 10.* + testbench: 8.* + exclude: + - laravel: 11.* + php: 8.1 + + name: PHP${{ matrix.php }} - Laravel ${{ matrix.laravel }} + + steps: + - name: Update apt + run: sudo apt-get update --fix-missing + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - name: Setup Problem Matches + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: | + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --prefer-dist --no-interaction --no-suggest + - name: Execute tests + run: vendor/bin/pest \ No newline at end of file diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml deleted file mode 100644 index cb38541..0000000 --- a/.github/workflows/php.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: PHP Composer Test - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - -permissions: - contents: read - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Validate composer.json and composer.lock - run: composer validate --strict - - - name: Cache Composer packages - id: composer-cache - uses: actions/cache@v3 - 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: Run Tests - run: composer run-script test - - # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" - # Docs: https://getcomposer.org/doc/articles/scripts.md diff --git a/LICENSE,md b/LICENSE,md new file mode 100644 index 0000000..931b833 --- /dev/null +++ b/LICENSE,md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Ashley Allen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 6cad682..f9eab1d 100644 --- a/README.md +++ b/README.md @@ -1,75 +1,291 @@ -# door-access -Generate random access codes unique to team members for a secure door access system. +# secure-code + +

+ +

+ +## Table of Contents + +- [Overview](#overview) +- [Installation](#installation) + - [Requirements](#requirements) + - [Install the Package](#install-the-package) + - [Publish the Config and Migrations](#publish-the-config-and-migrations) + - [Migrate the Database](#migrate-the-database) +- [Usage](#usage) + - [Generating Secure Codes](#denerating-secure-codess) + - [Quick Start](#quick-start) + - [Validation](#validation) + - [Defining your Custom Validation Class](#defining-your-custom-validation-classs) + - [Customizing Configs](#customizing-configs) + - [Using Custom Rules](#using-custom-rules) + - [Facade](#facade) + - [Using the Secure code Manager](#using-the-secure-code-manager) + - [Allocating a Code](#allocating-a-code) + - [Resetting a Code](#resetting-a-code) + - [Destroying a Code](#destroying-a-code) + - [Config Validation](#config-validation) + - [Custom Database Connection](#custom-database-connection) + - [Helper Methods](#helper-methods) + - [Find by code](#find-by-code) + - [Find by owner](#find-by-owner) + - [Model Factories](#model-factories) +- [Testing](#testing) +- [Security](#security) +- [Contribution](#contribution) +- [Changelog](#changelog) +- [Upgrading](#upgrading) +- [License](#license) + +# Overview + +A Laravel package that provides secure codes management system, allowing you to generate n-digit secure codes and manage it's allocation within you existing web app. ## Installation -This project uses Composer for easy installation: + + +### Requirements + +The package has been developed and tested to work with the following minimum requirements: + +- PHP 8.x +- Laravel 11.x + +Secure-code requires either the [BC Math](https://secure.php.net/manual/en/book.bc.php) or [GMP](https://secure.php.net/manual/en/book.gmp.php) PHP extensions in order to work. + +### Install the Package + +You can install the package via Composer: + +```bash +composer require veeqtoh/secure-code +``` + +### Publish the Config and Migrations + +You can then publish the package's config file and database migrations by using the following command: +```bash +php artisan vendor:publish --provider="Veeqtoh\SecureCode\Providers\SecureCodeProvider" ``` -$ composer require veeqtoh/door-access + +### Migrate the Database + +This package contains a migration file that add a new table to the database: ``` secure_codes ```. To run this migration, simply run the following command: +```bash +php artisan migrate ``` -## Requirements -* PHP version ^8.1.2 -* Laravel framework ^8.0 (if using within a Laravel application) +## Usage + +### Generating Secure Codes + +#### Quick Start -## Configuration -The package provides flexibility for code generation rules. You can customize the code generation process by modifying the rules in the `config/rules.php` file. +The quickest way to get started with generating a secure code is by using the snippet below. +```php +use Veeqtoh\SecureCode\Classes\CodeGenerator; + +$codeGenerator = new CodeGenerator(); +$secureCode = $codeGenerator->generate(); -## Database Setup -To store access codes and manage allocations, you need to set up the database and run the migration provided by the package. Use the following commands: +echo "Generated code: $code"; ``` -$ php artisan migrate + +#### Validation + +By default, the secure code is not validated against any condition. The library however, comes with three (3) inbuilt validation classes that checks for; +1. Palindrome +2. Repeated characters +3. Sequence length +These validation classes can be initialized and passed down to the code generator class in an array on it's constructor as shown below; +```php +use Veeqtoh\SecureCode\Classes\CodeGenerator; + +// Define specific validation rules. +$validators = [ + new NoPalindromeValidator(), + new RepeatingCharactersValidator(), + new MinimumUniqueCharactersValidator(), + ]; + +// Generate a secure n-digit code +$codeGenerator = new CodeGenerator(); +$secureCode = $codeGenerator->generate(); + +echo "Generated code: $code"; ``` +You may wish to define a custom validation yourself for more control. You can + +#### Custom Validation + +To achieve this, you must write a class that implements the library's base validation interface as follows; + +##### Defining your custom validation class -## Usage -Generate a door access code using the `CodeGenerator` class: ```php -generateCode(); + public function isValid(string $code): bool + { + return 'your custom rule'; + } + +} ``` +To use your custom rule, simply initialize and include it in the validators array as shown above. + +#### Customizing Configs + +Tailor the package to to your needs with customizable configuration options: + +- Database Connection: Specify a custom database connection if needed. +- Code Generation Rules: Define code length and constraints like character repetition and unique characters limit. +- Code Format: Choose from numeric, alphanumeric, or mixed formats for generated codes. +- Code Characters: Customize character sets for different code formats. +- Config Validation: Optionally validate configuration for safety. +- Eloquent Factories: Define factories for testing purposes. +With these options, you can configure the system to align with your security standards and requirements. + +Note: The code length cannot be more than 19. + +#### Facade + +If you prefer to use facades in Laravel, you can choose to use the provided ``` SecureCode ``` facade instead of instantiating +the ``` CodeGenerator ``` class manually. + + +### Using the Secure Code Manager + +The code manager provides functionality to manage generated access codes, including allocation, resetting, and destruction. + +#### Allocating a Code + +To allocate a code to an owner, you can use the allocateCode method: -## Database Management -The CodeManager class provides methods for allocating and resetting access codes. It uses the database to store and manage code allocations. Use it as follows: ```php -allocateCode('generated-code', 'owner-id'); + +echo "Allocated code: $code"; +``` + +#### Resetting a Code + +To reset a code and make it available for reallocation, you can use the resetCode method: + +```php +use Veeqtoh\DoorAccess\Classes\CodeManager; $manager = new CodeManager(); +$success = $manager->resetCode('allocated-code'); -// Allocate a code to a team member -$teamMemberId = 'team_member_1'; -$accessCode = $manager->allocateCode($teamMemberId); +if ($success) { + echo "Code reset successfully"; +} else { + echo "Failed to reset code"; +} -// Reset a code and make it available for reallocation -$code = '123456'; // Existing code to reset -$resetSuccess = $manager->resetCode($code); ``` -## Testing -To run the unit tests for the package, use PHPUnit: +#### Destroying a Code + +To permanently destroy a code, you can use the destroyCode method: + +```php +use Veeqtoh\DoorAccess\Classes\CodeManager; + +$manager = new CodeManager(); +$success = $manager->destroyCode('code-to-destroy'); + +if ($success) { + echo "Code destroyed successfully"; +} else { + echo "Failed to destroy code"; +} + ``` -$ phpunit + +#### Custom Database Connection + +By default, Secure code will use your application's default database connection. But there may be times that you'd like to use a different connection. For example, you might be building a multi-tenant application that uses a separate connection for each tenant, and you may want to store the Secure codes in a central database. + +To do this, you can set the connection name using the `connection` config value in the `config/secure-code.php` file like so: + +``` +'connection' => 'custom_database_connection_name', ``` -## Contributing -Contributions are'nt welcome at this time as this is a private project. If you'd like to contribute to the project, please reachout to me by mail [victorjohnukam@gmail.com](victorjohnukam@gmail.com). +### Helper Methods + +#### Find by code +To find the SecureCode model that corresponds to a given code, you can use the ``` ->findByCode() ``` method. + +For example, to find the SecureCode model of a code that has the code ``` abc123 ```, you could use the following: + +```php +$secureCode = \Veeqtoh\SecureCode\Models\SecureCode::findByCode('abc123'); +``` + +#### Find by Owner + +To find the SecureCode models that belongs to a given owner ID, you can use the ``` ->findByOwnerId() ``` method. + +For example, to find all of the SecureCode models of owners that belongs to ``` john doe ```, you could use +the following: + +```php +$secureCodes = \Veeqtoh\SecureCode\Models\SecureCode::findByOwnerId('john doe'); +``` + +### Model Factories + +The package comes with model factories included for testing purposes. + +```php +use Veeqtoh\SecureCode\Models\SecureCode; + +``` + +## Testing + +To run the package's unit tests, run the following command: + +``` bash +vendor/bin/pest +``` ## Security -When using the package in a production environment, make sure to follow security best practices and protect sensitive data. +If you find any security related issues, please contact me directly at [victorjohnukam@gmail.com](mailto:victorjohnukam@gmail.com) to report it. + +## Contribution + +If you wish to make any changes or improvements to the package, feel free to make a pull request. + +Note: A contribution guide will be added soon. + +## Changelog + +Check the [CHANGELOG](CHANGELOG.md) to get more information about the latest changes. + +## Upgrading + +Check the [UPGRADE](UPGRADE.md) guide to get more information on how to update this library to newer versions. ## License -This package is open-source and released under the MIT License. +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. + +## Support Me + +If you've found this package useful, please consider sponsoring this project. It will encourage me to keep maintaining it. \ No newline at end of file diff --git a/composer.json b/composer.json index 4d3b4b1..0b27109 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { - "name": "veeqtoh/door-access", - "description": "A Laravel package for creating secure door access systems, providing unique and random 6-digit codes for team members.", + "name": "veeqtoh/secure-code", + "description": "A Laravel package for creating secure codes, providing unique and random n-digit codes for any use.", "type": "library", - "homepage": "https://github.com/veeqtoh/door-access", + "homepage": "https://github.com/veeqtoh/secure-code", "license": "MIT", "authors": [ { @@ -13,8 +13,9 @@ "keywords": [ "veeqtoh", "victor-ukam", - "door-access", + "secure-code", "secure-access", + "secure-code", "unique-codes", "persistent-codes", "random-code-allocation", @@ -43,21 +44,21 @@ }, "autoload": { "psr-4": { - "Veeqtoh\\DoorAccess\\": "src/" + "Veeqtoh\\SecureCode\\": "src/" } }, "autoload-dev": { "psr-4": { - "Veeqtoh\\DoorAccess\\Tests\\": "tests/" + "Veeqtoh\\SecureCode\\Tests\\": "tests/" } }, "extra": { "laravel": { "providers": [ - "Veeqtoh\\DoorAccess\\Providers\\DoorAccessProvider" + "Veeqtoh\\SecureCode\\Providers\\SecureCodeProvider" ], "aliases": { - "DoorAccess": "Veeqtoh\\DoorAccess\\Facades\\DoorAccess" + "SecureCode": "Veeqtoh\\SecureCode\\Facades\\SecureCode" } } }, diff --git a/config/door-access.php b/config/secure-code.php similarity index 93% rename from config/door-access.php rename to config/secure-code.php index d7263e9..f5333f9 100644 --- a/config/door-access.php +++ b/config/secure-code.php @@ -19,7 +19,7 @@ | Code Generation Rules |-------------------------------------------------------------------------- | - | These are the default rules for generating secure door access codes. + | These are the default rules for generating secure codes. | You can customize these rules in the configuration file to meet your needs. | | NOTE: Maximum code length is 19. @@ -82,6 +82,6 @@ */ 'factories' => [ - \Veeqtoh\DoorAccess\Models\AccessCode::class => \Veeqtoh\DoorAccess\Models\Factories\AccessCodeFactory::class, + \Veeqtoh\SecureCode\Models\SecureCode::class => \Veeqtoh\SecureCode\Models\Factories\SecureCodeFactory::class, ], ]; diff --git a/database/migrations/2023_08_04_200132_create_access_codes_table.php b/database/migrations/2023_08_04_200132_create_secure_codes_table.php similarity index 80% rename from database/migrations/2023_08_04_200132_create_access_codes_table.php rename to database/migrations/2023_08_04_200132_create_secure_codes_table.php index 6031ec9..454a335 100644 --- a/database/migrations/2023_08_04_200132_create_access_codes_table.php +++ b/database/migrations/2023_08_04_200132_create_secure_codes_table.php @@ -11,7 +11,7 @@ */ public function up(): void { - Schema::connection(config('door-access.connection'))->create('access_codes', function (Blueprint $table) { + Schema::connection(config('secure-code.connection'))->create('secure_codes', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('code')->unique()->when( Schema::getConnection()->getConfig('driver') === 'mysql', @@ -31,6 +31,6 @@ function (Blueprint $column) { */ public function down(): void { - Schema::connection(config('door-access.connection'))->dropIfExists('access_codes'); + Schema::connection(config('secure-codes.connection'))->dropIfExists('secure_codes'); } }; diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..c372bff --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,13 @@ +includes: + - ./vendor/larastan/larastan/extension.neon + +parameters: + + paths: + - src + + level: 6 + + ignoreErrors: + + checkMissingIterableValueType: false \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 3e80e3b..90eea14 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ - + tests diff --git a/src/Classes/CodeGenerator.php b/src/Classes/CodeGenerator.php index 74938d3..114150d 100644 --- a/src/Classes/CodeGenerator.php +++ b/src/Classes/CodeGenerator.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Classes; +namespace Veeqtoh\SecureCode\Classes; -use Veeqtoh\DoorAccess\Classes\Traits\ConfigTrait; +use Veeqtoh\SecureCode\Classes\Traits\ConfigTrait; /** * Class CodeGenerator * This class is used for generating unique, secure codes. * - * @package Veeqtoh\DoorAccess\Classes + * @package Veeqtoh\SecureCode\Classes */ class CodeGenerator { diff --git a/src/Classes/CodeManager.php b/src/Classes/CodeManager.php index db61ebb..8aad8ec 100644 --- a/src/Classes/CodeManager.php +++ b/src/Classes/CodeManager.php @@ -2,33 +2,33 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Classes; +namespace Veeqtoh\SecureCode\Classes; use Illuminate\Support\Facades\Log; -use Veeqtoh\DoorAccess\Classes\Traits\ConfigTrait; -use Veeqtoh\DoorAccess\Exceptions\InvalidCodeException; -use Veeqtoh\DoorAccess\Models\AccessCode; +use Veeqtoh\SecureCode\Classes\Traits\ConfigTrait; +use Veeqtoh\SecureCode\Exceptions\InvalidCodeException; +use Veeqtoh\SecureCode\Models\SecureCode; /** * Class CodeManager * This class is used for managing generated codes. * - * @package Veeqtoh\DoorAccess\Classes + * @package Veeqtoh\SecureCode\Classes */ class CodeManager { use ConfigTrait; /** - * Save the access code in the database. + * Save the secure code in the database. * - * @param string $code The access code to be saved. + * @param string $code The secure code to be saved. * - * @return AccessCode The newly created AccessCode model. + * @return secureCode The newly created secureCode model. */ - public function saveCode(string $code): ?AccessCode + public function saveCode(string $code): ?secureCode { - return AccessCode::create(['code' => $code]); + return SecureCode::create(['code' => $code]); } /** @@ -37,11 +37,11 @@ public function saveCode(string $code): ?AccessCode * @param string $code The generated code to be allocated. * @param string $ownerId The owner to be allocated the code. * - * @return AccessCode The AccessCode model with the new allocation. + * @return secureCode The secureCode model with the new allocation. */ - public function allocateCode(string $code, string $ownerId): AccessCode + public function allocateCode(string $code, string $ownerId): secureCode { - $existingCode = AccessCode::whereCode($code)->first(); + $existingCode = SecureCode::whereCode($code)->first(); if ($existingCode) { if ($existingCode->isAllocated()) { @@ -51,7 +51,7 @@ public function allocateCode(string $code, string $ownerId): AccessCode return $existingCode->allocate($ownerId); } - return AccessCode::create([ + return SecureCode::create([ 'code' => $code, 'owner_id' => $ownerId ]); @@ -62,13 +62,13 @@ public function allocateCode(string $code, string $ownerId): AccessCode * * @param string $code, * - * @return AccessCode The AccessCode model with the new allocation. + * @return secureCode The secureCode model with the new allocation. * * @throws InvalidCodeException When code to be reset is not found. */ - public function resetCode(string $code): AccessCode + public function resetCode(string $code): secureCode { - $existingCode = AccessCode::whereCode($code)->first(); + $existingCode = SecureCode::whereCode($code)->first(); if (!$existingCode) { Log::error("The code ({$code}) you are trying to reset does not exist"); @@ -79,15 +79,15 @@ public function resetCode(string $code): AccessCode } /** - * Destroy the access code in the database. + * Destroy the secure code in the database. * - * @param string $code The access code to be destroyed. + * @param string $code The secure code to be destroyed. * - * @return bool Returns true if the access code was successfully destroyed, false otherwise. + * @return bool Returns true if the secure code was successfully destroyed, false otherwise. */ public function destroyCode(string $code): bool { - $existingCode = AccessCode::whereCode($code)->first(); + $existingCode = SecureCode::whereCode($code)->first(); if (!$existingCode) { Log::error("The code ({$code}) you are trying to destroy does not exist"); diff --git a/src/Classes/Traits/ConfigTrait.php b/src/Classes/Traits/ConfigTrait.php index 8934cb1..2b2b9c8 100644 --- a/src/Classes/Traits/ConfigTrait.php +++ b/src/Classes/Traits/ConfigTrait.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Classes\Traits; +namespace Veeqtoh\SecureCode\Classes\Traits; /** * Trait ConfigTrait - * This trait provides methods to retrieve configuration settings for the DoorAccess package. + * This trait provides methods to retrieve configuration settings for the SecureCode package. * - * @package Veeqtoh\DoorAccess\Classes\Traits + * @package Veeqtoh\SecureCode\Classes\Traits */ trait ConfigTrait { @@ -19,15 +19,15 @@ trait ConfigTrait */ public function getAllowedCharacters(): string { - $format = config('door-access.code_format'); + $format = config('secure-code.code_format'); switch ($format) { case 'numeric': - return config('door-access.numeric_characters') ?? '0123456789'; + return config('secure-code.numeric_characters') ?? '0123456789'; case 'mixed': - return config('door-access.mixed_characters') ?? '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+[]{};:<>,.?/'; + return config('secure-code.mixed_characters') ?? '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+[]{};:<>,.?/'; default: - return config('door-access.alphanumeric_characters') ?? '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + return config('secure-code.alphanumeric_characters') ?? '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; } } @@ -39,12 +39,12 @@ public function getAllowedCharacters(): string public function getCodeLength(): int { if( - config('door-access.code_length') - && config('door-access.code_length') > 19 + config('secure-code.code_length') + && config('secure-code.code_length') > 19 ){ return 19; } else { - return config('door-access.code_length') ?? 6; + return config('secure-code.code_length') ?? 6; } } @@ -56,12 +56,12 @@ public function getCodeLength(): int public function getCharacterRepeatedLimit(): int { if( - config('door-access.character_repeated_limit') - && config('door-access.character_repeated_limit') > $this->getCodeLength() + config('secure-code.character_repeated_limit') + && config('secure-code.character_repeated_limit') > $this->getCodeLength() ){ return $this->getCodeLength(); } else { - return config('door-access.character_repeated_limit') ?? 3; + return config('secure-code.character_repeated_limit') ?? 3; } } @@ -72,7 +72,7 @@ public function getCharacterRepeatedLimit(): int */ public function getSequenceLengthLimit(): int { - return config('door-access.sequence_length_limit') ?? 3; + return config('secure-code.sequence_length_limit') ?? 3; } /** @@ -82,7 +82,7 @@ public function getSequenceLengthLimit(): int */ public function getUniqueCharactersLimit(): int { - return config('door-access.unique_characters_limit') ?? 6; + return config('secure-code.unique_characters_limit') ?? 6; } /** @@ -92,7 +92,7 @@ public function getUniqueCharactersLimit(): int */ public function getCodeFormat(): string { - return config('door-access.code_format') ?? 'numeric'; + return config('secure-code.code_format') ?? 'numeric'; } } diff --git a/src/Classes/Validation.php b/src/Classes/Validation.php index 42f078d..3a76796 100644 --- a/src/Classes/Validation.php +++ b/src/Classes/Validation.php @@ -2,13 +2,19 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Classes; +namespace Veeqtoh\SecureCode\Classes; use AshAllenDesign\ConfigValidator\Exceptions\InvalidConfigValueException; use AshAllenDesign\ConfigValidator\Services\ConfigValidator; use AshAllenDesign\ConfigValidator\Services\Rule; -use Veeqtoh\DoorAccess\Exceptions\ValidationException; - +use Veeqtoh\SecureCode\Exceptions\ValidationException; + +/** + * Class Validation + * This class is used for validating the config values. + * + * @package Veeqtoh\SecureCode\Classes + */ class Validation { /** @@ -24,7 +30,7 @@ public function validateConfig(): bool $passes = $validator ->throwExceptionOnFailure(false) ->runInline([ - 'door-access' => [ + 'secure-code' => [ Rule::make('code_length')->rules(['required', 'integer', 'min:1', 'max:19']), Rule::make('character_repeated_limit')->rules(['required', 'integer', 'min:3', 'max:19']), Rule::make('sequence_length_limit')->rules(['required', 'integer', 'min:3']), diff --git a/src/Classes/Validators/MinimumUniqueCharactersValidator.php b/src/Classes/Validators/MinimumUniqueCharactersValidator.php index cf92a82..82ffb4e 100644 --- a/src/Classes/Validators/MinimumUniqueCharactersValidator.php +++ b/src/Classes/Validators/MinimumUniqueCharactersValidator.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Classes\Validators; +namespace Veeqtoh\SecureCode\Classes\Validators; -use Veeqtoh\DoorAccess\Classes\Traits\ConfigTrait; -use Veeqtoh\DoorAccess\Contracts\CodeValidator; +use Veeqtoh\SecureCode\Classes\Traits\ConfigTrait; +use Veeqtoh\SecureCode\Contracts\CodeValidator; /** * Class MinimumUniqueCharactersValidator * This class provides validation against minimum unique characters. * - * @package Veeqtoh\DoorAccess\Classes\Validators + * @package Veeqtoh\SecureCode\Classes\Validators */ class MinimumUniqueCharactersValidator implements CodeValidator { diff --git a/src/Classes/Validators/NoPalindromeValidator.php b/src/Classes/Validators/NoPalindromeValidator.php index f760b0b..15cc238 100644 --- a/src/Classes/Validators/NoPalindromeValidator.php +++ b/src/Classes/Validators/NoPalindromeValidator.php @@ -2,15 +2,15 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Classes\Validators; +namespace Veeqtoh\SecureCode\Classes\Validators; -use Veeqtoh\DoorAccess\Contracts\CodeValidator; +use Veeqtoh\SecureCode\Contracts\CodeValidator; /** * Class NoPalindromeValidator * This class provides validation against palindrome. * - * @package Veeqtoh\DoorAccess\Classes\Validators + * @package Veeqtoh\SecureCode\Classes\Validators */ class NoPalindromeValidator implements CodeValidator { diff --git a/src/Classes/Validators/RepeatingCharactersValidator.php b/src/Classes/Validators/RepeatingCharactersValidator.php index bd08120..3106ad1 100644 --- a/src/Classes/Validators/RepeatingCharactersValidator.php +++ b/src/Classes/Validators/RepeatingCharactersValidator.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Classes\Validators; +namespace Veeqtoh\SecureCode\Classes\Validators; -use Veeqtoh\DoorAccess\Classes\Traits\ConfigTrait; -use Veeqtoh\DoorAccess\Contracts\CodeValidator; +use Veeqtoh\SecureCode\Classes\Traits\ConfigTrait; +use Veeqtoh\SecureCode\Contracts\CodeValidator; /** * Class RepeatingCharactersValidator * This class provides validation against repeated limit. * - * @package Veeqtoh\DoorAccess\Classes\Validators + * @package Veeqtoh\SecureCode\Classes\Validators */ class RepeatingCharactersValidator implements CodeValidator { diff --git a/src/Contracts/CodeValidator.php b/src/Contracts/CodeValidator.php index 797c576..459249b 100644 --- a/src/Contracts/CodeValidator.php +++ b/src/Contracts/CodeValidator.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Contracts; +namespace Veeqtoh\SecureCode\Contracts; /** * Interface CodeValidator * This interface provides the basis of all validation classes behaviors. * - * @package Veeqtoh\DoorAccess\Contracts + * @package Veeqtoh\SecureCode\Contracts */ interface CodeValidator { diff --git a/src/Database/DatabaseFactory.php b/src/Database/DatabaseFactory.php deleted file mode 100644 index 19f8c97..0000000 --- a/src/Database/DatabaseFactory.php +++ /dev/null @@ -1,35 +0,0 @@ -insert([ - 'team_member_id' => $teamMemberId, - 'code' => $code, - 'created_at' => now(), - 'updated_at' => now(), - ]); - } - - /** - * Retrieve the code associated with the team member from the MySQL database. - * - * @param string $teamMemberId - * @return ?string - */ - public function retrieve(string $teamMemberId): ?string - { - return DB::table('door_access_codes') - ->where('team_member_id', $teamMemberId) - ->value('code'); - } - - /** - * Retrieve the team member id associated with a given code from the MySQL database. - * - * @param string $code - * @return int|null - */ - public function retrieveTeamMemberId(string $code): ?string - { - return DB::table('door_access_codes') - ->where('code', $code) - ->value('team_member_id'); - } - - /** - * Delete the code from the MySQL database. - * - * @param string $code - * @return bool - */ - public function delete(string $code): bool - { - return (bool) DB::table('door_access_codes') - ->where('code', $code) - ->delete(); - } -} diff --git a/src/Database/SQLiteDatabase.php b/src/Database/SQLiteDatabase.php deleted file mode 100644 index b9dc229..0000000 --- a/src/Database/SQLiteDatabase.php +++ /dev/null @@ -1,70 +0,0 @@ -insert([ - 'team_member_id' => $teamMemberId, - 'code' => $code, - 'created_at' => now(), - 'updated_at' => now(), - ]); - } - - /** - * Retrieve the code associated with the team member from the SQLite database. - * - * @param string $teamMemberId - * @return ?string - */ - public function retrieve(string $teamMemberId): ?string - { - return DB::table('door_access_codes') - ->where('team_member_id', $teamMemberId) - ->value('code'); - } - - /** - * Retrieve the team member id associated with a given code from the SQLite database. - * - * @param string $code - * @return int|null - */ - public function retrieveTeamMemberId(string $code): ?string - { - return DB::table('door_access_codes') - ->where('code', $code) - ->value('team_member_id'); - } - - /** - * Delete the code from the SQLite database. - * - * @param string $code - * @return bool - */ - public function delete(string $code): bool - { - return (bool) DB::table('door_access_codes') - ->where('code', $code) - ->delete(); - } -} diff --git a/src/Exceptions/InvalidCodeException.php b/src/Exceptions/InvalidCodeException.php index 6886308..cc7a0d4 100644 --- a/src/Exceptions/InvalidCodeException.php +++ b/src/Exceptions/InvalidCodeException.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Exceptions; +namespace Veeqtoh\SecureCode\Exceptions; use Exception; @@ -10,7 +10,7 @@ * class InvalidCodeException * This class provides the exception for library config validations. * - * @package Veeqtoh\DoorAccess\Exceptions + * @package Veeqtoh\SecureCode\Exceptions */ class InvalidCodeException extends Exception { diff --git a/src/Exceptions/ValidationException.php b/src/Exceptions/ValidationException.php index ab0ce3d..14197f8 100644 --- a/src/Exceptions/ValidationException.php +++ b/src/Exceptions/ValidationException.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Exceptions; +namespace Veeqtoh\SecureCode\Exceptions; use Exception; @@ -10,7 +10,7 @@ * class ValidationException * This class provides the exception for library config validations. * - * @package Veeqtoh\DoorAccess\Exceptions + * @package Veeqtoh\SecureCode\Exceptions */ class ValidationException extends Exception { diff --git a/src/Facades/DoorAccess.php b/src/Facades/SecureCode.php similarity index 65% rename from src/Facades/DoorAccess.php rename to src/Facades/SecureCode.php index 84a6b30..fd96dc9 100644 --- a/src/Facades/DoorAccess.php +++ b/src/Facades/SecureCode.php @@ -2,18 +2,18 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Facades; +namespace Veeqtoh\SecureCode\Facades; use Illuminate\Support\Facades\Facade; use RuntimeException; /** - * class DoorAccess + * class SecureCode * This class provides the facade for this library. * - * @package Veeqtoh\DoorAccess\Facades + * @package Veeqtoh\SecureCode\Facades */ -class DoorAccess extends Facade +class SecureCode extends Facade { /** * Get the registered name of the component. @@ -22,6 +22,6 @@ class DoorAccess extends Facade */ protected static function getFacadeAccessor(): string { - return 'door-access.code-generator'; + return 'secure-code.code-generator'; } } \ No newline at end of file diff --git a/src/Models/Factories/AccessCodeFactory.php b/src/Models/Factories/SecureCodeFactory.php similarity index 70% rename from src/Models/Factories/AccessCodeFactory.php rename to src/Models/Factories/SecureCodeFactory.php index d725e53..f7ac289 100644 --- a/src/Models/Factories/AccessCodeFactory.php +++ b/src/Models/Factories/SecureCodeFactory.php @@ -2,18 +2,18 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Models\Factories; +namespace Veeqtoh\SecureCode\Models\Factories; use Illuminate\Database\Eloquent\Factories\Factory; -use Veeqtoh\DoorAccess\Classes\CodeGenerator; -use Veeqtoh\DoorAccess\Models\AccessCode; +use Veeqtoh\SecureCode\Classes\CodeGenerator; +use Veeqtoh\SecureCode\Models\SecureCode; /** - * @extends Factory + * @extends Factory */ -class AccessCodeFactory extends Factory +class SecureCodeFactory extends Factory { - protected $model = AccessCode::class; + protected $model = SecureCode::class; public function definition(): array { @@ -30,7 +30,7 @@ public function definition(): array ]; } - public function deactivated(): AccessCodeFactory + public function deactivated(): SecureCodeFactory { return $this->state(function () { return [ @@ -39,7 +39,7 @@ public function deactivated(): AccessCodeFactory }); } - public function inactive(): AccessCodeFactory + public function inactive(): SecureCodeFactory { return $this->state(function () { return [ diff --git a/src/Models/AccessCode.php b/src/Models/SecureCode.php similarity index 70% rename from src/Models/AccessCode.php rename to src/Models/SecureCode.php index 6d44529..aaa4d51 100644 --- a/src/Models/AccessCode.php +++ b/src/Models/SecureCode.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Models; +namespace Veeqtoh\SecureCode\Models; use Carbon\Carbon; use Illuminate\Database\Eloquent\Collection; @@ -18,7 +18,7 @@ * @property Carbon $created_at * @property Carbon $updated_at */ -class AccessCode extends Model +class SecureCode extends Model { use HasFactory; @@ -27,7 +27,7 @@ class AccessCode extends Model * * @var string */ - protected $table = 'access_codes'; + protected $table = 'secure_codes'; /** * The attributes that are mass assignable. @@ -55,17 +55,17 @@ public function __construct(array $attributes = []) { parent::__construct($attributes); - if (config('door-access.connection')) { - $this->setConnection(config('door-access.connection')); + if (config('secure-code.connection')) { + $this->setConnection(config('secure-code.connection')); } } /** - * @return Factory + * @return Factory */ protected static function newFactory() { - $factoryConfig = config('door-access.factories'); + $factoryConfig = config('secure-code.factories'); $modelFactory = app($factoryConfig[__CLASS__]); @@ -73,7 +73,7 @@ protected static function newFactory() } /** - * A helper method that can be used for finding a AccessCode model with the + * A helper method that can be used for finding a secureCode model with the * given code key. */ public static function findByCode(string $code): ?self @@ -82,10 +82,10 @@ public static function findByCode(string $code): ?self } /** - * A helper method that can be used for finding all the AccessCode models + * A helper method that can be used for finding all the secureCode models * with the given owner id. * - * @return Collection + * @return Collection */ public static function findByOwnerId(string $ownerId): Collection { @@ -93,9 +93,9 @@ public static function findByOwnerId(string $ownerId): Collection } /** - * Check if the access code is currently allocated. + * Check if the secure code is currently allocated. * - * @return bool Returns true if the access code is allocated, false otherwise. + * @return bool Returns true if the secure code is allocated, false otherwise. */ public function isAllocated(): bool { @@ -103,9 +103,9 @@ public function isAllocated(): bool } /** - * Reset the access code, making it available for reallocation. + * Reset the secure code, making it available for reallocation. * - * @return self Returns the AccessCode model with the new allocation. + * @return self Returns the secureCode model with the new allocation. */ public function reset(): self { @@ -119,11 +119,11 @@ public function reset(): self } /** - * Allocate the access code to an owner. + * Allocate the secure code to an owner. * - * @param string $ownerId The id of the owner to whom the access code will be allocated. + * @param string $ownerId The id of the owner to whom the secure code will be allocated. * - * @return self Returns the AccessCode model with the new allocation. + * @return self Returns the secureCode model with the new allocation. */ public function allocate(string $ownerId): self { diff --git a/src/Providers/DoorAccessProvider.php b/src/Providers/SecureCodeProvider.php similarity index 64% rename from src/Providers/DoorAccessProvider.php rename to src/Providers/SecureCodeProvider.php index 2b19c62..57c1b5f 100644 --- a/src/Providers/DoorAccessProvider.php +++ b/src/Providers/SecureCodeProvider.php @@ -1,18 +1,18 @@ mergeConfigFrom(__DIR__ . '/../../config/door-access.php', 'door-access'); + $this->mergeConfigFrom(__DIR__ . '/../../config/secure-code.php', 'secure-code'); } /** @@ -32,16 +32,16 @@ public function boot(): void { // Publish the package configuration file to the Laravel application. $this->publishes([ - __DIR__ . '/../../config/door-access.php' => config_path('door-access.php'), + __DIR__ . '/../../config/secure-code.php' => config_path('secure-code.php'), ], 'config'); // Publish the package's migrations. $this->publishes([ __DIR__.'/../../database/migrations' => database_path('migrations'), - ], 'door-access-migrations'); + ], 'secure-code-migrations'); // Validate the library configs or not. - if (config('door-access') && config('door-access.validate_config')) { + if (config('secure-code') && config('secure-code.validate_config')) { (new Validation())->validateConfig(); } } diff --git a/tests/Architecture/ClassesTest.php b/tests/Architecture/ClassesTest.php index c2cb072..987bee1 100644 --- a/tests/Architecture/ClassesTest.php +++ b/tests/Architecture/ClassesTest.php @@ -1,44 +1,44 @@ expect('Veeqtoh\DoorAccess\Classes\Validators\MinimumUniqueCharactersValidator') + ->expect('Veeqtoh\SecureCode\Classes\Validators\MinimumUniqueCharactersValidator') ->classes() ->toImplement(CodeValidator::class); test('no palindrome validator implements the validator interface') - ->expect('Veeqtoh\DoorAccess\Classes\Validators\NoPalindromeValidator') + ->expect('Veeqtoh\SecureCode\Classes\Validators\NoPalindromeValidator') ->classes() ->toImplement(CodeValidator::class); test('repeating characters validator implements the validator interface') - ->expect('Veeqtoh\DoorAccess\Classes\Validators\RepeatingCharactersValidator') + ->expect('Veeqtoh\SecureCode\Classes\Validators\RepeatingCharactersValidator') ->classes() ->toImplement(CodeValidator::class); test('config trait is a trait') - ->expect('Veeqtoh\DoorAccess\Classes\Traits\ConfigTrait') + ->expect('Veeqtoh\SecureCode\Classes\Traits\ConfigTrait') ->classes() ->toBeTraits(); test('code generator uses config trait') - ->expect('Veeqtoh\DoorAccess\Classes\CodeGenerator') + ->expect('Veeqtoh\SecureCode\Classes\CodeGenerator') ->traits() ->toExtend(ConfigTrait::class); test('minimum unique characters validator uses config trait') - ->expect('Veeqtoh\DoorAccess\Classes\Validators\MinimumUniqueCharactersValidator') + ->expect('Veeqtoh\SecureCode\Classes\Validators\MinimumUniqueCharactersValidator') ->traits() ->toExtend(ConfigTrait::class); test('no palindrome validator validator uses config trait') - ->expect('Veeqtoh\DoorAccess\Classes\Validators\NoPalindromeValidator') + ->expect('Veeqtoh\SecureCode\Classes\Validators\NoPalindromeValidator') ->traits() ->toExtend(ConfigTrait::class); test('repeating characters validator validator uses config trait') - ->expect('Veeqtoh\DoorAccess\Classes\Validators\RepeatingCharactersValidator') + ->expect('Veeqtoh\SecureCode\Classes\Validators\RepeatingCharactersValidator') ->traits() ->toExtend(ConfigTrait::class); \ No newline at end of file diff --git a/tests/Architecture/ContractsTest.php b/tests/Architecture/ContractsTest.php index 1d42d6c..18decd9 100644 --- a/tests/Architecture/ContractsTest.php +++ b/tests/Architecture/ContractsTest.php @@ -1,5 +1,5 @@ expect('Veeqtoh\DoorAccess\contracts') + ->expect('Veeqtoh\SecureCode\contracts') ->toBeInterfaces(); \ No newline at end of file diff --git a/tests/Architecture/ExceptionsTest.php b/tests/Architecture/ExceptionsTest.php index b6781c9..e95e876 100644 --- a/tests/Architecture/ExceptionsTest.php +++ b/tests/Architecture/ExceptionsTest.php @@ -1,6 +1,6 @@ expect('Veeqtoh\DoorAccess\Exceptions') + ->expect('Veeqtoh\SecureCode\Exceptions') ->classes() ->toExtend(Exception::class); \ No newline at end of file diff --git a/tests/Architecture/GlobalsTest.php b/tests/Architecture/GlobalsTest.php index f3ef6ec..5a4073a 100644 --- a/tests/Architecture/GlobalsTest.php +++ b/tests/Architecture/GlobalsTest.php @@ -5,5 +5,5 @@ ->toBeUsedInNothing(); test('all classes use strict types') - ->expect('Veeqtoh\DoorAccess') + ->expect('Veeqtoh\SecureCode') ->toUseStrictTypes(); \ No newline at end of file diff --git a/tests/Architecture/ModelsTest.php b/tests/Architecture/ModelsTest.php index 71362e6..e16fca0 100644 --- a/tests/Architecture/ModelsTest.php +++ b/tests/Architecture/ModelsTest.php @@ -3,12 +3,12 @@ use Illuminate\Database\Eloquent\Model; test('models extends base model') - ->expect('Veeqtoh\DoorAccess\Models') + ->expect('Veeqtoh\SecureCode\Models') ->classes() ->toExtend(Model::class) - ->ignoring('Veeqtoh\DoorAccess\Models\Factories'); + ->ignoring('Veeqtoh\SecureCode\Models\Factories'); test('model factories extend the base factory class') - ->expect('Veeqtoh\DoorAccess\Models\Factories') + ->expect('Veeqtoh\SecureCode\Models\Factories') ->classes() ->toExtend('Illuminate\Database\Eloquent\Factories\Factory'); \ No newline at end of file diff --git a/tests/Architecture/ProvidersTest.php b/tests/Architecture/ProvidersTest.php index 43ca76b..a828c90 100644 --- a/tests/Architecture/ProvidersTest.php +++ b/tests/Architecture/ProvidersTest.php @@ -1,6 +1,6 @@ expect('Veeqtoh\DoorAccess\Providers') + ->expect('Veeqtoh\SecureCode\Providers') ->classes() ->toExtend(\Illuminate\Support\ServiceProvider::class); \ No newline at end of file diff --git a/tests/Architecture/TraitsTest.php b/tests/Architecture/TraitsTest.php index e4dfb26..ab95857 100644 --- a/tests/Architecture/TraitsTest.php +++ b/tests/Architecture/TraitsTest.php @@ -1,5 +1,5 @@ expect('Veeqtoh\DoorAccess\Traits') + ->expect('Veeqtoh\SecureCode\Traits') ->toBeTraits(); \ No newline at end of file diff --git a/tests/Unit/Classes/CodeGeneratorTest.php b/tests/Unit/Classes/CodeGeneratorTest.php index 94b01fd..45e1fe4 100644 --- a/tests/Unit/Classes/CodeGeneratorTest.php +++ b/tests/Unit/Classes/CodeGeneratorTest.php @@ -1,17 +1,17 @@ 'numeric']); + config(['secure-code.code_format' => 'numeric']); $generator = new CodeGenerator(); $code = $generator->generate(); @@ -23,7 +23,7 @@ public function testGenerateNumericCode(): void public function testGenerateAlphanumericCode(): void { // Set code format to alphanumeric. - config(['door-access.code_format' => 'alphanumeric']); + config(['secure-code.code_format' => 'alphanumeric']); $generator = new CodeGenerator(); $code = $generator->generate(); @@ -35,7 +35,7 @@ public function testGenerateAlphanumericCode(): void public function testGenerateMixedCode(): void { // Set code format to mixed. - config(['door-access.code_format' => 'mixed']); + config(['secure-code.code_format' => 'mixed']); $generator = new CodeGenerator(); $code = $generator->generate(); diff --git a/tests/Unit/Classes/CodeManagerTest.php b/tests/Unit/Classes/CodeManagerTest.php index 0eedb0f..434b7c9 100644 --- a/tests/Unit/Classes/CodeManagerTest.php +++ b/tests/Unit/Classes/CodeManagerTest.php @@ -1,13 +1,13 @@ saveCode($code); + $secureCode = $codeManager->saveCode($code); - $this->assertInstanceOf(AccessCode::class, $accessCode); - $this->assertEquals($code, $accessCode->code); + $this->assertInstanceOf(SecureCode::class, $secureCode); + $this->assertEquals($code, $secureCode->code); } #[Test] public function testAllocateCode(): void { - AccessCode::factory()->create(['code' => '123456', 'owner_id' => null]); + SecureCode::factory()->create(['code' => '123456', 'owner_id' => null]); $codeManager = new CodeManager(); $ownerId = 'user_123'; - $accessCode = $codeManager->allocateCode('123456', $ownerId); + $secureCode = $codeManager->allocateCode('123456', $ownerId); - $this->assertInstanceOf(AccessCode::class, $accessCode); - $this->assertEquals($ownerId, $accessCode->owner_id); + $this->assertInstanceOf(SecureCode::class, $secureCode); + $this->assertEquals($ownerId, $secureCode->owner_id); } public function testResetCode(): void { - AccessCode::factory()->create(['code' => '123456']); + SecureCode::factory()->create(['code' => '123456']); $codeManager = new CodeManager(); $code = '123456'; $result = $codeManager->resetCode($code); - $this->assertNull(AccessCode::where('code', $code)->first()->owner_id); + $this->assertNull(SecureCode::where('code', $code)->first()->owner_id); } public function testResetCodeWithNonExistingCode(): void @@ -59,14 +59,14 @@ public function testResetCodeWithNonExistingCode(): void public function testDestroyCode(): void { - AccessCode::factory()->create(['code' => '123456']); + SecureCode::factory()->create(['code' => '123456']); $codeManager = new CodeManager(); $code = '123456'; $result = $codeManager->destroyCode($code); $this->assertTrue($result); - $this->assertNull(AccessCode::where('code', $code)->first()); + $this->assertNull(SecureCode::where('code', $code)->first()); } public function testDestroyCodeNonExistingCode(): void diff --git a/tests/Unit/Classes/ValidationTest.php b/tests/Unit/Classes/ValidationTest.php index 86185d7..194ee03 100644 --- a/tests/Unit/Classes/ValidationTest.php +++ b/tests/Unit/Classes/ValidationTest.php @@ -1,12 +1,12 @@ expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.code_length field must be an integer.'); + $this->expectExceptionMessage('The secure-code.code_length field must be an integer.'); - Config::set('door-access.code_length', 'INVALID'); + Config::set('secure-code.code_length', 'INVALID'); $validation = new Validation(); $validation->validateConfig(); @@ -26,9 +26,9 @@ public function exception_is_thrown_if_the_code_length_is_not_an_integer(): void public function exception_is_thrown_if_the_code_length_is_not_provided(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.code_length field is required.'); + $this->expectExceptionMessage('The secure-code.code_length field is required.'); - Config::set('door-access.code_length', ''); + Config::set('secure-code.code_length', ''); $validation = new Validation(); $validation->validateConfig(); @@ -38,9 +38,9 @@ public function exception_is_thrown_if_the_code_length_is_not_provided(): void public function exception_is_thrown_if_the_code_length_is_greater_than_19(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.code_length field must not be greater than 19'); + $this->expectExceptionMessage('The secure-code.code_length field must not be greater than 19'); - Config::set('door-access.code_length', 29); + Config::set('secure-code.code_length', 29); $validation = new Validation(); $validation->validateConfig(); @@ -50,9 +50,9 @@ public function exception_is_thrown_if_the_code_length_is_greater_than_19(): voi public function exception_is_thrown_if_the_character_repeated_limit_is_not_provided(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.character_repeated_limit field is required.'); + $this->expectExceptionMessage('The secure-code.character_repeated_limit field is required.'); - Config::set('door-access.character_repeated_limit', ''); + Config::set('secure-code.character_repeated_limit', ''); $validation = new Validation(); $validation->validateConfig(); @@ -62,9 +62,9 @@ public function exception_is_thrown_if_the_character_repeated_limit_is_not_provi public function exception_is_thrown_if_the_character_repeated_limit_is_less_than_3(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.character_repeated_limit field must be at least 3.'); + $this->expectExceptionMessage('The secure-code.character_repeated_limit field must be at least 3.'); - Config::set('door-access.character_repeated_limit', 2); + Config::set('secure-code.character_repeated_limit', 2); $validation = new Validation(); $validation->validateConfig(); @@ -74,9 +74,9 @@ public function exception_is_thrown_if_the_character_repeated_limit_is_less_than public function exception_is_thrown_if_the_character_repeated_limit_is_greater_than_19(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.character_repeated_limit field must not be greater than 19.'); + $this->expectExceptionMessage('The secure-code.character_repeated_limit field must not be greater than 19.'); - Config::set('door-access.character_repeated_limit', 29); + Config::set('secure-code.character_repeated_limit', 29); $validation = new Validation(); $validation->validateConfig(); @@ -86,9 +86,9 @@ public function exception_is_thrown_if_the_character_repeated_limit_is_greater_t public function exception_is_thrown_if_the_sequence_length_limit_is_not_provided(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.sequence_length_limit field is required.'); + $this->expectExceptionMessage('The secure-code.sequence_length_limit field is required.'); - Config::set('door-access.sequence_length_limit', ''); + Config::set('secure-code.sequence_length_limit', ''); $validation = new Validation(); $validation->validateConfig(); @@ -98,9 +98,9 @@ public function exception_is_thrown_if_the_sequence_length_limit_is_not_provided public function exception_is_thrown_if_the_sequence_length_limit_is_less_than_3(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.sequence_length_limit field must be at least 3.'); + $this->expectExceptionMessage('The secure-code.sequence_length_limit field must be at least 3.'); - Config::set('door-access.sequence_length_limit', 2); + Config::set('secure-code.sequence_length_limit', 2); $validation = new Validation(); $validation->validateConfig(); @@ -110,9 +110,9 @@ public function exception_is_thrown_if_the_sequence_length_limit_is_less_than_3( public function exception_is_thrown_if_the_unique_characters_limit_is_not_provided(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.unique_characters_limit field is required.'); + $this->expectExceptionMessage('The secure-code.unique_characters_limit field is required.'); - Config::set('door-access.unique_characters_limit', ''); + Config::set('secure-code.unique_characters_limit', ''); $validation = new Validation(); $validation->validateConfig(); @@ -122,9 +122,9 @@ public function exception_is_thrown_if_the_unique_characters_limit_is_not_provid public function exception_is_thrown_if_the_unique_characters_limit_is_less_than_3(): void { $this->expectException(ValidationException::class); - $this->expectExceptionMessage('The door-access.unique_characters_limit field must be at least 3.'); + $this->expectExceptionMessage('The secure-code.unique_characters_limit field must be at least 3.'); - Config::set('door-access.unique_characters_limit', 2); + Config::set('secure-code.unique_characters_limit', 2); $validation = new Validation(); $validation->validateConfig(); diff --git a/tests/Unit/Models/AccessCodeFactoryTest.php b/tests/Unit/Models/AccessCodeFactoryTest.php deleted file mode 100644 index cbfe0de..0000000 --- a/tests/Unit/Models/AccessCodeFactoryTest.php +++ /dev/null @@ -1,27 +0,0 @@ -create(); - - $deactivatedAccessCode = AccessCode::factory()->deactivated()->create(); - - $inactiveAccessCode = AccessCode::factory()->inactive()->create(); - - $this->assertDatabaseCount('access_codes', 3) - ->assertModelExists($accessCode) - ->assertModelExists($deactivatedAccessCode) - ->assertModelExists($inactiveAccessCode); - - $this->assertTrue($accessCode->allocated_at !== null && $accessCode->reset_at == null); - $this->assertTrue($deactivatedAccessCode->allocated_at !== null && $deactivatedAccessCode->reset_at !== null); - $this->assertTrue($inactiveAccessCode->allocated_at == null && $inactiveAccessCode->reset_at == null); - } -} \ No newline at end of file diff --git a/tests/Unit/Models/AccessCodeTest.php b/tests/Unit/Models/AccessCodeTest.php deleted file mode 100644 index 8f97cd1..0000000 --- a/tests/Unit/Models/AccessCodeTest.php +++ /dev/null @@ -1,29 +0,0 @@ - 'custom']); - - $this->assertEquals( - 'custom', - (new AccessCode())->getConnectionName(), - ); - } - - #[Test] - public function default_connection_is_used_if_the_override_is_not_set(): void - { - $this->assertNull((new AccessCode())->getConnectionName()); - } -} \ No newline at end of file diff --git a/tests/Unit/Models/CastsTest.php b/tests/Unit/Models/CastsTest.php index fdbb8a0..3cffa19 100644 --- a/tests/Unit/Models/CastsTest.php +++ b/tests/Unit/Models/CastsTest.php @@ -2,19 +2,19 @@ declare(strict_types=1); -namespace Veeqtoh\DoorAccess\Tests\Unit\Models\AccessCode; +namespace Veeqtoh\SecureCode\Tests\Unit\Models\SecureCode; use Carbon\Carbon; use PHPUnit\Framework\Attributes\Test; -use Veeqtoh\DoorAccess\Models\AccessCode; -use Veeqtoh\DoorAccess\Tests\Unit\TestCase; +use Veeqtoh\SecureCode\Models\SecureCode; +use Veeqtoh\SecureCode\Tests\Unit\TestCase; final class CastsTest extends TestCase { #[Test] public function carbon_date_objects_are_returned(): void { - $accessCode = AccessCode::factory() + $secureCode = SecureCode::factory() ->create([ 'allocated_at' => now(), 'reset_at' => now(), @@ -22,11 +22,11 @@ public function carbon_date_objects_are_returned(): void 'updated_at' => now(), ]); - $accessCode->refresh(); + $secureCode->refresh(); - $this->assertInstanceOf(Carbon::class, $accessCode->allocated_at); - $this->assertInstanceOf(Carbon::class, $accessCode->reset_at); - $this->assertInstanceOf(Carbon::class, $accessCode->created_at); - $this->assertInstanceOf(Carbon::class, $accessCode->updated_at); + $this->assertInstanceOf(Carbon::class, $secureCode->allocated_at); + $this->assertInstanceOf(Carbon::class, $secureCode->reset_at); + $this->assertInstanceOf(Carbon::class, $secureCode->created_at); + $this->assertInstanceOf(Carbon::class, $secureCode->updated_at); } } \ No newline at end of file diff --git a/tests/Unit/Models/SecureCodeFactoryTest.php b/tests/Unit/Models/SecureCodeFactoryTest.php new file mode 100644 index 0000000..871b866 --- /dev/null +++ b/tests/Unit/Models/SecureCodeFactoryTest.php @@ -0,0 +1,27 @@ +create(); + + $deactivatedSecureCode = SecureCode::factory()->deactivated()->create(); + + $inactiveSecureCode = SecureCode::factory()->inactive()->create(); + + $this->assertDatabaseCount('secure_codes', 3) + ->assertModelExists($secureCode) + ->assertModelExists($deactivatedSecureCode) + ->assertModelExists($inactiveSecureCode); + + $this->assertTrue($secureCode->allocated_at !== null && $secureCode->reset_at == null); + $this->assertTrue($deactivatedSecureCode->allocated_at !== null && $deactivatedSecureCode->reset_at !== null); + $this->assertTrue($inactiveSecureCode->allocated_at == null && $inactiveSecureCode->reset_at == null); + } +} \ No newline at end of file diff --git a/tests/Unit/Models/SecureCodeTest.php b/tests/Unit/Models/SecureCodeTest.php new file mode 100644 index 0000000..73f2f80 --- /dev/null +++ b/tests/Unit/Models/SecureCodeTest.php @@ -0,0 +1,29 @@ + 'custom']); + + $this->assertEquals( + 'custom', + (new SecureCode())->getConnectionName(), + ); + } + + #[Test] + public function default_connection_is_used_if_the_override_is_not_set(): void + { + $this->assertNull((new SecureCode())->getConnectionName()); + } +} \ No newline at end of file diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php index 3dd0ac8..247e865 100644 --- a/tests/Unit/TestCase.php +++ b/tests/Unit/TestCase.php @@ -1,12 +1,12 @@ DoorAccess::class, + 'SecureCode' => SecureCode::class, ]; } } \ No newline at end of file