diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..3ee4bc6 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,54 @@ +# .github/workflows/ci.yaml +name: Code_Checks + +on: ["push", "pull_request"] + +jobs: + tests: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: ['8.0'] + stability: [ prefer-stable ] + include: + - php: '8.0' + stability: prefer-lowest + symfony-version: 4.4 + - php: '8.0' + symfony-version: 5.4 + - php: '8.1' + symfony-version: 5.4 + - php: '8.2' + symfony-version: 5.4 + - php: '8.2' + symfony-version: 6.2 + + name: PHP ${{ matrix.php }} - ${{ matrix.stability }} tests + steps: + # basically git clone + - uses: actions/checkout@v3 + + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ~/.composer/cache/files + key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + # use PHP of specific version + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: pcov + coverage: pcov + + - name: Install dependencies + env: + SYMFONY_REQUIRE: ${{ matrix.symfony-version }} + run: | + composer global config --no-plugins allow-plugins.symfony/flex true + composer global require --no-progress --no-scripts --no-plugins symfony/flex + composer update --no-interaction --prefer-dist --optimize-autoloader + + - name: Execute tests + run: vendor/bin/phpunit -c ./phpunit.xml.dist ./tests --verbose diff --git a/.gitignore b/.gitignore index a1956e2..e260c28 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,7 @@ Thumbs.db #composer related composer.phar composer.lock -vendor/ \ No newline at end of file +vendor/ + +.phpunit.result.cache +code-coverage \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 030b933..0000000 --- a/.travis.yml +++ /dev/null @@ -1,30 +0,0 @@ -language: php -sudo: false - -cache: - directories: - - $HOME/.composer/cache/files - -matrix: - fast_finish: true - include: - - php: 5.6 - - php: 7.0 - - php: 7.1 - - php: 7.2 - - php: 7.3 - -before_install: - - composer self-update - -before_script: - - mkdir -p build/logs - -install: - - composer install --no-interaction - -script: - - ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml - -after_success: - - travis_retry php ./vendor/bin/coveralls --coverage_clover=build/logs/clover.xml diff --git a/README.md b/README.md index c336646..0b5a96d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Constraint Class for international Zipcode Validation -[![Build Status](https://img.shields.io/travis/barbieswimcrew/zip-code-validator/master.svg?style=flat-square)](https://travis-ci.org/barbieswimcrew/zip-code-validator) +[![Build Status](https://github.com/barbieswimcrew/zip-code-validator/actions/workflows/ci.yaml/badge.svg)](https://github.com/barbieswimcrew/zip-code-validator/actions/workflows/ci.yaml) [![Downloads](https://img.shields.io/packagist/dt/barbieswimcrew/zip-code-validator.svg?style=flat-square)](https://packagist.org/packages/barbieswimcrew/zip-code-validator) [![Latest stable version](https://img.shields.io/packagist/v/barbieswimcrew/zip-code-validator.svg?style=flat-square)](https://packagist.org/packages/barbieswimcrew/zip-code-validator) [![PHP from Packagist](https://img.shields.io/packagist/php-v/barbieswimcrew/zip-code-validator.svg?style=flat-square)](./composer.json) @@ -26,14 +26,14 @@ For validating a zip code you need to instantiate a new ZipCode class provided b createFormBuilder($address) - ->add('zipcode', TextType::class, array( + ->add('zipcode', TextType::class, [ 'constraints' => array( - new ZipCodeValidator\Constraints\ZipCode(array( + new ZipCodeValidator\Constraints\ZipCode([ 'iso' => 'DE' - )) - ) + ]) + ] )) - ->add('save', SubmitType::class, array('label' => 'Create Task')) + ->add('save', SubmitType::class, ['label' => 'Create Task']) ->getForm(); ``` diff --git a/composer.json b/composer.json index d51c821..8c35e93 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "barbieswimcrew/zip-code-validator", "description": "Constraint class for international zipcode validation", - "keywords": ["symfony", "symfony4", "symfony3", "symfony2", "form", "constraints", "constraint", "validator", "zip code", "zipcode", "postal code", "postalcode", "validation"], + "keywords": ["symfony", "form", "constraints", "constraint", "validator", "zip code", "zipcode", "postal code", "postalcode", "validation"], "license": "MIT", "authors": [ { @@ -11,16 +11,20 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": ">=5.6.0", - "symfony/validator": ">=2.1" + "php": ">=8.0", + "symfony/validator": ">=4.4.40" }, "require-dev": { - "phpunit/phpunit": "^5" + "phpunit/phpunit": "^9.5" }, "autoload": { "psr-4": { - "ZipCodeValidator\\": "src/ZipCodeValidator", - "Tests\\Fixtures\\": "tests/Fixtures" + "ZipCodeValidator\\": "src/ZipCodeValidator" + } + }, + "autoload-dev": { + "psr-4": { + "ZipCodeValidator\\Tests\\": "tests/" } } } diff --git a/phpunit.xml b/phpunit.xml index a9ac924..c9ffb06 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,22 +1,17 @@ - + - - - ./tests - - - - - - ./tests - - + xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"> + + + ./tests + + + + + ./tests + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 80c0606..111b164 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,32 @@ - - + - - - + bootstrap="vendor/autoload.php"> + + + + ./src/ + + + ./tests + + + + + + + + + - - - Tests - - + + + Tests + + - - - ./tests/ - - + - - - diff --git a/src/ZipCodeValidator/Constraints/ZipCode.php b/src/ZipCodeValidator/Constraints/ZipCode.php index 1245dcd..0f09a25 100644 --- a/src/ZipCodeValidator/Constraints/ZipCode.php +++ b/src/ZipCodeValidator/Constraints/ZipCode.php @@ -6,44 +6,19 @@ use Symfony\Component\Validator\Exception\MissingOptionsException; /** - * Class ZipCode * @Annotation * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) * @author Martin Schindler - * @package ZipCodeValidator\Constraints */ class ZipCode extends Constraint { - /** - * @var string - */ - public $message = 'This value is not a valid ZIP code.'; + public string $message = 'This value is not a valid ZIP code.'; + public ?string $iso = null; + public ?string $getter = null; + public bool $strict = true; + public bool $caseSensitiveCheck = true; - /** - * @var string - */ - public $iso; - - /** - * @var string - */ - public $getter; - - /** - * @var bool - */ - public $strict = true; - - /** - * @var bool - */ - public $caseSensitiveCheck = true; - - /** - * ZipCode constructor. - * @param mixed $options - */ - public function __construct($options = null) + public function __construct(mixed $options = null) { if (null !== $options && !is_array($options)) { $options = array( @@ -54,7 +29,7 @@ public function __construct($options = null) parent::__construct($options); if (null === $this->iso && null === $this->getter) { - throw new MissingOptionsException(sprintf('Either the option "iso" or "getter" must be given for constraint %s', __CLASS__), array('iso', 'getter')); + throw new MissingOptionsException(sprintf('Either the option "iso" or "getter" must be given for constraint %s', __CLASS__), ['iso', 'getter']); } } diff --git a/src/ZipCodeValidator/Constraints/ZipCodeValidator.php b/src/ZipCodeValidator/Constraints/ZipCodeValidator.php index b2dab1e..bf594da 100644 --- a/src/ZipCodeValidator/Constraints/ZipCodeValidator.php +++ b/src/ZipCodeValidator/Constraints/ZipCodeValidator.php @@ -7,13 +7,10 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** - * Class ZipCodeValidator * @author Martin Schindler - * @package ZipCodeValidator\Constraints */ class ZipCodeValidator extends ConstraintValidator { - /** * ZipCode patterns from http://i18napis.appspot.com (updated: 2015-05-08) * @@ -30,7 +27,7 @@ class ZipCodeValidator extends ConstraintValidator * var_export($patterns); * */ - private $patterns = array( + private array $patterns = [ 'AC' => 'ASCN 1ZZ', 'AD' => 'AD[1-7]0\\d', 'AF' => '\\d{4}', @@ -213,15 +210,15 @@ class ZipCodeValidator extends ConstraintValidator 'YT' => '976\\d{2}', 'ZA' => '\\d{4}', 'ZM' => '\\d{5}', - ); + ]; /** * {@inheritdoc} */ - public function validate($value, Constraint $constraint) + public function validate($value, Constraint $constraint): void { if (!$constraint instanceof ZipCode) { - throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\ZipCode'); + throw new UnexpectedTypeException($constraint, ZipCode::class); } if (null === $value || '' === $value) { @@ -249,9 +246,9 @@ public function validate($value, Constraint $constraint) if (!isset($this->patterns[$iso])) { if ($constraint->strict) { throw new ConstraintDefinitionException(sprintf('Invalid iso code "%s" for validation', $iso)); - } else { - return; } + + return; } $pattern = $this->patterns[$iso]; diff --git a/tests/Constraints/ChZipCodeValidatorTest.php b/tests/Constraints/ChZipCodeValidatorTest.php index e97d0ff..da6a4d9 100644 --- a/tests/Constraints/ChZipCodeValidatorTest.php +++ b/tests/Constraints/ChZipCodeValidatorTest.php @@ -1,30 +1,31 @@ validator = new ZipCodeValidator; } /** * @dataProvider chValidZipCodes - * @param string $zipCode */ - public function testValidationOfChZipCode($zipCode) + public function testValidationOfChZipCode(string $zipCode): void { $constraint = new ZipCode('CH'); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -39,10 +40,7 @@ public function testValidationOfChZipCode($zipCode) $this->validator->validate($zipCode, $constraint); } - /** - * @return array - */ - public function chValidZipCodes() + public function chValidZipCodes(): array { return [ ['1000'], @@ -54,9 +52,8 @@ public function chValidZipCodes() /** * @dataProvider chInvalidZipCodes - * @param string $zipcode */ - public function testValidationErrorWithInvalidChZipCode($zipcode) + public function testValidationErrorWithInvalidChZipCode(string $zipcode): void { $constraint = new ZipCode('CH'); @@ -65,11 +62,11 @@ public function testValidationErrorWithInvalidChZipCode($zipcode) ->getMock(); $violationBuilderMock->expects($this->once())->method('setParameter')->willReturnSelf(); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); - $contextMock->expects($this->exactly(1)) + $contextMock->expects($this->once()) ->method('buildViolation') ->with($constraint->message) ->willReturn($violationBuilderMock); @@ -79,10 +76,7 @@ public function testValidationErrorWithInvalidChZipCode($zipcode) $this->validator->validate($zipcode, $constraint); } - /** - * @return array - */ - public function chInvalidZipCodes() + public function chInvalidZipCodes(): array { return [ ['0000'], @@ -93,5 +87,4 @@ public function chInvalidZipCodes() ]; } - } diff --git a/tests/Constraints/EsZipCodeValidatorTest.php b/tests/Constraints/EsZipCodeValidatorTest.php index d4d28fc..a5382a1 100644 --- a/tests/Constraints/EsZipCodeValidatorTest.php +++ b/tests/Constraints/EsZipCodeValidatorTest.php @@ -1,30 +1,31 @@ validator = new ZipCodeValidator; } /** * @dataProvider esValidZipCodes - * @param string $zipCode */ - public function testValidationOfEsZipCode($zipCode) + public function testValidationOfEsZipCode(string $zipCode): void { $constraint = new ZipCode('ES'); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -39,10 +40,7 @@ public function testValidationOfEsZipCode($zipCode) $this->validator->validate($zipCode, $constraint); } - /** - * @return array - */ - public function esValidZipCodes() + public function esValidZipCodes(): array { return [ ['08020'], @@ -54,9 +52,8 @@ public function esValidZipCodes() /** * @dataProvider esInvalidZipCodes - * @param string $zipcode */ - public function testValidationErrorWithInvalidEsZipCode($zipcode) + public function testValidationErrorWithInvalidEsZipCode(string $zipcode): void { $constraint = new ZipCode('ES'); @@ -65,11 +62,11 @@ public function testValidationErrorWithInvalidEsZipCode($zipcode) ->getMock(); $violationBuilderMock->expects($this->once())->method('setParameter')->willReturnSelf(); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); - $contextMock->expects($this->exactly(1)) + $contextMock->expects($this->once()) ->method('buildViolation') ->with($constraint->message) ->willReturn($violationBuilderMock); @@ -79,10 +76,7 @@ public function testValidationErrorWithInvalidEsZipCode($zipcode) $this->validator->validate($zipcode, $constraint); } - /** - * @return array - */ - public function esInvalidZipCodes() + public function esInvalidZipCodes(): array { return [ ['53456'], @@ -92,6 +86,4 @@ public function esInvalidZipCodes() ['2341'], ]; } - - } diff --git a/tests/Constraints/GbZipCodeValidatorTest.php b/tests/Constraints/GbZipCodeValidatorTest.php index 599d16c..b7d3732 100644 --- a/tests/Constraints/GbZipCodeValidatorTest.php +++ b/tests/Constraints/GbZipCodeValidatorTest.php @@ -1,17 +1,20 @@ validator = new ZipCodeValidator; } @@ -19,11 +22,11 @@ public function setUp() /** * @dataProvider GbZipCodes */ - public function testValidationOfGbZipCodeWithIso($zipCode) + public function testValidationOfGbZipCodeWithIso($zipCode): void { $constraint = new ZipCode('GB'); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -41,7 +44,7 @@ public function testValidationOfGbZipCodeWithIso($zipCode) /** * @dataProvider */ - public function gbZipCodes() + public function gbZipCodes(): array { return [ ['EC1A 1BB'], @@ -60,16 +63,15 @@ public function gbZipCodes() /** * @dataProvider gbZipCodesSmallCaps - * @param $zipCode */ - public function testValidationOfGbZipCodeWithIsoAndSmallCaps($zipCode) + public function testValidationOfGbZipCodeWithIsoAndSmallCaps(string $zipCode): void { $constraint = new ZipCode([ 'iso' => 'GB', 'caseSensitiveCheck' => false ]); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -86,7 +88,7 @@ public function testValidationOfGbZipCodeWithIsoAndSmallCaps($zipCode) /** * @dataProvider */ - public function gbZipCodesSmallCaps() + public function gbZipCodesSmallCaps(): array { return [ ['ec1a 1bb'], @@ -102,7 +104,7 @@ public function gbZipCodesSmallCaps() ]; } - public function testValidationErrorWithInvalidGbZipCode() + public function testValidationErrorWithInvalidGbZipCode(): void { $value = 'invalid'; $constraint = new ZipCode('GB'); @@ -112,7 +114,7 @@ public function testValidationErrorWithInvalidGbZipCode() ->getMock(); $violationBuilderMock->expects($this->once())->method('setParameter')->willReturnSelf(); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -126,10 +128,7 @@ public function testValidationErrorWithInvalidGbZipCode() $this->validator->validate($value, $constraint); } - /** - * @test - */ - public function testValidationErrorWithValidGbZipCodeWithExtraLeadingChars() + public function testValidationErrorWithValidGbZipCodeWithExtraLeadingChars(): void { $value = 'XEC1A 1BB'; $constraint = new ZipCode('GB'); @@ -139,7 +138,7 @@ public function testValidationErrorWithValidGbZipCodeWithExtraLeadingChars() ->getMock(); $violationBuilderMock->expects($this->once())->method('setParameter')->willReturnSelf(); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -153,10 +152,7 @@ public function testValidationErrorWithValidGbZipCodeWithExtraLeadingChars() $this->validator->validate($value, $constraint); } - /** - * @test - */ - public function testValidationErrorWithValidGbZipCodeWithExtraTrailingChars() + public function testValidationErrorWithValidGbZipCodeWithExtraTrailingChars(): void { $value = 'EC1A 1BBX'; $constraint = new ZipCode('GB'); @@ -166,7 +162,7 @@ public function testValidationErrorWithValidGbZipCodeWithExtraTrailingChars() ->getMock(); $violationBuilderMock->expects($this->once())->method('setParameter')->willReturnSelf(); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -180,10 +176,10 @@ public function testValidationErrorWithValidGbZipCodeWithExtraTrailingChars() $this->validator->validate($value, $constraint); } - public function testEmptyIsoWithValidZipCodeWillCallGetterMethodOnObject() + public function testEmptyIsoWithValidZipCodeWillCallGetterMethodOnObject(): void { $value = 'EC1A 1BB'; - /** @var ZipCode|PHPUnit_Framework_MockObject_MockObject $constraintMock */ + /** @var ZipCode|MockObject $constraintMock */ $constraintMock = $this->getMockBuilder(ZipCode::class) ->disableOriginalConstructor() ->getMock(); @@ -191,9 +187,9 @@ public function testEmptyIsoWithValidZipCodeWillCallGetterMethodOnObject() $constraintMock->iso = null; $constraintMock->getter = 'myValidationMethod'; - $gbObject = new \Tests\Fixtures\IsoObject('GB'); + $gbObject = new \ZipCodeValidator\Tests\Fixtures\IsoObject('GB'); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -206,10 +202,10 @@ public function testEmptyIsoWithValidZipCodeWillCallGetterMethodOnObject() $this->validator->validate($value, $constraintMock); } - public function testConstraintDefinitionExceptionWhenCallableMethodNotExists() + public function testConstraintDefinitionExceptionWhenCallableMethodNotExists(): void { $value = 'EC1A 1BB'; - /** @var ZipCode|PHPUnit_Framework_MockObject_MockObject $constraintMock */ + /** @var ZipCode|MockObject $constraintMock */ $constraintMock = $this->getMockBuilder(ZipCode::class) ->disableOriginalConstructor() ->getMock(); @@ -217,9 +213,9 @@ public function testConstraintDefinitionExceptionWhenCallableMethodNotExists() $constraintMock->iso = null; $constraintMock->getter = 'myFooMethod'; // not existing in GbObject - $gbObject = new \Tests\Fixtures\IsoObject('GB'); + $gbObject = new \ZipCodeValidator\Tests\Fixtures\IsoObject('GB'); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); diff --git a/tests/Constraints/JmZipCodeValidatorTest.php b/tests/Constraints/JmZipCodeValidatorTest.php index eb0b63e..4d27651 100644 --- a/tests/Constraints/JmZipCodeValidatorTest.php +++ b/tests/Constraints/JmZipCodeValidatorTest.php @@ -1,26 +1,22 @@ validator = new ZipCodeValidator; } @@ -29,14 +25,12 @@ public function setUp() * This test verifies that all known Jamaica codes are valid. * * @dataProvider getValidJamaicaZipCodes - * @test - * @param string $zipCode */ - public function testValidZipcodes($zipCode) + public function testValidZipcodes(string $zipCode): void { $constraint = new ZipCode('JM'); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -52,10 +46,8 @@ public function testValidZipcodes($zipCode) /** * used postal codes * from https://en.wikipedia.org/wiki/Postal_codes_in_Jamaica - * - * @return array */ - public function getValidJamaicaZipCodes() + public function getValidJamaicaZipCodes(): array { return [ ['KN'], @@ -71,17 +63,15 @@ public function getValidJamaicaZipCodes() * This test verifies that all known Jamaica codes are valid. * * @dataProvider getInvalidJamaicaZipCodes - * @test - * @param string $zipCode */ - public function testInvalidZipcodes($zipCode) + public function testInvalidZipcodes(string $zipCode): void { $constraint = new ZipCode('JM'); $violation = $this->createMock(ConstraintViolationBuilderInterface::class); $violation->expects($this->once())->method('setParameter')->willReturnSelf(); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -97,10 +87,8 @@ public function testInvalidZipcodes($zipCode) /** * used postal codes * from https://en.wikipedia.org/wiki/Postal_codes_in_Jamaica - * - * @return array */ - public function getInvalidJamaicaZipCodes() + public function getInvalidJamaicaZipCodes(): array { return [ ['12'], diff --git a/tests/Constraints/LtZipCodeValidatorTest.php b/tests/Constraints/LtZipCodeValidatorTest.php index 9473e1c..aac79a8 100644 --- a/tests/Constraints/LtZipCodeValidatorTest.php +++ b/tests/Constraints/LtZipCodeValidatorTest.php @@ -1,35 +1,31 @@ validator = new ZipCodeValidator; } /** * @dataProvider getValidLithuanianZipCodes - * @test - * @param string $zipCode */ - public function testValidZipcodes($zipCode) + public function testValidZipcodes(string $zipCode): void { $constraint = new ZipCode('LT'); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -45,10 +41,8 @@ public function testValidZipcodes($zipCode) /** * Valid Lithuanian postal codes are five-digit numbers, optionally prefixed with "LT-". * @see https://en.wikipedia.org/wiki/Postal_codes_in_Lithuania - * - * @return array */ - public function getValidLithuanianZipCodes() + public function getValidLithuanianZipCodes(): array { return [ ['12345'], @@ -62,17 +56,15 @@ public function getValidLithuanianZipCodes() /** * @dataProvider getInvalidLithuanianZipCodes - * @test - * @param string $zipCode */ - public function testInvalidZipcodes($zipCode) + public function testInvalidZipcodes(string $zipCode): void { $constraint = new ZipCode('LT'); $violation = $this->createMock(ConstraintViolationBuilderInterface::class); $violation->expects($this->once())->method('setParameter')->willReturnSelf(); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -88,10 +80,8 @@ public function testInvalidZipcodes($zipCode) /** * Valid Lithuanian postal codes are five-digit numbers, optionally prefixed with "LT-". * @see https://en.wikipedia.org/wiki/Postal_codes_in_Lithuania - * - * @return array */ - public function getInvalidLithuanianZipCodes() + public function getInvalidLithuanianZipCodes(): array { return [ ['1234'], diff --git a/tests/Constraints/NlZipCodeValidatorTest.php b/tests/Constraints/NlZipCodeValidatorTest.php index 7993d05..e0afda9 100644 --- a/tests/Constraints/NlZipCodeValidatorTest.php +++ b/tests/Constraints/NlZipCodeValidatorTest.php @@ -1,16 +1,19 @@ validator = new ZipCodeValidator; } @@ -18,7 +21,7 @@ public function setUp() /** * @doesNotPerformAssertions */ - public function testValidationOfNlZipWithIsoCode() + public function testValidationOfNlZipWithIsoCode(): void { $constraint = new ZipCode('NL'); @@ -30,7 +33,7 @@ public function testValidationOfNlZipWithIsoCode() /** * @doesNotPerformAssertions */ - public function testValidationOfNlZipWithIsoCodeAnSmallCaps() + public function testValidationOfNlZipWithIsoCodeAnSmallCaps(): void { $constraint = new ZipCode([ 'iso' => 'NL', @@ -42,7 +45,7 @@ public function testValidationOfNlZipWithIsoCodeAnSmallCaps() $this->validator->validate('1000 aa', $constraint); } - public function testValidationErrorWithInvalidNlZipCode() + public function testValidationErrorWithInvalidNlZipCode(): void { $value = "1000"; $constraint = new ZipCode('NL'); @@ -52,11 +55,11 @@ public function testValidationErrorWithInvalidNlZipCode() ->getMock(); $violationBuilderMock->expects($this->once())->method('setParameter')->willReturnSelf(); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); - $contextMock->expects($this->exactly(1)) + $contextMock->expects($this->once()) ->method('buildViolation') ->with($constraint->message) ->willReturn($violationBuilderMock); diff --git a/tests/Constraints/PaZipCodeValidatorTest.php b/tests/Constraints/PaZipCodeValidatorTest.php index 2230259..21c1a06 100644 --- a/tests/Constraints/PaZipCodeValidatorTest.php +++ b/tests/Constraints/PaZipCodeValidatorTest.php @@ -1,8 +1,10 @@ validator = new ZipCodeValidator; } @@ -28,14 +25,12 @@ public function setUp() * This test verifies that all known Panama codes are valid. * * @dataProvider getPanamaZipCodes - * @test - * @param string $zipCode */ - public function testZipcodes($zipCode) + public function testZipcodes(string $zipCode): void { $constraint = new ZipCode('PA'); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $contextMock */ + /** @var ExecutionContext|MockObject $contextMock */ $contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -51,10 +46,8 @@ public function testZipcodes($zipCode) /** * used postal codes * from https://en.wikipedia.org/wiki/Postal_codes_in_Panama - * - * @return array */ - public function getPanamaZipCodes() + public function getPanamaZipCodes(): array { return [ ['0101'], diff --git a/tests/Constraints/ZipCodeTest.php b/tests/Constraints/ZipCodeTest.php index c6602ec..6de7f5f 100644 --- a/tests/Constraints/ZipCodeTest.php +++ b/tests/Constraints/ZipCodeTest.php @@ -1,11 +1,14 @@ expectException(MissingOptionsException::class); $constraint = new ZipCode(null); diff --git a/tests/Constraints/ZipCodeValidatorTest.php b/tests/Constraints/ZipCodeValidatorTest.php index fc04c15..b374c57 100644 --- a/tests/Constraints/ZipCodeValidatorTest.php +++ b/tests/Constraints/ZipCodeValidatorTest.php @@ -1,21 +1,25 @@ contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() @@ -24,13 +28,10 @@ public function setUp() } /** - * @param string $isoCode - * @param string $value - * * @dataProvider validZipCodes * @doesNotPerformAssertions */ - public function testZipCodeValidationWithIso($isoCode, $value) + public function testZipCodeValidationWithIso(string|int $isoCode, string|int|null $value): void { $constraint = new ZipCode($isoCode); $this->validator->validate($value, $constraint); @@ -39,7 +40,7 @@ public function testZipCodeValidationWithIso($isoCode, $value) /** * @dataProvider */ - public function validZipCodes() + public function validZipCodes(): array { return [ ['HK', 999077], @@ -52,10 +53,9 @@ public function validZipCodes() ]; } - /** @test */ - public function testValidZipCodeValidationWithGetter() + public function testValidZipCodeValidationWithGetter(): void { - /** @var ZipCode|PHPUnit_Framework_MockObject_MockObject $constraintMock */ + /** @var ZipCode|MockObject $constraintMock */ $constraintMock = $this->getMockBuilder(ZipCode::class) ->disableOriginalConstructor() ->getMock(); @@ -63,7 +63,7 @@ public function testValidZipCodeValidationWithGetter() $constraintMock->iso = null; $constraintMock->getter = 'myValidationMethod'; - $gbObject = new \Tests\Fixtures\IsoObject('VN'); + $gbObject = new \ZipCodeValidator\Tests\Fixtures\IsoObject('VN'); $this->contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() @@ -77,7 +77,7 @@ public function testValidZipCodeValidationWithGetter() $this->validator->validate(123456, $constraintMock); } - public function testUnexpectedTypeException() + public function testUnexpectedTypeException(): void { $constraint = $this->getMockBuilder(Constraint::class)->disableOriginalConstructor()->getMock(); $this->expectException(UnexpectedTypeException::class); @@ -88,7 +88,7 @@ public function testUnexpectedTypeException() /** * @doesNotPerformAssertions */ - public function testValidationIgnoresBlankValues() + public function testValidationIgnoresBlankValues(): void { $constraint = new ZipCode('HK'); @@ -96,10 +96,9 @@ public function testValidationIgnoresBlankValues() $this->validator->validate(null, $constraint); } - /** @test */ - public function testValidationReturnsNothingOnEmptyIso() + public function testValidationReturnsNothingOnEmptyIso(): void { - /** @var ZipCode|PHPUnit_Framework_MockObject_MockObject $constraintMock */ + /** @var ZipCode|MockObject $constraintMock */ $constraintMock = $this->getMockBuilder(ZipCode::class) ->disableOriginalConstructor() ->getMock(); @@ -107,9 +106,9 @@ public function testValidationReturnsNothingOnEmptyIso() $constraintMock->iso = ''; $constraintMock->getter = 'myValidationMethod'; - $isoObject = new \Tests\Fixtures\IsoObject(''); + $isoObject = new \ZipCodeValidator\Tests\Fixtures\IsoObject(''); - /** @var ExecutionContext|PHPUnit_Framework_MockObject_MockObject $this->contextMock */ + /** @var ExecutionContext|MockObject $this->contextMock */ $this->contextMock = $this->getMockBuilder(ExecutionContext::class) ->disableOriginalConstructor() ->getMock(); @@ -123,7 +122,7 @@ public function testValidationReturnsNothingOnEmptyIso() $this->validator->validate('dummy', $constraintMock); } - public function testConstraintDefinitionExceptionWhenInvalidIsoInStrictMode() + public function testConstraintDefinitionExceptionWhenInvalidIsoInStrictMode(): void { $constraint = new ZipCode('FOO'); $this->expectException(ConstraintDefinitionException::class); diff --git a/tests/Fixtures/IsoObject.php b/tests/Fixtures/IsoObject.php index f35eb6f..55e4894 100644 --- a/tests/Fixtures/IsoObject.php +++ b/tests/Fixtures/IsoObject.php @@ -1,26 +1,14 @@ iso = $iso; } - /** - * @return string - */ - public function myValidationMethod() + public function myValidationMethod(): string { return $this->iso; } -} \ No newline at end of file +}