From 6ed1a46c167e9ad2a15a57e06b6dd03b08b9f76b Mon Sep 17 00:00:00 2001 From: velkuns Date: Fri, 15 Aug 2025 16:33:10 +0200 Subject: [PATCH] Component Validation: PHP 8.4 Support - Support for PHP 8.4 - Drop support for PHP 7.4, 8.0, 8.1, and 8.2 - String validator now throws an exception if values has not the required length, rather than returning default value. - Boolean validator now return `null` if the value is not a boolean, rather than return `false`. - Type as enforced on validator to return appropriate type (or null if applicable) - CI improvements --- .github/workflows/ci.yml | 56 +++++++------ .github/workflows/sonarcloud.yml | 46 +++++++++++ .php-cs-fixer.dist.php | 24 ++++++ CHANGELOG.md | 14 ++++ Makefile | 72 +++++++++-------- README.md | 45 +++++++++-- ci/composer-dependency-analyser.php | 11 +++ ci/php74-compatibility.neon | 9 --- ci/phpcs/eureka.xml | 8 -- ci/phpmax-compatibility.neon | 14 ++++ ...ibility.neon => phpmin-compatibility.neon} | 5 ++ composer.json | 27 ++++--- phpstan.neon.dist | 9 ++- phpunit.xml.dist | 42 +++++----- src/Entity/GenericEntity.php | 47 ++++------- src/Exception/ValidationException.php | 4 +- src/Validator/AbstractValidator.php | 10 +-- src/Validator/BooleanValidator.php | 18 ++--- src/Validator/DateTimeTrait.php | 11 +-- src/Validator/DateTimeValidator.php | 15 ++-- src/Validator/DateValidator.php | 15 ++-- src/Validator/DomainValidator.php | 19 +++-- src/Validator/EmailValidator.php | 19 +++-- src/Validator/FloatValidator.php | 19 +++-- src/Validator/IntegerValidator.php | 39 +++++----- src/Validator/IpValidator.php | 19 +++-- src/Validator/NullValidator.php | 11 +-- src/Validator/RegexpValidator.php | 19 +++-- src/Validator/StringValidator.php | 32 +++----- src/Validator/TimeValidator.php | 15 ++-- src/Validator/TimestampValidator.php | 19 +++-- src/Validator/UrlValidator.php | 19 +++-- src/ValidatorEntityFactoryInterface.php | 9 +-- src/ValidatorFactory.php | 62 ++++----------- src/ValidatorFactoryInterface.php | 9 --- src/ValidatorInterface.php | 9 +-- tests/Integration/.gitkeep | 0 tests/{ => Unit}/Entity/GenericEntityTest.php | 16 ++-- tests/{ => Unit}/FactoryValidatorTest.php | 10 +-- .../Validator/BooleanValidatorTest.php | 73 ++++++++--------- .../Validator/DateTimeValidatorTest.php | 41 +++++----- .../Validator/DateValidatorTest.php | 60 +++++--------- .../Validator/DomainValidatorTest.php | 51 +++++------- .../Validator/EmailValidatorTest.php | 51 +++++------- .../Validator/FloatValidatorTest.php | 57 +++++--------- .../Validator/IntegerValidatorTest.php | 73 +++++++---------- .../{ => Unit}/Validator/IpValidatorTest.php | 78 ++++++------------- .../Validator/NullValidatorTest.php | 6 +- .../Validator/RegexpValidatorTest.php | 6 +- .../Validator/StringValidatorTest.php | 56 ++++++------- .../Validator/TimeValidatorTest.php | 61 ++++++--------- .../Validator/TimestampValidatorTest.php | 61 +++++---------- .../{ => Unit}/Validator/UrlValidatorTest.php | 29 +++---- 53 files changed, 712 insertions(+), 838 deletions(-) create mode 100644 .github/workflows/sonarcloud.yml create mode 100644 .php-cs-fixer.dist.php create mode 100644 ci/composer-dependency-analyser.php delete mode 100644 ci/php74-compatibility.neon delete mode 100644 ci/phpcs/eureka.xml create mode 100644 ci/phpmax-compatibility.neon rename ci/{php83-compatibility.neon => phpmin-compatibility.neon} (53%) create mode 100644 tests/Integration/.gitkeep rename tests/{ => Unit}/Entity/GenericEntityTest.php (82%) rename tests/{ => Unit}/FactoryValidatorTest.php (91%) rename tests/{ => Unit}/Validator/BooleanValidatorTest.php (57%) rename tests/{ => Unit}/Validator/DateTimeValidatorTest.php (64%) rename tests/{ => Unit}/Validator/DateValidatorTest.php (56%) rename tests/{ => Unit}/Validator/DomainValidatorTest.php (77%) rename tests/{ => Unit}/Validator/EmailValidatorTest.php (68%) rename tests/{ => Unit}/Validator/FloatValidatorTest.php (57%) rename tests/{ => Unit}/Validator/IntegerValidatorTest.php (53%) rename tests/{ => Unit}/Validator/IpValidatorTest.php (50%) rename tests/{ => Unit}/Validator/NullValidatorTest.php (82%) rename tests/{ => Unit}/Validator/RegexpValidatorTest.php (78%) rename tests/{ => Unit}/Validator/StringValidatorTest.php (59%) rename tests/{ => Unit}/Validator/TimeValidatorTest.php (53%) rename tests/{ => Unit}/Validator/TimestampValidatorTest.php (55%) rename tests/{ => Unit}/Validator/UrlValidatorTest.php (78%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 624675d..3633fe5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,51 +7,59 @@ on: branches: [ master ] jobs: - build: + ci: runs-on: ubuntu-latest + strategy: + matrix: + php-versions: [ '8.3', '8.4' ] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: - fetch-depth: 0 # To avoid "Shallow clone detected" error in SonarCloud report + fetch-depth: 0 - - name: Validate composer.json and composer.lock - run: composer validate + #~ PHP Setup + - name: Setup PHP ${{ matrix.php-versions }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} - - name: Cache Composer packages + #~ Composer Cache + - name: Get Composer Cache Directory id: composer-cache - uses: actions/cache@v3 + run: | + echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - uses: actions/cache@v3 with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + #~ Composer install + - name: Validate composer.json + run: composer validate - name: Install dependencies if: steps.composer-cache.outputs.cache-hit != 'true' run: make install + #~ CI part + - name: Dependencies + run: make php/deps + - name: Check Code Style - run: make phpcs + run: make php/check - name: Units Tests - run: make tests + run: make php/tests - name: Fix unit tests report path run: | sed -i 's+'$GITHUB_WORKSPACE'+/github/workspace+g' build/reports/phpunit/clover.xml sed -i 's+'$GITHUB_WORKSPACE'+/github/workspace+g' build/reports/phpunit/unit.xml - - name: PHP 7.4 Compatibility - run: make php74compatibility - - - name: PHP 8.3 Compatibility - run: make php83compatibility - - name: PHP Static Analyze - run: make analyze - - - name: SonarCloud Scan - uses: SonarSource/sonarcloud-github-action@v2.1.1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: make php/analyze diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml new file mode 100644 index 0000000..8aca7aa --- /dev/null +++ b/.github/workflows/sonarcloud.yml @@ -0,0 +1,46 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + sonarcloud: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # To avoid "Shallow clone detected" error in SonarCloud report + + #~ PHP Setup + - name: Setup PHP 8.3 # Minimum version + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + + #~ Composer Cache + - name: Get Composer Cache Directory + id: composer-cache + run: | + echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - uses: actions/cache@v3 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Install dependencies + if: steps.composer-cache.outputs.cache-hit != 'true' + run: make install + + - name: Units Tests + run: make php/tests # To generate the coverage report + + - name: SonarQube Scan + uses: SonarSource/sonarqube-scan-action@v5.2.0 + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..de5ea3b --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,24 @@ +setRules( + [ + '@PER-CS2.0' => true, + ] + ) + + //~ Format + ->setFormat('txt') + + //~ Cache + ->setUsingCache(true) + ->setCacheFile(__DIR__ . '/build/.php-cs-fixer.cache') + + //~ Finder + ->setFinder((new PhpCsFixer\Finder())->in( + [ + __DIR__ . '/src', + __DIR__ . '/tests', + ])) +; diff --git a/CHANGELOG.md b/CHANGELOG.md index 961ca71..b4a4c1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [6.0.0] - 2025-08-015 +[6.0.0]: https://github.com/eureka-framework/component-validation/compare/5.3.0...6.0.0 +### Added +- Support for PHP 8.4 +### Removed +- Drop support for PHP 7.4, 8.0, 8.1, and 8.2 +### Changed +- String validator now throws an exception if values has not the required length, rather than returning default value. +- Boolean validator now return `null` if the value is not a boolean, rather than return `false`. +- Type as enforced on validator to return appropriate type (or null if applicable) +- CI improvements + +--- + ## [5.3.0] - 2024-02-06 [5.3.0]: https://github.com/eureka-framework/component-validation/compare/5.2.0...5.3.0 ### Changed diff --git a/Makefile b/Makefile index 1117034..28d874c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ -.PHONY: validate install update phpcs phpcbf php74compatibility php83compatibility phpstan analyze tests testdox ci clean +.PHONY: validate install update php/deps php/check php/fix php/min-compatibility php/max-compatibility php/phpstan php/analyze php/tests php/test php/testdox ci clean -PHP_FILES := $(shell find src tests -type f -name '*.php') +COMPOSER_BIN := composer +PHP_VERSION_MIN := 8.3 +PHP_VERSION_MAX := 8.4 define header = @if [ -t 1 ]; then printf "\n\e[37m\e[100m \e[104m $(1) \e[0m\n"; else printf "\n### $(1)\n"; fi endef @@ -8,69 +10,71 @@ endef #~ Composer dependency validate: $(call header,Composer Validation) - @composer validate + @${COMPOSER_BIN} validate install: $(call header,Composer Install) - @composer install + @${COMPOSER_BIN} install update: $(call header,Composer Update) - @composer update + @${COMPOSER_BIN} update + @${COMPOSER_BIN} bump --dev-only composer.lock: install #~ Vendor binaries dependencies -vendor/bin/phpcbf: -vendor/bin/phpcs: -vendor/bin/phpstan: -vendor/bin/phpunit: +vendor/bin/php-cs-fixer: composer.lock +vendor/bin/phpstan: composer.lock +vendor/bin/phpunit: composer.lock #~ Report directories dependencies build/reports/phpunit: @mkdir -p build/reports/phpunit -build/reports/phpcs: - @mkdir -p build/reports/cs - build/reports/phpstan: @mkdir -p build/reports/phpstan #~ main commands -phpcs: vendor/bin/phpcs build/reports/phpcs - $(call header,Checking Code Style) - @./vendor/bin/phpcs --standard=./ci/phpcs/eureka.xml --cache=./build/cs_eureka.cache -p --report-full --report-checkstyle=./build/reports/cs/eureka.xml src/ tests/ +php/deps: composer.json + $(call header,Checking Dependencies) + @XDEBUG_MODE=off ./vendor/bin/composer-dependency-analyser --config ./ci/composer-dependency-analyser.php # for shadow, unused required dependencies and ext-* missing dependencies -phpcbf: vendor/bin/phpcbf +php/check: vendor/bin/php-cs-fixer + $(call header,Checking Code Style) + @./vendor/bin/php-cs-fixer check -v --diff +php/fix: vendor/bin/php-cs-fixer $(call header,Fixing Code Style) - @./vendor/bin/phpcbf --standard=./ci/phpcs/eureka.xml src/ tests/ + @./vendor/bin/php-cs-fixer fix -v -php74compatibility: vendor/bin/phpstan build/reports/phpstan - $(call header,Checking PHP 7.4 compatibility) - @XDEBUG_MODE=off ./vendor/bin/phpstan analyse --configuration=./ci/php74-compatibility.neon --error-format=table +php/min-compatibility: vendor/bin/phpstan build/reports/phpstan + $(call header,Checking PHP ${PHP_VERSION_MIN} compatibility) + @XDEBUG_MODE=off ./vendor/bin/phpstan analyse --configuration=./ci/phpmin-compatibility.neon --error-format=table -php83compatibility: vendor/bin/phpstan build/reports/phpstan - $(call header,Checking PHP 8.3 compatibility) - @XDEBUG_MODE=off ./vendor/bin/phpstan analyse --configuration=./ci/php83-compatibility.neon --error-format=table +php/max-compatibility: vendor/bin/phpstan build/reports/phpstan #ci + $(call header,Checking PHP ${PHP_VERSION_MAX} compatibility) + @XDEBUG_MODE=off ./vendor/bin/phpstan analyse --configuration=./ci/phpmax-compatibility.neon --error-format=table -analyze: vendor/bin/phpstan build/reports/phpstan +php/analyze: vendor/bin/phpstan build/reports/phpstan #manual & ci $(call header,Running Static Analyze - Pretty tty format) @XDEBUG_MODE=off ./vendor/bin/phpstan analyse --error-format=table -phpstan: vendor/bin/phpstan build/reports/phpstan - $(call header,Running Static Analyze) - @XDEBUG_MODE=off ./vendor/bin/phpstan analyse --error-format=checkstyle > ./build/reports/phpstan/phpstan.xml - -tests: vendor/bin/phpunit build/reports/phpunit $(PHP_FILES) +php/tests: vendor/bin/phpunit build/reports/phpunit #ci $(call header,Running Unit Tests) - @XDEBUG_MODE=coverage php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-clover=./build/reports/phpunit/clover.xml --log-junit=./build/reports/phpunit/unit.xml --coverage-php=./build/reports/phpunit/unit.cov --coverage-html=./build/reports/coverage/ --fail-on-warning + @XDEBUG_MODE=coverage php ./vendor/bin/phpunit --testsuite=unit --coverage-clover=./build/reports/phpunit/clover.xml --log-junit=./build/reports/phpunit/unit.xml --coverage-php=./build/reports/phpunit/unit.cov --coverage-html=./build/reports/coverage/ --fail-on-warning + +php/test: php/tests + +php/integration: vendor/bin/phpunit build/reports/phpunit #manual + $(call header,Running Integration Tests) + @XDEBUG_MODE=coverage php ./vendor/bin/phpunit --testsuite=integration --fail-on-warning -testdox: vendor/bin/phpunit $(PHP_FILES) +php/testdox: vendor/bin/phpunit #manual $(call header,Running Unit Tests (Pretty format)) - @XDEBUG_MODE=coverage php -dzend_extension=xdebug.so ./vendor/bin/phpunit --fail-on-warning --testdox + @XDEBUG_MODE=coverage php ./vendor/bin/phpunit --testsuite=unit --fail-on-warning --testdox clean: - $(call header,Cleaning previous build) + $(call header,Cleaning previous build) #manual @if [ "$(shell ls -A ./build)" ]; then rm -rf ./build/*; fi; echo " done" -ci: clean validate install phpcs tests php74compatibility php83compatibility analyze +ci: clean validate install php/deps php/check php/tests php/integration php/min-compatibility php/max-compatibility php/analyze diff --git a/README.md b/README.md index 85fac5c..566b1f2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # component-validation [![Current version](https://img.shields.io/packagist/v/eureka/component-validation.svg?logo=composer)](https://packagist.org/packages/eureka/component-validation) -[![Supported PHP version](https://img.shields.io/static/v1?logo=php&label=PHP&message=7.4%20-%208.3&color=777bb4)](https://packagist.org/packages/eureka/component-validation) +[![Supported PHP version](https://img.shields.io/static/v1?logo=php&label=PHP&message=8.3%20-%208.4&color=777bb4)](https://packagist.org/packages/eureka/component-validation) ![CI](https://github.com/eureka-framework/component-validation/workflows/CI/badge.svg) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=eureka-framework_component-validation&metric=alert_status)](https://sonarcloud.io/dashboard?id=eureka-framework_component-validation) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=eureka-framework_component-validation&metric=coverage)](https://sonarcloud.io/dashboard?id=eureka-framework_component-validation) @@ -94,11 +94,44 @@ make update ``` -## Testing +## Testing & CI (Continuous Integration) -You can test the component with the following commands: +You can run tests on your side with following commands: ```bash -make phpcs -make tests -make testdox +make php/tests # run tests with coverage +make php/test # run tests with coverage +make php/testdox # run tests without coverage reports but with prettified output ``` + +You also can run code style check or code style fixes with following commands: +```bash +make php/check # run checks on check style +make php/fix # run check style auto fix +``` + +To perform a static analyze of your code (with phpstan, lvl 9 at default), you can use the following command: +```bash +make php/analyze # Same as phpstan but with CLI output as table +``` + +To ensure you code still compatible with current supported version and futures versions of php, you need to +run the following commands (both are required for full support): +```bash +make php/min-compatibility # run compatibility check on current minimal version of php we support +make php/max-compatibility # run compatibility check on last version of php we will support in future +``` + +And the last "helper" commands, you can run before commit and push is: +```bash +make ci +``` +This command clean the previous reports, install component if needed and run tests (with coverage report), +check the code style and check the php compatibility check, as it would be done in our CI. + +## Contributing + +See the [CONTRIBUTING](CONTRIBUTING.md) file. + +## License + +This project is currently under The MIT License (MIT). See [LICENCE](LICENSE) file for more information. diff --git a/ci/composer-dependency-analyser.php b/ci/composer-dependency-analyser.php new file mode 100644 index 0000000..8a3ce41 --- /dev/null +++ b/ci/composer-dependency-analyser.php @@ -0,0 +1,11 @@ +addPathToScan(__DIR__ . '/../src', isDev: false) + ->addPathToScan(__DIR__ . '/../tests', isDev: true) +; diff --git a/ci/php74-compatibility.neon b/ci/php74-compatibility.neon deleted file mode 100644 index 21ac1aa..0000000 --- a/ci/php74-compatibility.neon +++ /dev/null @@ -1,9 +0,0 @@ -parameters: - phpVersion: 70400 # PHP 7.4 - level: 0 - paths: - - ./../src - - ./../tests - - bootstrapFiles: - - ./../vendor/autoload.php diff --git a/ci/phpcs/eureka.xml b/ci/phpcs/eureka.xml deleted file mode 100644 index b9f405c..0000000 --- a/ci/phpcs/eureka.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - The coding standard for Eureka. - - - - - diff --git a/ci/phpmax-compatibility.neon b/ci/phpmax-compatibility.neon new file mode 100644 index 0000000..014ae77 --- /dev/null +++ b/ci/phpmax-compatibility.neon @@ -0,0 +1,14 @@ +includes: + - ./../vendor/phpstan/phpstan-phpunit/extension.neon + - ./../vendor/phpstan/phpstan-phpunit/rules.neon + +parameters: + phpVersion: 80400 + level: 0 + + paths: + - ./../src + - ./../tests + + bootstrapFiles: + - ./../vendor/autoload.php diff --git a/ci/php83-compatibility.neon b/ci/phpmin-compatibility.neon similarity index 53% rename from ci/php83-compatibility.neon rename to ci/phpmin-compatibility.neon index 9bd8d3c..bbc35f1 100644 --- a/ci/php83-compatibility.neon +++ b/ci/phpmin-compatibility.neon @@ -1,6 +1,11 @@ +includes: + - ./../vendor/phpstan/phpstan-phpunit/extension.neon + - ./../vendor/phpstan/phpstan-phpunit/rules.neon + parameters: phpVersion: 80300 level: 0 + paths: - ./../src - ./../tests diff --git a/composer.json b/composer.json index 704158e..70618fa 100644 --- a/composer.json +++ b/composer.json @@ -23,22 +23,25 @@ "Eureka\\Component\\Validation\\Tests\\": "./tests" } }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, "require": { - "php": "7.4.*||8.0.*||8.1.*||8.2.*||8.3.*" + "php": "8.3.*||8.4.*", + "ext-filter": "*", + "ext-mbstring": "*" }, "require-dev": { - "dealerdirect/phpcodesniffer-composer-installer": "^1.0", - "phpstan/phpstan": "^1.10", - "phpunit/phpcov": "^8.2", - "phpunit/phpunit": "^9.6", - "squizlabs/php_codesniffer": "^3.7", - "symfony/cache": "^5.4||^6.3" - }, - "config": { - "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true - } + "friendsofphp/php-cs-fixer": "^3.86.0", + "phpstan/phpstan": "^2.1.22", + "phpstan/phpstan-phpunit": "^2.0.7", + "phpstan/phpstan-strict-rules": "^2.0.6", + "phpunit/phpcov": "^11.0.1", + "phpunit/phpunit": "^12.3.4", + "shipmonk/composer-dependency-analyser": "^1.8.3" } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 797d98c..3ac6c49 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,10 @@ +includes: + - ./vendor/phpstan/phpstan-phpunit/extension.neon + - ./vendor/phpstan/phpstan-phpunit/rules.neon + - ./vendor/phpstan/phpstan-strict-rules/rules.neon + parameters: - phpVersion: 70400 # PHP 7.4 - Current minimal version supported + phpVersion: 80300 level: max paths: - ./src @@ -8,5 +13,7 @@ parameters: bootstrapFiles: - ./vendor/autoload.php + treatPhpDocTypesAsCertain: false + ignoreErrors: - '#Call to an undefined method Eureka\\Component\\Validation\\Entity\\GenericEntity::.+\(\).#' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ebc1da8..12fdea8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,34 +1,36 @@ + failOnEmptyTestSuite="false" + failOnIncomplete="true" + failOnRisky="true" + failOnWarning="true" + displayDetailsOnTestsThatTriggerDeprecations="true" + displayDetailsOnPhpunitDeprecations="true" + displayDetailsOnTestsThatTriggerErrors="true" + displayDetailsOnTestsThatTriggerNotices="true" + displayDetailsOnTestsThatTriggerWarnings="true" + cacheDirectory="build/.phpunit.cache"> - + + + + + ./src - + - - ./tests + + ./tests/Unit + + + ./tests/Integration diff --git a/src/Entity/GenericEntity.php b/src/Entity/GenericEntity.php index a715d34..7a671ca 100644 --- a/src/Entity/GenericEntity.php +++ b/src/Entity/GenericEntity.php @@ -49,7 +49,7 @@ public function __construct(ValidatorFactoryInterface $validatorFactory, array $ $this->validatorConfig[self::toPascalCase($name)] = $config; } - if (!empty($data)) { + if ($data !== []) { $this->setFromArray($data); } } @@ -59,7 +59,7 @@ public function __construct(ValidatorFactoryInterface $validatorFactory, array $ */ public function isValid(): bool { - return empty($this->errors); + return $this->errors === []; } /** @@ -102,36 +102,23 @@ public function setFromArray(array $data): self /** * Magic method to have getters & setters for generic entity. * - * @param string $name * @param array $arguments - * @return $this|int|float|bool|string|null * @throws \LogicException */ - public function __call(string $name, array $arguments) + public function __call(string $name, array $arguments): self|int|float|bool|string|null { - $prefixOriginal = substr($name, 0, 3); - $prefixAlternate = substr($name, 0, 2); - - switch (true) { - case $prefixAlternate === 'in': - case $prefixAlternate === 'is': - case $prefixOriginal === 'has': - return $this->get($name); - case $prefixOriginal === 'get': - return $this->get(substr($name, 3)); - case $prefixOriginal === 'set': - return $this->set(substr($name, 3), ...$arguments); - default: - throw new \LogicException('Invalid method name.'); - } + $prefix3Chars = \substr($name, 0, 3); + $prefix2Chars = \substr($name, 0, 2); + + return match (true) { + $prefix3Chars === 'has', $prefix2Chars === 'in', $prefix2Chars === 'is' => $this->get($name), + $prefix3Chars === 'get' => $this->get(\substr($name, 3)), + $prefix3Chars === 'set' => $this->set(\substr($name, 3), ...$arguments), + default => throw new \LogicException('Invalid method name.'), + }; } - /** - * @param string $name - * @param int|float|bool|string|null $value - * @return $this - */ - protected function set(string $name, $value): self + protected function set(string $name, int|float|bool|string|null $value): self { $name = self::toPascalCase($name); @@ -150,11 +137,7 @@ protected function set(string $name, $value): self return $this; } - /** - * @param string $name - * @return int|float|bool|string|null - */ - protected function get(string $name) + protected function get(string $name): int|float|bool|string|null { $name = self::toPascalCase($name); @@ -171,6 +154,6 @@ protected function get(string $name) */ protected static function toPascalCase(string $name): string { - return strtr(ucwords(strtr($name, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']); + return \strtr(\ucwords(\strtr($name, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']); } } diff --git a/src/Exception/ValidationException.php b/src/Exception/ValidationException.php index 76f520f..536659c 100644 --- a/src/Exception/ValidationException.php +++ b/src/Exception/ValidationException.php @@ -16,6 +16,4 @@ * * @author Romain Cottard */ -class ValidationException extends \RuntimeException -{ -} +class ValidationException extends \RuntimeException {} diff --git a/src/Validator/AbstractValidator.php b/src/Validator/AbstractValidator.php index 5cd87f2..2c11f37 100644 --- a/src/Validator/AbstractValidator.php +++ b/src/Validator/AbstractValidator.php @@ -11,19 +11,17 @@ namespace Eureka\Component\Validation\Validator; -use Eureka\Component\Validation\Exception\ValidationException; +use Eureka\Component\Validation\ValidatorInterface; /** - * Class BooleanValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class AbstractValidator { /** - * @param array $options + * @param OptionsType $options * @param int|null $flags - * @return array> + * @return array{options: OptionsType, flags: int|null} */ protected function getOptions(array $options = [], ?int $flags = FILTER_DEFAULT): array { diff --git a/src/Validator/BooleanValidator.php b/src/Validator/BooleanValidator.php index dd672d0..da90e6b 100644 --- a/src/Validator/BooleanValidator.php +++ b/src/Validator/BooleanValidator.php @@ -15,28 +15,22 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class BooleanValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class BooleanValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags - * @return bool|null + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null): ?bool + public function validate(mixed $value, array $options = [], ?int $flags = null): ?bool { if ($flags === null) { - $flags = !array_key_exists('default', $options) ? FILTER_NULL_ON_FAILURE : FILTER_DEFAULT; + $flags = !\array_key_exists('default', $options) ? \FILTER_NULL_ON_FAILURE : \FILTER_DEFAULT; } - /** @var bool|null $filteredValue */ - $filteredValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, $this->getOptions($options, $flags)); + $filteredValue = \filter_var($value, \FILTER_VALIDATE_BOOLEAN, $this->getOptions($options, $flags)); - if (null === $filteredValue) { + if (!\is_bool($filteredValue) && $filteredValue !== null) { throw new ValidationException('Given value is not a valid boolean!'); } diff --git a/src/Validator/DateTimeTrait.php b/src/Validator/DateTimeTrait.php index 9697096..33a2566 100644 --- a/src/Validator/DateTimeTrait.php +++ b/src/Validator/DateTimeTrait.php @@ -12,14 +12,15 @@ namespace Eureka\Component\Validation\Validator; use Eureka\Component\Validation\Exception\ValidationException; +use Eureka\Component\Validation\ValidatorInterface; +/** + * @phpstan-import-type OptionsType from ValidatorInterface + */ trait DateTimeTrait { /** - * @param string $value - * @param array $options - * @param string $defaultFormat - * @return string|null + * @param OptionsType $options */ protected function getDateOrDefault(string $value, array $options, string $defaultFormat): ?string { @@ -36,7 +37,7 @@ protected function getDateOrDefault(string $value, array $options, string $defau if (! $date instanceof \DateTimeImmutable) { if (!array_key_exists('default', $options)) { throw new ValidationException( - 'Given value is not a valid date or time according to following format: "' . $options['format'] . '"!' + 'Given value is not a valid date or time according to following format: "' . $options['format'] . '"!', ); } diff --git a/src/Validator/DateTimeValidator.php b/src/Validator/DateTimeValidator.php index 6f1bd5c..1e6e753 100644 --- a/src/Validator/DateTimeValidator.php +++ b/src/Validator/DateTimeValidator.php @@ -15,22 +15,21 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class DateTimeValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class DateTimeValidator extends AbstractValidator implements ValidatorInterface { use DateTimeTrait; /** - * @param string $value - * @param array $options - * @param int|null $flags Not used here. - * @return string|null Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null): ?string + public function validate(mixed $value, array $options = [], ?int $flags = null): ?string { + if (!\is_string($value)) { + throw new ValidationException('Value must be a string'); + } + return $this->getDateOrDefault($value, $options, 'Y-m-d H:i:s'); } } diff --git a/src/Validator/DateValidator.php b/src/Validator/DateValidator.php index 4b97516..1b4056f 100644 --- a/src/Validator/DateValidator.php +++ b/src/Validator/DateValidator.php @@ -15,22 +15,21 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class DateValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class DateValidator extends AbstractValidator implements ValidatorInterface { use DateTimeTrait; /** - * @param string $value - * @param array $options - * @param int|null $flags Not used here. - * @return string|null Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null): ?string + public function validate(mixed $value, array $options = [], ?int $flags = null): ?string { + if (!\is_string($value)) { + throw new ValidationException('Value must be a string'); + } + return $this->getDateOrDefault($value, $options, 'Y-m-d'); } } diff --git a/src/Validator/DomainValidator.php b/src/Validator/DomainValidator.php index 25eedbc..96da417 100644 --- a/src/Validator/DomainValidator.php +++ b/src/Validator/DomainValidator.php @@ -15,26 +15,25 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class DomainValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class DomainValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = FILTER_DEFAULT) + public function validate(mixed $value, array $options = [], ?int $flags = FILTER_DEFAULT): ?string { - $filteredValue = filter_var($value, FILTER_VALIDATE_DOMAIN, $this->getOptions($options, $flags)); + $filteredValue = \filter_var($value, FILTER_VALIDATE_DOMAIN, $this->getOptions($options, $flags)); - if (false === $filteredValue) { + if ($filteredValue === false) { throw new ValidationException('Given value is not a valid domain!'); } + if ($filteredValue !== null && !\is_string($filteredValue)) { + throw new ValidationException('Optional value must be a string or null!'); + } + return $filteredValue; } } diff --git a/src/Validator/EmailValidator.php b/src/Validator/EmailValidator.php index b977736..14d5f9e 100644 --- a/src/Validator/EmailValidator.php +++ b/src/Validator/EmailValidator.php @@ -15,26 +15,25 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class EmailValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class EmailValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = FILTER_DEFAULT) + public function validate(mixed $value, array $options = [], ?int $flags = FILTER_DEFAULT): ?string { - $filteredValue = filter_var($value, FILTER_VALIDATE_EMAIL, $this->getOptions($options, $flags)); + $filteredValue = \filter_var($value, FILTER_VALIDATE_EMAIL, $this->getOptions($options, $flags)); - if (false === $filteredValue) { + if ($filteredValue === false) { throw new ValidationException('Given value is not a valid email!'); } + if ($filteredValue !== null && !\is_string($filteredValue)) { + throw new ValidationException('Optional value must be a string or null!'); + } + return $filteredValue; } } diff --git a/src/Validator/FloatValidator.php b/src/Validator/FloatValidator.php index b626f15..4f99480 100644 --- a/src/Validator/FloatValidator.php +++ b/src/Validator/FloatValidator.php @@ -15,26 +15,25 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class FloatValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class FloatValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null) + public function validate(mixed $value, array $options = [], ?int $flags = null): ?float { - $filteredValue = filter_var($value, FILTER_VALIDATE_FLOAT, $this->getOptions($options, $flags)); + $filteredValue = \filter_var($value, FILTER_VALIDATE_FLOAT, $this->getOptions($options, $flags)); - if (false === $filteredValue) { + if ($filteredValue === false) { throw new ValidationException('Given value is not a valid float!'); } + if ($filteredValue !== null && !\is_float($filteredValue)) { + throw new ValidationException('Optional value must be a float or null!'); + } + return $filteredValue; } } diff --git a/src/Validator/IntegerValidator.php b/src/Validator/IntegerValidator.php index 8987a5c..5b4244d 100644 --- a/src/Validator/IntegerValidator.php +++ b/src/Validator/IntegerValidator.php @@ -15,37 +15,36 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class IntegerValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class IntegerValidator extends AbstractValidator implements ValidatorInterface { - const TINYINT_SIGNED = ['min_range' => -128, 'max_range' => 127]; - const TINYINT_UNSIGNED = ['min_range' => 0, 'max_range' => 255]; - const SMALLINT_SIGNED = ['min_range' => -32768, 'max_range' => 32767]; - const SMALLINT_UNSIGNED = ['min_range' => 0, 'max_range' => 65535]; - const MEDIUMINT_SIGNED = ['min_range' => -8388608, 'max_range' => 8388607]; - const MEDIUMINT_UNSIGNED = ['min_range' => 0, 'max_range' => 16777215]; - const INT_SIGNED = ['min_range' => -2147483648, 'max_range' => 2147483647]; - const INT_UNSIGNED = ['min_range' => 0, 'max_range' => 4294967295]; - const BIGINT_SIGNED = ['min_range' => -9223372036854775808, 'max_range' => 9223372036854775807]; - const BIGINT_UNSIGNED = ['min_range' => 0, 'max_range' => 18446744073709551615]; + public const array TINYINT_SIGNED = ['min_range' => -128, 'max_range' => 127]; + public const array TINYINT_UNSIGNED = ['min_range' => 0, 'max_range' => 255]; + public const array SMALLINT_SIGNED = ['min_range' => -32_768, 'max_range' => 32_767]; + public const array SMALLINT_UNSIGNED = ['min_range' => 0, 'max_range' => 65_535]; + public const array MEDIUMINT_SIGNED = ['min_range' => -8_388_608, 'max_range' => 8_388_607]; + public const array MEDIUMINT_UNSIGNED = ['min_range' => 0, 'max_range' => 16_777_215]; + public const array INT_SIGNED = ['min_range' => -2_147_483_648, 'max_range' => 2_147_483_647]; + public const array INT_UNSIGNED = ['min_range' => 0, 'max_range' => 4_294_967_295]; + public const array BIGINT_SIGNED = ['min_range' => -9_223_372_036_854_775_808, 'max_range' => 9_223_372_036_854_775_807]; + public const array BIGINT_UNSIGNED = ['min_range' => 0, 'max_range' => PHP_INT_MAX]; // PHP_INT_MAX is platform dependent, but usually it is 9_223_372_036_854_775_807 on 64-bit systems /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null) + public function validate(mixed $value, array $options = [], ?int $flags = null): ?int { - $filteredValue = filter_var($value, FILTER_VALIDATE_INT, $this->getOptions($options, $flags)); + $filteredValue = \filter_var($value, FILTER_VALIDATE_INT, $this->getOptions($options, $flags)); - if (false === $filteredValue) { + if ($filteredValue === false) { throw new ValidationException('Given value is not a valid integer!'); } + if ($filteredValue !== null && !\is_int($filteredValue)) { + throw new ValidationException('Optional value must be a float or null!'); + } + return $filteredValue; } } diff --git a/src/Validator/IpValidator.php b/src/Validator/IpValidator.php index 4ffdf24..0771b6d 100644 --- a/src/Validator/IpValidator.php +++ b/src/Validator/IpValidator.php @@ -15,28 +15,27 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class IpValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class IpValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null) + public function validate(mixed $value, array $options = [], ?int $flags = null): ?string { $flags = ($flags === null ? FILTER_FLAG_IPV4 : $flags); - $filteredValue = filter_var($value, FILTER_VALIDATE_IP, $this->getOptions($options, $flags)); + $filteredValue = \filter_var($value, FILTER_VALIDATE_IP, $this->getOptions($options, $flags)); - if (false === $filteredValue) { + if ($filteredValue === false) { throw new ValidationException('Given value is not a valid IP!'); } + if ($filteredValue !== null && !\is_string($filteredValue)) { + throw new ValidationException('Optional value must be a string or null!'); + } + return $filteredValue; } } diff --git a/src/Validator/NullValidator.php b/src/Validator/NullValidator.php index 1c7c4d7..06647f4 100644 --- a/src/Validator/NullValidator.php +++ b/src/Validator/NullValidator.php @@ -15,19 +15,14 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class NullValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class NullValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null) + public function validate(mixed $value, array $options = [], ?int $flags = null): null { if ($value !== null) { throw new ValidationException('Given value is not null!'); diff --git a/src/Validator/RegexpValidator.php b/src/Validator/RegexpValidator.php index 7566b25..8542047 100644 --- a/src/Validator/RegexpValidator.php +++ b/src/Validator/RegexpValidator.php @@ -15,26 +15,25 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class RegexpValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class RegexpValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null) + public function validate(mixed $value, array $options = [], ?int $flags = null): ?string { - $filteredValue = filter_var($value, FILTER_VALIDATE_REGEXP, $this->getOptions($options, FILTER_DEFAULT)); + $filteredValue = \filter_var($value, FILTER_VALIDATE_REGEXP, $this->getOptions($options)); - if (false === $filteredValue) { + if ($filteredValue === false) { throw new ValidationException('Given value is not a valid value according to the given regexp!'); } + if ($filteredValue !== null && !\is_string($filteredValue)) { + throw new ValidationException('Optional value must be a string or null!'); + } + return $filteredValue; } } diff --git a/src/Validator/StringValidator.php b/src/Validator/StringValidator.php index ff3fa07..1473e02 100644 --- a/src/Validator/StringValidator.php +++ b/src/Validator/StringValidator.php @@ -15,25 +15,21 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class StringValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class StringValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null) + public function validate(mixed $value, array $options = [], ?int $flags = null): ?string { - $default = array_key_exists('default', $options) ? $options['default'] : false; + $default = \array_key_exists('default', $options) ? $options['default'] : false; + $hasDefault = $default === null || \is_string($default); //~ Validate type - if (false === is_string($value)) { - if ($default === false) { + if (!\is_string($value)) { + if (!$hasDefault) { throw new ValidationException('Given value is not a string!'); } @@ -41,21 +37,13 @@ public function validate($value, array $options = [], ?int $flags = null) } //~ Validate min length - if (isset($options['min_length']) && mb_strlen($value) < (int) $options['min_length']) { - if ($default === false) { - throw new ValidationException('String must have at least ' . $options['min_length'] . ' characters!'); - } - - return $default; + if (isset($options['min_length']) && \mb_strlen($value) < (int) $options['min_length']) { + throw new ValidationException('String must have at least ' . $options['min_length'] . ' characters!'); } //~ Validate max length if (isset($options['max_length']) && mb_strlen($value) > (int) $options['max_length']) { - if ($default === false) { - throw new ValidationException('String must have maximum ' . $options['max_length'] . ' characters!'); - } - - return $default; + throw new ValidationException('String must have maximum ' . $options['max_length'] . ' characters!'); } return $value; diff --git a/src/Validator/TimeValidator.php b/src/Validator/TimeValidator.php index 40578c0..42f7106 100644 --- a/src/Validator/TimeValidator.php +++ b/src/Validator/TimeValidator.php @@ -15,22 +15,21 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class TimeValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class TimeValidator extends AbstractValidator implements ValidatorInterface { use DateTimeTrait; /** - * @param string $value - * @param array $options - * @param int|null $flags Not used here. - * @return string|null Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null): ?string + public function validate(mixed $value, array $options = [], ?int $flags = null): ?string { + if (!\is_string($value)) { + throw new ValidationException('Value must be a string'); + } + return $this->getDateOrDefault($value, $options, 'H:i:s'); } } diff --git a/src/Validator/TimestampValidator.php b/src/Validator/TimestampValidator.php index 3953ddd..d645610 100644 --- a/src/Validator/TimestampValidator.php +++ b/src/Validator/TimestampValidator.php @@ -15,29 +15,28 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class TimestampValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class TimestampValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null) + public function validate(mixed $value, array $options = [], ?int $flags = null): ?int { $options['min_range'] = 0; $options['max_range'] = 2147483647; - $filteredValue = filter_var($value, FILTER_VALIDATE_INT, $this->getOptions($options, $flags)); + $filteredValue = \filter_var($value, \FILTER_VALIDATE_INT, $this->getOptions($options, $flags)); - if (false === $filteredValue) { + if ($filteredValue === false) { throw new ValidationException('Given value is not a valid integer!'); } + if ($filteredValue !== null && !\is_int($filteredValue)) { + throw new ValidationException('Optional value must be a string or null!'); + } + return $filteredValue; } } diff --git a/src/Validator/UrlValidator.php b/src/Validator/UrlValidator.php index d3c924d..4605006 100644 --- a/src/Validator/UrlValidator.php +++ b/src/Validator/UrlValidator.php @@ -15,26 +15,25 @@ use Eureka\Component\Validation\ValidatorInterface; /** - * Class UrlValidator - * - * @author Romain Cottard + * @phpstan-import-type OptionsType from ValidatorInterface */ class UrlValidator extends AbstractValidator implements ValidatorInterface { /** - * @param mixed $value - * @param array $options - * @param int|null $flags Not used here. - * @return mixed Return value + * @param OptionsType $options */ - public function validate($value, array $options = [], ?int $flags = null) + public function validate(mixed $value, array $options = [], ?int $flags = null): ?string { - $filteredValue = filter_var($value, FILTER_VALIDATE_URL, $this->getOptions($options, FILTER_DEFAULT)); + $filteredValue = \filter_var($value, FILTER_VALIDATE_URL, $this->getOptions($options)); - if (false === $filteredValue) { + if ($filteredValue === false) { throw new ValidationException('Given value is not a valid url!'); } + if ($filteredValue !== null && !\is_string($filteredValue)) { + throw new ValidationException('Optional value must be a string or null!'); + } + return $filteredValue; } } diff --git a/src/ValidatorEntityFactoryInterface.php b/src/ValidatorEntityFactoryInterface.php index 63ddf19..0a99c33 100644 --- a/src/ValidatorEntityFactoryInterface.php +++ b/src/ValidatorEntityFactoryInterface.php @@ -11,16 +11,11 @@ namespace Eureka\Component\Validation; -/** - * Class ValidatorEntityFactoryInterface - * - * @author Romain Cottard - */ interface ValidatorEntityFactoryInterface { /** - * @param array $config - * @param array $data + * @param array|int|float|bool|string|null>> $config + * @param array $data * @return Entity\GenericEntity */ public function createGeneric(array $config, array $data = []): Entity\GenericEntity; diff --git a/src/ValidatorFactory.php b/src/ValidatorFactory.php index 5a135e1..bbf154f 100644 --- a/src/ValidatorFactory.php +++ b/src/ValidatorFactory.php @@ -11,60 +11,30 @@ namespace Eureka\Component\Validation; -use Eureka\Component\Validation\Validator; - -/** - * Validator factory - * - * @author Romain Cottard - */ class ValidatorFactory implements ValidatorFactoryInterface { /** @var ValidatorInterface[] $validators */ protected static array $validators = []; - /** - * @param string $type - * @return ValidatorInterface - * @throws \LogicException - */ public function getValidator(string $type): ValidatorInterface { - switch ($type) { - case 'boolean': - return $this->getBooleanValidator(); - case 'datetime': - return $this->getDateTimeValidator(); - case 'date': - return $this->getDateValidator(); - case 'time': - return $this->getTimeValidator(); - case 'timestamp': - return $this->getTimestampValidator(); - case 'email': - return $this->getEmailValidator(); - case 'float': - case 'double': - case 'decimal': - return $this->getFloatValidator(); - case 'integer': - return $this->getIntegerValidator(); - case 'null': - case '~': - case '': - return $this->getNullValidator(); - case 'ip': - return $this->getIpValidator(); - case 'regexp': - return $this->getRegexpValidator(); - case 'url': - return $this->getUrlValidator(); - case 'string': - return $this->getStringValidator(); - default: - throw new \LogicException('Invalid validator type (type: ' . $type . ')'); - } + return match ($type) { + 'boolean' => $this->getBooleanValidator(), + 'datetime' => $this->getDateTimeValidator(), + 'date' => $this->getDateValidator(), + 'time' => $this->getTimeValidator(), + 'timestamp' => $this->getTimestampValidator(), + 'email' => $this->getEmailValidator(), + 'integer' => $this->getIntegerValidator(), + 'null', '~', '' => $this->getNullValidator(), + 'ip' => $this->getIpValidator(), + 'regexp' => $this->getRegexpValidator(), + 'url' => $this->getUrlValidator(), + 'string' => $this->getStringValidator(), + 'float', 'double', 'decimal' => $this->getFloatValidator(), + default => throw new \LogicException('Invalid validator type (type: ' . $type . ')'), + }; } public function getBooleanValidator(): ValidatorInterface diff --git a/src/ValidatorFactoryInterface.php b/src/ValidatorFactoryInterface.php index f1e4595..51ada1d 100644 --- a/src/ValidatorFactoryInterface.php +++ b/src/ValidatorFactoryInterface.php @@ -11,16 +11,7 @@ namespace Eureka\Component\Validation; -/** - * Interface Validator Factory for Validator Factory classes - * - * @author Romain Cottard - */ interface ValidatorFactoryInterface { - /** - * @param string $type - * @return ValidatorInterface - */ public function getValidator(string $type): ValidatorInterface; } diff --git a/src/ValidatorInterface.php b/src/ValidatorInterface.php index dfa491b..7566d49 100644 --- a/src/ValidatorInterface.php +++ b/src/ValidatorInterface.php @@ -12,18 +12,15 @@ namespace Eureka\Component\Validation; /** - * Interface Validator for Validator classes - * - * @author Romain Cottard + * @phpstan-type OptionsType array */ interface ValidatorInterface { /** * @param mixed $value Value to validate - * @param array $options + * @param OptionsType $options * @param int|null $flags Validation flag. If null, use default flag or validator default flag. - * @return mixed Return value * @throws \RuntimeException */ - public function validate($value, array $options = [], int $flags = null); + public function validate(mixed $value, array $options = [], ?int $flags = null): string|float|int|bool|null; } diff --git a/tests/Integration/.gitkeep b/tests/Integration/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Entity/GenericEntityTest.php b/tests/Unit/Entity/GenericEntityTest.php similarity index 82% rename from tests/Entity/GenericEntityTest.php rename to tests/Unit/Entity/GenericEntityTest.php index c568db4..6db9e65 100644 --- a/tests/Entity/GenericEntityTest.php +++ b/tests/Unit/Entity/GenericEntityTest.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Entity; +namespace Eureka\Component\Validation\Tests\Unit\Entity; use Eureka\Component\Validation\Entity\GenericEntity; use Eureka\Component\Validation\Entity\ValidatorEntityFactory; @@ -43,13 +43,13 @@ public function testICanInstantiateAValidEntity(): void $entity = new GenericEntity(new ValidatorFactory(), $config, $data); - $this->assertTrue($entity->isValid()); - $this->assertTrue($entity->isEnabled()); - $this->assertSame(1, $entity->getUserId()); + self::assertTrue($entity->isValid()); + self::assertTrue($entity->isEnabled()); + self::assertSame(1, $entity->getUserId()); $entity->setUserId(2); - $this->assertSame(2, $entity->getUserId()); - $this->assertNull($entity->getAny()); + self::assertSame(2, $entity->getUserId()); + self::assertNull($entity->getAny()); } public function testIHaveAnExceptionWhenITryToGetValueWithAnInvalidMethodName(): void @@ -79,7 +79,7 @@ public function testICanInstantiateAnInvalidEntity(): void $entity = new GenericEntity(new ValidatorFactory(), $config, $data); - $this->assertFalse($entity->isValid()); - $this->assertCount(1, $entity->getErrors()); + self::assertFalse($entity->isValid()); + self::assertCount(1, $entity->getErrors()); } } diff --git a/tests/FactoryValidatorTest.php b/tests/Unit/FactoryValidatorTest.php similarity index 91% rename from tests/FactoryValidatorTest.php rename to tests/Unit/FactoryValidatorTest.php index aaead3e..f43678b 100644 --- a/tests/FactoryValidatorTest.php +++ b/tests/Unit/FactoryValidatorTest.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests; +namespace Eureka\Component\Validation\Tests\Unit; use Eureka\Component\Validation\Validator\BooleanValidator; use Eureka\Component\Validation\Validator\DateTimeValidator; @@ -26,6 +26,7 @@ use Eureka\Component\Validation\Validator\UrlValidator; use Eureka\Component\Validation\ValidatorFactory; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -50,13 +51,12 @@ public function setUp(): void * @param string $type * @param class-string $expectedClass * @return void - * - * @dataProvider dataProviderFactory */ + #[DataProvider('dataProviderFactory')] public function testICanGetValidatorWithFactory(string $type, string $expectedClass): void { $validator = $this->factory->getValidator($type); - $this->assertInstanceOf($expectedClass, $validator); + self::assertInstanceOf($expectedClass, $validator); } /** @@ -71,7 +71,7 @@ public function testIHaveAnExceptionWhenITryToGetValidatorWithInvalidType(): voi /** * @return string[][] */ - public function dataProviderFactory(): array + public static function dataProviderFactory(): array { return [ ['boolean', BooleanValidator::class], diff --git a/tests/Validator/BooleanValidatorTest.php b/tests/Unit/Validator/BooleanValidatorTest.php similarity index 57% rename from tests/Validator/BooleanValidatorTest.php rename to tests/Unit/Validator/BooleanValidatorTest.php index 0dbec77..2a98c5c 100644 --- a/tests/Validator/BooleanValidatorTest.php +++ b/tests/Unit/Validator/BooleanValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\BooleanValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,68 +32,58 @@ public function getValidator(): ValidatorInterface return new BooleanValidator(); } - /** - * @param mixed $value - * @param bool $excepted - * @return void - * @dataProvider validTrueValuesProvider - */ - public function testWithValidTrueValues($value, bool $excepted): void + #[DataProvider('validTrueValuesProvider')] + public function testWithValidTrueValues(mixed $value, bool $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param bool $excepted - * @return void - * @dataProvider validFalseValuesProvider - */ - public function testWithValidFalseValues($value, bool $excepted): void + #[DataProvider('validFalseValuesProvider')] + public function testWithValidFalseValues(mixed $value, bool $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param bool $excepted - * @return void - * @dataProvider invalidBooleanValuesProvider - */ - public function testWithInvalidBooleanValues($value, bool $excepted): void + #[DataProvider('invalidBooleanValuesProvider')] + public function testWithInvalidBooleanValuesAnd(mixed $value, bool $excepted): void + { + self::assertNull($this->getValidator()->validate($value)); + } + + #[DataProvider('invalidBooleanValuesProvider')] + public function testWithInvalidBooleanValuesAndValidDefaultValue(mixed $value, bool $excepted): void + { + self::assertSame($excepted, $this->getValidator()->validate($value, ['default' => $excepted])); + } + + public function testWithInvalidBooleanValuesAndInvalidDefaultValue(): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + $this->getValidator()->validate('n', ['default' => 'false']); } /** - * @param mixed $value * @param array $options - * @param bool $excepted - * @return void - * @dataProvider invalidBooleanValuesWithOptionsProvider */ - public function testWithInvalidBooleanWithDefaultValues($value, array $options, bool $excepted): void + #[DataProvider('invalidBooleanValuesWithOptionsProvider')] + public function testWithInvalidBooleanWithDefaultValues(mixed $value, array $options, bool $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param bool $excepted - * @return void - * @dataProvider invalidBooleanValuesWithOptionsProvider */ - public function testWithInvalidBooleanWithDefaultForceNullValues($value, array $options, bool $excepted): void + #[DataProvider('invalidBooleanValuesWithOptionsProvider')] + public function testWithInvalidBooleanWithDefaultForceNullValues(mixed $value, array $options, bool $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options, FILTER_NULL_ON_FAILURE)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options, FILTER_NULL_ON_FAILURE)); } /** * @return array> */ - public function validTrueValuesProvider(): array + public static function validTrueValuesProvider(): array { return [ ['yes', true], @@ -109,7 +100,7 @@ public function validTrueValuesProvider(): array /** * @return array> */ - public function validFalseValuesProvider(): array + public static function validFalseValuesProvider(): array { return [ ['no', false], @@ -129,7 +120,7 @@ public function validFalseValuesProvider(): array /** * @return array> */ - public function invalidBooleanValuesProvider(): array + public static function invalidBooleanValuesProvider(): array { return [ ['non', false], @@ -145,7 +136,7 @@ public function invalidBooleanValuesProvider(): array /** * @return array>> */ - public function invalidBooleanValuesWithOptionsProvider(): array + public static function invalidBooleanValuesWithOptionsProvider(): array { return [ ['non', ['default' => false], false], diff --git a/tests/Validator/DateTimeValidatorTest.php b/tests/Unit/Validator/DateTimeValidatorTest.php similarity index 64% rename from tests/Validator/DateTimeValidatorTest.php rename to tests/Unit/Validator/DateTimeValidatorTest.php index 120aebc..c75989d 100644 --- a/tests/Validator/DateTimeValidatorTest.php +++ b/tests/Unit/Validator/DateTimeValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\DateTimeValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -35,23 +36,23 @@ public function getValidator(): ValidatorInterface * @param mixed $value * @param mixed $excepted * @return void - * @dataProvider validValuesProvider */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** * @param mixed $value * @param mixed $excepted * @return void - * @dataProvider invalidValuesProvider */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** @@ -59,11 +60,11 @@ public function testWithInvalidValues($value, $excepted): void * @param array $options * @param mixed $excepted * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** @@ -71,17 +72,17 @@ public function testWithValidValueAndDefaultValues($value, array $options, $exce * @param array $options * @param mixed $excepted * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ ['2018-01-01 00:00:00', '2018-01-01 00:00:00'], @@ -96,7 +97,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ ['-2018-01-01 00:00:00', false], @@ -108,7 +109,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { $default = '2001-01-01'; $options = ['format' => 'Y-m-d H:i:s', 'format_output' => 'Y-m-d H:i:s', 'default' => $default]; @@ -118,16 +119,16 @@ public function validValuesWithOptionsProvider(): array ['2099-12-31 23:59:59', $options, '2099-12-31 23:59:59'], ['1901-07-01 12:00:00', $options, '1901-07-01 12:00:00'], ['2020-02-29 00:00:00', $options, '2020-02-29 00:00:00'], - ['12-31-2099 00:00:00', array_merge($options, ['format' => 'm-d-Y H:i:s', 'format_output' => 'd-m-Y H:i:s']), '31-12-2099 00:00:00'], - ['99-12-31 00:00:00', array_merge($options, ['format' => 'y-m-d H:i:s']), '1999-12-31 00:00:00'], - ['01-07-1901 13:13', array_merge($options, ['format' => 'd-m-Y H:i']), '1901-07-01 13:13:00'], + ['12-31-2099 00:00:00', \array_merge($options, ['format' => 'm-d-Y H:i:s', 'format_output' => 'd-m-Y H:i:s']), '31-12-2099 00:00:00'], + ['99-12-31 00:00:00', \array_merge($options, ['format' => 'y-m-d H:i:s']), '1999-12-31 00:00:00'], + ['01-07-1901 13:13', \array_merge($options, ['format' => 'd-m-Y H:i']), '1901-07-01 13:13:00'], ]; } /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { $default = '2001-01-01 12:13:14'; $options = ['format' => 'Y-m-d H:i:s', 'format_output' => 'Y-m-d H:i:s', 'default' => $default]; diff --git a/tests/Validator/DateValidatorTest.php b/tests/Unit/Validator/DateValidatorTest.php similarity index 56% rename from tests/Validator/DateValidatorTest.php rename to tests/Unit/Validator/DateValidatorTest.php index 10bff2c..91b76c2 100644 --- a/tests/Validator/DateValidatorTest.php +++ b/tests/Unit/Validator/DateValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\DateValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -23,65 +24,46 @@ */ class DateValidatorTest extends TestCase { - /** - * @return ValidatorInterface - */ public function getValidator(): ValidatorInterface { return new DateValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ ['2018-01-01', '2018-01-01'], @@ -96,7 +78,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ ['-2018-01-01', false], @@ -108,7 +90,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { $default = '2001-01-01'; $options = ['format' => 'Y-m-d', 'format_output' => 'Y-m-d', 'default' => $default]; @@ -118,16 +100,16 @@ public function validValuesWithOptionsProvider(): array ['2099-12-31', $options, '2099-12-31'], ['1901-07-01', $options, '1901-07-01'], ['2020-02-29', $options, '2020-02-29'], - ['12-31-2099', array_merge($options, ['format' => 'm-d-Y', 'format_output' => 'd-m-Y']), '31-12-2099'], - ['99-12-31', array_merge($options, ['format' => 'y-m-d']), '1999-12-31'], - ['01-07-1901', array_merge($options, ['format' => 'd-m-Y']), '1901-07-01'], + ['12-31-2099', \array_merge($options, ['format' => 'm-d-Y', 'format_output' => 'd-m-Y']), '31-12-2099'], + ['99-12-31', \array_merge($options, ['format' => 'y-m-d']), '1999-12-31'], + ['01-07-1901', \array_merge($options, ['format' => 'd-m-Y']), '1901-07-01'], ]; } /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { $default = '2001-01-01'; $options = ['format' => 'Y-m-d', 'format_output' => 'Y-m-d', 'default' => $default]; diff --git a/tests/Validator/DomainValidatorTest.php b/tests/Unit/Validator/DomainValidatorTest.php similarity index 77% rename from tests/Validator/DomainValidatorTest.php rename to tests/Unit/Validator/DomainValidatorTest.php index 1ce38cb..6db3fa8 100644 --- a/tests/Validator/DomainValidatorTest.php +++ b/tests/Unit/Validator/DomainValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\DomainValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,57 +32,41 @@ public function getValidator(): ValidatorInterface return new DomainValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ //~ Normal & complex cases @@ -109,7 +94,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ ['http://www.math..uio.no.example.net/faq/compression-faq/part1.html', false], @@ -119,7 +104,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { return [ //~ Normal & complex cases @@ -145,7 +130,7 @@ public function validValuesWithOptionsProvider(): array /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { return [ ['http://www.math..uio.no.example.net/faq/compression-faq/part1.html', ['default' => null], null], diff --git a/tests/Validator/EmailValidatorTest.php b/tests/Unit/Validator/EmailValidatorTest.php similarity index 68% rename from tests/Validator/EmailValidatorTest.php rename to tests/Unit/Validator/EmailValidatorTest.php index b14d0f5..6725d86 100644 --- a/tests/Validator/EmailValidatorTest.php +++ b/tests/Unit/Validator/EmailValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\EmailValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,57 +32,41 @@ public function getValidator(): ValidatorInterface return new EmailValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ ['test@localhost.com', 'test@localhost.com'], @@ -97,7 +82,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ ['Test Example ', false], @@ -111,7 +96,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { $default = 'test@example.com'; $options = ['default' => $default]; @@ -130,7 +115,7 @@ public function validValuesWithOptionsProvider(): array /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { $default = null; $options = ['default' => $default]; diff --git a/tests/Validator/FloatValidatorTest.php b/tests/Unit/Validator/FloatValidatorTest.php similarity index 57% rename from tests/Validator/FloatValidatorTest.php rename to tests/Unit/Validator/FloatValidatorTest.php index 626d4cb..3c9466a 100644 --- a/tests/Validator/FloatValidatorTest.php +++ b/tests/Unit/Validator/FloatValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\FloatValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,51 +32,35 @@ public function getValidator(): ValidatorInterface return new FloatValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** @@ -83,13 +68,13 @@ public function testWithInvalidValueAndDefaultValues($value, array $options, $ex */ public function testWithValidValuesWithThousandSeparator(): void { - $this->assertSame(1000.01, $this->getValidator()->validate('1,000.01', [], FILTER_FLAG_ALLOW_THOUSAND)); + self::assertSame(1000.01, $this->getValidator()->validate('1,000.01', [], FILTER_FLAG_ALLOW_THOUSAND)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ [0.0, 0.0], @@ -103,7 +88,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ ['0.0.0', false], @@ -117,7 +102,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { $default = -1.0; $options = ['default' => $default]; @@ -126,16 +111,16 @@ public function validValuesWithOptionsProvider(): array [0.0, $options, 0.0], [1.20, $options, 1.20], [0, $options, 0.0], - ['0,9999999999', array_merge($options, ['decimal' => ',']), 0.9999999999], + ['0,9999999999', \array_merge($options, ['decimal' => ',']), 0.9999999999], ['0.0', $options, 0.0], - ['0,9911111111', array_merge($options, ['decimal' => ',']), 0.9911111111], + ['0,9911111111', \array_merge($options, ['decimal' => ',']), 0.9911111111], ]; } /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { $default = 0.0; $options = ['default' => $default]; diff --git a/tests/Validator/IntegerValidatorTest.php b/tests/Unit/Validator/IntegerValidatorTest.php similarity index 53% rename from tests/Validator/IntegerValidatorTest.php rename to tests/Unit/Validator/IntegerValidatorTest.php index aee60e8..d3e38bc 100644 --- a/tests/Validator/IntegerValidatorTest.php +++ b/tests/Unit/Validator/IntegerValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\IntegerValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,73 +32,51 @@ public function getValidator(): ValidatorInterface return new IntegerValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } - /** - * @return void - */ public function testWithOctalValueAsValidValues(): void { - $this->assertSame(octdec('777'), $this->getValidator()->validate(0777, [], FILTER_FLAG_ALLOW_OCTAL)); + self::assertSame(octdec('777'), $this->getValidator()->validate(0777, [], FILTER_FLAG_ALLOW_OCTAL)); } - /** - * @return void - */ public function testWithHexadecimalValueAsValidValues(): void { - $this->assertSame(hexdec('fa11'), $this->getValidator()->validate('0xfa11', [], FILTER_FLAG_ALLOW_HEX)); + self::assertSame(hexdec('fa11'), $this->getValidator()->validate('0xfa11', [], FILTER_FLAG_ALLOW_HEX)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ [0.0, 0], @@ -110,7 +89,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ [1.20, 1], @@ -128,7 +107,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { $default = -1; $options = ['default' => $default]; @@ -137,16 +116,16 @@ public function validValuesWithOptionsProvider(): array [0.0, ['default' => $default], 0], [15, ['default' => $default, 'min_range' => 0, 'max_range' => 100], 15], ['42', ['default' => $default, 'min_range' => 42, 'max_range' => 42], 42], - [-128, array_merge($options, IntegerValidator::TINYINT_SIGNED), -128], - [127, array_merge($options, IntegerValidator::TINYINT_SIGNED), 127], - [128, array_merge($options, IntegerValidator::TINYINT_UNSIGNED), 128], + [-128, \array_merge($options, IntegerValidator::TINYINT_SIGNED), -128], + [127, \array_merge($options, IntegerValidator::TINYINT_SIGNED), 127], + [128, \array_merge($options, IntegerValidator::TINYINT_UNSIGNED), 128], ]; } /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { $default = -1; $options = ['default' => $default]; @@ -156,9 +135,9 @@ public function invalidValuesWithOptionsProvider(): array [null, $options, $default], [1.20, ['default' => $default], $default], [1.20, ['default' => $default, 'min_range' => 1, 'max_range' => 2], $default], - [1.01, array_merge($options, ['min_range' => 0, 'max_range' => 1]), $default], - [254, array_merge($options, IntegerValidator::TINYINT_SIGNED), $default], - [256, array_merge($options, IntegerValidator::TINYINT_UNSIGNED), $default], + [1.01, \array_merge($options, ['min_range' => 0, 'max_range' => 1]), $default], + [254, \array_merge($options, IntegerValidator::TINYINT_SIGNED), $default], + [256, \array_merge($options, IntegerValidator::TINYINT_UNSIGNED), $default], ]; } } diff --git a/tests/Validator/IpValidatorTest.php b/tests/Unit/Validator/IpValidatorTest.php similarity index 50% rename from tests/Validator/IpValidatorTest.php rename to tests/Unit/Validator/IpValidatorTest.php index 69e3440..0568baa 100644 --- a/tests/Validator/IpValidatorTest.php +++ b/tests/Unit/Validator/IpValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\IpValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,101 +32,70 @@ public function getValidator(): ValidatorInterface return new IpValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } - /** - * @return void - */ public function testIpNotInReservedNorPrivateRangesWithPublicIp(): void { - $this->assertSame('193.1.2.3', $this->getValidator()->validate('193.1.2.3', [], FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE)); + self::assertSame('193.1.2.3', $this->getValidator()->validate('193.1.2.3', [], FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE)); } - /** - * @return void - */ public function testIpNotInReservedRangeWithReservedIp(): void { $this->expectException(ValidationException::class); - $this->assertSame('240.0.0.1', $this->getValidator()->validate('240.0.0.1', [], FILTER_FLAG_NO_RES_RANGE)); + self::assertSame('240.0.0.1', $this->getValidator()->validate('240.0.0.1', [], FILTER_FLAG_NO_RES_RANGE)); } - /** - * @return void - */ public function testIpNotInPrivateRangeWithPrivateIp(): void { $this->expectException(ValidationException::class); - $this->assertSame('172.16.0.1', $this->getValidator()->validate('172.16.0.1', [], FILTER_FLAG_NO_PRIV_RANGE)); + self::assertSame('172.16.0.1', $this->getValidator()->validate('172.16.0.1', [], FILTER_FLAG_NO_PRIV_RANGE)); } - /** - * @return void - */ public function testIpV6WithValidValue(): void { - $this->assertSame('2001:0db8:0000:85a3:0000:0000:ac1f:8001', $this->getValidator()->validate('2001:0db8:0000:85a3:0000:0000:ac1f:8001', [], FILTER_FLAG_IPV6)); - $this->assertSame('2001:db8:0:85a3:0:0:ac1f:8001', $this->getValidator()->validate('2001:db8:0:85a3:0:0:ac1f:8001', [], FILTER_FLAG_IPV6)); + self::assertSame('2001:0db8:0000:85a3:0000:0000:ac1f:8001', $this->getValidator()->validate('2001:0db8:0000:85a3:0000:0000:ac1f:8001', [], FILTER_FLAG_IPV6)); + self::assertSame('2001:db8:0:85a3:0:0:ac1f:8001', $this->getValidator()->validate('2001:db8:0:85a3:0:0:ac1f:8001', [], FILTER_FLAG_IPV6)); } - /** - * @return void - */ public function testIpV6WithInvalidValue(): void { $this->expectException(ValidationException::class); - $this->assertSame('test:0db8:0000:85a3:0000:0000:ac1f:8001', $this->getValidator()->validate('test:0db8:0000:85a3:0000:0000:ac1f:8001', [], FILTER_FLAG_IPV4)); + self::assertSame('test:0db8:0000:85a3:0000:0000:ac1f:8001', $this->getValidator()->validate('test:0db8:0000:85a3:0000:0000:ac1f:8001', [], FILTER_FLAG_IPV4)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ ['192.168.0.1', '192.168.0.1'], @@ -137,7 +107,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ ['192.168.0.256', false], @@ -148,7 +118,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { return [ ['192.168.0.1', ['default' => null], '192.168.0.1'], @@ -160,7 +130,7 @@ public function validValuesWithOptionsProvider(): array /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { return [ ['192.168.0.256', ['default' => null], null], diff --git a/tests/Validator/NullValidatorTest.php b/tests/Unit/Validator/NullValidatorTest.php similarity index 82% rename from tests/Validator/NullValidatorTest.php rename to tests/Unit/Validator/NullValidatorTest.php index c96d722..51e53fa 100644 --- a/tests/Validator/NullValidatorTest.php +++ b/tests/Unit/Validator/NullValidatorTest.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\NullValidator; @@ -36,7 +36,7 @@ public function getValidator(): ValidatorInterface */ public function testWithNullValue(): void { - $this->assertSame(null, $this->getValidator()->validate(null)); + self::assertNull($this->getValidator()->validate(null)); } /** @@ -45,6 +45,6 @@ public function testWithNullValue(): void public function testWithEmptyStringValue(): void { $this->expectException(ValidationException::class); - $this->assertSame(null, $this->getValidator()->validate('')); + self::assertNull($this->getValidator()->validate('')); } } diff --git a/tests/Validator/RegexpValidatorTest.php b/tests/Unit/Validator/RegexpValidatorTest.php similarity index 78% rename from tests/Validator/RegexpValidatorTest.php rename to tests/Unit/Validator/RegexpValidatorTest.php index d46f715..abffeb8 100644 --- a/tests/Validator/RegexpValidatorTest.php +++ b/tests/Unit/Validator/RegexpValidatorTest.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\RegexpValidator; @@ -37,7 +37,7 @@ public function getValidator(): ValidatorInterface public function testWithValidValue(): void { $text = 'The test passed!'; - $this->assertSame($text, $this->getValidator()->validate($text, ['regexp' => '`(.*)passed!`'])); + self::assertSame($text, $this->getValidator()->validate($text, ['regexp' => '`(.*)passed!`'])); } /** @@ -47,6 +47,6 @@ public function testWithEmptyStringValue(): void { $text = 'The test failed!'; $this->expectException(ValidationException::class); - $this->assertSame($text, $this->getValidator()->validate($text, ['regexp' => '`(.+)passed(.*)`'])); + self::assertSame($text, $this->getValidator()->validate($text, ['regexp' => '`(.+)passed(.*)`'])); } } diff --git a/tests/Validator/StringValidatorTest.php b/tests/Unit/Validator/StringValidatorTest.php similarity index 59% rename from tests/Validator/StringValidatorTest.php rename to tests/Unit/Validator/StringValidatorTest.php index 8e99706..40b73e5 100644 --- a/tests/Validator/StringValidatorTest.php +++ b/tests/Unit/Validator/StringValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\StringValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,71 +32,58 @@ public function getValidator(): ValidatorInterface return new StringValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @return void - * @dataProvider invalidValuesProvider */ - public function testWithInvalidValues($value, array $options): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, array $options): void { $this->expectException(ValidationException::class); $this->getValidator()->validate($value, $options); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ ['', ''], ['string', 'string'], ['This is a valid string', 'This is a valid string'], [' ! €$#!@ :)', ' ! €$#!@ :)'], - ['42', '42'] + ['42', '42'], ]; } /** * @return array>> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ [42, []], @@ -103,13 +91,16 @@ public function invalidValuesProvider(): array [false, []], ['a', ['min_length' => 2]], ['abcdefghijklmnopqrstuvwxyz', ['min_length' => 1, 'max_length' => 25]], + ['', ['default' => '', 'min_length' => 1]], + ['abcdefghijklmnopqrstuvwxyz', ['default' => '', 'min_length' => 1, 'max_length' => 25]], + ['abcdefghijklmnopqrstuvwxyz', ['default' => '', 'min_length' => 1, 'max_length' => 25]], ]; } /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { $options = ['default' => '']; return [ @@ -117,14 +108,14 @@ public function validValuesWithOptionsProvider(): array ['string', $options, 'string'], ['This is a valid string', $options, 'This is a valid string'], [' ! €$#!@ :)', $options, ' ! €$#!@ :)'], - ['42', $options, '42'] + ['42', $options, '42'], ]; } /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { $default = ''; $options = ['default' => $default]; @@ -133,9 +124,6 @@ public function invalidValuesWithOptionsProvider(): array [42, $options, $default], [null, $options, $default], [false, $options, $default], - ['', array_merge($options, ['min_length' => 1]), $default], - ['abcdefghijklmnopqrstuvwxyz', array_merge($options, ['max_length' => 25]), $default], - ['abcdefghijklmnopqrstuvwxyz', array_merge($options, ['min_length' => 1, 'max_length' => 25]), $default], ]; } } diff --git a/tests/Validator/TimeValidatorTest.php b/tests/Unit/Validator/TimeValidatorTest.php similarity index 53% rename from tests/Validator/TimeValidatorTest.php rename to tests/Unit/Validator/TimeValidatorTest.php index fb639a5..9a24d69 100644 --- a/tests/Validator/TimeValidatorTest.php +++ b/tests/Unit/Validator/TimeValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\TimeValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,57 +32,41 @@ public function getValidator(): ValidatorInterface return new TimeValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ ['00:00:00', '00:00:00'], @@ -94,7 +79,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ ['-00:00:00', false], @@ -106,7 +91,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { $default = '00:00:00'; $options = ['format' => 'H:i:s', 'format_output' => 'H:i:s', 'default' => $default]; @@ -116,8 +101,8 @@ public function validValuesWithOptionsProvider(): array ['23:59:59', $options, '23:59:59'], ['12:00:00', $options, '12:00:00'], ['24:00:00', $options, '00:00:00'], - ['23:59:59', array_merge($options, ['format_output' => 'H:i']), '23:59'], - ['23:59', array_merge($options, ['format' => 'H:i']), '23:59:00'], + ['23:59:59', \array_merge($options, ['format_output' => 'H:i']), '23:59'], + ['23:59', \array_merge($options, ['format' => 'H:i']), '23:59:00'], ['1:00', $options, '00:00:00'], ]; } @@ -125,15 +110,15 @@ public function validValuesWithOptionsProvider(): array /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { $default = '00:00:00'; $options = ['format' => 'H:i:s', 'format_output' => 'H:i:s', 'default' => $default]; return [ - ['-10:00:00', array_merge($options, ['format' => '-H:i:s']), '10:00:00'], - ['10:00', array_merge($options, ['format' => 'H:i']), '10:00:00'], - ['1:00', array_merge($options, ['format' => 'h:i']), '01:00:00'], + ['-10:00:00', \array_merge($options, ['format' => '-H:i:s']), '10:00:00'], + ['10:00', \array_merge($options, ['format' => 'H:i']), '10:00:00'], + ['1:00', \array_merge($options, ['format' => 'h:i']), '01:00:00'], ]; } } diff --git a/tests/Validator/TimestampValidatorTest.php b/tests/Unit/Validator/TimestampValidatorTest.php similarity index 55% rename from tests/Validator/TimestampValidatorTest.php rename to tests/Unit/Validator/TimestampValidatorTest.php index e71f798..8650616 100644 --- a/tests/Validator/TimestampValidatorTest.php +++ b/tests/Unit/Validator/TimestampValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\TimestampValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,73 +32,51 @@ public function getValidator(): ValidatorInterface return new TimestampValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider validValuesWithOptionsProvider */ - public function testWithValidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('validValuesWithOptionsProvider')] + public function testWithValidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } /** - * @param mixed $value * @param array $options - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesWithOptionsProvider */ - public function testWithInvalidValueAndDefaultValues($value, array $options, $excepted): void + #[DataProvider('invalidValuesWithOptionsProvider')] + public function testWithInvalidValueAndDefaultValues(mixed $value, array $options, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value, $options)); + self::assertSame($excepted, $this->getValidator()->validate($value, $options)); } - /** - * @return void - */ public function testWithOctalValueAsValidValues(): void { - $this->assertSame(octdec('777'), $this->getValidator()->validate(0777, [], FILTER_FLAG_ALLOW_OCTAL)); + self::assertSame(octdec('777'), $this->getValidator()->validate(0777, [], FILTER_FLAG_ALLOW_OCTAL)); } - /** - * @return void - */ public function testWithHexadecimalValueAsValidValues(): void { - $this->assertSame(hexdec('fa11'), $this->getValidator()->validate('0xfa11', [], FILTER_FLAG_ALLOW_HEX)); + self::assertSame(hexdec('fa11'), $this->getValidator()->validate('0xfa11', [], FILTER_FLAG_ALLOW_HEX)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ [0.0, 0], @@ -110,7 +89,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [ [-1, false], @@ -122,7 +101,7 @@ public function invalidValuesProvider(): array /** * @return array>> */ - public function validValuesWithOptionsProvider(): array + public static function validValuesWithOptionsProvider(): array { $default = -1; $options = ['default' => $default]; @@ -137,7 +116,7 @@ public function validValuesWithOptionsProvider(): array /** * @return array>> */ - public function invalidValuesWithOptionsProvider(): array + public static function invalidValuesWithOptionsProvider(): array { $default = 0; $options = ['default' => $default]; diff --git a/tests/Validator/UrlValidatorTest.php b/tests/Unit/Validator/UrlValidatorTest.php similarity index 78% rename from tests/Validator/UrlValidatorTest.php rename to tests/Unit/Validator/UrlValidatorTest.php index 2e49b81..8a28632 100644 --- a/tests/Validator/UrlValidatorTest.php +++ b/tests/Unit/Validator/UrlValidatorTest.php @@ -9,11 +9,12 @@ declare(strict_types=1); -namespace Eureka\Component\Validation\Tests\Validator; +namespace Eureka\Component\Validation\Tests\Unit\Validator; use Eureka\Component\Validation\Exception\ValidationException; use Eureka\Component\Validation\Validator\UrlValidator; use Eureka\Component\Validation\ValidatorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; /** @@ -31,33 +32,23 @@ public function getValidator(): ValidatorInterface return new UrlValidator(); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider validValuesProvider - */ - public function testWithValidValues($value, $excepted): void + #[DataProvider('validValuesProvider')] + public function testWithValidValues(mixed $value, mixed $excepted): void { - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } - /** - * @param mixed $value - * @param mixed $excepted - * @return void - * @dataProvider invalidValuesProvider - */ - public function testWithInvalidValues($value, $excepted): void + #[DataProvider('invalidValuesProvider')] + public function testWithInvalidValues(mixed $value, mixed $excepted): void { $this->expectException(ValidationException::class); - $this->assertSame($excepted, $this->getValidator()->validate($value)); + self::assertSame($excepted, $this->getValidator()->validate($value)); } /** * @return array> */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { return [ ['http://www.math.uio.no.example.net/faq/compression-faq/part1.html', 'http://www.math.uio.no.example.net/faq/compression-faq/part1.html'], @@ -78,7 +69,7 @@ public function validValuesProvider(): array /** * @return array> */ - public function invalidValuesProvider(): array + public static function invalidValuesProvider(): array { return [