From 9296d6ee27c5b045f0574f60fa01de1091171a23 Mon Sep 17 00:00:00 2001 From: Pierre Tondereau Date: Mon, 5 Oct 2020 20:41:15 +0200 Subject: [PATCH 01/24] Add PHP8 support. --- .travis.yml | 13 +++++++++---- composer.json | 2 +- tests/Assert/Tests/AssertTest.php | 8 ++++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index f551481d..fe9f2614 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,10 +12,10 @@ matrix: - php: 7.1 - php: 7.2 - php: 7.3 - - php: 7.3.0 - - php: 7.3.1 - - php: 7.3.2 + - php: 7.4 - php: nightly + env: + - IGNORE_PLATFORM_REQ=true - php: hhvm-3.9 sudo: required dist: trusty @@ -36,7 +36,12 @@ before_install: - xdebug-disable before_script: - - travis_retry composer update --no-interaction --prefer-source --prefer-stable + - if [[ "$IGNORE_PLATFORM_REQ" == "true" ]]; then + composer self-update --preview; + travis_retry composer update --no-interaction --prefer-stable --ignore-platform-req=php; + else + travis_retry composer update --no-interaction --prefer-stable; + fi script: - if [[ "$LINT" == "true" ]]; then diff --git a/composer.json b/composer.json index 619b8e27..cc326282 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "sort-packages": true }, "require": { - "php": "^7", + "php": "^7.0 || ^8.0", "ext-intl": "*", "ext-simplexml": "*", "ext-mbstring": "*", diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 8f4d3fea..5c0cd1d2 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -1832,7 +1832,11 @@ public function testValidCallable() { $this->assertTrue(Assertion::isCallable('\is_callable')); $this->assertTrue(Assertion::isCallable(__NAMESPACE__.'\\Fixtures\\someCallable')); - $this->assertTrue(Assertion::isCallable([OneCountable::class, 'count'])); + if (PHP_VERSION_ID >= 70400) { + $this->assertTrue(Assertion::isCallable([new OneCountable(), 'count'])); + } else { + $this->assertTrue(Assertion::isCallable([OneCountable::class, 'count'])); + } $this->assertTrue( Assertion::isCallable( function () { @@ -2227,7 +2231,7 @@ public function testInvalidPropertiesExist($properties) public function testIsResource() { - self::assertTrue(Assertion::isResource(\curl_init())); + self::assertTrue(Assertion::isResource(fopen('php://memory', 'w'))); } /** From 4991d6c629490088f68f3492e5d9129f68e2fca7 Mon Sep 17 00:00:00 2001 From: Pierre Tondereau Date: Mon, 5 Oct 2020 21:39:12 +0200 Subject: [PATCH 02/24] Prove is_callable. --- tests/Assert/Tests/AssertTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 5c0cd1d2..6df51161 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -16,6 +16,7 @@ use Assert\Assertion; use Assert\AssertionFailedException; +use Assert\Tests\Fixtures\CustomAssertion; use Assert\Tests\Fixtures\OneCountable; use PDO; use PHPUnit\Framework\TestCase; @@ -1832,11 +1833,8 @@ public function testValidCallable() { $this->assertTrue(Assertion::isCallable('\is_callable')); $this->assertTrue(Assertion::isCallable(__NAMESPACE__.'\\Fixtures\\someCallable')); - if (PHP_VERSION_ID >= 70400) { - $this->assertTrue(Assertion::isCallable([new OneCountable(), 'count'])); - } else { - $this->assertTrue(Assertion::isCallable([OneCountable::class, 'count'])); - } + $this->assertTrue(Assertion::isCallable([new OneCountable(), 'count'])); + $this->assertTrue(Assertion::isCallable([CustomAssertion::class, 'clearCalls'])); $this->assertTrue( Assertion::isCallable( function () { From 5c4dd239f1fbc036c9912203fef02ddae911e80a Mon Sep 17 00:00:00 2001 From: Pierre Tondereau Date: Tue, 6 Oct 2020 10:34:28 +0200 Subject: [PATCH 03/24] Update tests/Assert/Tests/AssertTest.php Co-authored-by: Fabien Villepinte --- tests/Assert/Tests/AssertTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 6df51161..317ac166 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -2229,7 +2229,7 @@ public function testInvalidPropertiesExist($properties) public function testIsResource() { - self::assertTrue(Assertion::isResource(fopen('php://memory', 'w'))); + self::assertTrue(Assertion::isResource(\fopen('php://memory', 'w'))); } /** From c9ed634f8f000c98ca63e515011cd01dcbe1ae1e Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Fri, 13 Nov 2020 19:52:18 +0100 Subject: [PATCH 04/24] Switch from travis to Github actions (#304) * Switch from travis to Github actions * Update PHPUnit with yoast polyfill for 7.0 to 8.0 support. * Housekeeping: CS * Comment out lint for now, lots of failures, why? --- .github/workflows/ci.yml | 64 ++ .travis.yml | 65 -- .travis/xdebug.sh | 22 - composer.json | 3 +- tests/Assert/Tests/AssertTest.php | 582 +++++++----------- .../Tests/AssertionChainFunctionsTest.php | 12 +- tests/Assert/Tests/AssertionChainTest.php | 14 +- .../Tests/AssertionExceptionCallbackTest.php | 18 +- .../Assert/Tests/CustomAssertionClassTest.php | 23 +- tests/Assert/Tests/LazyAssertionTest.php | 54 +- .../Tests/LazyFunctionAssertionTest.php | 42 +- 11 files changed, 335 insertions(+), 564 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .travis.yml delete mode 100644 .travis/xdebug.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..04a76023 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,64 @@ +on: push +name: CI +jobs: + tests: + name: Tests + runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] + + steps: + - name: Checkout + uses: actions/checkout@v1 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + tools: "cs2pr" + + - name: "Cache dependencies installed with composer" + uses: "actions/cache@v1" + with: + path: "~/.composer/cache" + key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}" + restore-keys: "php-${{ matrix.php-version }}-composer-locked-" + + - name: "Composer" + run: "composer update --prefer-stable" + + - name: "PHPUnit" + run: "php vendor/bin/phpunit" + +# lint: +# name: Lint +# runs-on: ubuntu-latest + +# steps: +# - name: Checkout +# uses: actions/checkout@v1 + +# - name: Setup PHP +# uses: shivammathur/setup-php@v2 +# with: +# php-version: 7.4 + +# - name: "Cache dependencies installed with composer" +# uses: "actions/cache@v1" +# with: +# path: "~/.composer/cache" +# key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}" +# restore-keys: "php-${{ matrix.php-version }}-composer-locked-" + +# - name: "Composer" +# run: "composer update --prefer-stable" + +# - name: "assert:cs-lint" +# run: "composer assert:cs-lint" + +# - name: "assert:sa-code" +# run: "composer assert:sa-code" + +# - name: "assert:sa-tests" +# run: "composer assert:sa-tests" diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index fe9f2614..00000000 --- a/.travis.yml +++ /dev/null @@ -1,65 +0,0 @@ -language: php -sudo: false - -git: - depth: 2 - -matrix: - include: - - php: 7.0 - env: - - LINT=true COVERAGE=true - - php: 7.1 - - php: 7.2 - - php: 7.3 - - php: 7.4 - - php: nightly - env: - - IGNORE_PLATFORM_REQ=true - - php: hhvm-3.9 - sudo: required - dist: trusty - group: edge - - fast_finish: true - allow_failures: - - php: nightly - - php: hhvm-3.9 - -cache: - directories: - - $HOME/.composer/cache - - $HOME/.php-cs-fixer - -before_install: - - source .travis/xdebug.sh - - xdebug-disable - -before_script: - - if [[ "$IGNORE_PLATFORM_REQ" == "true" ]]; then - composer self-update --preview; - travis_retry composer update --no-interaction --prefer-stable --ignore-platform-req=php; - else - travis_retry composer update --no-interaction --prefer-stable; - fi - -script: - - if [[ "$LINT" == "true" ]]; then - composer assert:cs-lint; - composer assert:sa-code; - composer assert:sa-tests; - ./bin/travis/lint-docs; - fi - - if [[ "$COVERAGE" == "true" ]]; then - xdebug-enable; - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover; - xdebug-disable; - else - vendor/bin/phpunit; - fi - -after_script: - - if [[ "$COVERAGE" == "true" ]]; then - wget https://scrutinizer-ci.com/ocular.phar; - php ocular.phar code-coverage:upload --format=php-clover coverage.clover; - fi diff --git a/.travis/xdebug.sh b/.travis/xdebug.sh deleted file mode 100644 index c3cebe3c..00000000 --- a/.travis/xdebug.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash - -# The problem is that we do not want to remove the configuration file, just disable it for a few tasks, then enable it -# -# For reference, see -# -# - https://docs.travis-ci.com/user/languages/php#Disabling-preinstalled-PHP-extensions -# - https://docs.travis-ci.com/user/languages/php#Custom-PHP-configuration - -config="/home/travis/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini" - -function xdebug-disable() { - if [[ -f $config ]]; then - mv $config "$config.bak" - fi -} - -function xdebug-enable() { - if [[ -f "$config.bak" ]]; then - mv "$config.bak" $config - fi -} diff --git a/composer.json b/composer.json index cc326282..d34acd3f 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "require-dev": { "friendsofphp/php-cs-fixer": "*", "phpstan/phpstan": "*", - "phpunit/phpunit": ">=6.0.0 <8" + "phpunit/phpunit": ">=6.0.0", + "yoast/phpunit-polyfills": "^0.1.0" }, "autoload": { "psr-4": { diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 317ac166..21eab650 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -19,10 +19,10 @@ use Assert\Tests\Fixtures\CustomAssertion; use Assert\Tests\Fixtures\OneCountable; use PDO; -use PHPUnit\Framework\TestCase; use ResourceBundle; use SimpleXMLElement; use stdClass; +use Yoast\PHPUnitPolyfills\TestCases\TestCase; class AssertTest extends TestCase { @@ -40,13 +40,13 @@ public static function dataInvalidFloat() /** * @dataProvider dataInvalidFloat - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_FLOAT * * @param mixed $nonFloat */ public function testInvalidFloat($nonFloat) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_FLOAT); Assertion::float($nonFloat); } @@ -72,13 +72,13 @@ public static function dataInvalidInteger() /** * @dataProvider dataInvalidInteger - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_INTEGER * * @param mixed $nonInteger */ public function testInvalidInteger($nonInteger) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_INTEGER); Assertion::integer($nonInteger); } @@ -150,13 +150,13 @@ public static function dataInvalidIntegerish() /** * @dataProvider dataInvalidIntegerish - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_INTEGERISH * * @param mixed $nonInteger */ public function testInvalidIntegerish($nonInteger) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_INTEGERISH); Assertion::integerish($nonInteger); } @@ -166,21 +166,17 @@ public function testValidBoolean() $this->assertTrue(Assertion::boolean(false)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_BOOLEAN - */ public function testInvalidBoolean() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_BOOLEAN); Assertion::boolean(1); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_SCALAR - */ public function testInvalidScalar() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_SCALAR); Assertion::scalar(new stdClass()); } @@ -205,13 +201,13 @@ public static function dataInvalidNotEmpty() /** * @dataProvider dataInvalidNotEmpty - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::VALUE_EMPTY * * @param mixed $value */ public function testInvalidNotEmpty($value) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::VALUE_EMPTY); Assertion::notEmpty($value); } @@ -244,13 +240,13 @@ public static function dataInvalidEmpty() /** * @dataProvider dataInvalidEmpty - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::VALUE_NOT_EMPTY * * @param mixed $value */ public function testInvalidEmpty($value) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::VALUE_NOT_EMPTY); Assertion::noContent($value); } @@ -268,13 +264,13 @@ public static function dataInvalidNull() /** * @dataProvider dataInvalidNull - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::VALUE_NOT_NULL * * @param mixed $value */ public function testInvalidNull($value) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::VALUE_NOT_NULL); Assertion::null($value); } @@ -292,12 +288,10 @@ public function testNotNull() $this->assertTrue(Assertion::notNull(false)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::VALUE_NULL - */ public function testInvalidNotNull() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::VALUE_NULL); Assertion::notNull(null); } @@ -309,13 +303,13 @@ public function testString() /** * @dataProvider dataInvalidString - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_STRING * * @param mixed $invalidString */ public function testInvalidString($invalidString) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_STRING); Assertion::string($invalidString); } @@ -336,21 +330,17 @@ public function testValidRegex() $this->assertTrue(Assertion::regex('some string', '/.*/')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_REGEX - */ public function testInvalidRegex() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_REGEX); Assertion::regex('foo', '(bar)'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_STRING - */ public function testInvalidRegexValueNotString() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_STRING); Assertion::regex(['foo'], '(bar)'); } @@ -359,21 +349,17 @@ public function testValidNotRegex() $this->assertTrue(Assertion::notRegex('some string', '/[0-9]+/')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_NOT_REGEX - */ public function testInvalidNotRegex() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_NOT_REGEX); Assertion::notRegex('some string', '/.*/'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_MIN_LENGTH - */ public function testInvalidMinLength() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_MIN_LENGTH); Assertion::minLength('foo', 4); } @@ -386,12 +372,10 @@ public function testValidMinLength() $this->assertTrue(Assertion::minLength('址址', 2)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_MAX_LENGTH - */ public function testInvalidMaxLength() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_MAX_LENGTH); Assertion::maxLength('foo', 2); } @@ -403,21 +387,17 @@ public function testValidMaxLength() $this->assertTrue(Assertion::maxLength('址址', 2)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_MIN_LENGTH - */ public function testInvalidBetweenLengthMin() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_MIN_LENGTH); Assertion::betweenLength('foo', 4, 100); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_MAX_LENGTH - */ public function testInvalidBetweenLengthMax() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_MAX_LENGTH); Assertion::betweenLength('foo', 0, 2); } @@ -427,21 +407,17 @@ public function testValidBetweenLength() $this->assertTrue(Assertion::betweenLength('址址', 2, 2)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_STRING_START - */ public function testInvalidStartsWith() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_STRING_START); Assertion::startsWith('foo', 'bar'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_STRING_START - */ public function testInvalidStartsWithDueToWrongEncoding() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_STRING_START); Assertion::startsWith('址', '址址', null, null, 'ASCII'); } @@ -453,21 +429,17 @@ public function testValidStartsWith() $this->assertTrue(Assertion::startsWith('址foo', '址')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_STRING_END - */ public function testInvalidEndsWith() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_STRING_END); Assertion::endsWith('foo', 'bar'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_STRING_END - */ public function testInvalidEndsWithDueToWrongEncoding() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_STRING_END); Assertion::endsWith('址', '址址', null, null, 'ASCII'); } @@ -479,12 +451,10 @@ public function testValidEndsWith() $this->assertTrue(Assertion::endsWith('foo址', '址')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_STRING_CONTAINS - */ public function testInvalidContains() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_STRING_CONTAINS); Assertion::contains('foo', 'bar'); } @@ -494,12 +464,10 @@ public function testValidContains() $this->assertTrue(Assertion::contains('foo', 'oo')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_STRING_NOT_CONTAINS - */ public function testInvalidNotContains() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_STRING_NOT_CONTAINS); Assertion::notContains('foo', 'o'); } @@ -509,12 +477,10 @@ public function testValidNotContains() $this->assertTrue(Assertion::notContains('foo', 'p')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_CHOICE - */ public function testInvalidChoice() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_CHOICE); Assertion::choice('foo', ['bar', 'baz']); } @@ -523,12 +489,10 @@ public function testValidChoice() $this->assertTrue(Assertion::choice('foo', ['foo'])); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_CHOICE - */ public function testInvalidInArray() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_CHOICE); Assertion::inArray('bar', ['baz']); } @@ -537,12 +501,10 @@ public function testValidInArray() $this->assertTrue(Assertion::inArray('foo', ['foo'])); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_NUMERIC - */ public function testInvalidNumeric() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_NUMERIC); Assertion::numeric('foo'); } @@ -568,13 +530,13 @@ public static function dataInvalidArray() /** * @dataProvider dataInvalidArray - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_ARRAY * * @param mixed $value */ public function testInvalidArray($value) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_ARRAY); Assertion::isArray($value); } @@ -585,12 +547,10 @@ public function testValidArray() $this->assertTrue(Assertion::isArray([[], []])); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_KEY_EXISTS - */ public function testInvalidKeyExists() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_KEY_EXISTS); Assertion::keyExists(['foo' => 'bar'], 'baz'); } @@ -599,12 +559,10 @@ public function testValidKeyExists() $this->assertTrue(Assertion::keyExists(['foo' => 'bar'], 'foo')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_KEY_NOT_EXISTS - */ public function testInvalidKeyNotExists() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_KEY_NOT_EXISTS); Assertion::keyNotExists(['foo' => 'bar'], 'foo'); } @@ -629,11 +587,11 @@ public static function dataInvalidUniqueValues() /** * @dataProvider dataInvalidUniqueValues - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_UNIQUE_VALUES */ public function testInvalidUniqueValues($array) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_UNIQUE_VALUES); Assertion::uniqueValues($array, 'quux'); } @@ -673,13 +631,13 @@ public static function dataInvalidNotBlank() /** * @dataProvider dataInvalidNotBlank - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_NOT_BLANK * * @param mixed $notBlank */ public function testInvalidNotBlank($notBlank) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_NOT_BLANK); Assertion::notBlank($notBlank); } @@ -688,12 +646,10 @@ public function testValidNotBlank() $this->assertTrue(Assertion::notBlank('foo')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_NOT_INSTANCE_OF - */ public function testInvalidNotInstanceOf() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_NOT_INSTANCE_OF); Assertion::notIsInstanceOf(new stdClass(), stdClass::class); } @@ -702,12 +658,10 @@ public function testValidNotIsInstanceOf() $this->assertTrue(Assertion::notIsInstanceOf(new stdClass(), PDO::class)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_INSTANCE_OF - */ public function testInvalidInstanceOf() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_INSTANCE_OF); Assertion::isInstanceOf(new stdClass(), PDO::class); } @@ -716,12 +670,10 @@ public function testValidInstanceOf() $this->assertTrue(Assertion::isInstanceOf(new stdClass(), stdClass::class)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_SUBCLASS_OF - */ public function testInvalidSubclassOf() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_SUBCLASS_OF); Assertion::subclassOf(new stdClass(), PDO::class); } @@ -730,12 +682,10 @@ public function testValidSubclassOf() $this->assertTrue(Assertion::subclassOf(new Fixtures\ChildStdClass(), stdClass::class)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_RANGE - */ public function testInvalidRange() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_RANGE); Assertion::range(1, 2, 3); Assertion::range(1.5, 2, 3); } @@ -748,12 +698,10 @@ public function testValidRange() $this->assertTrue(Assertion::range(2.5, 2.25, 2.75)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_EMAIL - */ public function testInvalidEmail() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_EMAIL); Assertion::email('foo'); } @@ -764,13 +712,13 @@ public function testValidEmail() /** * @dataProvider dataInvalidUrl - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_URL * * @param string $url */ public function testInvalidUrl($url) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_URL); Assertion::url($url); } @@ -881,12 +829,10 @@ public static function dataValidUrl() ]; } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_DIGIT - */ public function testInvalidDigit() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_DIGIT); Assertion::digit(-1); } @@ -905,12 +851,10 @@ public function testValidAlnum() $this->assertTrue(Assertion::alnum('a1b2c3')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_ALNUM - */ public function testInvalidAlnum() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_ALNUM); Assertion::alnum('1a'); } @@ -919,12 +863,10 @@ public function testValidTrue() $this->assertTrue(Assertion::true(1 == 1)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_TRUE - */ public function testInvalidTrue() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_TRUE); Assertion::true(false); } @@ -933,21 +875,17 @@ public function testValidFalse() $this->assertTrue(Assertion::false(1 == 0)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_FALSE - */ public function testInvalidFalse() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_FALSE); Assertion::false(true); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_CLASS - */ public function testInvalidClass() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_CLASS); Assertion::classExists(\Foo::class); } @@ -956,12 +894,10 @@ public function testValidClass() $this->assertTrue(Assertion::classExists(\Exception::class)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_SAME - */ public function testSame() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_SAME); $this->assertTrue(Assertion::same(1, 1)); $this->assertTrue(Assertion::same('foo', 'foo')); $this->assertTrue(Assertion::same($obj = new stdClass(), $obj)); @@ -969,12 +905,10 @@ public function testSame() Assertion::same(new stdClass(), new stdClass()); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_EQ - */ public function testEq() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_EQ); $this->assertTrue(Assertion::eq(1, '1')); $this->assertTrue(Assertion::eq('foo', true)); $this->assertTrue(Assertion::eq($obj = new stdClass(), $obj)); @@ -982,36 +916,30 @@ public function testEq() Assertion::eq('2', 1); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_NOT_EQ - */ public function testNotEq() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_NOT_EQ); $this->assertTrue(Assertion::notEq('1', false)); $this->assertTrue(Assertion::notEq(new stdClass(), [])); Assertion::notEq('1', 1); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_NOT_SAME - */ public function testNotSame() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_NOT_SAME); $this->assertTrue(Assertion::notSame('1', 2)); $this->assertTrue(Assertion::notSame(new stdClass(), [])); Assertion::notSame(1, 1); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_VALUE_IN_ARRAY - */ public function testNotInArray() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_VALUE_IN_ARRAY); $this->assertTrue(Assertion::notInArray(6, \range(1, 5))); $this->assertTrue(Assertion::notInArray('a', \range('b', 'z'))); @@ -1027,15 +955,15 @@ public function testMin() /** * @dataProvider dataInvalidMin - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_MIN - * @expectedExceptionMessageRegExp /Number "(0\.5|0)" was expected to be at least "(1|2\.5)"/ * * @param float|int $value * @param float|int $min */ public function testInvalidMin($value, $min) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_MIN); + $this->expectExceptionMessageMatches('/Number "(0\.5|0)" was expected to be at least "(1|2\.5)"/'); Assertion::min($value, $min); } @@ -1056,15 +984,15 @@ public function testMax() /** * @dataProvider dataInvalidMax - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_MAX - * @expectedExceptionMessageRegExp /Number "(2.5|2)" was expected to be at most "(1|0\.5)"/ * * @param float|int $value * @param float|int $min */ public function testInvalidMax($value, $min) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_MAX); + $this->expectExceptionMessageMatches('/Number "(2.5|2)" was expected to be at most "(1|0\.5)"/'); Assertion::max($value, $min); } @@ -1082,12 +1010,10 @@ public function testNullOr() $this->assertTrue(Assertion::nullOrMax(null, 2)); } - /** - * @expectedException \BadMethodCallException - * @expectedExceptionMessage Missing the first argument. - */ public function testNullOrWithNoValueThrows() { + $this->expectException('BadMethodCallException'); + $this->expectExceptionMessage('Missing the first argument.'); Assertion::nullOrMax(); } @@ -1116,21 +1042,17 @@ public function testLengthUtf8Characters($value, $expected) $this->assertTrue(Assertion::length($value, $expected)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_LENGTH - */ public function testLengthFailed() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_LENGTH); Assertion::length('asdf', 3); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_LENGTH - */ public function testLengthFailedForWrongEncoding() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_LENGTH); Assertion::length('址', 1, null, null, 'ASCII'); } @@ -1144,83 +1066,67 @@ public function testFile() $this->assertTrue(Assertion::file(__FILE__)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::VALUE_EMPTY - */ public function testFileWithEmptyFilename() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::VALUE_EMPTY); Assertion::file(''); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_FILE - */ public function testFileDoesNotExists() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_FILE); Assertion::file(__DIR__.'/does-not-exists'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_DIRECTORY - */ public function testDirectory() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_DIRECTORY); $this->assertTrue(Assertion::directory(__DIR__)); Assertion::directory(__DIR__.'/does-not-exist'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_READABLE - */ public function testReadable() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_READABLE); $this->assertTrue(Assertion::readable(__FILE__)); Assertion::readable(__DIR__.'/does-not-exist'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_WRITEABLE - */ public function testWriteable() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_WRITEABLE); $this->assertTrue(Assertion::writeable(\sys_get_temp_dir())); Assertion::writeable(__DIR__.'/does-not-exist'); } - /** - * @expectedException \BadMethodCallException - * @expectedExceptionMessage No assertion - */ public function testFailedNullOrMethodCall() { + $this->expectException('BadMethodCallException'); + $this->expectExceptionMessage('No assertion'); Assertion::nullOrAssertionDoesNotExist(''); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INTERFACE_NOT_IMPLEMENTED - */ public function testImplementsInterface() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INTERFACE_NOT_IMPLEMENTED); $this->assertTrue(Assertion::implementsInterface(\ArrayIterator::class, \Traversable::class)); Assertion::implementsInterface(\Exception::class, \Traversable::class); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INTERFACE_NOT_IMPLEMENTED - */ public function testImplementsInterfaceWithClassObject() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INTERFACE_NOT_IMPLEMENTED); $class = new \ArrayObject(); $this->assertTrue(Assertion::implementsInterface($class, \Traversable::class)); @@ -1228,13 +1134,11 @@ public function testImplementsInterfaceWithClassObject() Assertion::implementsInterface($class, \SplObserver::class); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INTERFACE_NOT_IMPLEMENTED - * @expectedExceptionMessage Class "not_a_class" failed reflection - */ public function testImplementsInterfaceThrowsExceptionForInvalidSubject() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INTERFACE_NOT_IMPLEMENTED); + $this->expectExceptionMessage('Class "not_a_class" failed reflection'); $this->assertTrue(Assertion::implementsInterface('not_a_class', \Traversable::class)); Assertion::implementsInterface(\Exception::class, \Traversable::class); @@ -1262,13 +1166,13 @@ public static function isJsonStringDataprovider() /** * @dataProvider isJsonStringInvalidStringDataprovider - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_JSON_STRING * * @param mixed $invalidString */ public function testIsJsonStringExpectingException($invalidString) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_JSON_STRING); Assertion::isJsonString($invalidString); } @@ -1292,13 +1196,13 @@ public function testValidUuids($uuid) /** * @dataProvider providesInvalidUuids - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_UUID * * @param string $uuid */ public function testInvalidUuids($uuid) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_UUID); Assertion::uuid($uuid); } @@ -1338,13 +1242,13 @@ public function testValidE164s($e164) /** * @dataProvider providesInvalidE164s - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_E164 * * @param string $e164 */ public function testInvalidE164s($e164) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_E164); Assertion::e164($e164); } @@ -1370,21 +1274,17 @@ public function testValidNotEmptyKey() $this->assertTrue(Assertion::notEmptyKey(['keyExists' => 'notEmpty'], 'keyExists')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::VALUE_EMPTY - */ public function testInvalidNotEmptyKeyEmptyKey() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::VALUE_EMPTY); Assertion::notEmptyKey(['keyExists' => ''], 'keyExists'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_KEY_ISSET - */ public function testInvalidNotEmptyKeyKeyNotExists() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_KEY_ISSET); Assertion::notEmptyKey(['key' => 'notEmpty'], 'keyNotExists'); } @@ -1393,12 +1293,10 @@ public function testAllWithSimpleAssertion() $this->assertTrue(Assertion::allTrue([true, true])); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_TRUE - */ public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_TRUE); Assertion::allTrue([true, false]); } @@ -1407,20 +1305,16 @@ public function testAllWithComplexAssertion() $this->assertTrue(Assertion::allIsInstanceOf([new stdClass(), new stdClass()], stdClass::class)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_INSTANCE_OF - */ public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_INSTANCE_OF); Assertion::allIsInstanceOf([new stdClass(), new stdClass()], PDO::class, 'Assertion failed', 'foos'); } - /** - * @expectedException \BadMethodCallException - */ public function testAllWithNoValueThrows() { + $this->expectException('BadMethodCallException'); Assertion::allTrue(); } @@ -1447,15 +1341,15 @@ public static function dataInvalidCount() /** * @dataProvider dataInvalidCount - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_COUNT - * @expectedExceptionMessageRegExp /List does not contain exactly \d+ elements \(\d+ given\)./ * * @param mixed $countable * @param int $count */ public function testInvalidCount($countable, $count) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_COUNT); + $this->expectExceptionMessageMatches('/List does not contain exactly \d+ elements \(\d+ given\)./'); Assertion::count($countable, $count); } @@ -1480,15 +1374,15 @@ public static function dataInvalidMinCount() /** * @dataProvider dataInvalidMinCount - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_MIN_COUNT - * @expectedExceptionMessageRegExp /List should have at least \d+ elements, but has \d elements./ * * @param mixed $countable * @param int $count */ public function testInvalidMinCount($countable, $count) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_MIN_COUNT); + $this->expectExceptionMessageMatches('/List should have at least \d+ elements, but has \d elements./'); Assertion::minCount($countable, $count); } @@ -1513,12 +1407,12 @@ public static function dataInvalidMaxCount() /** * @dataProvider dataInvalidMaxCount - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_MAX_COUNT - * @expectedExceptionMessageRegExp /List should have at most \d+ elements, but has \d elements./ */ public function testInvalidMaxCount($countable, $count) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_MAX_COUNT); + $this->expectExceptionMessageMatches('/List should have at most \d+ elements, but has \d elements./'); Assertion::maxCount($countable, $count); } @@ -1534,23 +1428,21 @@ public function testChoicesNotEmpty() /** * @dataProvider invalidChoicesProvider - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::VALUE_EMPTY * * @param mixed $values * @param mixed $choices */ public function testChoicesNotEmptyExpectingExceptionEmptyValue($values, $choices) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::VALUE_EMPTY); Assertion::choicesNotEmpty($values, $choices); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_KEY_ISSET - */ public function testChoicesNotEmptyExpectingExceptionInvalidKeyIsset() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_KEY_ISSET); Assertion::choicesNotEmpty(['tux' => ''], ['invalidChoice']); } @@ -1567,12 +1459,10 @@ public function testIsObject() $this->assertTrue(Assertion::isObject(new stdClass())); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_OBJECT - */ public function testIsObjectExpectingException() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_OBJECT); Assertion::isObject('notAnObject'); } @@ -1581,12 +1471,10 @@ public function testMethodExists() $this->assertTrue(Assertion::methodExists('methodExists', new Assertion())); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_METHOD - */ public function testMethodExistsFailure() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_METHOD); Assertion::methodExists('methodNotExists', new Assertion()); } @@ -1624,14 +1512,14 @@ public function invalidLessProvider() /** * @dataProvider invalidLessProvider - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_LESS * * @param mixed $value * @param mixed $limit */ public function testLessThanThrowsException($value, $limit) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_LESS); Assertion::lessThan($value, $limit); } @@ -1657,14 +1545,14 @@ public function invalidLessOrEqualProvider() /** * @dataProvider invalidLessOrEqualProvider - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_LESS_OR_EQUAL * * @param mixed $value * @param mixed $limit */ public function testLessOrEqualThanThrowsException($value, $limit) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_LESS_OR_EQUAL); Assertion::lessOrEqualThan($value, $limit); } @@ -1711,14 +1599,14 @@ public function validDateProvider() /** * @dataProvider invalidGreaterProvider - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_GREATER * * @param mixed $value * @param mixed $limit */ public function testGreaterThanThrowsException($value, $limit) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_GREATER); Assertion::greaterThan($value, $limit); } @@ -1744,27 +1632,27 @@ public function invalidGreaterOrEqualProvider() /** * @dataProvider invalidGreaterOrEqualProvider - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_GREATER_OR_EQUAL * * @param mixed $value * @param mixed $limit */ public function testGreaterOrEqualThanThrowsException($value, $limit) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_GREATER_OR_EQUAL); Assertion::greaterOrEqualThan($value, $limit); } /** * @dataProvider invalidDateProvider - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_DATE * * @param string $value * @param string $format */ public function testInvalidDate($value, $format) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_DATE); Assertion::date($value, $format); } @@ -1782,12 +1670,10 @@ public function testValidTraversable() $this->assertTrue(Assertion::isTraversable(new \ArrayObject())); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_TRAVERSABLE - */ public function testInvalidTraversable() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_TRAVERSABLE); Assertion::isTraversable('not traversable'); } @@ -1797,12 +1683,10 @@ public function testValidCountable() $this->assertTrue(Assertion::isCountable(new \ArrayObject())); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_COUNTABLE - */ public function testInvalidCountable() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_COUNTABLE); Assertion::isCountable('not countable'); } @@ -1811,21 +1695,17 @@ public function testValidArrayAccessible() $this->assertTrue(Assertion::isArrayAccessible(new \ArrayObject())); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_ARRAY_ACCESSIBLE - */ public function testInvalidArrayAccessible() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_ARRAY_ACCESSIBLE); Assertion::isArrayAccessible('not array accessible'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_CALLABLE - */ public function testInvalidCallable() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_CALLABLE); Assertion::isCallable('nonExistingFunction'); } @@ -1843,12 +1723,10 @@ function () { ); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_SATISFY - */ public function testInvalidSatisfy() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_SATISFY); Assertion::satisfy( null, function ($value) { @@ -1904,14 +1782,14 @@ public function validIpProvider() /** * @dataProvider invalidIpProvider - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_IP * * @param string $value * @param int|null $flag */ public function testInvalidIp($value, $flag = null) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_IP); Assertion::ip($value, $flag); } @@ -1933,12 +1811,10 @@ public function testValidIpv4() $this->assertTrue(Assertion::ipv4('109.188.127.26')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_IP - */ public function testInvalidIpv4() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_IP); Assertion::ipv4('2001:db8:85a3:8d3:1319:8a2e:370:7348'); } @@ -1947,21 +1823,17 @@ public function testValidIpv6() $this->assertTrue(Assertion::ipv6('2001:db8:85a3:8d3:1319:8a2e:370:7348')); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_IP - */ public function testInvalidIpv6() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_IP); Assertion::ipv6('109.188.127.26'); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_INTERFACE - */ public function testInvalidInterfaceExists() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_INTERFACE); Assertion::interfaceExists('Foo'); } @@ -1972,8 +1844,6 @@ public function testValidInterfaceExists() /** * @dataProvider providerInvalidBetween - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_BETWEEN * * @param mixed $value * @param mixed $lowerLimit @@ -1981,6 +1851,8 @@ public function testValidInterfaceExists() */ public function testInvalidBetween($value, $lowerLimit, $upperLimit) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_BETWEEN); Assertion::between($value, $lowerLimit, $upperLimit); } @@ -2028,8 +1900,6 @@ public function providerValidBetween() /** * @dataProvider providerInvalidBetweenExclusive - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_BETWEEN_EXCLUSIVE * * @param mixed $value * @param mixed $lowerLimit @@ -2037,6 +1907,8 @@ public function providerValidBetween() */ public function testInvalidBetweenExclusive($value, $lowerLimit, $upperLimit) { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_BETWEEN_EXCLUSIVE); Assertion::betweenExclusive($value, $lowerLimit, $upperLimit); } @@ -2079,25 +1951,21 @@ public function providerValidBetweenExclusive() ]; } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_FLOAT - * @expectedExceptionMessage 1234567... - */ public function testStringifyTruncatesStringValuesLongerThan100CharactersAppropriately() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_FLOAT); + $this->expectExceptionMessage('1234567...'); $string = \str_repeat('1234567890', 11); $this->assertTrue(Assertion::float($string)); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionCode \Assert\Assertion::INVALID_FLOAT - * @expectedExceptionMessage stream - */ public function testStringifyReportsResourceType() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_FLOAT); + $this->expectExceptionMessage('stream'); $this->assertTrue(Assertion::float(\fopen('php://stdin', 'rb'))); } @@ -2106,11 +1974,9 @@ public function testExtensionLoaded() $this->assertTrue(Assertion::extensionLoaded('date')); } - /** - * @expectedException \Assert\InvalidArgumentException - */ public function testExtensionNotLoaded() { + $this->expectException('Assert\InvalidArgumentException'); Assertion::extensionLoaded('NOT_LOADED'); } @@ -2119,11 +1985,9 @@ public function testValidConstant() $this->assertTrue(Assertion::defined('PHP_VERSION')); } - /** - * @expectedException \Assert\InvalidArgumentException - */ public function testInvalidConstant() { + $this->expectException('Assert\InvalidArgumentException'); Assertion::defined('NOT_A_CONSTANT'); } @@ -2132,19 +1996,15 @@ public function testValidVersion() $this->assertTrue(Assertion::version('1.0.0', '<', '2.0.0')); } - /** - * @expectedException \Assert\InvalidArgumentException - */ public function testInvalidVersion() { + $this->expectException('Assert\InvalidArgumentException'); Assertion::version('1.0.0', 'eq', '2.0.0'); } - /** - * @expectedException \Assert\InvalidArgumentException - */ public function testInvalidVersionOperator() { + $this->expectException('Assert\InvalidArgumentException'); Assertion::version('1.0.0', null, '2.0.0'); } @@ -2153,11 +2013,9 @@ public function testValidPhpVersion() $this->assertTrue(Assertion::phpVersion('>', '4.0.0')); } - /** - * @expectedException \Assert\InvalidArgumentException - */ public function testInvalidPhpVersion() { + $this->expectException('Assert\InvalidArgumentException'); Assertion::phpVersion('<', '5.0.0'); } @@ -2166,11 +2024,9 @@ public function testValidExtensionVersion() $this->assertTrue(Assertion::extensionVersion('json', '>', '1.0.0')); } - /** - * @expectedException \Assert\InvalidArgumentException - */ public function testInvalidExtensionVersion() { + $this->expectException('Assert\InvalidArgumentException'); Assertion::extensionVersion('json', '<', '0.1.0'); } @@ -2180,11 +2036,9 @@ public function testObjectOrClass() self::assertTrue(Assertion::objectOrClass(stdClass::class)); } - /** - * @expectedException \Assert\InvalidArgumentException - */ public function testNotObjectOrClass() { + $this->expectException('Assert\InvalidArgumentException'); Assertion::objectOrClass('InvalidClassName'); } @@ -2193,12 +2047,10 @@ public function testPropertyExists() self::assertTrue(Assertion::propertyExists(new \Exception(), 'message')); } - /** - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionCode \Assert\Assertion::INVALID_PROPERTY - */ public function testInvalidPropertyExists() { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_PROPERTY); Assertion::propertyExists(new \Exception(), 'invalidProperty'); } @@ -2217,13 +2069,13 @@ public function invalidPropertiesExistProvider() /** * @dataProvider invalidPropertiesExistProvider - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionCode \Assert\Assertion::INVALID_PROPERTY * * @param array $properties */ public function testInvalidPropertiesExist($properties) { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_PROPERTY); Assertion::propertiesExist(new \Exception(), $properties); } @@ -2232,11 +2084,9 @@ public function testIsResource() self::assertTrue(Assertion::isResource(\fopen('php://memory', 'w'))); } - /** - * @expectedException \Assert\InvalidArgumentException - */ public function testIsNotResource() { + $this->expectException('Assert\InvalidArgumentException'); Assertion::isResource(new stdClass()); } @@ -2247,12 +2097,10 @@ public function testBase64() $this->assertTrue(Assertion::base64($base64String)); } - /** - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionCode \Assert\Assertion::INVALID_BASE64 - */ public function testNotBase64() { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_BASE64); Assertion::base64('wrong-content'); } @@ -2288,23 +2136,21 @@ public function testEqArraySubsetValid() /** * @dataProvider invalidEqArraySubsetProvider - * - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionCode \Assert\Assertion::INVALID_ARRAY */ public function testEqArraySubsetInvalid($value, $value2) { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_ARRAY); Assertion::eqArraySubset($value, $value2); } /** * @dataProvider invalidEqArraySubsetProvider - * - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionCode \Assert\Assertion::INVALID_EQ */ public function testEqArraySubsetMismatchingSubset() { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_EQ); Assertion::eqArraySubset( [ 'a' => 'b', diff --git a/tests/Assert/Tests/AssertionChainFunctionsTest.php b/tests/Assert/Tests/AssertionChainFunctionsTest.php index e971db4d..b24ef24f 100644 --- a/tests/Assert/Tests/AssertionChainFunctionsTest.php +++ b/tests/Assert/Tests/AssertionChainFunctionsTest.php @@ -32,12 +32,10 @@ public function testThatAssertionChainFunctionsShiftsArgumentsBy1() $this->assertInstanceOf(AssertionChain::class, \Assert\that(10)->eq(10)); } - /** - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionMessage Not Null and such - */ public function testThatAssertionChainFunctionsKnowsDefaultErrorMessage() { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionMessage('Not Null and such'); \Assert\that(null, 'Not Null and such')->notEmpty(); } @@ -61,12 +59,10 @@ public function testAssertionChainFunctionsNullOrShortcut() $this->assertInstanceOf(AssertionChain::class, \Assert\thatNullOr(null)->integer()->eq(10)); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage Assertion 'unknownAssertion' does not exist. - */ public function testThatAssertionChainFunctionsThrowsExceptionForUnknownAssertion() { + $this->expectException('RuntimeException'); + $this->expectExceptionMessage('Assertion \'unknownAssertion\' does not exist.'); \Assert\that(null)->unknownAssertion(); } diff --git a/tests/Assert/Tests/AssertionChainTest.php b/tests/Assert/Tests/AssertionChainTest.php index bf54c61b..fd865211 100644 --- a/tests/Assert/Tests/AssertionChainTest.php +++ b/tests/Assert/Tests/AssertionChainTest.php @@ -31,12 +31,10 @@ public function testThatAssertionChainShiftsArgumentsBy1() $this->assertInstanceOf(AssertionChain::class, Assert::that(10)->eq(10)); } - /** - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionMessage Not Null and such - */ public function testThatAssertionChainKnowsDefaultErrorMessage() { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionMessage('Not Null and such'); Assert::that(null, 'Not Null and such')->notEmpty(); } @@ -60,12 +58,10 @@ public function testAssertionChainNullOrShortcut() $this->assertInstanceOf(AssertionChain::class, Assert::thatNullOr(null)->integer()->eq(10)); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessage Assertion 'unknownAssertion' does not exist. - */ public function testThatAssertionChainThrowsExceptionForUnknownAssertion() { + $this->expectException('RuntimeException'); + $this->expectExceptionMessage('Assertion \'unknownAssertion\' does not exist.'); Assert::that(null)->unknownAssertion(); } @@ -95,12 +91,12 @@ public function testThatCustomAssertionClassIsUsedWhenSet() /** * @dataProvider provideDataToTestThatSetAssertionClassNameWillNotAcceptInvalidAssertionClasses - * @expectedException \LogicException * * @param mixed $assertionClassName */ public function testThatSetAssertionClassNameWillNotAcceptInvalidAssertionClasses($assertionClassName) { + $this->expectException('LogicException'); $lazyAssertion = new AssertionChain('foo'); $lazyAssertion->setAssertionClassName($assertionClassName); diff --git a/tests/Assert/Tests/AssertionExceptionCallbackTest.php b/tests/Assert/Tests/AssertionExceptionCallbackTest.php index 0507a6cf..9c2cc8dd 100644 --- a/tests/Assert/Tests/AssertionExceptionCallbackTest.php +++ b/tests/Assert/Tests/AssertionExceptionCallbackTest.php @@ -19,12 +19,10 @@ class AssertionExceptionCallbackTest extends TestCase { - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionMessage The assertion Assert\Assertion::string() failed for 3.1415926535898 - */ public function testMessageUsingCallbackForString() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionMessage('The assertion Assert\Assertion::string() failed for 3.1415926535898'); Assertion::string( M_PI, function (array $parameters) { @@ -38,12 +36,10 @@ function (array $parameters) { ); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionMessage The assertion Assert\Assertion::string() failed for 3.1415926535898 - */ public function testMessageUsingCallbackForRegexFailingAtTheStringAssertion() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionMessage('The assertion Assert\Assertion::string() failed for 3.1415926535898'); Assertion::regex( M_PI, '`[A-Z]++`', @@ -58,12 +54,10 @@ function (array $parameters) { ); } - /** - * @expectedException \Assert\AssertionFailedException - * @expectedExceptionMessage The assertion Assert\Assertion::regex() failed for 3.1415926535898 against the pattern `^[0-9]++$` - */ public function testMessageUsingCallbackForRegexFailingAtTheRegexAssertion() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionMessage('The assertion Assert\Assertion::regex() failed for 3.1415926535898 against the pattern `^[0-9]++$`'); Assertion::regex( (string)M_PI, '`^[0-9]++$`', diff --git a/tests/Assert/Tests/CustomAssertionClassTest.php b/tests/Assert/Tests/CustomAssertionClassTest.php index b7a5759a..c6916207 100644 --- a/tests/Assert/Tests/CustomAssertionClassTest.php +++ b/tests/Assert/Tests/CustomAssertionClassTest.php @@ -20,24 +20,21 @@ class CustomAssertionClassTest extends TestCase { - protected function setUp() + /** @before */ + protected function setUpBefore() { CustomAssertion::clearCalls(); } - /** - * @expectedException \Assert\Tests\Fixtures\CustomException - */ public function testThatCustomAssertionUsesCustomException() { + $this->expectException('Assert\Tests\Fixtures\CustomException'); CustomAssertion::integer('foo'); } - /** - * @expectedException \Assert\Tests\Fixtures\CustomException - */ public function testThatCustomAssertionsUsesCustomExceptionForAssertionChains() { + $this->expectException('Assert\Tests\Fixtures\CustomException'); $string = 's'.\uniqid(); Fixtures\CustomAssert::that($string)->string(); $this->assertSame([['string', $string]], CustomAssertion::getCalls()); @@ -45,21 +42,17 @@ public function testThatCustomAssertionsUsesCustomExceptionForAssertionChains() Fixtures\CustomAssert::that($string)->integer(); } - /** - * @expectedException \Assert\Tests\Fixtures\CustomLazyAssertionException - */ public function testThatCustomAssertionsUsesCustomAssertionExceptionForLazyAssertionChains() { + $this->expectException('Assert\Tests\Fixtures\CustomLazyAssertionException'); Fixtures\CustomAssert::lazy() ->that('foo', 'foo')->integer() ->verifyNow(); } - /** - * @expectedException \Assert\Tests\Fixtures\CustomLazyAssertionException - */ public function testThatCustomAssertionsUsesCustomAssertionExceptionWhenFirstAssertionDoesNotFail() { + $this->expectException('Assert\Tests\Fixtures\CustomLazyAssertionException'); Fixtures\CustomAssert::lazy() ->that('foo', 'foo')->string() ->that('bar', 'bar')->integer() @@ -88,11 +81,11 @@ public function testThatCustomLazyAssertionContainsOnlyCustomAssertionExceptions } /** - * @expectedException \Assert\Tests\Fixtures\CustomLazyAssertionException * @expectedExceptionMessageRegex /The following 4 assertions failed:\s+1) foo: Value "foo" is not an integer.\s+2) foo: Value "foo" is not an array.\s+3) bar: Value "123" expected to be string, type integer given.\s+4) bar: Value "123" is not an array./ */ public function testThatCustomAssertionsExceptionsForLazyAssertionChainsTryAllTheAssertionsPerChain() { + $this->expectException('Assert\Tests\Fixtures\CustomLazyAssertionException'); Fixtures\CustomAssert::lazy() ->that('foo', 'foo')->tryAll()->integer()->isArray() ->that(123, 'bar')->tryAll()->string()->isArray() @@ -100,11 +93,11 @@ public function testThatCustomAssertionsExceptionsForLazyAssertionChainsTryAllTh } /** - * @expectedException \Assert\Tests\Fixtures\CustomLazyAssertionException * @expectedExceptionMessageRegex /The following 4 assertions failed:\s+1) foo: Value "foo" is not an integer.\s+2) foo: Value "foo" is not an array.\s+3) bar: Value "123" expected to be string, type integer given.\s+4) bar: Value "123" is not an array./ */ public function testThatCustomAssertionsExceptionsForLazyAssertionChainsTryAllTheAssertions() { + $this->expectException('Assert\Tests\Fixtures\CustomLazyAssertionException'); Fixtures\CustomAssert::lazy() ->tryAll() ->that('foo', 'foo')->integer()->isArray() diff --git a/tests/Assert/Tests/LazyAssertionTest.php b/tests/Assert/Tests/LazyAssertionTest.php index 77b069e9..85aedc10 100644 --- a/tests/Assert/Tests/LazyAssertionTest.php +++ b/tests/Assert/Tests/LazyAssertionTest.php @@ -21,12 +21,10 @@ class LazyAssertionTest extends TestCase { - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 3 assertions failed: - */ public function testThatLazyAssertionsCollectsAllErrorsUntilAssertAll() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 3 assertions failed:'); Assert::lazy() ->that(10, 'foo')->string() ->that(null, 'bar')->notEmpty() @@ -34,12 +32,10 @@ public function testThatLazyAssertionsCollectsAllErrorsUntilAssertAll() ->verifyNow(); } - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 1 assertions failed: - */ public function testThatLazyAssertionsSkipsAssertionsOfCurrentChainAfterFailure() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 1 assertions failed:'); Assert::lazy() ->that(null, 'foo')->notEmpty()->string() ->verifyNow(); @@ -101,48 +97,40 @@ function (\Exception $ex) { } } - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 1 assertions failed: - */ public function testCallsToThatFollowingTryAllSkipAssertionsAfterFailure() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 1 assertions failed:'); Assert::lazy() ->that(10, 'foo')->tryAll()->integer() ->that(null, 'foo')->notEmpty()->string() ->verifyNow(); } - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 4 assertions failed: - */ public function testCallsToThatWithTryAllWithMultipleAssertionsAllGetReported() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 4 assertions failed:'); Assert::lazy() ->that(10, 'foo')->tryAll()->float()->greaterThan(100) ->that(null, 'foo')->tryAll()->notEmpty()->string() ->verifyNow(); } - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 4 assertions failed: - */ public function testCallsToTryAllOnLazyAlwaysReportAllGetReported() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 4 assertions failed:'); Assert::lazy()->tryAll() ->that(10, 'foo')->float()->greaterThan(100) ->that(null, 'foo')->notEmpty()->string() ->verifyNow(); } - /** - * @expectedException \Assert\Tests\Fixtures\CustomLazyAssertionException - * @expectedExceptionMessage The following 1 assertions failed: - */ public function testThatLazyAssertionThrowsCustomExceptionWhenSet() { + $this->expectException('Assert\Tests\Fixtures\CustomLazyAssertionException'); + $this->expectExceptionMessage('The following 1 assertions failed:'); $lazyAssertion = new LazyAssertion(); $lazyAssertion->setExceptionClass(Fixtures\CustomLazyAssertionException::class); @@ -153,34 +141,28 @@ public function testThatLazyAssertionThrowsCustomExceptionWhenSet() ); } - /** - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionMessage The following 4 assertions failed: - */ public function testLazyAssertionExceptionExtendsInvalidArgumentException() { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionMessage('The following 4 assertions failed:'); Assert::lazy()->tryAll() ->that(10, 'foo')->float()->greaterThan(100) ->that(null, 'foo')->notEmpty()->string() ->verifyNow(); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage stdClass is not (a subclass of) Assert\LazyAssertionException - */ public function testLazyAssertionThrowsExceptionWhenPassingInvalidClassToSetExceptionClass() { + $this->expectException('LogicException'); + $this->expectExceptionMessage('stdClass is not (a subclass of) Assert\LazyAssertionException'); $lazyAssertion = new LazyAssertion(); $lazyAssertion->setExceptionClass(\stdClass::class); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage stdClass is not (a subclass of) Assert\Assert - */ public function testLazyAssertionThrowsExceptionWhenPassingInvalidClassToSetAssertClass() { + $this->expectException('LogicException'); + $this->expectExceptionMessage('stdClass is not (a subclass of) Assert\Assert'); $lazyAssertion = new LazyAssertion(); $lazyAssertion->setAssertClass(\stdClass::class); } diff --git a/tests/Assert/Tests/LazyFunctionAssertionTest.php b/tests/Assert/Tests/LazyFunctionAssertionTest.php index dbf6cda9..359303be 100644 --- a/tests/Assert/Tests/LazyFunctionAssertionTest.php +++ b/tests/Assert/Tests/LazyFunctionAssertionTest.php @@ -21,12 +21,10 @@ class LazyFunctionAssertionTest extends TestCase { - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 3 assertions failed: - */ public function testThatLazyAssertionsCollectsAllErrorsUntilAssertAll() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 3 assertions failed:'); \Assert\lazy() ->that(10, 'foo')->string() ->that(null, 'bar')->notEmpty() @@ -34,12 +32,10 @@ public function testThatLazyAssertionsCollectsAllErrorsUntilAssertAll() ->verifyNow(); } - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 1 assertions failed: - */ public function testThatLazyAssertionsSkipsAssertionsOfCurrentChainAfterFailure() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 1 assertions failed:'); \Assert\lazy() ->that(null, 'foo')->notEmpty()->string() ->verifyNow(); @@ -101,48 +97,40 @@ function (\Exception $ex) { } } - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 1 assertions failed: - */ public function testCallsToThatFollowingTryAllSkipAssertionsAfterFailure() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 1 assertions failed:'); \Assert\lazy() ->that(10, 'foo')->tryAll()->integer() ->that(null, 'foo')->notEmpty()->string() ->verifyNow(); } - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 4 assertions failed: - */ public function testCallsToThatWithTryAllWithMultipleAssertionsAllGetReported() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 4 assertions failed:'); \Assert\lazy() ->that(10, 'foo')->tryAll()->float()->greaterThan(100) ->that(null, 'foo')->tryAll()->notEmpty()->string() ->verifyNow(); } - /** - * @expectedException \Assert\LazyAssertionException - * @expectedExceptionMessage The following 4 assertions failed: - */ public function testCallsToTryAllOnLazyAlwaysReportAllGetReported() { + $this->expectException('Assert\LazyAssertionException'); + $this->expectExceptionMessage('The following 4 assertions failed:'); \Assert\lazy()->tryAll() ->that(10, 'foo')->float()->greaterThan(100) ->that(null, 'foo')->notEmpty()->string() ->verifyNow(); } - /** - * @expectedException \Assert\Tests\Fixtures\CustomLazyAssertionException - * @expectedExceptionMessage The following 1 assertions failed: - */ public function testThatLazyAssertionThrowsCustomExceptionWhenSet() { + $this->expectException('Assert\Tests\Fixtures\CustomLazyAssertionException'); + $this->expectExceptionMessage('The following 1 assertions failed:'); $lazyAssertion = new LazyAssertion(); $lazyAssertion->setExceptionClass(CustomLazyAssertionException::class); @@ -153,12 +141,10 @@ public function testThatLazyAssertionThrowsCustomExceptionWhenSet() ); } - /** - * @expectedException \Assert\InvalidArgumentException - * @expectedExceptionMessage The following 4 assertions failed: - */ public function testLazyAssertionExceptionExtendsInvalidArgumentException() { + $this->expectException('Assert\InvalidArgumentException'); + $this->expectExceptionMessage('The following 4 assertions failed:'); \Assert\lazy()->tryAll() ->that(10, 'foo')->float()->greaterThan(100) ->that(null, 'foo')->notEmpty()->string() From 5367e3895976b49704ae671f75bc5f0ba1b986ab Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Fri, 13 Nov 2020 21:02:54 +0100 Subject: [PATCH 05/24] Add psalm assertions (#305) * Add Psalm assertions * Remove dodgy is_subclass_of Co-authored-by: Matthew Brown --- lib/Assert/Assertion.php | 214 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 9a292497..3a6b43ff 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -348,6 +348,13 @@ public static function eqArraySubset($value, $value2, $message = null, string $p * @param mixed $value * @param mixed $value2 * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-template ExpectedType + * @psalm-param ExpectedType $value2 + * @psalm-assert =ExpectedType $value + * + * @return bool * * @throws AssertionFailedException */ @@ -395,6 +402,13 @@ public static function notEq($value1, $value2, $message = null, string $property * @param mixed $value1 * @param mixed $value2 * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-template ExpectedType + * @psalm-param ExpectedType $value2 + * @psalm-assert !=ExpectedType $value + * + * @return bool * * @throws AssertionFailedException */ @@ -439,6 +453,11 @@ public static function notInArray($value, array $choices, $message = null, strin * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert int $value + * + * @return bool * * @throws AssertionFailedException */ @@ -461,6 +480,11 @@ public static function integer($value, $message = null, string $propertyPath = n * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert float $value + * + * @return bool * * @throws AssertionFailedException */ @@ -483,6 +507,11 @@ public static function float($value, $message = null, string $propertyPath = nul * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert =numeric $value + * + * @return bool * * @throws AssertionFailedException */ @@ -540,6 +569,11 @@ public static function integerish($value, $message = null, string $propertyPath * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert bool $value + * + * @return bool * * @throws AssertionFailedException */ @@ -562,6 +596,11 @@ public static function boolean($value, $message = null, string $propertyPath = n * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert scalar $value + * + * @return bool * * @throws AssertionFailedException */ @@ -584,6 +623,11 @@ public static function scalar($value, $message = null, string $propertyPath = nu * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert !empty $value + * + * @return bool * * @throws AssertionFailedException */ @@ -606,6 +650,11 @@ public static function notEmpty($value, $message = null, string $propertyPath = * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert empty $value + * + * @return bool * * @throws AssertionFailedException */ @@ -628,6 +677,11 @@ public static function noContent($value, $message = null, string $propertyPath = * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert null $value + * + * @return bool */ public static function null($value, $message = null, string $propertyPath = null): bool { @@ -648,6 +702,11 @@ public static function null($value, $message = null, string $propertyPath = null * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert !null $value + * + * @return bool * * @throws AssertionFailedException */ @@ -670,6 +729,9 @@ public static function notNull($value, $message = null, string $propertyPath = n * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert string $value * * @return bool * @@ -696,6 +758,11 @@ public static function string($value, $message = null, string $propertyPath = nu * @param mixed $value * @param string $pattern * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert =string $value + * + * @return bool * * @throws AssertionFailedException */ @@ -721,6 +788,9 @@ public static function regex($value, $pattern, $message = null, string $property * @param mixed $value * @param string $pattern * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert !=string $value * * @throws AssertionFailedException */ @@ -746,8 +816,13 @@ public static function notRegex($value, $pattern, $message = null, string $prope * @param mixed $value * @param int $length * @param string|callable|null $message + * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $value + * + * @return bool + * * @throws AssertionFailedException */ public static function length($value, $length, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool @@ -774,8 +849,13 @@ public static function length($value, $length, $message = null, string $property * @param mixed $value * @param int $minLength * @param string|callable|null $message + * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $value + * + * @return bool + * * @throws AssertionFailedException */ public static function minLength($value, $minLength, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool @@ -802,8 +882,13 @@ public static function minLength($value, $minLength, $message = null, string $pr * @param mixed $value * @param int $maxLength * @param string|callable|null $message + * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $value + * + * @return bool + * * @throws AssertionFailedException */ public static function maxLength($value, $maxLength, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool @@ -831,8 +916,13 @@ public static function maxLength($value, $maxLength, $message = null, string $pr * @param int $minLength * @param int $maxLength * @param string|callable|null $message + * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $value + * + * @return bool + * * @throws AssertionFailedException */ public static function betweenLength($value, $minLength, $maxLength, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool @@ -850,8 +940,13 @@ public static function betweenLength($value, $minLength, $maxLength, $message = * @param mixed $string * @param string $needle * @param string|callable|null $message + * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $string + * + * @return bool + * * @throws AssertionFailedException */ public static function startsWith($string, $needle, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool @@ -877,8 +972,13 @@ public static function startsWith($string, $needle, $message = null, string $pro * @param mixed $string * @param string $needle * @param string|callable|null $message + * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $string + * + * @return bool + * * @throws AssertionFailedException */ public static function endsWith($string, $needle, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool @@ -906,8 +1006,13 @@ public static function endsWith($string, $needle, $message = null, string $prope * @param mixed $string * @param string $needle * @param string|callable|null $message + * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $string + * + * @return bool + * * @throws AssertionFailedException */ public static function contains($string, $needle, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool @@ -933,8 +1038,13 @@ public static function contains($string, $needle, $message = null, string $prope * @param mixed $string * @param string $needle * @param string|callable|null $message + * @param string|null $propertyPath * @param string $encoding * + * @psalm-assert =string $string + * + * @return bool + * * @throws AssertionFailedException */ public static function notContains($string, $needle, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool @@ -997,6 +1107,11 @@ public static function inArray($value, array $choices, $message = null, string $ * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert numeric $string + * + * @return bool * * @throws AssertionFailedException */ @@ -1019,6 +1134,11 @@ public static function numeric($value, $message = null, string $propertyPath = n * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert resource $string + * + * @return bool */ public static function isResource($value, $message = null, string $propertyPath = null): bool { @@ -1039,6 +1159,11 @@ public static function isResource($value, $message = null, string $propertyPath * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert array $string + * + * @return bool * * @throws AssertionFailedException */ @@ -1061,6 +1186,11 @@ public static function isArray($value, $message = null, string $propertyPath = n * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert iterable $string + * + * @return bool * * @throws AssertionFailedException */ @@ -1105,6 +1235,11 @@ public static function isArrayAccessible($value, $message = null, string $proper * * @param array|Countable|ResourceBundle|SimpleXMLElement $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert countable $string + * + * @return bool * * @throws AssertionFailedException */ @@ -1272,6 +1407,13 @@ public static function notBlank($value, $message = null, string $propertyPath = * @param mixed $value * @param string $className * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-template ExpectedType of object + * @psalm-param class-string $className + * @psalm-assert ExpectedType $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1296,6 +1438,13 @@ public static function isInstanceOf($value, $className, $message = null, string * @param mixed $value * @param string $className * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-template ExpectedType of object + * @psalm-param class-string $className + * @psalm-assert !ExpectedType $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1345,6 +1494,11 @@ public static function subclassOf($value, $className, $message = null, string $p * @param mixed $minValue * @param mixed $maxValue * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert =numeric $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1372,6 +1526,11 @@ public static function range($value, $minValue, $maxValue, $message = null, stri * @param mixed $value * @param mixed $minValue * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert =numeric $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1398,6 +1557,11 @@ public static function min($value, $minValue, $message = null, string $propertyP * @param mixed $value * @param mixed $maxValue * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert =numeric $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1520,6 +1684,11 @@ public static function writeable($value, $message = null, string $propertyPath = * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert =string $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1546,6 +1715,11 @@ public static function email($value, $message = null, string $propertyPath = nul * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert =string $value + * + * @return bool * * @throws AssertionFailedException * @@ -1619,6 +1793,11 @@ public static function alnum($value, $message = null, string $propertyPath = nul * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert true $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1641,6 +1820,11 @@ public static function true($value, $message = null, string $propertyPath = null * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert false $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1663,6 +1847,11 @@ public static function false($value, $message = null, string $propertyPath = nul * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert class-string $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1685,6 +1874,11 @@ public static function classExists($value, $message = null, string $propertyPath * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert class-string $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1746,6 +1940,11 @@ public static function implementsInterface($class, $interfaceName, $message = nu * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert =string $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1823,6 +2022,11 @@ public static function e164($value, $message = null, string $propertyPath = null * @param array|Countable|ResourceBundle|SimpleXMLElement $countable * @param int $count * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert non-empty-countable $value + * + * @return bool * * @throws AssertionFailedException */ @@ -1986,6 +2190,11 @@ public static function methodExists($value, $object, $message = null, string $pr * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert object $value + * + * @return bool * * @throws AssertionFailedException */ @@ -2349,6 +2558,11 @@ public static function extensionVersion($extension, $operator, $version, $messag * * @param mixed $value * @param string|callable|null $message + * @param string|null $propertyPath + * + * @psalm-assert callable $value + * + * @return bool * * @throws AssertionFailedException */ From c5e24c5682af23daa3b0bfd3490a2c80425134c9 Mon Sep 17 00:00:00 2001 From: Eric Braun Date: Thu, 3 Dec 2020 13:23:26 +0100 Subject: [PATCH 06/24] Fix psalm assertions (#308) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix psalm assertions Some @psalm-assert annotations refer to not existent params, which leads to them not working at all. * Remove invalid psalm assertion (#307) Psalm doesn´t know about the type `non-empty-count`. Also the assertion was wrong, since this method does not assert that the `$countable` param is empty. --- lib/Assert/Assertion.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 3a6b43ff..a91aa8e3 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -406,7 +406,7 @@ public static function notEq($value1, $value2, $message = null, string $property * * @psalm-template ExpectedType * @psalm-param ExpectedType $value2 - * @psalm-assert !=ExpectedType $value + * @psalm-assert !=ExpectedType $value1 * * @return bool * @@ -1109,7 +1109,7 @@ public static function inArray($value, array $choices, $message = null, string $ * @param string|callable|null $message * @param string|null $propertyPath * - * @psalm-assert numeric $string + * @psalm-assert numeric $value * * @return bool * @@ -1136,7 +1136,7 @@ public static function numeric($value, $message = null, string $propertyPath = n * @param string|callable|null $message * @param string|null $propertyPath * - * @psalm-assert resource $string + * @psalm-assert resource $value * * @return bool */ @@ -1161,7 +1161,7 @@ public static function isResource($value, $message = null, string $propertyPath * @param string|callable|null $message * @param string|null $propertyPath * - * @psalm-assert array $string + * @psalm-assert array $value * * @return bool * @@ -1188,7 +1188,7 @@ public static function isArray($value, $message = null, string $propertyPath = n * @param string|callable|null $message * @param string|null $propertyPath * - * @psalm-assert iterable $string + * @psalm-assert iterable $value * * @return bool * @@ -1237,7 +1237,7 @@ public static function isArrayAccessible($value, $message = null, string $proper * @param string|callable|null $message * @param string|null $propertyPath * - * @psalm-assert countable $string + * @psalm-assert countable $value * * @return bool * @@ -2024,8 +2024,6 @@ public static function e164($value, $message = null, string $propertyPath = null * @param string|callable|null $message * @param string|null $propertyPath * - * @psalm-assert non-empty-countable $value - * * @return bool * * @throws AssertionFailedException From 3f6cb5c991c947d6f43465b096b8fec7461d1baf Mon Sep 17 00:00:00 2001 From: George Wilson Date: Sun, 18 Apr 2021 20:59:02 +0100 Subject: [PATCH 07/24] Merge in 3.2.7 (#310) * Remove ext-intl as a required extension, making it a suggested extension for use with Assertion::count(), Assertion::isCountable(), Assertion::minCount(), and Assertion::maxCount() to operate on ResourceBundles. * Correct @method return typehint for LazyAssertion chaining * Restored `\Assert\Assertion::createException()` signature to 3.2.2. Sincerely apologies to the BC break. * Fix docblock typehint * Prepare v3.2.4 * Remove a hard dependency on the intl extension (#289) Fixes #288. While #268 and #269 updated count() and isCountable() to now work with intl resources, #286 made that the whole beberlei/assert package now requires the intl extension to be installed. However, the intl extension dependency is a soft dependency. The package does not need intl for most of its features and can safely work without it. * Fix docblocks for `all` to show that they are expected to receive an array of things * Preapare v3.2.5 * Make `Assert\Assertion::stringify()` UTF-8 safe. * Prepare v3.2.6 * Reinstate LazyAssertion docblock * ## 3.2.7 - 2019-12-19 ### Fixes - Reinstated the `@method` return type for `Assert\LazyAssertion` methods to show that the return type is `LazyAssertion`. * Fixes Co-authored-by: Richard Quadling Co-authored-by: Jakub Zalas --- CHANGELOG.md | 40 ++++ bin/MethodDocGenerator.php | 8 +- composer.json | 4 +- lib/Assert/Assertion.php | 184 ++++++++-------- lib/Assert/LazyAssertion.php | 200 +++++++++-------- tests/Assert/Tests/AssertTest.php | 346 ++++++++++++++++++------------ 6 files changed, 452 insertions(+), 330 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0f6e614..05570ce1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,46 @@ # Change Log All notable changes to this project will be documented in this file. +## 3.2.7 - 2019-12-19 + +### Fixes +- Reinstated the `@method` return type for `Assert\LazyAssertion` methods to show that the return type is `LazyAssertion`. + +## 3.2.6 - 2019-10-10 + +### Fixes +- Make `Assert\Assertion::stringify()` UTF-8 safe (Thanks to [Pedram Azimaei](https://github.com/beberlei/assert/pull/290)) + +## 3.2.5 - 2019-10-10 - Fix the broken things release. + +### Notice +- Sigh! + - Richard Quadling + +### Fixes +- REALLY Removed dependency of the intl extension. +- Updated the Docblocks for `Assert\Assertion::all()` helper to show that the value is expected to be an array. + +## 3.2.4 - 2019-10-10 - Fix the broken things release. + +### Notice +- It seems I've been slightly lax in verifying the signature changes and expected extensions. + Hopefully, both of these have been fixed in this version. + Truly sorry for breaking the internet! + - Richard Quadling + +### Fixes +- Restored `\Assert\Assertion::createException()` signature to 3.2.2. +- Removed dependency of the intl extension. If the extension is available, then `Assert\Assertion::count()`, + `Assert\Assertion::isCountable()`, `Assert\Assertion::minCount()`, and `Assert\Assertion::maxCount()` will operate on + `ResourceBundles`. +- Fixed the `@method` return type for `Assert\LazyAssertion` methods to show that the return type is `static` for + extensions of `Assert\LazyAssertion`. + *NOTE :* Docblock does not have the ability to differentiate between a non static `@method` whose returns type is of + the subclass and a `@method` that is called statically ([PSR-5#899](https://github.com/php-fig/fig-standards/pull/899)). + So the use of `static static` is a fudge that sort of works for IDEs that need to know about the method that MAY be + overridden in a subclass. + ## 3.2.3 - 2019-08-23 ### Other changes diff --git a/bin/MethodDocGenerator.php b/bin/MethodDocGenerator.php index c6b75946..e6281d70 100644 --- a/bin/MethodDocGenerator.php +++ b/bin/MethodDocGenerator.php @@ -85,6 +85,10 @@ private function generateMethodDocs($methods, $format, $skipParameterTest, $pref } } + if ($prefix === 'all' && strpos($type, 'null') === false && $parameterIndex === 0) { + $type = str_replace('|', '[]|', $type).'[]'; + } + if ($prefix === 'nullOr' && strpos($type, 'null') === false && $parameterIndex === 0) { $type .= '|null'; } @@ -214,8 +218,8 @@ public function generateLazyAssertionDocs() }; $docs = array_merge( - $this->generateMethodDocs($this->gatherAssertions(), ' * @method $this %s(%s) %s.', $skipParameterTest), - $this->generateMethodDocs($this->gatherAssertionChainSwitches(), ' * @method $this %s(%s) %s.', false) + $this->generateMethodDocs($this->gatherAssertions(), ' * @method LazyAssertion %s(%s) %s.', $skipParameterTest), + $this->generateMethodDocs($this->gatherAssertionChainSwitches(), ' * @method LazyAssertion %s(%s) %s.', false) ); $this->generateFile($phpFile, $docs, 'class'); diff --git a/composer.json b/composer.json index d34acd3f..1485a008 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,6 @@ }, "require": { "php": "^7.0 || ^8.0", - "ext-intl": "*", "ext-simplexml": "*", "ext-mbstring": "*", "ext-ctype": "*", @@ -58,5 +57,8 @@ "assert:cs-fix": "php-cs-fixer fix . -vvv || true", "assert:sa-code": "vendor/bin/phpstan analyse --configuration=phpstan-code.neon --no-progress --ansi -l 7 bin lib", "assert:sa-tests": "vendor/bin/phpstan analyse --configuration=phpstan-tests.neon --no-progress --ansi -l 7 tests" + }, + "suggest": { + "ext-intl": "Needed to allow Assertion::count(), Assertion::isCountable(), Assertion::minCount(), and Assertion::maxCount() to operate on ResourceBundles" } } diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index a91aa8e3..31382e5d 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -30,95 +30,95 @@ * * @author Benjamin Eberlei * - * @method static bool allAlnum(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric for all values. - * @method static bool allBase64(string $value, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined for all values. - * @method static bool allBetween(mixed $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit for all values. - * @method static bool allBetweenExclusive(mixed $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit for all values. - * @method static bool allBetweenLength(mixed $value, int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths for all values. - * @method static bool allBoolean(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is php boolean for all values. - * @method static bool allChoice(mixed $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices for all values. - * @method static bool allChoicesNotEmpty(array $values, array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content for all values. - * @method static bool allClassExists(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the class exists for all values. - * @method static bool allContains(mixed $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars for all values. - * @method static bool allCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the count of countable is equal to count for all values. - * @method static bool allDate(string $value, string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format for all values. - * @method static bool allDefined(mixed $constant, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined for all values. - * @method static bool allDigit(mixed $value, string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit for all values. - * @method static bool allDirectory(string $value, string|callable $message = null, string $propertyPath = null) Assert that a directory exists for all values. - * @method static bool allE164(string $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number for all values. - * @method static bool allEmail(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL) for all values. - * @method static bool allEndsWith(mixed $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars for all values. - * @method static bool allEq(mixed $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==) for all values. - * @method static bool allEqArraySubset(mixed $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset for all values. - * @method static bool allExtensionLoaded(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded for all values. - * @method static bool allExtensionVersion(string $extension, string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed for all values. - * @method static bool allFalse(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False for all values. - * @method static bool allFile(string $value, string|callable $message = null, string $propertyPath = null) Assert that a file exists for all values. - * @method static bool allFloat(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php float for all values. - * @method static bool allGreaterOrEqualThan(mixed $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit for all values. - * @method static bool allGreaterThan(mixed $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit for all values. - * @method static bool allImplementsInterface(mixed $class, string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface for all values. - * @method static bool allInArray(mixed $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice() for all values. - * @method static bool allInteger(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php integer for all values. - * @method static bool allIntegerish(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish for all values. - * @method static bool allInterfaceExists(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the interface exists for all values. - * @method static bool allIp(string $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address for all values. - * @method static bool allIpv4(string $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address for all values. - * @method static bool allIpv6(string $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address for all values. - * @method static bool allIsArray(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array for all values. - * @method static bool allIsArrayAccessible(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object for all values. - * @method static bool allIsCallable(mixed $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable for all values. - * @method static bool allIsCountable(array|Countable|ResourceBundle|SimpleXMLElement $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable for all values. - * @method static bool allIsInstanceOf(mixed $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name for all values. - * @method static bool allIsJsonString(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string for all values. - * @method static bool allIsObject(mixed $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object for all values. - * @method static bool allIsResource(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is a resource for all values. - * @method static bool allIsTraversable(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object for all values. - * @method static bool allKeyExists(mixed $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array for all values. - * @method static bool allKeyIsset(mixed $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() for all values. - * @method static bool allKeyNotExists(mixed $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array for all values. - * @method static bool allLength(mixed $value, int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length for all values. - * @method static bool allLessOrEqualThan(mixed $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit for all values. - * @method static bool allLessThan(mixed $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit for all values. - * @method static bool allMax(mixed $value, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit for all values. - * @method static bool allMaxCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at most $count elements for all values. - * @method static bool allMaxLength(mixed $value, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars for all values. - * @method static bool allMethodExists(string $value, mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object for all values. - * @method static bool allMin(mixed $value, mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit for all values. - * @method static bool allMinCount(array|Countable|ResourceBundle|SimpleXMLElement $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at least $count elements for all values. - * @method static bool allMinLength(mixed $value, int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long for all values. - * @method static bool allNoContent(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is empty for all values. - * @method static bool allNotBlank(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not blank for all values. - * @method static bool allNotContains(mixed $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars for all values. - * @method static bool allNotEmpty(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not empty for all values. - * @method static bool allNotEmptyKey(mixed $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty for all values. - * @method static bool allNotEq(mixed $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using ==) for all values. - * @method static bool allNotInArray(mixed $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices for all values. - * @method static bool allNotIsInstanceOf(mixed $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name for all values. - * @method static bool allNotNull(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not null for all values. - * @method static bool allNotRegex(mixed $value, string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value does not match a regex for all values. - * @method static bool allNotSame(mixed $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using ===) for all values. - * @method static bool allNull(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is null for all values. - * @method static bool allNumeric(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is numeric for all values. - * @method static bool allObjectOrClass(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists for all values. - * @method static bool allPhpVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version for all values. - * @method static bool allPropertiesExist(mixed $value, array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist for all values. - * @method static bool allPropertyExists(mixed $value, string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists for all values. - * @method static bool allRange(mixed $value, mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers for all values. - * @method static bool allReadable(string $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something readable for all values. - * @method static bool allRegex(mixed $value, string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex for all values. - * @method static bool allSame(mixed $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===) for all values. - * @method static bool allSatisfy(mixed $value, callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback for all values. - * @method static bool allScalar(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar for all values. - * @method static bool allStartsWith(mixed $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars for all values. - * @method static bool allString(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is a string for all values. - * @method static bool allSubclassOf(mixed $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name for all values. - * @method static bool allTrue(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True for all values. - * @method static bool allUniqueValues(array $values, string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality) for all values. - * @method static bool allUrl(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an URL for all values. - * @method static bool allUuid(string $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID for all values. - * @method static bool allVersion(string $version1, string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions for all values. - * @method static bool allWriteable(string $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable for all values. + * @method static bool allAlnum(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric for all values. + * @method static bool allBase64(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined for all values. + * @method static bool allBetween(mixed[] $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit for all values. + * @method static bool allBetweenExclusive(mixed[] $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit for all values. + * @method static bool allBetweenLength(mixed[] $value, int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths for all values. + * @method static bool allBoolean(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is php boolean for all values. + * @method static bool allChoice(mixed[] $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices for all values. + * @method static bool allChoicesNotEmpty(array[] $values, array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content for all values. + * @method static bool allClassExists(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the class exists for all values. + * @method static bool allContains(mixed[] $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars for all values. + * @method static bool allCount(array[]|Countable[]|ResourceBundle[]|SimpleXMLElement[] $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the count of countable is equal to count for all values. + * @method static bool allDate(string[] $value, string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format for all values. + * @method static bool allDefined(mixed[] $constant, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined for all values. + * @method static bool allDigit(mixed[] $value, string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit for all values. + * @method static bool allDirectory(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that a directory exists for all values. + * @method static bool allE164(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number for all values. + * @method static bool allEmail(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL) for all values. + * @method static bool allEndsWith(mixed[] $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars for all values. + * @method static bool allEq(mixed[] $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==) for all values. + * @method static bool allEqArraySubset(mixed[] $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset for all values. + * @method static bool allExtensionLoaded(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded for all values. + * @method static bool allExtensionVersion(string[] $extension, string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed for all values. + * @method static bool allFalse(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False for all values. + * @method static bool allFile(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that a file exists for all values. + * @method static bool allFloat(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php float for all values. + * @method static bool allGreaterOrEqualThan(mixed[] $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit for all values. + * @method static bool allGreaterThan(mixed[] $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit for all values. + * @method static bool allImplementsInterface(mixed[] $class, string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface for all values. + * @method static bool allInArray(mixed[] $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice() for all values. + * @method static bool allInteger(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php integer for all values. + * @method static bool allIntegerish(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish for all values. + * @method static bool allInterfaceExists(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the interface exists for all values. + * @method static bool allIp(string[] $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address for all values. + * @method static bool allIpv4(string[] $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address for all values. + * @method static bool allIpv6(string[] $value, int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address for all values. + * @method static bool allIsArray(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array for all values. + * @method static bool allIsArrayAccessible(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object for all values. + * @method static bool allIsCallable(mixed[] $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable for all values. + * @method static bool allIsCountable(array[]|Countable[]|ResourceBundle[]|SimpleXMLElement[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable for all values. + * @method static bool allIsInstanceOf(mixed[] $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name for all values. + * @method static bool allIsJsonString(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string for all values. + * @method static bool allIsObject(mixed[] $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object for all values. + * @method static bool allIsResource(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a resource for all values. + * @method static bool allIsTraversable(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object for all values. + * @method static bool allKeyExists(mixed[] $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array for all values. + * @method static bool allKeyIsset(mixed[] $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() for all values. + * @method static bool allKeyNotExists(mixed[] $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array for all values. + * @method static bool allLength(mixed[] $value, int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length for all values. + * @method static bool allLessOrEqualThan(mixed[] $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit for all values. + * @method static bool allLessThan(mixed[] $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit for all values. + * @method static bool allMax(mixed[] $value, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit for all values. + * @method static bool allMaxCount(array[]|Countable[]|ResourceBundle[]|SimpleXMLElement[] $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at most $count elements for all values. + * @method static bool allMaxLength(mixed[] $value, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars for all values. + * @method static bool allMethodExists(string[] $value, mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object for all values. + * @method static bool allMin(mixed[] $value, mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit for all values. + * @method static bool allMinCount(array[]|Countable[]|ResourceBundle[]|SimpleXMLElement[] $countable, int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at least $count elements for all values. + * @method static bool allMinLength(mixed[] $value, int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long for all values. + * @method static bool allNoContent(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is empty for all values. + * @method static bool allNotBlank(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is not blank for all values. + * @method static bool allNotContains(mixed[] $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars for all values. + * @method static bool allNotEmpty(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is not empty for all values. + * @method static bool allNotEmptyKey(mixed[] $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty for all values. + * @method static bool allNotEq(mixed[] $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using ==) for all values. + * @method static bool allNotInArray(mixed[] $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices for all values. + * @method static bool allNotIsInstanceOf(mixed[] $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name for all values. + * @method static bool allNotNull(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is not null for all values. + * @method static bool allNotRegex(mixed[] $value, string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value does not match a regex for all values. + * @method static bool allNotSame(mixed[] $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using ===) for all values. + * @method static bool allNull(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is null for all values. + * @method static bool allNumeric(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is numeric for all values. + * @method static bool allObjectOrClass(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists for all values. + * @method static bool allPhpVersion(string[] $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version for all values. + * @method static bool allPropertiesExist(mixed[] $value, array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist for all values. + * @method static bool allPropertyExists(mixed[] $value, string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists for all values. + * @method static bool allRange(mixed[] $value, mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers for all values. + * @method static bool allReadable(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something readable for all values. + * @method static bool allRegex(mixed[] $value, string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex for all values. + * @method static bool allSame(mixed[] $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===) for all values. + * @method static bool allSatisfy(mixed[] $value, callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback for all values. + * @method static bool allScalar(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar for all values. + * @method static bool allStartsWith(mixed[] $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars for all values. + * @method static bool allString(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is a string for all values. + * @method static bool allSubclassOf(mixed[] $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name for all values. + * @method static bool allTrue(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True for all values. + * @method static bool allUniqueValues(array[] $values, string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality) for all values. + * @method static bool allUrl(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is an URL for all values. + * @method static bool allUuid(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID for all values. + * @method static bool allVersion(string[] $version1, string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions for all values. + * @method static bool allWriteable(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable for all values. * @method static bool nullOrAlnum(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric or that the value is null. * @method static bool nullOrBase64(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined or that the value is null. * @method static bool nullOrBetween(mixed|null $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit or that the value is null. @@ -2716,7 +2716,7 @@ public static function base64($value, $message = null, string $propertyPath = nu * * @return mixed */ - protected static function createException($value, $message, $code, string $propertyPath = null, array $constraints = []) + protected static function createException($value, $message, $code, $propertyPath = null, array $constraints = []) { $exceptionClass = static::$exceptionClass; @@ -2737,8 +2737,8 @@ protected static function stringify($value): string } elseif (\is_scalar($value)) { $val = (string)$value; - if (\strlen($val) > 100) { - $val = \substr($val, 0, 97).'...'; + if (\mb_strlen($val) > 100) { + $val = \mb_substr($val, 0, 97).'...'; } $result = $val; diff --git a/lib/Assert/LazyAssertion.php b/lib/Assert/LazyAssertion.php index 1c27e813..b3052178 100644 --- a/lib/Assert/LazyAssertion.php +++ b/lib/Assert/LazyAssertion.php @@ -21,97 +21,97 @@ * * @author Benjamin Eberlei * - * @method $this alnum(string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric. - * @method $this base64(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined. - * @method $this between(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. - * @method $this betweenExclusive(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit. - * @method $this betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths. - * @method $this boolean(string|callable $message = null, string $propertyPath = null) Assert that value is php boolean. - * @method $this choice(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. - * @method $this choicesNotEmpty(array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content. - * @method $this classExists(string|callable $message = null, string $propertyPath = null) Assert that the class exists. - * @method $this contains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars. - * @method $this count(int $count, string|callable $message = null, string $propertyPath = null) Assert that the count of countable is equal to count. - * @method $this date(string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format. - * @method $this defined(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined. - * @method $this digit(string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit. - * @method $this directory(string|callable $message = null, string $propertyPath = null) Assert that a directory exists. - * @method $this e164(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number. - * @method $this email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL). - * @method $this endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars. - * @method $this eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==). - * @method $this eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset. - * @method $this extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded. - * @method $this extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed. - * @method $this false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False. - * @method $this file(string|callable $message = null, string $propertyPath = null) Assert that a file exists. - * @method $this float(string|callable $message = null, string $propertyPath = null) Assert that value is a php float. - * @method $this greaterOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit. - * @method $this greaterThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit. - * @method $this implementsInterface(string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface. - * @method $this inArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice(). - * @method $this integer(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer. - * @method $this integerish(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish. - * @method $this interfaceExists(string|callable $message = null, string $propertyPath = null) Assert that the interface exists. - * @method $this ip(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address. - * @method $this ipv4(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address. - * @method $this ipv6(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address. - * @method $this isArray(string|callable $message = null, string $propertyPath = null) Assert that value is an array. - * @method $this isArrayAccessible(string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object. - * @method $this isCallable(string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable. - * @method $this isCountable(string|callable $message = null, string $propertyPath = null) Assert that value is countable. - * @method $this isInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name. - * @method $this isJsonString(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string. - * @method $this isObject(string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object. - * @method $this isResource(string|callable $message = null, string $propertyPath = null) Assert that value is a resource. - * @method $this isTraversable(string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object. - * @method $this keyExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array. - * @method $this keyIsset(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset(). - * @method $this keyNotExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array. - * @method $this length(int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length. - * @method $this lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit. - * @method $this lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit. - * @method $this max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit. - * @method $this maxCount(int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at most $count elements. - * @method $this maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars. - * @method $this methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object. - * @method $this min(mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit. - * @method $this minCount(int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at least $count elements. - * @method $this minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long. - * @method $this noContent(string|callable $message = null, string $propertyPath = null) Assert that value is empty. - * @method $this notBlank(string|callable $message = null, string $propertyPath = null) Assert that value is not blank. - * @method $this notContains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars. - * @method $this notEmpty(string|callable $message = null, string $propertyPath = null) Assert that value is not empty. - * @method $this notEmptyKey(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty. - * @method $this notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using ==). - * @method $this notInArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices. - * @method $this notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name. - * @method $this notNull(string|callable $message = null, string $propertyPath = null) Assert that value is not null. - * @method $this notRegex(string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value does not match a regex. - * @method $this notSame(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using ===). - * @method $this null(string|callable $message = null, string $propertyPath = null) Assert that value is null. - * @method $this numeric(string|callable $message = null, string $propertyPath = null) Assert that value is numeric. - * @method $this objectOrClass(string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists. - * @method $this phpVersion(mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version. - * @method $this propertiesExist(array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist. - * @method $this propertyExists(string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists. - * @method $this range(mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers. - * @method $this readable(string|callable $message = null, string $propertyPath = null) Assert that the value is something readable. - * @method $this regex(string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex. - * @method $this same(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===). - * @method $this satisfy(callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback. - * @method $this scalar(string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar. - * @method $this startsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars. - * @method $this string(string|callable $message = null, string $propertyPath = null) Assert that value is a string. - * @method $this subclassOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name. - * @method $this true(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True. - * @method $this uniqueValues(string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality). - * @method $this url(string|callable $message = null, string $propertyPath = null) Assert that value is an URL. - * @method $this uuid(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID. - * @method $this version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions. - * @method $this writeable(string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable. - * @method $this all() Switch chain into validation mode for an array of values. - * @method $this nullOr() Switch chain into mode allowing nulls, ignoring further assertions. + * @method LazyAssertion alnum(string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric. + * @method LazyAssertion base64(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined. + * @method LazyAssertion between(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. + * @method LazyAssertion betweenExclusive(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit. + * @method LazyAssertion betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths. + * @method LazyAssertion boolean(string|callable $message = null, string $propertyPath = null) Assert that value is php boolean. + * @method LazyAssertion choice(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. + * @method LazyAssertion choicesNotEmpty(array $choices, string|callable $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content. + * @method LazyAssertion classExists(string|callable $message = null, string $propertyPath = null) Assert that the class exists. + * @method LazyAssertion contains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars. + * @method LazyAssertion count(int $count, string|callable $message = null, string $propertyPath = null) Assert that the count of countable is equal to count. + * @method LazyAssertion date(string $format, string|callable $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format. + * @method LazyAssertion defined(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined. + * @method LazyAssertion digit(string|callable $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit. + * @method LazyAssertion directory(string|callable $message = null, string $propertyPath = null) Assert that a directory exists. + * @method LazyAssertion e164(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number. + * @method LazyAssertion email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL). + * @method LazyAssertion endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars. + * @method LazyAssertion eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==). + * @method LazyAssertion eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset. + * @method LazyAssertion extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded. + * @method LazyAssertion extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed. + * @method LazyAssertion false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False. + * @method LazyAssertion file(string|callable $message = null, string $propertyPath = null) Assert that a file exists. + * @method LazyAssertion float(string|callable $message = null, string $propertyPath = null) Assert that value is a php float. + * @method LazyAssertion greaterOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit. + * @method LazyAssertion greaterThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is greater than given limit. + * @method LazyAssertion implementsInterface(string $interfaceName, string|callable $message = null, string $propertyPath = null) Assert that the class implements the interface. + * @method LazyAssertion inArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is in array of choices. This is an alias of Assertion::choice(). + * @method LazyAssertion integer(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer. + * @method LazyAssertion integerish(string|callable $message = null, string $propertyPath = null) Assert that value is a php integer'ish. + * @method LazyAssertion interfaceExists(string|callable $message = null, string $propertyPath = null) Assert that the interface exists. + * @method LazyAssertion ip(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address. + * @method LazyAssertion ipv4(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv4 address. + * @method LazyAssertion ipv6(int $flag = null, string|callable $message = null, string $propertyPath = null) Assert that value is an IPv6 address. + * @method LazyAssertion isArray(string|callable $message = null, string $propertyPath = null) Assert that value is an array. + * @method LazyAssertion isArrayAccessible(string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object. + * @method LazyAssertion isCallable(string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable. + * @method LazyAssertion isCountable(string|callable $message = null, string $propertyPath = null) Assert that value is countable. + * @method LazyAssertion isInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name. + * @method LazyAssertion isJsonString(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string. + * @method LazyAssertion isObject(string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object. + * @method LazyAssertion isResource(string|callable $message = null, string $propertyPath = null) Assert that value is a resource. + * @method LazyAssertion isTraversable(string|callable $message = null, string $propertyPath = null) Assert that value is an array or a traversable object. + * @method LazyAssertion keyExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array. + * @method LazyAssertion keyIsset(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset(). + * @method LazyAssertion keyNotExists(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key does not exist in an array. + * @method LazyAssertion length(int $length, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length. + * @method LazyAssertion lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit. + * @method LazyAssertion lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit. + * @method LazyAssertion max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit. + * @method LazyAssertion maxCount(int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at most $count elements. + * @method LazyAssertion maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars. + * @method LazyAssertion methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object. + * @method LazyAssertion min(mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit. + * @method LazyAssertion minCount(int $count, string|callable $message = null, string $propertyPath = null) Assert that the countable have at least $count elements. + * @method LazyAssertion minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long. + * @method LazyAssertion noContent(string|callable $message = null, string $propertyPath = null) Assert that value is empty. + * @method LazyAssertion notBlank(string|callable $message = null, string $propertyPath = null) Assert that value is not blank. + * @method LazyAssertion notContains(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars. + * @method LazyAssertion notEmpty(string|callable $message = null, string $propertyPath = null) Assert that value is not empty. + * @method LazyAssertion notEmptyKey(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty. + * @method LazyAssertion notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using ==). + * @method LazyAssertion notInArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices. + * @method LazyAssertion notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name. + * @method LazyAssertion notNull(string|callable $message = null, string $propertyPath = null) Assert that value is not null. + * @method LazyAssertion notRegex(string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value does not match a regex. + * @method LazyAssertion notSame(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not the same (using ===). + * @method LazyAssertion null(string|callable $message = null, string $propertyPath = null) Assert that value is null. + * @method LazyAssertion numeric(string|callable $message = null, string $propertyPath = null) Assert that value is numeric. + * @method LazyAssertion objectOrClass(string|callable $message = null, string $propertyPath = null) Assert that the value is an object, or a class that exists. + * @method LazyAssertion phpVersion(mixed $version, string|callable $message = null, string $propertyPath = null) Assert on PHP version. + * @method LazyAssertion propertiesExist(array $properties, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the properties all exist. + * @method LazyAssertion propertyExists(string $property, string|callable $message = null, string $propertyPath = null) Assert that the value is an object or class, and that the property exists. + * @method LazyAssertion range(mixed $minValue, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that value is in range of numbers. + * @method LazyAssertion readable(string|callable $message = null, string $propertyPath = null) Assert that the value is something readable. + * @method LazyAssertion regex(string $pattern, string|callable $message = null, string $propertyPath = null) Assert that value matches a regex. + * @method LazyAssertion same(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are the same (using ===). + * @method LazyAssertion satisfy(callable $callback, string|callable $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback. + * @method LazyAssertion scalar(string|callable $message = null, string $propertyPath = null) Assert that value is a PHP scalar. + * @method LazyAssertion startsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars. + * @method LazyAssertion string(string|callable $message = null, string $propertyPath = null) Assert that value is a string. + * @method LazyAssertion subclassOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name. + * @method LazyAssertion true(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True. + * @method LazyAssertion uniqueValues(string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality). + * @method LazyAssertion url(string|callable $message = null, string $propertyPath = null) Assert that value is an URL. + * @method LazyAssertion uuid(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID. + * @method LazyAssertion version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions. + * @method LazyAssertion writeable(string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable. + * @method LazyAssertion all() Switch chain into validation mode for an array of values. + * @method LazyAssertion nullOr() Switch chain into mode allowing nulls, ignoring further assertions. */ class LazyAssertion { @@ -131,7 +131,7 @@ class LazyAssertion * @param mixed $value * @param string|callable|null $defaultMessage * - * @return $this + * @return static */ public function that($value, string $propertyPath = null, $defaultMessage = null) { @@ -144,7 +144,7 @@ public function that($value, string $propertyPath = null, $defaultMessage = null } /** - * @return $this + * @return static */ public function tryAll() { @@ -157,6 +157,12 @@ public function tryAll() return $this; } + /** + * @param string $method + * @param array $args + * + * @return static + */ public function __call($method, $args) { if (false === $this->alwaysTryAll @@ -189,7 +195,9 @@ public function verifyNow(): bool } /** - * @return $this + * @param string $className + * + * @return static */ public function setAssertClass(string $className): LazyAssertion { @@ -203,7 +211,9 @@ public function setAssertClass(string $className): LazyAssertion } /** - * @return $this + * @param string $className + * + * @return static */ public function setExceptionClass(string $className): LazyAssertion { diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 21eab650..b79a986b 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -14,19 +14,35 @@ namespace Assert\Tests; +use ArrayObject; +use ArrayIterator; use Assert\Assertion; use Assert\AssertionFailedException; use Assert\Tests\Fixtures\CustomAssertion; use Assert\Tests\Fixtures\OneCountable; -use PDO; +use Countable; +use DateTime; +use Exception; +use Foo; use ResourceBundle; use SimpleXMLElement; +use SplObserver; use stdClass; use Yoast\PHPUnitPolyfills\TestCases\TestCase; +use Traversable; +use function base64_encode; +use function extension_loaded; +use function fopen; +use function is_bool; +use function is_null; +use function json_encode; +use function range; +use function str_repeat; +use function sys_get_temp_dir; class AssertTest extends TestCase { - public static function dataInvalidFloat() + public function dataInvalidFloat(): array { return [ [1], @@ -57,7 +73,7 @@ public function testValidFloat() $this->assertTrue(Assertion::float(-1.1)); } - public static function dataInvalidInteger() + public function dataInvalidInteger(): array { return [ [1.23], @@ -66,7 +82,7 @@ public static function dataInvalidInteger() [null], ['1.23'], ['10'], - [new \DateTime()], + [new DateTime()], ]; } @@ -88,7 +104,7 @@ public function testValidInteger() $this->assertTrue(Assertion::integer(0)); } - public function dataValidIntergerish() + public function dataValidIntergerish(): array { return [ [10], @@ -118,7 +134,7 @@ public function testValidIntegerish($value) $this->assertTrue(Assertion::integerish($value)); } - public static function dataInvalidIntegerish() + public function dataInvalidIntegerish(): array { return [ 'A float' => [1.23], @@ -128,7 +144,7 @@ public static function dataInvalidIntegerish() 'A null' => [null], 'A float in a string' => ['1.23'], 'A negative float in a string' => ['-1.23'], - 'A file pointer' => [\fopen(__FILE__, 'r')], + 'A file pointer' => [fopen(__FILE__, 'r')], 'A float in a string with a leading space' => [' 1.23'], 'An integer in a string with a leading space' => [' 123'], 'A negative integer in a string with a leading space' => [' -123'], @@ -188,7 +204,7 @@ public function testValidScalar() $this->assertTrue(Assertion::scalar(false)); } - public static function dataInvalidNotEmpty() + public function dataInvalidNotEmpty(): array { return [ [''], @@ -227,7 +243,7 @@ public function testEmpty() $this->assertTrue(Assertion::noContent([])); } - public static function dataInvalidEmpty() + public function dataInvalidEmpty(): array { return [ ['foo'], @@ -250,7 +266,7 @@ public function testInvalidEmpty($value) Assertion::noContent($value); } - public static function dataInvalidNull() + public function dataInvalidNull(): array { return [ ['foo'], @@ -313,12 +329,12 @@ public function testInvalidString($invalidString) Assertion::string($invalidString); } - public static function dataInvalidString() + public function dataInvalidString(): array { return [ [1.23], [false], - [new \ArrayObject()], + [new ArrayObject()], [null], [10], [true], @@ -515,7 +531,7 @@ public function testValidNumeric() $this->assertTrue(Assertion::numeric(1.23)); } - public static function dataInvalidArray() + public function dataInvalidArray(): array { return [ [null], @@ -524,7 +540,7 @@ public static function dataInvalidArray() [1], [1.23], [new stdClass()], - [\fopen('php://memory', 'r')], + [fopen('php://memory', 'r')], ]; } @@ -571,7 +587,7 @@ public function testValidKeyNotExists() $this->assertTrue(Assertion::keyNotExists(['foo' => 'bar'], 'baz')); } - public static function dataInvalidUniqueValues() + public function dataInvalidUniqueValues(): array { $object = new \stdClass(); @@ -615,7 +631,7 @@ public function testValidUniqueValues($array) $this->assertTrue(Assertion::uniqueValues($array, 'baz')); } - public static function dataInvalidNotBlank() + public function dataInvalidNotBlank(): array { return [ [''], @@ -655,14 +671,29 @@ public function testInvalidNotInstanceOf() public function testValidNotIsInstanceOf() { - $this->assertTrue(Assertion::notIsInstanceOf(new stdClass(), PDO::class)); + $this->assertTrue( + Assertion::notIsInstanceOf( + new stdClass(), + new class() { + } + ) + ); } public function testInvalidInstanceOf() { $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_INSTANCE_OF); - Assertion::isInstanceOf(new stdClass(), PDO::class); + + Assertion::isInstanceOf( + new stdClass(), + new class() { + public function __toString(): string + { + return 'Anonymous'; + } + } + ); } public function testValidInstanceOf() @@ -674,7 +705,16 @@ public function testInvalidSubclassOf() { $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_SUBCLASS_OF); - Assertion::subclassOf(new stdClass(), PDO::class); + + Assertion::subclassOf( + new stdClass(), + new class() { + public function __toString(): string + { + return 'Anonymous'; + } + } + ); } public function testValidSubclassOf() @@ -722,7 +762,7 @@ public function testInvalidUrl($url) Assertion::url($url); } - public static function dataInvalidUrl() + public function dataInvalidUrl(): array { return [ ['google.com'], @@ -755,7 +795,7 @@ public function testValidUrl($url) $this->assertTrue(Assertion::url($url)); } - public static function dataValidUrl() + public function dataValidUrl(): array { return [ ['http://a.pl'], @@ -886,12 +926,12 @@ public function testInvalidClass() { $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_CLASS); - Assertion::classExists(\Foo::class); + Assertion::classExists(Foo::class); } public function testValidClass() { - $this->assertTrue(Assertion::classExists(\Exception::class)); + $this->assertTrue(Assertion::classExists(Exception::class)); } public function testSame() @@ -940,10 +980,10 @@ public function testNotInArray() { $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_VALUE_IN_ARRAY); - $this->assertTrue(Assertion::notInArray(6, \range(1, 5))); - $this->assertTrue(Assertion::notInArray('a', \range('b', 'z'))); + $this->assertTrue(Assertion::notInArray(6, range(1, 5))); + $this->assertTrue(Assertion::notInArray('a', range('b', 'z'))); - Assertion::notInArray(1, \range(1, 5)); + Assertion::notInArray(1, range(1, 5)); } public function testMin() @@ -967,7 +1007,7 @@ public function testInvalidMin($value, $min) Assertion::min($value, $min); } - public function dataInvalidMin() + public function dataInvalidMin(): array { return [ [0, 1], @@ -996,7 +1036,7 @@ public function testInvalidMax($value, $min) Assertion::max($value, $min); } - public function dataInvalidMax() + public function dataInvalidMax(): array { return [ [2, 1], @@ -1023,7 +1063,7 @@ public function testLength() $this->assertTrue(Assertion::length('', 0)); } - public static function dataLengthUtf8Characters() + public function dataLengthUtf8Characters(): array { return [ ['址', 1], @@ -1102,7 +1142,7 @@ public function testWriteable() { $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_WRITEABLE); - $this->assertTrue(Assertion::writeable(\sys_get_temp_dir())); + $this->assertTrue(Assertion::writeable(sys_get_temp_dir())); Assertion::writeable(__DIR__.'/does-not-exist'); } @@ -1118,20 +1158,20 @@ public function testImplementsInterface() { $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INTERFACE_NOT_IMPLEMENTED); - $this->assertTrue(Assertion::implementsInterface(\ArrayIterator::class, \Traversable::class)); + $this->assertTrue(Assertion::implementsInterface(ArrayIterator::class, Traversable::class)); - Assertion::implementsInterface(\Exception::class, \Traversable::class); + Assertion::implementsInterface(Exception::class, Traversable::class); } public function testImplementsInterfaceWithClassObject() { $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INTERFACE_NOT_IMPLEMENTED); - $class = new \ArrayObject(); + $class = new ArrayObject(); - $this->assertTrue(Assertion::implementsInterface($class, \Traversable::class)); + $this->assertTrue(Assertion::implementsInterface($class, Traversable::class)); - Assertion::implementsInterface($class, \SplObserver::class); + Assertion::implementsInterface($class, SplObserver::class); } public function testImplementsInterfaceThrowsExceptionForInvalidSubject() @@ -1139,9 +1179,9 @@ public function testImplementsInterfaceThrowsExceptionForInvalidSubject() $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INTERFACE_NOT_IMPLEMENTED); $this->expectExceptionMessage('Class "not_a_class" failed reflection'); - $this->assertTrue(Assertion::implementsInterface('not_a_class', \Traversable::class)); + $this->assertTrue(Assertion::implementsInterface('not_a_class', Traversable::class)); - Assertion::implementsInterface(\Exception::class, \Traversable::class); + Assertion::implementsInterface(Exception::class, Traversable::class); } /** @@ -1154,11 +1194,11 @@ public function testIsJsonString($content) $this->assertTrue(Assertion::isJsonString($content)); } - public static function isJsonStringDataprovider() + public function isJsonStringDataprovider(): array { return [ - '»null« value' => [\json_encode(null)], - '»false« value' => [\json_encode(false)], + '»null« value' => [json_encode(null)], + '»false« value' => [json_encode(false)], 'array value' => ['["false"]'], 'object value' => ['{"tux":"false"}'], ]; @@ -1176,7 +1216,7 @@ public function testIsJsonStringExpectingException($invalidString) Assertion::isJsonString($invalidString); } - public static function isJsonStringInvalidStringDataprovider() + public function isJsonStringInvalidStringDataprovider(): array { return [ 'no json string' => ['invalid json encoded string'], @@ -1206,7 +1246,7 @@ public function testInvalidUuids($uuid) Assertion::uuid($uuid); } - public static function providesValidUuids() + public function providesValidUuids(): array { return [ ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], @@ -1219,7 +1259,7 @@ public static function providesValidUuids() ]; } - public static function providesInvalidUuids() + public function providesInvalidUuids(): array { return [ ['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'], @@ -1252,7 +1292,7 @@ public function testInvalidE164s($e164) Assertion::e164($e164); } - public static function providesValidE164s() + public function providesValidE164s(): array { return [ ['+33626525690'], @@ -1261,7 +1301,7 @@ public static function providesValidE164s() ]; } - public static function providesInvalidE164s() + public function providesInvalidE164s(): array { return [ ['+3362652569e'], @@ -1309,7 +1349,13 @@ public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAsse { $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_INSTANCE_OF); - Assertion::allIsInstanceOf([new stdClass(), new stdClass()], PDO::class, 'Assertion failed', 'foos'); + Assertion::allIsInstanceOf( + [new stdClass(), new stdClass()], + new class() { + }, + 'Assertion failed', + 'foos' + ); } public function testAllWithNoValueThrows() @@ -1324,12 +1370,19 @@ public function testValidCount() $this->assertTrue(Assertion::count(['Hi', 'There'], 2)); $this->assertTrue(Assertion::count(new Fixtures\OneCountable(), 1)); $this->assertTrue(Assertion::count(new SimpleXMLElement(''), 2)); + } + + /** + * @requires extension intl + */ + public function testValidCountWithIntlResourceBundle() + { // Test ResourceBundle counting using resources generated for PHP testing of ResourceBundle // https://github.com/php/php-src/commit/8f4337f2551e28d98290752e9ca99fc7f87d93b5 $this->assertTrue(Assertion::count(new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 6)); } - public static function dataInvalidCount() + public function dataInvalidCount(): array { return [ [['Hi', 'There'], 3], @@ -1359,17 +1412,24 @@ public function testValidMinCount() $this->assertTrue(Assertion::minCount(['Hi', 'There'], 1)); $this->assertTrue(Assertion::minCount(new Fixtures\OneCountable(), 1)); $this->assertTrue(Assertion::minCount(new SimpleXMLElement(''), 1)); + } + + /** + * @requires extension intl + */ + public function testValidMinCountWithIntlResourceBundle() + { $this->assertTrue(Assertion::minCount(new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 2)); } - public static function dataInvalidMinCount() + public function dataInvalidMinCount(): \Generator { - return [ - '2 elements while at least 3 expected' => [['Hi', 'There'], 3], - '1 countable while at least 2 expected' => [new Fixtures\OneCountable(), 2], - '2 countable while at least 3 expected' => [new SimpleXMLElement(''), 3], - '6 countable while at least 7 expected' => [new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 7], - ]; + yield '2 elements while at least 3 expected' => [['Hi', 'There'], 3]; + yield '1 countable while at least 2 expected' => [new Fixtures\OneCountable(), 2]; + yield '2 countable while at least 3 expected' => [new SimpleXMLElement(''), 3]; + if (extension_loaded('intl')) { + yield '6 countable while at least 7 expected' => [new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 7]; + } } /** @@ -1392,17 +1452,24 @@ public function testValidMaxCount() $this->assertTrue(Assertion::maxCount(['Hi', 'There'], 2)); $this->assertTrue(Assertion::maxCount(new Fixtures\OneCountable(), 1)); $this->assertTrue(Assertion::maxCount(new SimpleXMLElement(''), 3)); + } + + /** + * @requires extension intl + */ + public function testValidMaxCountWithIntlResourceBundle() + { $this->assertTrue(Assertion::maxCount(new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 7)); } - public static function dataInvalidMaxCount() + public function dataInvalidMaxCount(): \Generator { - return [ - '2 elements while at most 1 expected' => [['Hi', 'There'], 1], - '1 countable while at most 0 expected' => [new Fixtures\OneCountable(), 0], - '2 countable while at most 1 expected' => [new SimpleXMLElement(''), 1], - '6 countable while at most 5 expected' => [new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 5], - ]; + yield '2 elements while at most 1 expected' => [['Hi', 'There'], 1]; + yield '1 countable while at most 0 expected' => [new Fixtures\OneCountable(), 0]; + yield '2 countable while at most 1 expected' => [new SimpleXMLElement(''), 1]; + if (extension_loaded('intl')) { + yield '6 countable while at most 5 expected' => [new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 5]; + } } /** @@ -1446,7 +1513,7 @@ public function testChoicesNotEmptyExpectingExceptionInvalidKeyIsset() Assertion::choicesNotEmpty(['tux' => ''], ['invalidChoice']); } - public function invalidChoicesProvider() + public function invalidChoicesProvider(): array { return [ 'empty values' => [[], ['tux'], Assertion::VALUE_EMPTY], @@ -1484,7 +1551,7 @@ public function testThatAssertionExceptionCanAccessValueAndSupplyConstraints() Assertion::range(0, 10, 20); $this->fail('Exception expected'); - } catch (AssertionFailedException $e) { + } catch (\Assert\AssertionFailedException $e) { $this->assertEquals(0, $e->getValue()); $this->assertEquals(['min' => 10, 'max' => 20], $e->getConstraints()); } @@ -1495,18 +1562,18 @@ public function testLessThan() $this->assertTrue(Assertion::lessThan(1, 2)); $this->assertTrue(Assertion::lessThan('aaa', 'bbb')); $this->assertTrue(Assertion::lessThan('aaa', 'aaaa')); - $this->assertTrue(Assertion::lessThan(new \DateTime('today'), new \DateTime('tomorrow'))); + $this->assertTrue(Assertion::lessThan(new DateTime('today'), new DateTime('tomorrow'))); } - public function invalidLessProvider() + public function invalidLessProvider(): array { return [ [2, 1], [2, 2], ['aaa', 'aaa'], ['aaaa', 'aaa'], - [new \DateTime('today'), new \DateTime('yesterday')], - [new \DateTime('today'), new \DateTime('today')], + [new DateTime('today'), new DateTime('yesterday')], + [new DateTime('today'), new DateTime('today')], ]; } @@ -1530,16 +1597,16 @@ public function testLessOrEqualThan() $this->assertTrue(Assertion::lessOrEqualThan('aaa', 'bbb')); $this->assertTrue(Assertion::lessOrEqualThan('aaa', 'aaaa')); $this->assertTrue(Assertion::lessOrEqualThan('aaa', 'aaa')); - $this->assertTrue(Assertion::lessOrEqualThan(new \DateTime('today'), new \DateTime('tomorrow'))); - $this->assertTrue(Assertion::lessOrEqualThan(new \DateTime('today'), new \DateTime('today'))); + $this->assertTrue(Assertion::lessOrEqualThan(new DateTime('today'), new DateTime('tomorrow'))); + $this->assertTrue(Assertion::lessOrEqualThan(new DateTime('today'), new DateTime('today'))); } - public function invalidLessOrEqualProvider() + public function invalidLessOrEqualProvider(): array { return [ [2, 1], ['aaaa', 'aaa'], - [new \DateTime('today'), new \DateTime('yesterday')], + [new DateTime('today'), new DateTime('yesterday')], ]; } @@ -1561,18 +1628,18 @@ public function testGreaterThan() $this->assertTrue(Assertion::greaterThan(2, 1)); $this->assertTrue(Assertion::greaterThan('bbb', 'aaa')); $this->assertTrue(Assertion::greaterThan('aaaa', 'aaa')); - $this->assertTrue(Assertion::greaterThan(new \DateTime('tomorrow'), new \DateTime('today'))); + $this->assertTrue(Assertion::greaterThan(new DateTime('tomorrow'), new DateTime('today'))); } - public function invalidGreaterProvider() + public function invalidGreaterProvider(): array { return [ [1, 2], [2, 2], ['aaa', 'aaa'], ['aaa', 'aaaa'], - [new \DateTime('yesterday'), new \DateTime('today')], - [new \DateTime('today'), new \DateTime('today')], + [new DateTime('yesterday'), new DateTime('today')], + [new DateTime('today'), new DateTime('today')], ]; } @@ -1587,7 +1654,7 @@ public function testValidDate($value, $format) $this->assertTrue(Assertion::date($value, $format)); } - public function validDateProvider() + public function validDateProvider(): array { return [ ['2012-03-13', 'Y-m-d'], @@ -1617,16 +1684,16 @@ public function testGreaterOrEqualThan() $this->assertTrue(Assertion::greaterOrEqualThan('bbb', 'aaa')); $this->assertTrue(Assertion::greaterOrEqualThan('aaaa', 'aaa')); $this->assertTrue(Assertion::greaterOrEqualThan('aaa', 'aaa')); - $this->assertTrue(Assertion::greaterOrEqualThan(new \DateTime('tomorrow'), new \DateTime('today'))); - $this->assertTrue(Assertion::greaterOrEqualThan(new \DateTime('today'), new \DateTime('today'))); + $this->assertTrue(Assertion::greaterOrEqualThan(new DateTime('tomorrow'), new DateTime('today'))); + $this->assertTrue(Assertion::greaterOrEqualThan(new DateTime('today'), new DateTime('today'))); } - public function invalidGreaterOrEqualProvider() + public function invalidGreaterOrEqualProvider(): array { return [ [1, 2], ['aaa', 'aaaa'], - [new \DateTime('yesterday'), new \DateTime('tomorrow')], + [new DateTime('yesterday'), new DateTime('tomorrow')], ]; } @@ -1656,7 +1723,7 @@ public function testInvalidDate($value, $format) Assertion::date($value, $format); } - public function invalidDateProvider() + public function invalidDateProvider(): array { return [ ['this is not the date', 'Y-m-d'], @@ -1667,7 +1734,7 @@ public function invalidDateProvider() public function testValidTraversable() { - $this->assertTrue(Assertion::isTraversable(new \ArrayObject())); + $this->assertTrue(Assertion::isTraversable(new ArrayObject())); } public function testInvalidTraversable() @@ -1680,7 +1747,7 @@ public function testInvalidTraversable() public function testValidCountable() { $this->assertTrue(Assertion::isCountable([])); - $this->assertTrue(Assertion::isCountable(new \ArrayObject())); + $this->assertTrue(Assertion::isCountable(new ArrayObject())); } public function testInvalidCountable() @@ -1692,7 +1759,7 @@ public function testInvalidCountable() public function testValidArrayAccessible() { - $this->assertTrue(Assertion::isArrayAccessible(new \ArrayObject())); + $this->assertTrue(Assertion::isArrayAccessible(new ArrayObject())); } public function testInvalidArrayAccessible() @@ -1729,8 +1796,8 @@ public function testInvalidSatisfy() $this->expectExceptionCode(\Assert\Assertion::INVALID_SATISFY); Assertion::satisfy( null, - function ($value) { - return !\is_null($value); + function ($value): bool { + return !is_null($value); } ); } @@ -1741,8 +1808,8 @@ public function testValidSatisfy() $this->assertTrue( Assertion::satisfy( null, - function ($value) { - return \is_null($value); + function ($value) : bool { + return is_null($value); } ) ); @@ -1751,8 +1818,11 @@ function ($value) { $this->assertTrue( Assertion::satisfy( true, + /** + * @return bool|void + */ function ($value) { - if (!\is_bool($value)) { + if (!is_bool($value)) { return false; } } @@ -1770,7 +1840,7 @@ public function testValidIp($value) $this->assertTrue(Assertion::ip($value)); } - public function validIpProvider() + public function validIpProvider(): array { return [ ['0.0.0.0'], @@ -1793,7 +1863,7 @@ public function testInvalidIp($value, $flag = null) Assertion::ip($value, $flag); } - public function invalidIpProvider() + public function invalidIpProvider(): array { return [ ['invalid ip address'], @@ -1839,7 +1909,7 @@ public function testInvalidInterfaceExists() public function testValidInterfaceExists() { - $this->assertTrue(Assertion::interfaceExists(\Countable::class)); + $this->assertTrue(Assertion::interfaceExists(Countable::class)); } /** @@ -1856,18 +1926,15 @@ public function testInvalidBetween($value, $lowerLimit, $upperLimit) Assertion::between($value, $lowerLimit, $upperLimit); } - /** - * @return array - */ - public function providerInvalidBetween() + public function providerInvalidBetween(): array { return [ [1, 2, 3], [3, 1, 2], ['aaa', 'bbb', 'ccc'], ['ddd', 'bbb', 'ccc'], - [new \DateTime('yesterday'), new \DateTime('today'), new \DateTime('tomorrow')], - [new \DateTime('tomorrow'), new \DateTime('yesterday'), new \DateTime('today')], + [new DateTime('yesterday'), new DateTime('today'), new DateTime('tomorrow')], + [new DateTime('tomorrow'), new DateTime('yesterday'), new DateTime('today')], ]; } @@ -1883,18 +1950,15 @@ public function testValidBetween($value, $lowerLimit, $upperLimit) $this->assertTrue(Assertion::between($value, $lowerLimit, $upperLimit)); } - /** - * @return array - */ - public function providerValidBetween() + public function providerValidBetween(): array { return [ [2, 1, 3], [1, 1, 1], ['bbb', 'aaa', 'ccc'], ['aaa', 'aaa', 'aaa'], - [new \DateTime('today'), new \DateTime('yesterday'), new \DateTime('tomorrow')], - [new \DateTime('today'), new \DateTime('today'), new \DateTime('today')], + [new DateTime('today'), new DateTime('yesterday'), new DateTime('tomorrow')], + [new DateTime('today'), new DateTime('today'), new DateTime('today')], ]; } @@ -1912,18 +1976,15 @@ public function testInvalidBetweenExclusive($value, $lowerLimit, $upperLimit) Assertion::betweenExclusive($value, $lowerLimit, $upperLimit); } - /** - * @return array - */ - public function providerInvalidBetweenExclusive() + public function providerInvalidBetweenExclusive(): array { return [ [1, 1, 2], [2, 1, 2], ['aaa', 'aaa', 'bbb'], ['bbb', 'aaa', 'bbb'], - [new \DateTime('today'), new \DateTime('today'), new \DateTime('tomorrow')], - [new \DateTime('tomorrow'), new \DateTime('today'), new \DateTime('tomorrow')], + [new DateTime('today'), new DateTime('today'), new DateTime('tomorrow')], + [new DateTime('tomorrow'), new DateTime('today'), new DateTime('tomorrow')], ]; } @@ -1939,24 +2000,29 @@ public function testValidBetweenExclusive($value, $lowerLimit, $upperLimit) $this->assertTrue(Assertion::betweenExclusive($value, $lowerLimit, $upperLimit)); } - /** - * @return array - */ - public function providerValidBetweenExclusive() + public function providerValidBetweenExclusive(): array { return [ [2, 1, 3], ['bbb', 'aaa', 'ccc'], - [new \DateTime('today'), new \DateTime('yesterday'), new \DateTime('tomorrow')], + [new DateTime('today'), new DateTime('yesterday'), new DateTime('tomorrow')], ]; } public function testStringifyTruncatesStringValuesLongerThan100CharactersAppropriately() { - $this->expectException('Assert\AssertionFailedException'); - $this->expectExceptionCode(\Assert\Assertion::INVALID_FLOAT); - $this->expectExceptionMessage('1234567...'); - $string = \str_repeat('1234567890', 11); + $string = str_repeat('1234567890', 11); + + $this->assertTrue(Assertion::float($string)); + } + + public function testStringifyTruncatesStringValuesLongerThan100CharactersAppropriatelyAndIsMultibyteValid() + { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_FLOAT); + $this->expectExceptionMessage('ငါကနံပါတ်မဟုတ်ဘူးငါကနံပါတ်မဟုတ်ဘူးငါကနံပါတ်မဟုတ်ဘူးငါကနံပါတ်မဟုတ်ဘူးငါကနံပါတ်မဟုတ်ဘူးငါကနံပါတ်မဟု...'); + + $string = str_repeat('ငါကနံပါတ်မဟုတ်ဘူး', 11); $this->assertTrue(Assertion::float($string)); } @@ -1966,7 +2032,7 @@ public function testStringifyReportsResourceType() $this->expectException('Assert\AssertionFailedException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_FLOAT); $this->expectExceptionMessage('stream'); - $this->assertTrue(Assertion::float(\fopen('php://stdin', 'rb'))); + $this->assertTrue(Assertion::float(fopen('php://stdin', 'rb'))); } public function testExtensionLoaded() @@ -1976,7 +2042,7 @@ public function testExtensionLoaded() public function testExtensionNotLoaded() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); Assertion::extensionLoaded('NOT_LOADED'); } @@ -1987,7 +2053,7 @@ public function testValidConstant() public function testInvalidConstant() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); Assertion::defined('NOT_A_CONSTANT'); } @@ -1998,13 +2064,13 @@ public function testValidVersion() public function testInvalidVersion() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); Assertion::version('1.0.0', 'eq', '2.0.0'); } public function testInvalidVersionOperator() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); Assertion::version('1.0.0', null, '2.0.0'); } @@ -2015,7 +2081,7 @@ public function testValidPhpVersion() public function testInvalidPhpVersion() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); Assertion::phpVersion('<', '5.0.0'); } @@ -2026,7 +2092,7 @@ public function testValidExtensionVersion() public function testInvalidExtensionVersion() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); Assertion::extensionVersion('json', '<', '0.1.0'); } @@ -2038,28 +2104,28 @@ public function testObjectOrClass() public function testNotObjectOrClass() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); Assertion::objectOrClass('InvalidClassName'); } public function testPropertyExists() { - self::assertTrue(Assertion::propertyExists(new \Exception(), 'message')); + self::assertTrue(Assertion::propertyExists(new Exception(), 'message')); } public function testInvalidPropertyExists() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_PROPERTY); - Assertion::propertyExists(new \Exception(), 'invalidProperty'); + Assertion::propertyExists(new Exception(), 'invalidProperty'); } public function testPropertiesExist() { - self::assertTrue(Assertion::propertiesExist(new \Exception(), ['message', 'code', 'previous'])); + self::assertTrue(Assertion::propertiesExist(new Exception(), ['message', 'code', 'previous'])); } - public function invalidPropertiesExistProvider() + public function invalidPropertiesExistProvider(): array { return [ [['invalidProperty']], @@ -2074,9 +2140,9 @@ public function invalidPropertiesExistProvider() */ public function testInvalidPropertiesExist($properties) { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_PROPERTY); - Assertion::propertiesExist(new \Exception(), $properties); + Assertion::propertiesExist(new Exception(), $properties); } public function testIsResource() @@ -2086,25 +2152,25 @@ public function testIsResource() public function testIsNotResource() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); Assertion::isResource(new stdClass()); } public function testBase64() { - $base64String = \base64_encode('content'); + $base64String = base64_encode('content'); $this->assertTrue(Assertion::base64($base64String)); } public function testNotBase64() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_BASE64); Assertion::base64('wrong-content'); } - public function invalidEqArraySubsetProvider() + public function invalidEqArraySubsetProvider(): array { return [ 'firstArgumentNotArray' => ['notArray', []], @@ -2139,7 +2205,7 @@ public function testEqArraySubsetValid() */ public function testEqArraySubsetInvalid($value, $value2) { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_ARRAY); Assertion::eqArraySubset($value, $value2); } @@ -2149,7 +2215,7 @@ public function testEqArraySubsetInvalid($value, $value2) */ public function testEqArraySubsetMismatchingSubset() { - $this->expectException('Assert\InvalidArgumentException'); + $this->expectException('InvalidArgumentException'); $this->expectExceptionCode(\Assert\Assertion::INVALID_EQ); Assertion::eqArraySubset( [ From 2f73d859d04424e9ff967d57f467cc7cbbba8a06 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 18 Apr 2021 22:06:05 +0200 Subject: [PATCH 08/24] Reintroduce expected exception assertion that got lost. --- tests/Assert/Tests/AssertTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index b79a986b..849d6441 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -2011,6 +2011,9 @@ public function providerValidBetweenExclusive(): array public function testStringifyTruncatesStringValuesLongerThan100CharactersAppropriately() { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_FLOAT); + $string = str_repeat('1234567890', 11); $this->assertTrue(Assertion::float($string)); From 5e721d7e937ca3ba2cdec1e1adf195f9e5188372 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 18 Apr 2021 22:11:03 +0200 Subject: [PATCH 09/24] Remove scrutinizer --- .scrutinizer.yml | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .scrutinizer.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index 87fda201..00000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,2 +0,0 @@ -tools: - external_code_coverage: true From dbc94e548dd88b58f8e2a5ced24a82b941676864 Mon Sep 17 00:00:00 2001 From: Frank van Hest Date: Thu, 16 Dec 2021 22:37:32 +0100 Subject: [PATCH 10/24] PHP 8.1 compatible (#320) Co-authored-by: Frank van Hest --- .github/workflows/ci.yml | 2 +- lib/Assert/Assertion.php | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04a76023..79636e6a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] + php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] steps: - name: Checkout diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 31382e5d..243e64d2 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -953,7 +953,7 @@ public static function startsWith($string, $needle, $message = null, string $pro { static::string($string, $message, $propertyPath); - if (0 !== \mb_strpos($string, $needle, null, $encoding)) { + if (0 !== \mb_strpos($string, $needle, 0, $encoding)) { $message = \sprintf( static::generateMessage($message ?: 'Value "%s" does not start with "%s".'), static::stringify($string), @@ -987,7 +987,7 @@ public static function endsWith($string, $needle, $message = null, string $prope $stringPosition = \mb_strlen($string, $encoding) - \mb_strlen($needle, $encoding); - if (\mb_strripos($string, $needle, null, $encoding) !== $stringPosition) { + if (\mb_strripos($string, $needle, 0, $encoding) !== $stringPosition) { $message = \sprintf( static::generateMessage($message ?: 'Value "%s" does not end with "%s".'), static::stringify($string), @@ -1019,7 +1019,7 @@ public static function contains($string, $needle, $message = null, string $prope { static::string($string, $message, $propertyPath); - if (false === \mb_strpos($string, $needle, null, $encoding)) { + if (false === \mb_strpos($string, $needle, 0, $encoding)) { $message = \sprintf( static::generateMessage($message ?: 'Value "%s" does not contain "%s".'), static::stringify($string), @@ -1051,7 +1051,7 @@ public static function notContains($string, $needle, $message = null, string $pr { static::string($string, $message, $propertyPath); - if (false !== \mb_strpos($string, $needle, null, $encoding)) { + if (false !== \mb_strpos($string, $needle, 0, $encoding)) { $message = \sprintf( static::generateMessage($message ?: 'Value "%s" contains "%s".'), static::stringify($string), @@ -2620,7 +2620,12 @@ public static function satisfy($value, $callback, $message = null, string $prope public static function ip($value, $flag = null, $message = null, string $propertyPath = null): bool { static::string($value, $message, $propertyPath); - if (!\filter_var($value, FILTER_VALIDATE_IP, $flag)) { + if ($flag === null) { + $filterVarResult = \filter_var($value, FILTER_VALIDATE_IP); + } else { + $filterVarResult = \filter_var($value, FILTER_VALIDATE_IP, $flag); + } + if (!$filterVarResult) { $message = \sprintf( static::generateMessage($message ?: 'Value "%s" was expected to be a valid IP address.'), static::stringify($value) From fc352a303fd833f8d4f1c7f7040820d86b13ddab Mon Sep 17 00:00:00 2001 From: Marcin Czarnecki Date: Thu, 16 Dec 2021 22:39:48 +0100 Subject: [PATCH 11/24] Improve performance of Assertion Chain class (#318) --- lib/Assert/AssertionChain.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/Assert/AssertionChain.php b/lib/Assert/AssertionChain.php index b3f8fa5e..4c444350 100644 --- a/lib/Assert/AssertionChain.php +++ b/lib/Assert/AssertionChain.php @@ -15,7 +15,6 @@ namespace Assert; use LogicException; -use ReflectionClass; /** * Chaining builder for assertions. @@ -171,13 +170,12 @@ public function __call($methodName, $args): AssertionChain return $this; } - if (!\method_exists($this->assertionClassName, $methodName)) { + try { + $method = new \ReflectionMethod($this->assertionClassName, $methodName); + } catch (\ReflectionException $exception) { throw new \RuntimeException("Assertion '".$methodName."' does not exist."); } - $reflClass = new ReflectionClass($this->assertionClassName); - $method = $reflClass->getMethod($methodName); - \array_unshift($args, $this->value); $params = $method->getParameters(); @@ -186,12 +184,13 @@ public function __call($methodName, $args): AssertionChain continue; } - if ('message' == $param->getName()) { - $args[$idx] = $this->defaultMessage; - } - - if ('propertyPath' == $param->getName()) { - $args[$idx] = $this->defaultPropertyPath; + switch ($param->getName()) { + case 'message': + $args[$idx] = $this->defaultMessage; + break; + case 'propertyPath': + $args[$idx] = $this->defaultPropertyPath; + break; } } From cb70015c04be1baee6f5f5c953703347c0ac1655 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 16 Dec 2021 22:41:27 +0100 Subject: [PATCH 12/24] Run tests on pull_request --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 79636e6a..7003ff49 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -on: push +on: [push, pull_request] name: CI jobs: tests: From e74044d22ae014685a8e20122475025108ebc125 Mon Sep 17 00:00:00 2001 From: Grzegorz Korba Date: Tue, 27 Sep 2022 10:24:15 +0200 Subject: [PATCH 13/24] Allow any value to be passed to Assertion::isCountable() (#303) * Allow any value to be passed to Assertion::isCountable() Static analysis tools complain about type of `$value` argument but in fact it could be of any type since it will be checked later (that's the purpose of the method). * Revert unwanted changes Github's web IDE did something weird I think, I did not change those line on purpose (obviously). --- lib/Assert/Assertion.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 243e64d2..3c2f0d7c 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -1233,7 +1233,7 @@ public static function isArrayAccessible($value, $message = null, string $proper /** * Assert that value is countable. * - * @param array|Countable|ResourceBundle|SimpleXMLElement $value + * @param mixed $value * @param string|callable|null $message * @param string|null $propertyPath * From 60313415c987302b158c47a2914218a8b8441828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Vil=C3=A0?= Date: Sat, 25 Feb 2023 22:27:33 +0100 Subject: [PATCH 14/24] Update ci.yml (#332) --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7003ff49..ae632756 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] + php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] steps: - name: Checkout From be26899dc2d5daceb188632d306a4432a4eb7088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 19 Mar 2024 08:49:15 +0100 Subject: [PATCH 15/24] Fix typo in CONTRIBUTING.md (#327) --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 520b0ba2..db6175dc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ # How to contribute -Thanks for contributing to assert! Just follow these single guidelines: +Thanks for contributing to assert! Just follow these simple guidelines: - You must use [feature / topic branches](https://git-scm.com/book/en/v2/Git-Branching-Branching-Workflows) to ease the merge of contributions. - Coding standard compliance must be ensured before committing or opening pull requests by running `composer assert:cs-fix` or `composer assert:cs-lint` in the root directory of this repository. From cc6400bd3bc3c8dc4370ef7245de6c681f722ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 19 Mar 2024 08:49:31 +0100 Subject: [PATCH 16/24] Add `.github/ export-ignore` to `.gitattributes` (#326) --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index c3d63168..d01eb838 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,6 +3,7 @@ .editorconfig export-ignore .gitattributes export-ignore +.github/ export-ignore .gitignore export-ignore .php_cs export-ignore .travis/ export-ignore From 9eb17b7be8c2ee41e658d17decc3456f8c1456fb Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Tue, 23 Apr 2024 20:21:12 +0530 Subject: [PATCH 17/24] Bump minimum PHP requirement to PHP 7.1 (#338) PR #337 depends on this. Bumps the minimum required PHP version to PHP 7.1 to use explicit nullable parameter type syntax. --- .github/workflows/ci.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae632756..82919582 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] + php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] steps: - name: Checkout diff --git a/composer.json b/composer.json index 1485a008..cbf18a95 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "sort-packages": true }, "require": { - "php": "^7.0 || ^8.0", + "php": "^7.1 || ^8.0", "ext-simplexml": "*", "ext-mbstring": "*", "ext-ctype": "*", From 78aa6be746ca3b3337e19256696c111268ccce10 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Tue, 23 Apr 2024 20:22:45 +0530 Subject: [PATCH 18/24] [PHP 8.4] Fixes for implicit nullability deprecation (#337) Fixes all issues that emit deprecation notices on PHP 8.4 for implicit nullable parameter type declarations. See: - [RFC](https://wiki.php.net/rfc/deprecate-implicitly-nullable-types) - [PHP 8.4: Implicitly nullable parameter declarations deprecated](https://php.watch/versions/8.4/implicitly-marking-parameter-type-nullable-deprecated) --- lib/Assert/Assert.php | 6 +- lib/Assert/Assertion.php | 178 +++++++++--------- lib/Assert/AssertionChain.php | 2 +- lib/Assert/InvalidArgumentException.php | 2 +- lib/Assert/LazyAssertion.php | 2 +- lib/Assert/functions.php | 6 +- .../Assert/Tests/Fixtures/CustomAssertion.php | 2 +- 7 files changed, 99 insertions(+), 99 deletions(-) diff --git a/lib/Assert/Assert.php b/lib/Assert/Assert.php index 3614b345..201bce57 100644 --- a/lib/Assert/Assert.php +++ b/lib/Assert/Assert.php @@ -42,7 +42,7 @@ abstract class Assert * The assertion chain can be stateful, that means be careful when you reuse * it. You should never pass around the chain. */ - public static function that($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain + public static function that($value, $defaultMessage = null, ?string $defaultPropertyPath = null): AssertionChain { $assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath); @@ -55,7 +55,7 @@ public static function that($value, $defaultMessage = null, string $defaultPrope * @param mixed $values * @param string|callable|null $defaultMessage */ - public static function thatAll($values, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain + public static function thatAll($values, $defaultMessage = null, ?string $defaultPropertyPath = null): AssertionChain { return static::that($values, $defaultMessage, $defaultPropertyPath)->all(); } @@ -66,7 +66,7 @@ public static function thatAll($values, $defaultMessage = null, string $defaultP * @param mixed $value * @param string|callable|null $defaultMessage */ - public static function thatNullOr($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain + public static function thatNullOr($value, $defaultMessage = null, ?string $defaultPropertyPath = null): AssertionChain { return static::that($value, $defaultMessage, $defaultPropertyPath)->nullOr(); } diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 3c2f0d7c..81bc9780 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -307,7 +307,7 @@ class Assertion * * @throws AssertionFailedException */ - public static function eq($value, $value2, $message = null, string $propertyPath = null): bool + public static function eq($value, $value2, $message = null, ?string $propertyPath = null): bool { if ($value != $value2) { $message = \sprintf( @@ -331,7 +331,7 @@ public static function eq($value, $value2, $message = null, string $propertyPath * * @throws AssertionFailedException */ - public static function eqArraySubset($value, $value2, $message = null, string $propertyPath = null): bool + public static function eqArraySubset($value, $value2, $message = null, ?string $propertyPath = null): bool { static::isArray($value, $message, $propertyPath); static::isArray($value2, $message, $propertyPath); @@ -358,7 +358,7 @@ public static function eqArraySubset($value, $value2, $message = null, string $p * * @throws AssertionFailedException */ - public static function same($value, $value2, $message = null, string $propertyPath = null): bool + public static function same($value, $value2, $message = null, ?string $propertyPath = null): bool { if ($value !== $value2) { $message = \sprintf( @@ -382,7 +382,7 @@ public static function same($value, $value2, $message = null, string $propertyPa * * @throws AssertionFailedException */ - public static function notEq($value1, $value2, $message = null, string $propertyPath = null): bool + public static function notEq($value1, $value2, $message = null, ?string $propertyPath = null): bool { if ($value1 == $value2) { $message = \sprintf( @@ -412,7 +412,7 @@ public static function notEq($value1, $value2, $message = null, string $property * * @throws AssertionFailedException */ - public static function notSame($value1, $value2, $message = null, string $propertyPath = null): bool + public static function notSame($value1, $value2, $message = null, ?string $propertyPath = null): bool { if ($value1 === $value2) { $message = \sprintf( @@ -434,7 +434,7 @@ public static function notSame($value1, $value2, $message = null, string $proper * * @throws AssertionFailedException */ - public static function notInArray($value, array $choices, $message = null, string $propertyPath = null): bool + public static function notInArray($value, array $choices, $message = null, ?string $propertyPath = null): bool { if (true === \in_array($value, $choices)) { $message = \sprintf( @@ -461,7 +461,7 @@ public static function notInArray($value, array $choices, $message = null, strin * * @throws AssertionFailedException */ - public static function integer($value, $message = null, string $propertyPath = null): bool + public static function integer($value, $message = null, ?string $propertyPath = null): bool { if (!\is_int($value)) { $message = \sprintf( @@ -488,7 +488,7 @@ public static function integer($value, $message = null, string $propertyPath = n * * @throws AssertionFailedException */ - public static function float($value, $message = null, string $propertyPath = null): bool + public static function float($value, $message = null, ?string $propertyPath = null): bool { if (!\is_float($value)) { $message = \sprintf( @@ -515,7 +515,7 @@ public static function float($value, $message = null, string $propertyPath = nul * * @throws AssertionFailedException */ - public static function digit($value, $message = null, string $propertyPath = null): bool + public static function digit($value, $message = null, ?string $propertyPath = null): bool { if (!\ctype_digit((string)$value)) { $message = \sprintf( @@ -537,7 +537,7 @@ public static function digit($value, $message = null, string $propertyPath = nul * * @throws AssertionFailedException */ - public static function integerish($value, $message = null, string $propertyPath = null): bool + public static function integerish($value, $message = null, ?string $propertyPath = null): bool { if ( \is_resource($value) || @@ -577,7 +577,7 @@ public static function integerish($value, $message = null, string $propertyPath * * @throws AssertionFailedException */ - public static function boolean($value, $message = null, string $propertyPath = null): bool + public static function boolean($value, $message = null, ?string $propertyPath = null): bool { if (!\is_bool($value)) { $message = \sprintf( @@ -604,7 +604,7 @@ public static function boolean($value, $message = null, string $propertyPath = n * * @throws AssertionFailedException */ - public static function scalar($value, $message = null, string $propertyPath = null): bool + public static function scalar($value, $message = null, ?string $propertyPath = null): bool { if (!\is_scalar($value)) { $message = \sprintf( @@ -631,7 +631,7 @@ public static function scalar($value, $message = null, string $propertyPath = nu * * @throws AssertionFailedException */ - public static function notEmpty($value, $message = null, string $propertyPath = null): bool + public static function notEmpty($value, $message = null, ?string $propertyPath = null): bool { if (empty($value)) { $message = \sprintf( @@ -658,7 +658,7 @@ public static function notEmpty($value, $message = null, string $propertyPath = * * @throws AssertionFailedException */ - public static function noContent($value, $message = null, string $propertyPath = null): bool + public static function noContent($value, $message = null, ?string $propertyPath = null): bool { if (!empty($value)) { $message = \sprintf( @@ -683,7 +683,7 @@ public static function noContent($value, $message = null, string $propertyPath = * * @return bool */ - public static function null($value, $message = null, string $propertyPath = null): bool + public static function null($value, $message = null, ?string $propertyPath = null): bool { if (null !== $value) { $message = \sprintf( @@ -710,7 +710,7 @@ public static function null($value, $message = null, string $propertyPath = null * * @throws AssertionFailedException */ - public static function notNull($value, $message = null, string $propertyPath = null): bool + public static function notNull($value, $message = null, ?string $propertyPath = null): bool { if (null === $value) { $message = \sprintf( @@ -737,7 +737,7 @@ public static function notNull($value, $message = null, string $propertyPath = n * * @throws AssertionFailedException */ - public static function string($value, $message = null, string $propertyPath = null) + public static function string($value, $message = null, ?string $propertyPath = null) { if (!\is_string($value)) { $message = \sprintf( @@ -766,7 +766,7 @@ public static function string($value, $message = null, string $propertyPath = nu * * @throws AssertionFailedException */ - public static function regex($value, $pattern, $message = null, string $propertyPath = null): bool + public static function regex($value, $pattern, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); @@ -794,7 +794,7 @@ public static function regex($value, $pattern, $message = null, string $property * * @throws AssertionFailedException */ - public static function notRegex($value, $pattern, $message = null, string $propertyPath = null): bool + public static function notRegex($value, $pattern, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); @@ -825,7 +825,7 @@ public static function notRegex($value, $pattern, $message = null, string $prope * * @throws AssertionFailedException */ - public static function length($value, $length, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool + public static function length($value, $length, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool { static::string($value, $message, $propertyPath); @@ -858,7 +858,7 @@ public static function length($value, $length, $message = null, string $property * * @throws AssertionFailedException */ - public static function minLength($value, $minLength, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool + public static function minLength($value, $minLength, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool { static::string($value, $message, $propertyPath); @@ -891,7 +891,7 @@ public static function minLength($value, $minLength, $message = null, string $pr * * @throws AssertionFailedException */ - public static function maxLength($value, $maxLength, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool + public static function maxLength($value, $maxLength, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool { static::string($value, $message, $propertyPath); @@ -925,7 +925,7 @@ public static function maxLength($value, $maxLength, $message = null, string $pr * * @throws AssertionFailedException */ - public static function betweenLength($value, $minLength, $maxLength, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool + public static function betweenLength($value, $minLength, $maxLength, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool { static::string($value, $message, $propertyPath); static::minLength($value, $minLength, $message, $propertyPath, $encoding); @@ -949,7 +949,7 @@ public static function betweenLength($value, $minLength, $maxLength, $message = * * @throws AssertionFailedException */ - public static function startsWith($string, $needle, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool + public static function startsWith($string, $needle, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool { static::string($string, $message, $propertyPath); @@ -981,7 +981,7 @@ public static function startsWith($string, $needle, $message = null, string $pro * * @throws AssertionFailedException */ - public static function endsWith($string, $needle, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool + public static function endsWith($string, $needle, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool { static::string($string, $message, $propertyPath); @@ -1015,7 +1015,7 @@ public static function endsWith($string, $needle, $message = null, string $prope * * @throws AssertionFailedException */ - public static function contains($string, $needle, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool + public static function contains($string, $needle, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool { static::string($string, $message, $propertyPath); @@ -1047,7 +1047,7 @@ public static function contains($string, $needle, $message = null, string $prope * * @throws AssertionFailedException */ - public static function notContains($string, $needle, $message = null, string $propertyPath = null, $encoding = 'utf8'): bool + public static function notContains($string, $needle, $message = null, ?string $propertyPath = null, $encoding = 'utf8'): bool { static::string($string, $message, $propertyPath); @@ -1072,7 +1072,7 @@ public static function notContains($string, $needle, $message = null, string $pr * * @throws AssertionFailedException */ - public static function choice($value, array $choices, $message = null, string $propertyPath = null): bool + public static function choice($value, array $choices, $message = null, ?string $propertyPath = null): bool { if (!\in_array($value, $choices, true)) { $message = \sprintf( @@ -1097,7 +1097,7 @@ public static function choice($value, array $choices, $message = null, string $p * * @throws AssertionFailedException */ - public static function inArray($value, array $choices, $message = null, string $propertyPath = null): bool + public static function inArray($value, array $choices, $message = null, ?string $propertyPath = null): bool { return static::choice($value, $choices, $message, $propertyPath); } @@ -1115,7 +1115,7 @@ public static function inArray($value, array $choices, $message = null, string $ * * @throws AssertionFailedException */ - public static function numeric($value, $message = null, string $propertyPath = null): bool + public static function numeric($value, $message = null, ?string $propertyPath = null): bool { if (!\is_numeric($value)) { $message = \sprintf( @@ -1140,7 +1140,7 @@ public static function numeric($value, $message = null, string $propertyPath = n * * @return bool */ - public static function isResource($value, $message = null, string $propertyPath = null): bool + public static function isResource($value, $message = null, ?string $propertyPath = null): bool { if (!\is_resource($value)) { $message = \sprintf( @@ -1167,7 +1167,7 @@ public static function isResource($value, $message = null, string $propertyPath * * @throws AssertionFailedException */ - public static function isArray($value, $message = null, string $propertyPath = null): bool + public static function isArray($value, $message = null, ?string $propertyPath = null): bool { if (!\is_array($value)) { $message = \sprintf( @@ -1194,7 +1194,7 @@ public static function isArray($value, $message = null, string $propertyPath = n * * @throws AssertionFailedException */ - public static function isTraversable($value, $message = null, string $propertyPath = null): bool + public static function isTraversable($value, $message = null, ?string $propertyPath = null): bool { if (!\is_array($value) && !$value instanceof Traversable) { $message = \sprintf( @@ -1216,7 +1216,7 @@ public static function isTraversable($value, $message = null, string $propertyPa * * @throws AssertionFailedException */ - public static function isArrayAccessible($value, $message = null, string $propertyPath = null): bool + public static function isArrayAccessible($value, $message = null, ?string $propertyPath = null): bool { if (!\is_array($value) && !$value instanceof ArrayAccess) { $message = \sprintf( @@ -1243,7 +1243,7 @@ public static function isArrayAccessible($value, $message = null, string $proper * * @throws AssertionFailedException */ - public static function isCountable($value, $message = null, string $propertyPath = null): bool + public static function isCountable($value, $message = null, ?string $propertyPath = null): bool { if (\function_exists('is_countable')) { $assert = \is_countable($value); @@ -1272,7 +1272,7 @@ public static function isCountable($value, $message = null, string $propertyPath * * @throws AssertionFailedException */ - public static function keyExists($value, $key, $message = null, string $propertyPath = null): bool + public static function keyExists($value, $key, $message = null, ?string $propertyPath = null): bool { static::isArray($value, $message, $propertyPath); @@ -1297,7 +1297,7 @@ public static function keyExists($value, $key, $message = null, string $property * * @throws AssertionFailedException */ - public static function keyNotExists($value, $key, $message = null, string $propertyPath = null): bool + public static function keyNotExists($value, $key, $message = null, ?string $propertyPath = null): bool { static::isArray($value, $message, $propertyPath); @@ -1321,7 +1321,7 @@ public static function keyNotExists($value, $key, $message = null, string $prope * * @throws AssertionFailedException */ - public static function uniqueValues(array $values, $message = null, string $propertyPath = null): bool + public static function uniqueValues(array $values, $message = null, ?string $propertyPath = null): bool { foreach ($values as $key => $value) { if (\array_search($value, $values, true) !== $key) { @@ -1346,7 +1346,7 @@ public static function uniqueValues(array $values, $message = null, string $prop * * @throws AssertionFailedException */ - public static function keyIsset($value, $key, $message = null, string $propertyPath = null): bool + public static function keyIsset($value, $key, $message = null, ?string $propertyPath = null): bool { static::isArrayAccessible($value, $message, $propertyPath); @@ -1371,7 +1371,7 @@ public static function keyIsset($value, $key, $message = null, string $propertyP * * @throws AssertionFailedException */ - public static function notEmptyKey($value, $key, $message = null, string $propertyPath = null): bool + public static function notEmptyKey($value, $key, $message = null, ?string $propertyPath = null): bool { static::keyIsset($value, $key, $message, $propertyPath); static::notEmpty($value[$key], $message, $propertyPath); @@ -1387,7 +1387,7 @@ public static function notEmptyKey($value, $key, $message = null, string $proper * * @throws AssertionFailedException */ - public static function notBlank($value, $message = null, string $propertyPath = null): bool + public static function notBlank($value, $message = null, ?string $propertyPath = null): bool { if (false === $value || (empty($value) && '0' != $value) || (\is_string($value) && '' === \trim($value))) { $message = \sprintf( @@ -1417,7 +1417,7 @@ public static function notBlank($value, $message = null, string $propertyPath = * * @throws AssertionFailedException */ - public static function isInstanceOf($value, $className, $message = null, string $propertyPath = null): bool + public static function isInstanceOf($value, $className, $message = null, ?string $propertyPath = null): bool { if (!($value instanceof $className)) { $message = \sprintf( @@ -1448,7 +1448,7 @@ public static function isInstanceOf($value, $className, $message = null, string * * @throws AssertionFailedException */ - public static function notIsInstanceOf($value, $className, $message = null, string $propertyPath = null): bool + public static function notIsInstanceOf($value, $className, $message = null, ?string $propertyPath = null): bool { if ($value instanceof $className) { $message = \sprintf( @@ -1472,7 +1472,7 @@ public static function notIsInstanceOf($value, $className, $message = null, stri * * @throws AssertionFailedException */ - public static function subclassOf($value, $className, $message = null, string $propertyPath = null): bool + public static function subclassOf($value, $className, $message = null, ?string $propertyPath = null): bool { if (!\is_subclass_of($value, $className)) { $message = \sprintf( @@ -1502,7 +1502,7 @@ public static function subclassOf($value, $className, $message = null, string $p * * @throws AssertionFailedException */ - public static function range($value, $minValue, $maxValue, $message = null, string $propertyPath = null): bool + public static function range($value, $minValue, $maxValue, $message = null, ?string $propertyPath = null): bool { static::numeric($value, $message, $propertyPath); @@ -1534,7 +1534,7 @@ public static function range($value, $minValue, $maxValue, $message = null, stri * * @throws AssertionFailedException */ - public static function min($value, $minValue, $message = null, string $propertyPath = null): bool + public static function min($value, $minValue, $message = null, ?string $propertyPath = null): bool { static::numeric($value, $message, $propertyPath); @@ -1565,7 +1565,7 @@ public static function min($value, $minValue, $message = null, string $propertyP * * @throws AssertionFailedException */ - public static function max($value, $maxValue, $message = null, string $propertyPath = null): bool + public static function max($value, $maxValue, $message = null, ?string $propertyPath = null): bool { static::numeric($value, $message, $propertyPath); @@ -1590,7 +1590,7 @@ public static function max($value, $maxValue, $message = null, string $propertyP * * @throws AssertionFailedException */ - public static function file($value, $message = null, string $propertyPath = null): bool + public static function file($value, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); static::notEmpty($value, $message, $propertyPath); @@ -1615,7 +1615,7 @@ public static function file($value, $message = null, string $propertyPath = null * * @throws AssertionFailedException */ - public static function directory($value, $message = null, string $propertyPath = null): bool + public static function directory($value, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); @@ -1639,7 +1639,7 @@ public static function directory($value, $message = null, string $propertyPath = * * @throws AssertionFailedException */ - public static function readable($value, $message = null, string $propertyPath = null): bool + public static function readable($value, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); @@ -1663,7 +1663,7 @@ public static function readable($value, $message = null, string $propertyPath = * * @throws AssertionFailedException */ - public static function writeable($value, $message = null, string $propertyPath = null): bool + public static function writeable($value, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); @@ -1692,7 +1692,7 @@ public static function writeable($value, $message = null, string $propertyPath = * * @throws AssertionFailedException */ - public static function email($value, $message = null, string $propertyPath = null): bool + public static function email($value, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); @@ -1726,7 +1726,7 @@ public static function email($value, $message = null, string $propertyPath = nul * @see https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php * @see https://github.com/symfony/Validator/blob/master/Constraints/Url.php */ - public static function url($value, $message = null, string $propertyPath = null): bool + public static function url($value, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); @@ -1772,7 +1772,7 @@ public static function url($value, $message = null, string $propertyPath = null) * * @throws AssertionFailedException */ - public static function alnum($value, $message = null, string $propertyPath = null): bool + public static function alnum($value, $message = null, ?string $propertyPath = null): bool { try { static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath); @@ -1801,7 +1801,7 @@ public static function alnum($value, $message = null, string $propertyPath = nul * * @throws AssertionFailedException */ - public static function true($value, $message = null, string $propertyPath = null): bool + public static function true($value, $message = null, ?string $propertyPath = null): bool { if (true !== $value) { $message = \sprintf( @@ -1828,7 +1828,7 @@ public static function true($value, $message = null, string $propertyPath = null * * @throws AssertionFailedException */ - public static function false($value, $message = null, string $propertyPath = null): bool + public static function false($value, $message = null, ?string $propertyPath = null): bool { if (false !== $value) { $message = \sprintf( @@ -1855,7 +1855,7 @@ public static function false($value, $message = null, string $propertyPath = nul * * @throws AssertionFailedException */ - public static function classExists($value, $message = null, string $propertyPath = null): bool + public static function classExists($value, $message = null, ?string $propertyPath = null): bool { if (!\class_exists($value)) { $message = \sprintf( @@ -1882,7 +1882,7 @@ public static function classExists($value, $message = null, string $propertyPath * * @throws AssertionFailedException */ - public static function interfaceExists($value, $message = null, string $propertyPath = null): bool + public static function interfaceExists($value, $message = null, ?string $propertyPath = null): bool { if (!\interface_exists($value)) { $message = \sprintf( @@ -1905,7 +1905,7 @@ public static function interfaceExists($value, $message = null, string $property * * @throws AssertionFailedException */ - public static function implementsInterface($class, $interfaceName, $message = null, string $propertyPath = null): bool + public static function implementsInterface($class, $interfaceName, $message = null, ?string $propertyPath = null): bool { try { $reflection = new ReflectionClass($class); @@ -1948,7 +1948,7 @@ public static function implementsInterface($class, $interfaceName, $message = nu * * @throws AssertionFailedException */ - public static function isJsonString($value, $message = null, string $propertyPath = null): bool + public static function isJsonString($value, $message = null, ?string $propertyPath = null): bool { if (null === \json_decode($value) && JSON_ERROR_NONE !== \json_last_error()) { $message = \sprintf( @@ -1972,7 +1972,7 @@ public static function isJsonString($value, $message = null, string $propertyPat * * @throws AssertionFailedException */ - public static function uuid($value, $message = null, string $propertyPath = null): bool + public static function uuid($value, $message = null, ?string $propertyPath = null): bool { $value = \str_replace(['urn:', 'uuid:', '{', '}'], '', $value); @@ -2002,7 +2002,7 @@ public static function uuid($value, $message = null, string $propertyPath = null * * @throws AssertionFailedException */ - public static function e164($value, $message = null, string $propertyPath = null): bool + public static function e164($value, $message = null, ?string $propertyPath = null): bool { if (!\preg_match('/^\+?[1-9]\d{1,14}$/', $value)) { $message = \sprintf( @@ -2028,7 +2028,7 @@ public static function e164($value, $message = null, string $propertyPath = null * * @throws AssertionFailedException */ - public static function count($countable, $count, $message = null, string $propertyPath = null): bool + public static function count($countable, $count, $message = null, ?string $propertyPath = null): bool { if ($count !== \count($countable)) { $message = \sprintf( @@ -2052,7 +2052,7 @@ public static function count($countable, $count, $message = null, string $proper * * @throws AssertionFailedException */ - public static function minCount($countable, $count, $message = null, string $propertyPath = null): bool + public static function minCount($countable, $count, $message = null, ?string $propertyPath = null): bool { if ($count > \count($countable)) { $message = \sprintf( @@ -2076,7 +2076,7 @@ public static function minCount($countable, $count, $message = null, string $pro * * @throws AssertionFailedException */ - public static function maxCount($countable, $count, $message = null, string $propertyPath = null): bool + public static function maxCount($countable, $count, $message = null, ?string $propertyPath = null): bool { if ($count < \count($countable)) { $message = \sprintf( @@ -2147,7 +2147,7 @@ public static function __callStatic($method, $args) * * @throws AssertionFailedException */ - public static function choicesNotEmpty(array $values, array $choices, $message = null, string $propertyPath = null): bool + public static function choicesNotEmpty(array $values, array $choices, $message = null, ?string $propertyPath = null): bool { static::notEmpty($values, $message, $propertyPath); @@ -2167,7 +2167,7 @@ public static function choicesNotEmpty(array $values, array $choices, $message = * * @throws AssertionFailedException */ - public static function methodExists($value, $object, $message = null, string $propertyPath = null): bool + public static function methodExists($value, $object, $message = null, ?string $propertyPath = null): bool { static::isObject($object, $message, $propertyPath); @@ -2196,7 +2196,7 @@ public static function methodExists($value, $object, $message = null, string $pr * * @throws AssertionFailedException */ - public static function isObject($value, $message = null, string $propertyPath = null): bool + public static function isObject($value, $message = null, ?string $propertyPath = null): bool { if (!\is_object($value)) { $message = \sprintf( @@ -2219,7 +2219,7 @@ public static function isObject($value, $message = null, string $propertyPath = * * @throws AssertionFailedException */ - public static function lessThan($value, $limit, $message = null, string $propertyPath = null): bool + public static function lessThan($value, $limit, $message = null, ?string $propertyPath = null): bool { if ($value >= $limit) { $message = \sprintf( @@ -2243,7 +2243,7 @@ public static function lessThan($value, $limit, $message = null, string $propert * * @throws AssertionFailedException */ - public static function lessOrEqualThan($value, $limit, $message = null, string $propertyPath = null): bool + public static function lessOrEqualThan($value, $limit, $message = null, ?string $propertyPath = null): bool { if ($value > $limit) { $message = \sprintf( @@ -2267,7 +2267,7 @@ public static function lessOrEqualThan($value, $limit, $message = null, string $ * * @throws AssertionFailedException */ - public static function greaterThan($value, $limit, $message = null, string $propertyPath = null): bool + public static function greaterThan($value, $limit, $message = null, ?string $propertyPath = null): bool { if ($value <= $limit) { $message = \sprintf( @@ -2291,7 +2291,7 @@ public static function greaterThan($value, $limit, $message = null, string $prop * * @throws AssertionFailedException */ - public static function greaterOrEqualThan($value, $limit, $message = null, string $propertyPath = null): bool + public static function greaterOrEqualThan($value, $limit, $message = null, ?string $propertyPath = null): bool { if ($value < $limit) { $message = \sprintf( @@ -2317,7 +2317,7 @@ public static function greaterOrEqualThan($value, $limit, $message = null, strin * * @throws AssertionFailedException */ - public static function between($value, $lowerLimit, $upperLimit, $message = null, string $propertyPath = null): bool + public static function between($value, $lowerLimit, $upperLimit, $message = null, ?string $propertyPath = null): bool { if ($lowerLimit > $value || $value > $upperLimit) { $message = \sprintf( @@ -2344,7 +2344,7 @@ public static function between($value, $lowerLimit, $upperLimit, $message = null * * @throws AssertionFailedException */ - public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, string $propertyPath = null): bool + public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, ?string $propertyPath = null): bool { if ($lowerLimit >= $value || $value >= $upperLimit) { $message = \sprintf( @@ -2368,7 +2368,7 @@ public static function betweenExclusive($value, $lowerLimit, $upperLimit, $messa * * @throws AssertionFailedException */ - public static function extensionLoaded($value, $message = null, string $propertyPath = null): bool + public static function extensionLoaded($value, $message = null, ?string $propertyPath = null): bool { if (!\extension_loaded($value)) { $message = \sprintf( @@ -2394,7 +2394,7 @@ public static function extensionLoaded($value, $message = null, string $property * * @see http://php.net/manual/function.date.php#refsect1-function.date-parameters */ - public static function date($value, $format, $message = null, string $propertyPath = null): bool + public static function date($value, $format, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); static::string($format, $message, $propertyPath); @@ -2422,7 +2422,7 @@ public static function date($value, $format, $message = null, string $propertyPa * * @throws AssertionFailedException */ - public static function objectOrClass($value, $message = null, string $propertyPath = null): bool + public static function objectOrClass($value, $message = null, ?string $propertyPath = null): bool { if (!\is_object($value)) { static::classExists($value, $message, $propertyPath); @@ -2440,7 +2440,7 @@ public static function objectOrClass($value, $message = null, string $propertyPa * * @throws AssertionFailedException */ - public static function propertyExists($value, $property, $message = null, string $propertyPath = null): bool + public static function propertyExists($value, $property, $message = null, ?string $propertyPath = null): bool { static::objectOrClass($value); @@ -2465,7 +2465,7 @@ public static function propertyExists($value, $property, $message = null, string * * @throws AssertionFailedException */ - public static function propertiesExist($value, array $properties, $message = null, string $propertyPath = null): bool + public static function propertiesExist($value, array $properties, $message = null, ?string $propertyPath = null): bool { static::objectOrClass($value); static::allString($properties, $message, $propertyPath); @@ -2500,7 +2500,7 @@ public static function propertiesExist($value, array $properties, $message = nul * * @throws AssertionFailedException */ - public static function version($version1, $operator, $version2, $message = null, string $propertyPath = null): bool + public static function version($version1, $operator, $version2, $message = null, ?string $propertyPath = null): bool { static::notEmpty($operator, 'versionCompare operator is required and cannot be empty.'); @@ -2527,7 +2527,7 @@ public static function version($version1, $operator, $version2, $message = null, * * @throws AssertionFailedException */ - public static function phpVersion($operator, $version, $message = null, string $propertyPath = null): bool + public static function phpVersion($operator, $version, $message = null, ?string $propertyPath = null): bool { static::defined('PHP_VERSION'); @@ -2544,7 +2544,7 @@ public static function phpVersion($operator, $version, $message = null, string $ * * @throws AssertionFailedException */ - public static function extensionVersion($extension, $operator, $version, $message = null, string $propertyPath = null): bool + public static function extensionVersion($extension, $operator, $version, $message = null, ?string $propertyPath = null): bool { static::extensionLoaded($extension, $message, $propertyPath); @@ -2564,7 +2564,7 @@ public static function extensionVersion($extension, $operator, $version, $messag * * @throws AssertionFailedException */ - public static function isCallable($value, $message = null, string $propertyPath = null): bool + public static function isCallable($value, $message = null, ?string $propertyPath = null): bool { if (!\is_callable($value)) { $message = \sprintf( @@ -2589,7 +2589,7 @@ public static function isCallable($value, $message = null, string $propertyPath * * @throws AssertionFailedException */ - public static function satisfy($value, $callback, $message = null, string $propertyPath = null): bool + public static function satisfy($value, $callback, $message = null, ?string $propertyPath = null): bool { static::isCallable($callback); @@ -2617,7 +2617,7 @@ public static function satisfy($value, $callback, $message = null, string $prope * * @see http://php.net/manual/filter.filters.flags.php */ - public static function ip($value, $flag = null, $message = null, string $propertyPath = null): bool + public static function ip($value, $flag = null, $message = null, ?string $propertyPath = null): bool { static::string($value, $message, $propertyPath); if ($flag === null) { @@ -2648,7 +2648,7 @@ public static function ip($value, $flag = null, $message = null, string $propert * * @see http://php.net/manual/filter.filters.flags.php */ - public static function ipv4($value, $flag = null, $message = null, string $propertyPath = null): bool + public static function ipv4($value, $flag = null, $message = null, ?string $propertyPath = null): bool { static::ip($value, $flag | FILTER_FLAG_IPV4, static::generateMessage($message ?: 'Value "%s" was expected to be a valid IPv4 address.'), $propertyPath); @@ -2667,7 +2667,7 @@ public static function ipv4($value, $flag = null, $message = null, string $prope * * @see http://php.net/manual/filter.filters.flags.php */ - public static function ipv6($value, $flag = null, $message = null, string $propertyPath = null): bool + public static function ipv6($value, $flag = null, $message = null, ?string $propertyPath = null): bool { static::ip($value, $flag | FILTER_FLAG_IPV6, static::generateMessage($message ?: 'Value "%s" was expected to be a valid IPv6 address.'), $propertyPath); @@ -2680,7 +2680,7 @@ public static function ipv6($value, $flag = null, $message = null, string $prope * @param mixed $constant * @param string|callable|null $message */ - public static function defined($constant, $message = null, string $propertyPath = null): bool + public static function defined($constant, $message = null, ?string $propertyPath = null): bool { if (!\defined($constant)) { $message = \sprintf(static::generateMessage($message ?: 'Value "%s" expected to be a defined constant.'), $constant); @@ -2699,7 +2699,7 @@ public static function defined($constant, $message = null, string $propertyPath * * @throws AssertionFailedException */ - public static function base64($value, $message = null, string $propertyPath = null): bool + public static function base64($value, $message = null, ?string $propertyPath = null): bool { if (false === \base64_decode($value, true)) { $message = \sprintf(static::generateMessage($message ?: 'Value "%s" is not a valid base64 string.'), $value); diff --git a/lib/Assert/AssertionChain.php b/lib/Assert/AssertionChain.php index 4c444350..8d1f1b3c 100644 --- a/lib/Assert/AssertionChain.php +++ b/lib/Assert/AssertionChain.php @@ -151,7 +151,7 @@ class AssertionChain * @param mixed $value * @param string|callable|null $defaultMessage */ - public function __construct($value, $defaultMessage = null, string $defaultPropertyPath = null) + public function __construct($value, $defaultMessage = null, ?string $defaultPropertyPath = null) { $this->value = $value; $this->defaultMessage = $defaultMessage; diff --git a/lib/Assert/InvalidArgumentException.php b/lib/Assert/InvalidArgumentException.php index 9516e077..54f85aad 100644 --- a/lib/Assert/InvalidArgumentException.php +++ b/lib/Assert/InvalidArgumentException.php @@ -31,7 +31,7 @@ class InvalidArgumentException extends \InvalidArgumentException implements Asse */ private $constraints; - public function __construct($message, $code, string $propertyPath = null, $value = null, array $constraints = []) + public function __construct($message, $code, ?string $propertyPath = null, $value = null, array $constraints = []) { parent::__construct($message, $code); diff --git a/lib/Assert/LazyAssertion.php b/lib/Assert/LazyAssertion.php index b3052178..f7b6cd71 100644 --- a/lib/Assert/LazyAssertion.php +++ b/lib/Assert/LazyAssertion.php @@ -133,7 +133,7 @@ class LazyAssertion * * @return static */ - public function that($value, string $propertyPath = null, $defaultMessage = null) + public function that($value, ?string $propertyPath = null, $defaultMessage = null) { $this->currentChainFailed = false; $this->thisChainTryAll = false; diff --git a/lib/Assert/functions.php b/lib/Assert/functions.php index 1a4e84d9..77cdcedd 100644 --- a/lib/Assert/functions.php +++ b/lib/Assert/functions.php @@ -32,7 +32,7 @@ * The assertion chain can be stateful, that means be careful when you reuse * it. You should never pass around the chain. */ -function that($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain +function that($value, $defaultMessage = null, ?string $defaultPropertyPath = null): AssertionChain { return Assert::that($value, $defaultMessage, $defaultPropertyPath); } @@ -44,7 +44,7 @@ function that($value, $defaultMessage = null, string $defaultPropertyPath = null * @param string|callable|null $defaultMessage * @param string $defaultPropertyPath */ -function thatAll($values, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain +function thatAll($values, $defaultMessage = null, ?string $defaultPropertyPath = null): AssertionChain { return Assert::thatAll($values, $defaultMessage, $defaultPropertyPath); } @@ -58,7 +58,7 @@ function thatAll($values, $defaultMessage = null, string $defaultPropertyPath = * * @deprecated In favour of Assert::thatNullOr($value, $defaultMessage = null, $defaultPropertyPath = null) */ -function thatNullOr($value, $defaultMessage = null, string $defaultPropertyPath = null): AssertionChain +function thatNullOr($value, $defaultMessage = null, ?string $defaultPropertyPath = null): AssertionChain { return Assert::thatNullOr($value, $defaultMessage, $defaultPropertyPath); } diff --git a/tests/Assert/Tests/Fixtures/CustomAssertion.php b/tests/Assert/Tests/Fixtures/CustomAssertion.php index 5b206b2e..d3b5cdc7 100644 --- a/tests/Assert/Tests/Fixtures/CustomAssertion.php +++ b/tests/Assert/Tests/Fixtures/CustomAssertion.php @@ -31,7 +31,7 @@ public static function getCalls() return self::$calls; } - public static function string($value, $message = null, string $propertyPath = null): bool + public static function string($value, $message = null, ?string $propertyPath = null): bool { self::$calls[] = ['string', $value]; From b5fd8eacd8915a1b627b8bfc027803f1939734dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 15 Jul 2024 15:18:35 +0200 Subject: [PATCH 19/24] Add PHP 8.4 to CI (#341) --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 82919582..7a60ac6e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] + php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] steps: - name: Checkout From 8c10bf1152578b10c8bf0b12f0fc6db219b0be39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 11 Sep 2025 17:52:31 +0200 Subject: [PATCH 20/24] Update GitHub Action versions (#347) --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7a60ac6e..cb8918ae 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v1 + uses: actions/checkout@v5 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -19,7 +19,7 @@ jobs: tools: "cs2pr" - name: "Cache dependencies installed with composer" - uses: "actions/cache@v1" + uses: "actions/cache@v4" with: path: "~/.composer/cache" key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}" From db789557efbebaee68ec94a2e6d84455891b7b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 14 Oct 2025 00:48:42 +0200 Subject: [PATCH 21/24] Add PHP 8.5 to CI (#346) --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cb8918ae..e53304ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] + php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5'] steps: - name: Checkout From 7faccd84676a058b457e8460ac78bf83acf99914 Mon Sep 17 00:00:00 2001 From: Volker Dusch <247397+edorian@users.noreply.github.com> Date: Wed, 10 Jun 2026 21:44:29 +0200 Subject: [PATCH 22/24] Fix copy-pasted docblock on base64 assertion (#351) --- lib/Assert/Assertion.php | 6 +++--- lib/Assert/AssertionChain.php | 2 +- lib/Assert/LazyAssertion.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 81bc9780..fc22a207 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -31,7 +31,7 @@ * @author Benjamin Eberlei * * @method static bool allAlnum(mixed[] $value, string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric for all values. - * @method static bool allBase64(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined for all values. + * @method static bool allBase64(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid base64 string for all values. * @method static bool allBetween(mixed[] $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit for all values. * @method static bool allBetweenExclusive(mixed[] $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit for all values. * @method static bool allBetweenLength(mixed[] $value, int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths for all values. @@ -120,7 +120,7 @@ * @method static bool allVersion(string[] $version1, string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions for all values. * @method static bool allWriteable(string[] $value, string|callable $message = null, string $propertyPath = null) Assert that the value is something writeable for all values. * @method static bool nullOrAlnum(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric or that the value is null. - * @method static bool nullOrBase64(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that a constant is defined or that the value is null. + * @method static bool nullOrBase64(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid base64 string or that the value is null. * @method static bool nullOrBetween(mixed|null $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit or that the value is null. * @method static bool nullOrBetweenExclusive(mixed|null $value, mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit or that the value is null. * @method static bool nullOrBetweenLength(mixed|null $value, int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths or that the value is null. @@ -2692,7 +2692,7 @@ public static function defined($constant, $message = null, ?string $propertyPath } /** - * Assert that a constant is defined. + * Assert that the given string is a valid base64 string. * * @param string $value * @param string|callable|null $message diff --git a/lib/Assert/AssertionChain.php b/lib/Assert/AssertionChain.php index 8d1f1b3c..a45e76e5 100644 --- a/lib/Assert/AssertionChain.php +++ b/lib/Assert/AssertionChain.php @@ -22,7 +22,7 @@ * @author Benjamin Eberlei * * @method AssertionChain alnum(string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric. - * @method AssertionChain base64(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined. + * @method AssertionChain base64(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid base64 string. * @method AssertionChain between(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. * @method AssertionChain betweenExclusive(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit. * @method AssertionChain betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths. diff --git a/lib/Assert/LazyAssertion.php b/lib/Assert/LazyAssertion.php index f7b6cd71..cb146abc 100644 --- a/lib/Assert/LazyAssertion.php +++ b/lib/Assert/LazyAssertion.php @@ -22,7 +22,7 @@ * @author Benjamin Eberlei * * @method LazyAssertion alnum(string|callable $message = null, string $propertyPath = null) Assert that value is alphanumeric. - * @method LazyAssertion base64(string|callable $message = null, string $propertyPath = null) Assert that a constant is defined. + * @method LazyAssertion base64(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid base64 string. * @method LazyAssertion between(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. * @method LazyAssertion betweenExclusive(mixed $lowerLimit, mixed $upperLimit, string|callable $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit. * @method LazyAssertion betweenLength(int $minLength, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min and max lengths. From 0962bb15976995489d0969baee262706156eab12 Mon Sep 17 00:00:00 2001 From: Volker Dusch <247397+edorian@users.noreply.github.com> Date: Wed, 10 Jun 2026 21:45:22 +0200 Subject: [PATCH 23/24] Reject trailing newline in alnum, url and e164 (#352) Add PCRE_DOLLAR_ENDONLY modifier to disallow strings with trailing newlines. Asserts that wrap PHP functions like numeric("123\n") are untouched. --- lib/Assert/Assertion.php | 6 +++--- tests/Assert/Tests/AssertTest.php | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index fc22a207..515fddd0 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -1748,7 +1748,7 @@ public static function url($value, $message = null, ?string $propertyPath = null (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%%[0-9A-Fa-f]{2})* )* # a path (?:\? (?:[\pL\pN\-._\~!$&\'\[\]()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a query (optional) (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional) - $~ixu'; + $~ixuD'; $pattern = \sprintf($pattern, \implode('|', $protocols)); @@ -1775,7 +1775,7 @@ public static function url($value, $message = null, ?string $propertyPath = null public static function alnum($value, $message = null, ?string $propertyPath = null): bool { try { - static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath); + static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)D', $message, $propertyPath); } catch (Throwable $e) { $message = \sprintf( static::generateMessage($message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.'), @@ -2004,7 +2004,7 @@ public static function uuid($value, $message = null, ?string $propertyPath = nul */ public static function e164($value, $message = null, ?string $propertyPath = null): bool { - if (!\preg_match('/^\+?[1-9]\d{1,14}$/', $value)) { + if (!\preg_match('/^\+?[1-9]\d{1,14}$/D', $value)) { $message = \sprintf( static::generateMessage($message ?: 'Value "%s" is not a valid E164.'), static::stringify($value) diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 849d6441..60ac482b 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -782,6 +782,7 @@ public function dataInvalidUrl(): array ['http://:password@@symfony.com'], ['http://username:passwordsymfony.com'], ['http://usern@me:password@symfony.com'], + ["http://www.google.com\n"], ]; } @@ -898,6 +899,13 @@ public function testInvalidAlnum() Assertion::alnum('1a'); } + public function testInvalidAlnumWithTrailingNewline() + { + $this->expectException('Assert\AssertionFailedException'); + $this->expectExceptionCode(\Assert\Assertion::INVALID_ALNUM); + Assertion::alnum("a1b2c3\n"); + } + public function testValidTrue() { $this->assertTrue(Assertion::true(1 == 1)); @@ -1306,6 +1314,7 @@ public function providesInvalidE164s(): array return [ ['+3362652569e'], ['+3361231231232652569'], + ["+33626525690\n"], ]; } From f193f4613c7d7fbcee2c05e4daff4061d49c040e Mon Sep 17 00:00:00 2001 From: Volker Dusch <247397+edorian@users.noreply.github.com> Date: Wed, 10 Jun 2026 21:47:05 +0200 Subject: [PATCH 24/24] Fix uuid() accepting {, }, and prefixes inside the string (#353) Instead of stripping "urn:", "uuid:", "{" and "}" from anywhere in the value before matching. So strings like "5{}50e8400-..." or an interior "urn:" passed the assertion and the un-normalised string was returned to the caller. Moved the validated into the regex: - The prefixes are only accepted at the start - A leading "{" must be paired with a closing "}" using a PCRE conditional --- lib/Assert/Assertion.php | 8 +------- tests/Assert/Tests/AssertTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 515fddd0..53898ca2 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -1974,13 +1974,7 @@ public static function isJsonString($value, $message = null, ?string $propertyPa */ public static function uuid($value, $message = null, ?string $propertyPath = null): bool { - $value = \str_replace(['urn:', 'uuid:', '{', '}'], '', $value); - - if ('00000000-0000-0000-0000-000000000000' === $value) { - return true; - } - - if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) { + if (!\preg_match('/^(?:urn:)?(?:uuid:)?(\{)?[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}(?(1)\})$/D', $value)) { $message = \sprintf( static::generateMessage($message ?: 'Value "%s" is not a valid UUID.'), static::stringify($value) diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 60ac482b..4dfc39bf 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -1264,6 +1264,12 @@ public function providesValidUuids(): array ['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'], ['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'], ['00000000-0000-0000-0000-000000000000'], + ['urn:uuid:ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], + ['urn:ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], + ['uuid:ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], + ['{ff6f8cb0-c57d-11e1-9b21-0800200c9a66}'], + ['urn:uuid:{ff6f8cb0-c57d-11e1-9b21-0800200c9a66}'], + ['{00000000-0000-0000-0000-000000000000}'], ]; } @@ -1275,6 +1281,12 @@ public function providesInvalidUuids(): array ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'], ['af6f8cb-c57d-11e1-9b21-0800200c9a66'], ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'], + ['{ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], + ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66}'], + ['ff6f8cb0-{c57d}-11e1-9b21-0800200c9a66'], + ['ff6f8cb0-c57d-11e1-9b21-0800uuid:200c9a66'], + ["ff6f8cb0-c57d-11e1-9b21-0800200c9a66\n"], + ["{ff6f8cb0-c57d-11e1-9b21-0800200c9a66}\n"], ]; }