diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..138d4dc --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,29 @@ +# Base image is from Microsoft +FROM mcr.microsoft.com/vscode/devcontainers/base:ubuntu-22.04 + +# Avoid interactive prompts (e.g. tzdata) during installation +ENV DEBIAN_FRONTEND=noninteractive + +RUN mkdir -p /var/www/html + +# Install 8.3 from Ondrej's PPA +RUN apt-get update && apt-get install -y \ + software-properties-common \ + && add-apt-repository ppa:ondrej/php -y \ + && apt-get update && apt-get install -y sqlite3 \ + && apt-get install -y php8.4-cli php8.4-dev \ + php8.4-pgsql php8.4-sqlite3 php8.4-gd \ + php8.4-curl \ + php8.4-imap php8.4-mysql php8.4-mbstring \ + php8.4-xml php8.4-zip php8.4-bcmath php8.4-soap \ + php8.4-intl php8.4-readline \ + php8.4-ldap \ + php8.4-msgpack php8.4-igbinary php8.4-redis php8.4-swoole \ + php8.4-memcached php8.4-pcov php8.4-imagick php8.4-xdebug \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +# Install Composer +RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \ + && php composer-setup.php --install-dir=/usr/local/bin --filename=composer \ + && php -r "unlink('composer-setup.php');" \ + && composer --version \ No newline at end of file diff --git a/.devcontainer/devcontainer.bash b/.devcontainer/devcontainer.bash new file mode 100644 index 0000000..5d8fe63 --- /dev/null +++ b/.devcontainer/devcontainer.bash @@ -0,0 +1,46 @@ +alias art='php artisan --ansi' +alias tinker='art tinker' +alias format='php vendor/bin/pint' +alias analyze='php vendor/bin/phpstan analyse' +alias test='php vendor/bin/paratest --coverage-html coverage' +alias stf='php vendor/bin/phpunit --filter' + +# commit AI +function commit() { + commitMessage="$*" + + git add . + + if [ "$commitMessage" = "" ]; then + aicommits + return + fi + + eval "git commit -a -m '${commitMessage}'" +} + +# function gfind +function gfind() { + local excludeVendor="--exclude-dir=vendor" # Default to excluding the vendor directory + local searchString="" + local searchPath="./" + + # Process all arguments + for arg in "$@"; do + if [[ "$arg" == "-w" || "$arg" == "--with-vendor" ]]; then + excludeVendor="" # Remove the exclude directive to include vendor + elif [[ -z "$searchString" && "$arg" != -* ]]; then + searchString="$arg" # Set the search string if it's not a flag and is the first non-flag argument + fi + done + + # Check if a search string was provided + if [[ -z "$searchString" ]]; then + echo -e "${RED}Error: Missing required search string.${NC}" + echo -e "${YELLOW}Usage: ${NC}gfind searchString [-w|--with-vendor]" + return 1 + fi + + # Execute grep command + grep --include=\*.php $excludeVendor -rnw $searchPath -e "$searchString" +} \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..fa94e81 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,27 @@ +{ + "name": "Sanitizer", + "build": { + "dockerfile": "Dockerfile", + "context": "." + }, + "features": {}, + "customizations": { + "vscode": { + "extensions": [ + "bmewburn.vscode-intelephense-client", + "calebporzio.better-phpunit", + "laravel.vscode-laravel", + "mikestead.dotenv", + "ms-azuretools.vscode-docker", + "php.intelephense" + ], + "settings": { + "intelephense.environment.phpVersion": "8.4" + } + } + }, + "remoteUser": "vscode", + "postCreateCommand": "cat ./.devcontainer/devcontainer.bash >> /home/vscode/.bashrc", + "forwardPorts": [], + "portsAttributes": {} + } \ No newline at end of file diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..39b1580 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,19 @@ +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" + + - package-ecosystem: "composer" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml new file mode 100644 index 0000000..a4ebb3e --- /dev/null +++ b/.github/workflows/dependabot-auto-merge.yml @@ -0,0 +1,33 @@ +name: dependabot-auto-merge +on: pull_request_target + +permissions: + pull-requests: write + contents: write + +jobs: + dependabot: + runs-on: ubuntu-latest + timeout-minutes: 5 + if: ${{ github.actor == 'dependabot[bot]' }} + steps: + + - name: Dependabot metadata + id: metadata + uses: dependabot/fetch-metadata@v2.5.0 + with: + github-token: "${{ secrets.GITHUB_TOKEN }}" + + - name: Auto-merge Dependabot PRs for semver-minor updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Auto-merge Dependabot PRs for semver-patch updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/laravel.yml b/.github/workflows/run-tests.yml similarity index 100% rename from .github/workflows/laravel.yml rename to .github/workflows/run-tests.yml diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 0000000..c6c3e24 --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,32 @@ +name: "Update Changelog" + +on: + release: + types: [released] + +permissions: + contents: write + +jobs: + update: + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Checkout code + uses: actions/checkout@v6 + with: + ref: main + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + latest-version: ${{ github.event.release.name }} + release-notes: ${{ github.event.release.body }} + + - name: Commit updated CHANGELOG + uses: stefanzweifel/git-auto-commit-action@v7 + with: + branch: main + commit_message: Update CHANGELOG + file_pattern: CHANGELOG.md diff --git a/.gitignore b/.gitignore index 58e6535..9cf4d43 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /vendor/ composer.lock .phpunit.result.cache +.php-cs-fixer.cache \ No newline at end of file diff --git a/README.md b/README.md index 53fdb8d..4cf738d 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ +[](https://supportukrainenow.org) + # Sanitizer -![https://github.com/binary-cats/sanitizer/actions](https://github.com/binary-cats/sanitizer/workflows/Laravel/badge.svg) -![https://github.styleci.io/repos/316763653](https://github.styleci.io/repos/316763653/shield) -![https://scrutinizer-ci.com/g/binary-cats/sanitizer/](https://scrutinizer-ci.com/g/binary-cats/sanitizer/badges/quality-score.png?b=master) -[![Build Status](https://img.shields.io/travis/binary-cats/sanitizer/master.svg?style=flat-square)](https://travis-ci.org/binary-cats/sanitizer) +[![Latest Version on Packagist](https://img.shields.io/packagist/v/binary-cats/sanitizer.svg?style=flat-square)](https://packagist.org/packages/binary-cats/sanitizer) +[![Tests](https://img.shields.io/github/actions/workflow/status/binary-cats/sanitizer/run-tests.yml?branch=v13.x&label=tests&style=flat-square)](https://github.com/binary-cats/sanitizer/actions/workflows/run-tests.yml) +[![Code Style Action Status](https://github.styleci.io/repos/316763653/shield?branch=v13.x)](https://github.styleci.io/repos/316763653) ## Introduction diff --git a/UPGRADING.md b/UPGRADING.md index f9c5815..070a88d 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,2 +1,22 @@ # Upgrading +## Version Changes + +### Dependencies +- **illuminate/support**: Updated to version `^12.0` +- **illuminate/validation**: Updated to version `^12.0` +- **nesbot/carbon**: Updated to version `^3.0` +- **phpunit/phpunit**: Updated to version `^11.0` for development. + +### Minimum Stability +- The minimum stability is set to `dev`, which may allow for unstable packages to be installed. Ensure that you are aware of the implications of using development versions. + +## Upgrade Steps +1. **Update Dependencies**: Run `composer update` to install the latest versions of the required packages. +2. **Check for Breaking Changes**: Review the changelogs for `illuminate/support`, `illuminate/validation`, and `nesbot/carbon` for any breaking changes that may affect your application. +3. **Test Your Application**: After updating, run your tests to ensure everything works as expected. Use the command `composer test` to execute your PHPUnit tests. +4. **Review Laravel Configuration**: Ensure that the service provider and facade alias are correctly registered in your Laravel application. + +## Additional Notes +- If you encounter issues, consider checking the documentation for each package for guidance on resolving compatibility problems. + diff --git a/composer.json b/composer.json index 2c77211..c5b927d 100644 --- a/composer.json +++ b/composer.json @@ -17,12 +17,12 @@ "email": "cyrill.kalita@gmail.com" }], "require": { - "illuminate/support": "^8.0|^9.0", - "illuminate/validation": "^8.0|^9.0", - "nesbot/carbon": "^2.0" + "illuminate/support": "^12.0|^13.0", + "illuminate/validation": "^12.0|^13.0", + "nesbot/carbon": "^3.0" }, "require-dev": { - "phpunit/phpunit": "^9.0" + "phpunit/phpunit": "^11.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index c136f86..9bec3e5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,17 +1,15 @@ - - ./tests/ + ./tests/ diff --git a/src/Contracts/Filter.php b/src/Contracts/Filter.php index fbc5f0b..272c82d 100644 --- a/src/Contracts/Filter.php +++ b/src/Contracts/Filter.php @@ -7,8 +7,8 @@ interface Filter /** * Return the result of applying this filter to the given input. * - * @param mixed $value - * @return mixed + * @param mixed $value + * @return mixed */ public function apply($value, $options = []); } diff --git a/src/Filters/Capitalize.php b/src/Filters/Capitalize.php index ccde60c..0b17415 100644 --- a/src/Filters/Capitalize.php +++ b/src/Filters/Capitalize.php @@ -9,8 +9,8 @@ class Capitalize implements Filter /** * Capitalize the given string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { diff --git a/src/Filters/Cast.php b/src/Filters/Cast.php index b7255e8..14b8aa5 100644 --- a/src/Filters/Cast.php +++ b/src/Filters/Cast.php @@ -10,35 +10,22 @@ class Cast implements Filter /** * Capitalize the given string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { - $type = isset($options[0]) ? $options[0] : null; - switch ($type) { - case 'int': - case 'integer': - return (int) $value; - case 'real': - case 'float': - case 'double': - return (float) $value; - case 'string': - return (string) $value; - case 'bool': - case 'boolean': - return (bool) $value; - case 'object': - return is_array($value) ? (object) $value : json_decode($value, false); - case 'array': - return json_decode($value, true); - case 'collection': - $array = is_array($value) ? $value : json_decode($value, true); + $type = $options[0] ?? null; - return new Collection($array); - default: - throw new \InvalidArgumentException("Wrong Sanitizer casting format: {$type}."); - } + return match ($type) { + 'int', 'integer' => (int) $value, + 'real', 'float', 'double' => (float) $value, + 'string' => (string) $value, + 'bool', 'boolean' => (bool) $value, + 'object' => is_array($value) ? (object) $value : json_decode($value, false), + 'array' => json_decode($value, true), + 'collection' => new Collection(is_array($value) ? $value : json_decode($value, true)), + default => throw new \InvalidArgumentException("Wrong Sanitizer casting format: {$type}."), + }; } } diff --git a/src/Filters/Digit.php b/src/Filters/Digit.php index 5b664de..f07b5f4 100644 --- a/src/Filters/Digit.php +++ b/src/Filters/Digit.php @@ -9,8 +9,8 @@ class Digit implements Filter /** * Get only digit characters from the string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { diff --git a/src/Filters/EscapeHTML.php b/src/Filters/EscapeHTML.php index 7dcab26..a8345a4 100644 --- a/src/Filters/EscapeHTML.php +++ b/src/Filters/EscapeHTML.php @@ -9,11 +9,11 @@ class EscapeHTML implements Filter /** * Remove HTML tags and encode special characters from the given string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { - return is_string($value) ? filter_var($value, FILTER_SANITIZE_STRING) : $value; + return is_string($value) ? htmlspecialchars(strip_tags($value), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : $value; } } diff --git a/src/Filters/FilterIf.php b/src/Filters/FilterIf.php index 33b59cd..93a4306 100644 --- a/src/Filters/FilterIf.php +++ b/src/Filters/FilterIf.php @@ -9,9 +9,9 @@ class FilterIf implements Filter /** * Checks if filters should run if there is value passed that matches. * - * @param array $values - * @param array $options - * @return bool + * @param array $values + * @param array $options + * @return bool */ public function apply($values, $options = []) { diff --git a/src/Filters/FormatDate.php b/src/Filters/FormatDate.php index 676257e..359db11 100644 --- a/src/Filters/FormatDate.php +++ b/src/Filters/FormatDate.php @@ -11,8 +11,8 @@ class FormatDate implements Filter /** * Lowercase the given string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { diff --git a/src/Filters/Lowercase.php b/src/Filters/Lowercase.php index 253e7b8..54f7ebb 100644 --- a/src/Filters/Lowercase.php +++ b/src/Filters/Lowercase.php @@ -9,8 +9,8 @@ class Lowercase implements Filter /** * Lowercase the given string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { diff --git a/src/Filters/StripTags.php b/src/Filters/StripTags.php index 549466d..a60c94b 100644 --- a/src/Filters/StripTags.php +++ b/src/Filters/StripTags.php @@ -9,8 +9,8 @@ class StripTags implements Filter /** * Strip tags from the given string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { diff --git a/src/Filters/Trim.php b/src/Filters/Trim.php index e4d4f66..a524e5b 100644 --- a/src/Filters/Trim.php +++ b/src/Filters/Trim.php @@ -9,8 +9,8 @@ class Trim implements Filter /** * Trims the given string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { diff --git a/src/Filters/Uppercase.php b/src/Filters/Uppercase.php index 762e39a..c6f75e8 100644 --- a/src/Filters/Uppercase.php +++ b/src/Filters/Uppercase.php @@ -9,8 +9,8 @@ class Uppercase implements Filter /** * Lowercase the given string. * - * @param string $value - * @return string + * @param string $value + * @return string */ public function apply($value, $options = []) { diff --git a/src/Laravel/Factory.php b/src/Laravel/Factory.php index a7fe05f..dd452ac 100644 --- a/src/Laravel/Factory.php +++ b/src/Laravel/Factory.php @@ -12,7 +12,7 @@ class Factory /** * List of custom filters. * - * @var array + * @var array */ protected $customFilters; @@ -29,9 +29,9 @@ public function __construct() /** * Create a new Sanitizer instance. * - * @param array $data Data to be sanitized - * @param array $rules Filters to be applied to the given data - * @return Sanitizer + * @param array $data Data to be sanitized + * @param array $rules Filters to be applied to the given data + * @return Sanitizer */ public function make(array $data, array $rules) { @@ -43,11 +43,11 @@ public function make(array $data, array $rules) /** * Add a custom filters to all Sanitizers created with this Factory. * - * @param string $name Name of the filter - * @param mixed $extension Either the full class name of a Filter class implementing the Filter contract, or a Closure. - * @return void + * @param string $name Name of the filter + * @param mixed $extension Either the full class name of a Filter class implementing the Filter contract, or a Closure. + * @return void * - * @throws InvalidArgumentException + * @throws InvalidArgumentException */ public function extend($name, $customFilter) { diff --git a/src/Laravel/FormRequest.php b/src/Laravel/FormRequest.php index c7e9c85..dd90304 100644 --- a/src/Laravel/FormRequest.php +++ b/src/Laravel/FormRequest.php @@ -9,7 +9,7 @@ class FormRequest extends LaravelFormRequest /** * Sanitize input before validating. * - * @return void + * @return void */ public function validate() { @@ -20,7 +20,7 @@ public function validate() /** * Sanitize this request's input. * - * @return void + * @return void */ public function sanitize() { @@ -31,7 +31,7 @@ public function sanitize() /** * Filters to be applied to the input. * - * @return array + * @return array */ public function filters() { @@ -41,7 +41,7 @@ public function filters() /** * Validation rules to be applied to the input. * - * @return array + * @return array */ public function rules() { diff --git a/src/Laravel/SanitizerServiceProvider.php b/src/Laravel/SanitizerServiceProvider.php index bdcd8e8..16379d9 100644 --- a/src/Laravel/SanitizerServiceProvider.php +++ b/src/Laravel/SanitizerServiceProvider.php @@ -15,7 +15,7 @@ public function register() { // Register the sanitizer factory: $this->app->singleton('sanitizer', function ($app) { - return new Factory; + return new Factory(); }); // Register make request command diff --git a/src/Laravel/SanitizesInput.php b/src/Laravel/SanitizesInput.php index bff5c52..f808537 100644 --- a/src/Laravel/SanitizesInput.php +++ b/src/Laravel/SanitizesInput.php @@ -7,7 +7,7 @@ trait SanitizesInput /** * Sanitize input before validating. * - * @return void + * @return void */ public function validateResolved() { @@ -18,7 +18,7 @@ public function validateResolved() /** * Sanitize this request's input. * - * @return void + * @return void */ public function sanitize() { @@ -30,7 +30,7 @@ public function sanitize() /** * Add custom fields to the Sanitizer. * - * @return void + * @return void */ public function addCustomFilters() { @@ -42,7 +42,7 @@ public function addCustomFilters() /** * Filters to be applied to the input. * - * @return array + * @return array */ public function filters() { @@ -52,7 +52,7 @@ public function filters() /** * Custom Filters to be applied to the input. * - * @return array + * @return array */ public function customFilters() { diff --git a/src/Sanitizer.php b/src/Sanitizer.php index ff070de..a983e42 100644 --- a/src/Sanitizer.php +++ b/src/Sanitizer.php @@ -13,42 +13,42 @@ class Sanitizer /** * Data to sanitize. * - * @var array + * @var array */ protected $data; /** * Filters to apply. * - * @var array + * @var array */ protected $rules; /** * Available filters as $name => $classPath. * - * @var array + * @var array */ protected $filters = [ - 'capitalize' => \BinaryCats\Sanitizer\Filters\Capitalize::class, - 'cast' => \BinaryCats\Sanitizer\Filters\Cast::class, - 'escape' => \BinaryCats\Sanitizer\Filters\EscapeHTML::class, + 'capitalize' => \BinaryCats\Sanitizer\Filters\Capitalize::class, + 'cast' => \BinaryCats\Sanitizer\Filters\Cast::class, + 'escape' => \BinaryCats\Sanitizer\Filters\EscapeHTML::class, 'format_date' => \BinaryCats\Sanitizer\Filters\FormatDate::class, - 'lowercase' => \BinaryCats\Sanitizer\Filters\Lowercase::class, - 'uppercase' => \BinaryCats\Sanitizer\Filters\Uppercase::class, - 'trim' => \BinaryCats\Sanitizer\Filters\Trim::class, - 'strip_tags' => \BinaryCats\Sanitizer\Filters\StripTags::class, - 'digit' => \BinaryCats\Sanitizer\Filters\Digit::class, - 'filter_if' => \BinaryCats\Sanitizer\Filters\FilterIf::class, + 'lowercase' => \BinaryCats\Sanitizer\Filters\Lowercase::class, + 'uppercase' => \BinaryCats\Sanitizer\Filters\Uppercase::class, + 'trim' => \BinaryCats\Sanitizer\Filters\Trim::class, + 'strip_tags' => \BinaryCats\Sanitizer\Filters\StripTags::class, + 'digit' => \BinaryCats\Sanitizer\Filters\Digit::class, + 'filter_if' => \BinaryCats\Sanitizer\Filters\FilterIf::class, ]; /** * Create a new sanitizer instance. * - * @param array $data - * @param array $rules Rules to be applied to each data attribute - * @param array $filters Available filters for this sanitizer - * @return Sanitizer + * @param array $data + * @param array $rules Rules to be applied to each data attribute + * @param array $filters Available filters for this sanitizer + * @return Sanitizer */ public function __construct(array $data, array $rules, array $customFilters = []) { @@ -60,8 +60,8 @@ public function __construct(array $data, array $rules, array $customFilters = [] /** * Parse a rules array. * - * @param array $rules - * @return array + * @param array $rules + * @return array */ protected function parseRules(array $rules) { @@ -84,8 +84,8 @@ protected function parseRules(array $rules) /** * Parse a rule. * - * @param string|Closure $rule - * @return array|Closure + * @param string|Closure $rule + * @return array|Closure */ protected function parseRule($rule) { @@ -101,8 +101,8 @@ protected function parseRule($rule) /** * Parse a rule string formatted as filterName:option1, option2 into an array formatted as [name => filterName, options => [option1, option2]]. * - * @param string $rule Formatted as 'filterName:option1, option2' or just 'filterName' - * @return array Formatted as [name => filterName, options => [option1, option2]]. Empty array if no filter name was found. + * @param string $rule Formatted as 'filterName:option1, option2' or just 'filterName' + * @return array Formatted as [name => filterName, options => [option1, option2]]. Empty array if no filter name was found. */ protected function parseRuleString($rule) { @@ -123,8 +123,8 @@ protected function parseRuleString($rule) /** * Apply the given filter by its name. * - * @param string|Closure $rule - * @return Filter + * @param string|Closure $rule + * @return Filter */ protected function applyFilter($rule, $value) { @@ -145,14 +145,14 @@ protected function applyFilter($rule, $value) if ($filter instanceof Closure) { return call_user_func_array($filter, [$value, $options]); } else { - return (new $filter)->apply($value, $options); + return (new $filter())->apply($value, $options); } } /** * Sanitize the given data. * - * @return array + * @return array */ public function sanitize() { diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 0dec7b3..0094ae1 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -2,6 +2,7 @@ use BinaryCats\Sanitizer\Laravel\Factory; use BinaryCats\Sanitizer\Sanitizer; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; class FactoryTest extends TestCase @@ -9,17 +10,18 @@ class FactoryTest extends TestCase public function sanitize($data, $rules) { $sanitizer = new Sanitizer($data, $rules, [ - 'capitalize' => \BinaryCats\Sanitizer\Filters\Capitalize::class, - 'escape' => \BinaryCats\Sanitizer\Filters\EscapeHTML::class, + 'capitalize' => \BinaryCats\Sanitizer\Filters\Capitalize::class, + 'escape' => \BinaryCats\Sanitizer\Filters\EscapeHTML::class, 'format_date' => \BinaryCats\Sanitizer\Filters\FormatDate::class, - 'lowercase' => \BinaryCats\Sanitizer\Filters\Lowercase::class, - 'uppercase' => \BinaryCats\Sanitizer\Filters\Uppercase::class, - 'trim' => \BinaryCats\Sanitizer\Filters\Trim::class, + 'lowercase' => \BinaryCats\Sanitizer\Filters\Lowercase::class, + 'uppercase' => \BinaryCats\Sanitizer\Filters\Uppercase::class, + 'trim' => \BinaryCats\Sanitizer\Filters\Trim::class, ]); return $sanitizer->sanitize(); } + #[Test] public function test_custom_closure_filter() { $factory = new Factory; @@ -38,6 +40,7 @@ public function test_custom_closure_filter() $this->assertEquals(sha1('TEST'), $newData['name']); } + #[Test] public function test_custom_class_filter() { $factory = new Factory; @@ -54,6 +57,7 @@ public function test_custom_class_filter() $this->assertEquals('TESTTEST', $newData['name']); } + #[Test] public function test_replace_filter() { $factory = new Factory; diff --git a/tests/Filters/CapitalizeTest.php b/tests/Filters/CapitalizeTest.php index 279b17e..52146a4 100644 --- a/tests/Filters/CapitalizeTest.php +++ b/tests/Filters/CapitalizeTest.php @@ -1,13 +1,14 @@ sanitize(); } - /** - * @test - */ + #[Test] public function it_capitalizes_strings() { $result = $this->sanitize(['name' => ' jon snow 145'], ['name' => 'capitalize']); $this->assertEquals(' Jon Snow 145', $result['name']); } - /** - * @test - */ + #[Test] public function it_capitalizes_special_characters() { $result = $this->sanitize(['name' => 'Τάχιστη αλώπηξ'], ['name' => 'capitalize']); diff --git a/tests/Filters/CastTest.php b/tests/Filters/CastTest.php index d2f514e..6530366 100644 --- a/tests/Filters/CastTest.php +++ b/tests/Filters/CastTest.php @@ -5,11 +5,9 @@ class CastTest extends TestCase { - use _PHPUnitShim; - /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/Filters/DigitTest.php b/tests/Filters/DigitTest.php index 6adac5b..aac9643 100644 --- a/tests/Filters/DigitTest.php +++ b/tests/Filters/DigitTest.php @@ -6,8 +6,8 @@ class DigitTest extends TestCase { /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/Filters/EscapeHTMLTest.php b/tests/Filters/EscapeHTMLTest.php index f78edd4..c8b3d2f 100644 --- a/tests/Filters/EscapeHTMLTest.php +++ b/tests/Filters/EscapeHTMLTest.php @@ -6,8 +6,8 @@ class EscapeHTMLTest extends TestCase { /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/Filters/FilterIfTest.php b/tests/Filters/FilterIfTest.php index 0747b09..acbf4fc 100644 --- a/tests/Filters/FilterIfTest.php +++ b/tests/Filters/FilterIfTest.php @@ -6,8 +6,8 @@ class FilterIfTest extends TestCase { /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/Filters/FormatDateTest.php b/tests/Filters/FormatDateTest.php index b363d91..f86d84a 100644 --- a/tests/Filters/FormatDateTest.php +++ b/tests/Filters/FormatDateTest.php @@ -6,8 +6,8 @@ class FormatDateTest extends TestCase { /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/Filters/LowercaseTest.php b/tests/Filters/LowercaseTest.php index 9b7bf76..de91636 100644 --- a/tests/Filters/LowercaseTest.php +++ b/tests/Filters/LowercaseTest.php @@ -6,8 +6,8 @@ class LowercaseTest extends TestCase { /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/Filters/StripTagsTest.php b/tests/Filters/StripTagsTest.php index 0e0a396..82923b2 100644 --- a/tests/Filters/StripTagsTest.php +++ b/tests/Filters/StripTagsTest.php @@ -6,8 +6,8 @@ class StripTagsTest extends TestCase { /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/Filters/TrimTest.php b/tests/Filters/TrimTest.php index ac53e33..2fbb770 100644 --- a/tests/Filters/TrimTest.php +++ b/tests/Filters/TrimTest.php @@ -6,8 +6,8 @@ class TrimTest extends TestCase { /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/Filters/UppercaseTest.php b/tests/Filters/UppercaseTest.php index 711b291..3489859 100644 --- a/tests/Filters/UppercaseTest.php +++ b/tests/Filters/UppercaseTest.php @@ -6,8 +6,8 @@ class UppercaseTest extends TestCase { /** - * @param $data - * @param $rules + * @param $data + * @param $rules * @return mixed */ public function sanitize($data, $rules) diff --git a/tests/SanitizerTest.php b/tests/SanitizerTest.php index e54ebad..103d3b3 100644 --- a/tests/SanitizerTest.php +++ b/tests/SanitizerTest.php @@ -1,13 +1,14 @@ sanitize(); } + #[Test] public function test_combine_filters() { $data = [ @@ -29,6 +31,7 @@ public function test_combine_filters() $this->assertEquals('Hello Everybody', $data['name']); } + #[Test] public function test_input_unchanged_if_no_filter() { $data = [ @@ -41,6 +44,7 @@ public function test_input_unchanged_if_no_filter() $this->assertEquals(' HellO EverYboDy ', $data['name']); } + #[Test] public function test_array_filters() { $data = [ @@ -53,35 +57,34 @@ public function test_array_filters() $this->assertEquals('Hello Everybody', $data['name']); } + #[Test] public function test_wildcard_filters() { $data = [ - 'name' => [ + 'name' => [ 'first' => ' John ', - 'last' => ' Doe ', + 'last' => ' Doe ', ], 'address' => [ 'street' => ' Some street ', - 'city' => ' New York ', + 'city' => ' New York ', ], ]; $rules = [ - 'name.*' => 'trim', + 'name.*' => 'trim', 'address.city' => 'trim', ]; $data = $this->sanitize($data, $rules); $sanitized = [ - 'name' => ['first' => 'John', 'last' => 'Doe'], + 'name' => ['first' => 'John', 'last' => 'Doe'], 'address' => ['street' => ' Some street ', 'city' => 'New York'], ]; $this->assertEquals($sanitized, $data); } - /** - * @test - */ + #[Test] public function it_throws_exception_if_non_existing_filter() { $this->expectException(InvalidArgumentException::class); @@ -94,6 +97,7 @@ public function it_throws_exception_if_non_existing_filter() $data = $this->sanitize($data, $rules); } + #[Test] public function test_it_should_only_sanitize_passed_data() { $data = [ @@ -112,6 +116,7 @@ public function test_it_should_only_sanitize_passed_data() $this->assertEquals(1, count($data)); } + #[Test] public function test_closure_rule() { $data = [ diff --git a/tests/_PHPUnitShim.php b/tests/_PHPUnitShim.php deleted file mode 100644 index a314e6d..0000000 --- a/tests/_PHPUnitShim.php +++ /dev/null @@ -1,37 +0,0 @@ -=')) { - trait _PHPUnitShim - { - } -} - -if (version_compare(PHP_VERSION, '7.2.0', '<')) { - trait _PHPUnitShim - { - public function assertIsArray($actual, $message = '') - { - $this->assertInternalType('array', $actual, $message); - } - - public function assertIsBool($actual, $message = '') - { - $this->assertInternalType('bool', $actual, $message); - } - - public function assertIsFloat($actual, $message = '') - { - $this->assertInternalType('float', $actual, $message); - } - - public function assertIsInt($actual, $message = '') - { - $this->assertInternalType('integer', $actual, $message); - } - - public function assertIsString($actual, $message = '') - { - $this->assertInternalType('string', $actual, $message); - } - } -}