diff --git a/.gitattributes b/.gitattributes index dcd303f..cdba42e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,9 +1,10 @@ .editorconfig export-ignore .gitattributes export-ignore +.github/ export-ignore .gitignore export-ignore .scrutinizer.yml export-ignore .travis.yml export-ignore composer.json export-ignore phpcs.xml export-ignore phpunit.xml.dist export-ignore -tests export-ignore +tests/ export-ignore diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..de76a8d --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,63 @@ +name: PHP Composer + +on: + push: + pull_request: + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php: + - '7.4' + - '8.0' + - '8.1' + - '8.2' + - '8.3' + - '8.4' + - '8.5' + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - name: Composer validate + run: composer validate + + - name: Cache Composer cache + uses: actions/cache@v4 + with: + path: ~/.composer/cache + key: composer-cache-php${{ matrix.php }} + + - name: Composer install + run: composer install --no-progress --no-suggest --no-interaction --prefer-dist --optimize-autoloader + - name: Run test suite + run: composer ci + + - name: Setup PHP for coverage + if: matrix.php == '7.4' + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: xdebug + + - name: Generate coverage + if: matrix.php == '7.4' + run: vendor/bin/phpunit --coverage-clover coverage.clover + + - name: Upload Scrutinizer coverage + if: matrix.php == '7.4' + uses: sudo-bot/action-scrutinizer@latest + with: + cli-args: "--format=php-clover coverage.clover" diff --git a/.gitignore b/.gitignore index 577795c..da794e8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,6 @@ build/ vendor/ composer.phar -composer.lock \ No newline at end of file +composer.lock + +.phpunit.result.cache diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d3e813a..18ed129 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -3,7 +3,6 @@ inherit: true tools: external_code_coverage: true - php_code_coverage: true php_code_sniffer: true php_cpd: true php_cs_fixer: true diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 74748b6..0000000 --- a/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -language: php - -dist: trusty - -php: - - 5.5 - - 5.6 - - 7 - - 7.1 - - hhvm - -sudo: false - -install: travis_retry composer install - -script: composer ci - -after_success: - - if [[ "`phpenv version-name`" != "7.1" ]]; then exit 0; fi - - vendor/bin/phpunit --coverage-clover coverage.clover - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover coverage.clover - -cache: - directories: - - $HOME/.composer/cache diff --git a/README.md b/README.md index 3503808..f0d5acd 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,16 @@ # DataValues Common -DataValues Common is a small library build on top of DataValues that provides common -implementations of the DataValues, ValueParsers, ValueFormatters and ValueValidators interfaces. +DataValues Common is a small library built on top of DataValues that provides common +implementations of the DataValues, ValueParsers, ValueFormatters, and ValueValidators interfaces. It is part of the [DataValues set of libraries](https://github.com/DataValues). -[![Build Status](https://secure.travis-ci.org/DataValues/Common.png?branch=master)](http://travis-ci.org/DataValues/Common) -[![Code Coverage](https://scrutinizer-ci.com/g/DataValues/Common/badges/coverage.png?s=728b9287ebdd13fbe15255d4d55575c5b5d47b8f)](https://scrutinizer-ci.com/g/DataValues/Common/) -[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/DataValues/Common/badges/quality-score.png?s=3195539d2e929aafaefb4bc006fb0da6c09a4d2a)](https://scrutinizer-ci.com/g/DataValues/Common/) +[![Build Status](https://img.shields.io/github/actions/workflow/status/DataValues/Common/php.yml?branch=master)](https://github.com/DataValues/Common/actions/workflows/php.yml?query=workflow%3APHP) +[![Code Coverage](https://scrutinizer-ci.com/g/DataValues/Common/badges/coverage.png?s=6432d29bf3fed068995e66093ad52e053099a916)](https://scrutinizer-ci.com/g/DataValues/Common/) On [Packagist](https://packagist.org/packages/data-values/common): -[![Latest Stable Version](https://poser.pugx.org/data-values/common/version.png)](https://packagist.org/packages/data-values/common) -[![Download count](https://poser.pugx.org/data-values/common/d/total.png)](https://packagist.org/packages/data-values/common) +[![Latest Stable Version](https://poser.pugx.org/data-values/common/v/stable)](https://packagist.org/packages/data-values/common) +[![Download count](https://poser.pugx.org/data-values/common/downloads)](https://packagist.org/packages/data-values/common) ## Installation @@ -20,11 +19,11 @@ The recommended way to use this library is via [Composer](http://getcomposer.org To add this package as a local, per-project dependency to your project, simply add a dependency on `data-values/common` to your project's `composer.json` file. Here is a minimal example of a `composer.json` file that just defines a dependency on -version 1.0 of this package: +version 1.x of this package: { "require": { - "data-values/common": "^1.0.0" + "data-values/common": "^1.1.0" } } @@ -32,7 +31,7 @@ version 1.0 of this package: This library comes with a set up PHPUnit tests that cover all non-trivial code. You can run these tests using the PHPUnit configuration file found in the root directory. The tests can also be run -via TravisCI, as a TravisCI configuration file is also provided in the root directory. +via Github Actions. ### Running the tests @@ -55,11 +54,40 @@ employees for the [Wikidata project](https://wikidata.org/). ## Release notes -### 1.0.0 (dev) +### 1.2.0 (2024-12-10) -* Added `TrimmingStringNormalizer` +* Dropped support for PHP 7.2 and PHP 7.3 +* Improved support for PHP 8.4 by explicitly declaring nullable types +* Upgraded codesniffer rules to current `mediawiki/mediawiki-codesniffer` version (45.0.0) +* Added testing with PHP 8.1 up to 8.4 + +### 1.1.0 (2022-10-21) + +* Improved compatibility with PHP 8.1; + in particular, the new `__serialize`/`__unserialize` methods are implemented now + (in addition to the still supported `Serializable` interface). + Make sure to also use `data-values/data-values` version 3.1.0 (or later) to keep hashes stable. + +### 1.0.0 (2021-01-22) + +* Updated minimum required PHP version from 5.5.9 to 7.2 +* Added compatibility with `data-values/data-values` 3.x +* Added compatibility with `data-values/interfaces` 1.x * Removed the `DATAVALUES_COMMON_VERSION` constant +* Deprecated `getSortKey` methods from `DataValue` implementations +* Classes in the `ValueParsers\Test` namespace are now package private. Notably `ValueParserTestBase` and `StringValueParserTest` * The `StringFormatter` constructor does not accept options any more +* `StringParser::parse` now throws a `ParseException` instead of an `InvalidArgumentException` +* Added `TrimmingStringNormalizer` +* Made `FORMAT_NAME` constants in the Parser classes private + +### 0.4.3 (2019-06-28) + +* Fixed typo in error message in `DispatchingValueParser` + +### 0.4.2 (2018-08-16) + +* The component can now be installed together with DataValues 2.x ### 0.4.1 (2017-08-09) @@ -130,4 +158,3 @@ Initial release with these features: ## Links * [DataValues Common on Packagist](https://packagist.org/packages/data-values/common) -* [DataValues Common on TravisCI](https://travis-ci.org/DataValues/Common) diff --git a/composer.json b/composer.json index 755a727..6e51d8f 100644 --- a/composer.json +++ b/composer.json @@ -23,14 +23,14 @@ "irc": "irc://irc.freenode.net/wikidata" }, "require": { - "php": ">=5.5.9", - "data-values/data-values": "~2.0|~1.0|~0.1", - "data-values/interfaces": "~0.2.0" + "php": ">=7.4", + "data-values/data-values": "~3.0|~2.0|~1.0|~0.1", + "data-values/interfaces": "~1.0|~0.2.0" }, "require-dev": { - "ockcyp/covers-validator": "~0.4", - "phpunit/phpunit": "~4.8", - "wikibase/wikibase-codesniffer": "^0.1.0" + "phpunit/phpunit": "~8.0", + "ockcyp/covers-validator": "^1.3.3", + "mediawiki/mediawiki-codesniffer": "^45" }, "extra": { "branch-alias": { @@ -42,23 +42,35 @@ "DataValues\\": "src/DataValues/", "ValueFormatters\\": "src/ValueFormatters/", "ValueParsers\\": "src/ValueParsers/" - }, - "classmap": [ - "tests/ValueParsers" - ] + } + }, + "autoload-dev": { + "psr-4": { + "DataValues\\Tests\\": "tests/DataValues/", + "ValueFormatters\\Tests\\": "tests/ValueFormatters/", + "ValueParsers\\Tests\\": "tests/ValueParsers/" + } }, "scripts": { + "fix": [ + "phpcbf" + ], "cs": [ "phpcs -p -s" ], "test": [ "composer validate --no-interaction", "covers-validator", - "phpunit" + "vendor/bin/phpunit" ], "ci": [ "@cs", "@test" ] + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpcs.xml b/phpcs.xml index ac20681..52b4a07 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,12 +1,11 @@ - + src/ + tests/ - - - + + - - . + diff --git a/src/DataValues/MonolingualTextValue.php b/src/DataValues/MonolingualTextValue.php index 5cf35fc..e331064 100644 --- a/src/DataValues/MonolingualTextValue.php +++ b/src/DataValues/MonolingualTextValue.php @@ -1,5 +1,7 @@ */ class MonolingualTextValue extends DataValueObject { @@ -46,7 +48,11 @@ public function __construct( $languageCode, $text ) { * @return string */ public function serialize() { - return serialize( [ $this->languageCode, $this->text ] ); + return serialize( $this->__serialize() ); + } + + public function __serialize(): array { + return [ $this->languageCode, $this->text ]; } /** @@ -55,7 +61,11 @@ public function serialize() { * @param string $value */ public function unserialize( $value ) { - list( $languageCode, $text ) = unserialize( $value ); + $this->__unserialize( unserialize( $value ) ); + } + + public function __unserialize( array $data ): void { + [ $languageCode, $text ] = $data; $this->__construct( $languageCode, $text ); } @@ -69,7 +79,8 @@ public static function getType() { } /** - * @see DataValue::getSortKey + * @deprecated Kept for compatibility with older DataValues versions. + * Do not use. * * @return string */ diff --git a/src/DataValues/MultilingualTextValue.php b/src/DataValues/MultilingualTextValue.php index 0335f84..32165cf 100644 --- a/src/DataValues/MultilingualTextValue.php +++ b/src/DataValues/MultilingualTextValue.php @@ -1,5 +1,7 @@ */ class MultilingualTextValue extends DataValueObject { @@ -27,13 +29,17 @@ class MultilingualTextValue extends DataValueObject { public function __construct( array $monolingualValues ) { foreach ( $monolingualValues as $monolingualValue ) { if ( !( $monolingualValue instanceof MonolingualTextValue ) ) { - throw new IllegalValueException( 'Can only construct MultilingualTextValue from MonolingualTextValue objects' ); + throw new IllegalValueException( + 'Can only construct MultilingualTextValue from MonolingualTextValue objects' + ); } $languageCode = $monolingualValue->getLanguageCode(); if ( array_key_exists( $languageCode, $this->texts ) ) { - throw new IllegalValueException( 'Can only add a single MonolingualTextValue per language to a MultilingualTextValue' ); + throw new IllegalValueException( + 'Can only add a single MonolingualTextValue per language to a MultilingualTextValue' + ); } $this->texts[$languageCode] = $monolingualValue; @@ -46,7 +52,11 @@ public function __construct( array $monolingualValues ) { * @return string */ public function serialize() { - return serialize( $this->texts ); + return serialize( $this->__serialize() ); + } + + public function __serialize(): array { + return $this->texts; } /** @@ -55,7 +65,11 @@ public function serialize() { * @param string $value */ public function unserialize( $value ) { - $this->__construct( unserialize( $value ) ); + $this->__unserialize( unserialize( $value ) ); + } + + public function __unserialize( array $data ): void { + $this->__construct( $data ); } /** @@ -68,7 +82,8 @@ public static function getType() { } /** - * @see DataValue::getSortKey + * @deprecated Kept for compatibility with older DataValues versions. + * Do not use. * * @return string|float|int */ diff --git a/src/ValueFormatters/Exceptions/MismatchingDataValueTypeException.php b/src/ValueFormatters/Exceptions/MismatchingDataValueTypeException.php index f041e17..1c25dec 100644 --- a/src/ValueFormatters/Exceptions/MismatchingDataValueTypeException.php +++ b/src/ValueFormatters/Exceptions/MismatchingDataValueTypeException.php @@ -1,5 +1,7 @@ * @author Thiemo Kreuz */ @@ -34,7 +36,7 @@ public function __construct( $expectedValueType, $dataValueType, $message = '', - Exception $previous = null + ?Exception $previous = null ) { $this->expectedValueType = $expectedValueType; $this->dataValueType = $dataValueType; diff --git a/src/ValueFormatters/StringFormatter.php b/src/ValueFormatters/StringFormatter.php index c5ebaf9..077e86b 100644 --- a/src/ValueFormatters/StringFormatter.php +++ b/src/ValueFormatters/StringFormatter.php @@ -1,5 +1,7 @@ */ class StringFormatter implements ValueFormatter { diff --git a/src/ValueParsers/BoolParser.php b/src/ValueParsers/BoolParser.php index 2835e66..99faf7f 100644 --- a/src/ValueParsers/BoolParser.php +++ b/src/ValueParsers/BoolParser.php @@ -1,5 +1,7 @@ */ class BoolParser extends StringValueParser { - const FORMAT_NAME = 'bool'; + private const FORMAT_NAME = 'bool'; + /** + * @var Mapping from possible string values to their + * boolean equivalents + */ private static $values = [ 'yes' => true, 'on' => true, diff --git a/src/ValueParsers/DispatchingValueParser.php b/src/ValueParsers/DispatchingValueParser.php index fb34287..0ad0fce 100644 --- a/src/ValueParsers/DispatchingValueParser.php +++ b/src/ValueParsers/DispatchingValueParser.php @@ -1,5 +1,7 @@ */ class FloatParser extends StringValueParser { - const FORMAT_NAME = 'float'; + private const FORMAT_NAME = 'float'; /** * @see StringValueParser::stringParse diff --git a/src/ValueParsers/IntParser.php b/src/ValueParsers/IntParser.php index 93547b0..ffd1650 100644 --- a/src/ValueParsers/IntParser.php +++ b/src/ValueParsers/IntParser.php @@ -1,5 +1,7 @@ */ class IntParser extends StringValueParser { - const FORMAT_NAME = 'int'; + private const FORMAT_NAME = 'int'; /** * @see StringValueParser::stringParse diff --git a/src/ValueParsers/Normalizers/NullStringNormalizer.php b/src/ValueParsers/Normalizers/NullStringNormalizer.php index 3f14402..a16730f 100644 --- a/src/ValueParsers/Normalizers/NullStringNormalizer.php +++ b/src/ValueParsers/Normalizers/NullStringNormalizer.php @@ -1,5 +1,7 @@ */ class NullParser implements ValueParser { diff --git a/src/ValueParsers/StringParser.php b/src/ValueParsers/StringParser.php index d07f91f..4827694 100644 --- a/src/ValueParsers/StringParser.php +++ b/src/ValueParsers/StringParser.php @@ -1,9 +1,10 @@ normalizer = $normalizer ?: new NullStringNormalizer(); } @@ -34,12 +35,12 @@ public function __construct( StringNormalizer $normalizer = null ) { * * @param string $value * - * @throws InvalidArgumentException if $value is not a string + * @throws ParseException if the provided value is not a string * @return StringValue */ public function parse( $value ) { if ( !is_string( $value ) ) { - throw new InvalidArgumentException( 'Parameter $value must be a string' ); + throw new ParseException( 'Parameter $value must be a string' ); } $value = $this->normalizer->normalize( $value ); diff --git a/src/ValueParsers/StringValueParser.php b/src/ValueParsers/StringValueParser.php index 6ea7f1d..ac638c0 100644 --- a/src/ValueParsers/StringValueParser.php +++ b/src/ValueParsers/StringValueParser.php @@ -1,5 +1,7 @@ */ abstract class StringValueParser implements ValueParser { @@ -26,7 +28,7 @@ abstract class StringValueParser implements ValueParser { /** * @param ParserOptions|null $options */ - public function __construct( ParserOptions $options = null ) { + public function __construct( ?ParserOptions $options = null ) { $this->options = $options ?: new ParserOptions(); $this->defaultOption( ValueParser::OPT_LANG, 'en' ); diff --git a/tests/DataValues/MonolingualTextValueTest.php b/tests/DataValues/MonolingualTextValueTest.php index 38e69fc..2ead346 100644 --- a/tests/DataValues/MonolingualTextValueTest.php +++ b/tests/DataValues/MonolingualTextValueTest.php @@ -1,37 +1,57 @@ */ -class MonolingualTextValueTest extends DataValueTest { +class MonolingualTextValueTest extends TestCase { + + public function testGetters() { + $value = new MonolingualTextValue( 'en', 'foo' ); + $this->assertSame( 'monolingualtext', $value->getType() ); + $this->assertSame( 'enfoo', $value->getSortKey() ); + $this->assertSame( 'foo', $value->getText() ); + $this->assertSame( 'en', $value->getLanguageCode() ); + } + + public function testArrayAndEquals() { + $value = new MonolingualTextValue( 'en', 'foo' ); + $array = $value->getArrayValue(); + $value2 = MonolingualTextValue::newFromArray( $array ); + $this->assertTrue( $value->equals( $value2 ) ); + $this->assertEquals( $value, $value2 ); + } + + public function testSerialize() { + $value = new MonolingualTextValue( 'en', 'foo' ); + $serialization = serialize( $value ); + $value2 = unserialize( $serialization ); + $this->assertEquals( $value, $value2 ); + } /** - * @see DataValueTest::getClass - * - * @return string + * @dataProvider invalidConstructorArgumentsProvider */ - public function getClass() { - return MonolingualTextValue::class; - } + public function testConstructorWithInvalidArguments( $languageCode, $text ) { + $this->expectException( Exception::class ); - public function validConstructorArgumentsProvider() { - return [ - [ 'en', 'foo' ], - [ 'en', ' foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz ' ], - ]; + $dataItem = new MonolingualTextValue( $languageCode, $text ); } public function invalidConstructorArgumentsProvider() { @@ -49,17 +69,11 @@ public function invalidConstructorArgumentsProvider() { ]; } - public function testNewFromArray() { - $array = [ 'text' => 'foo', 'language' => 'en' ]; - $value = MonolingualTextValue::newFromArray( $array ); - $this->assertSame( $array, $value->getArrayValue() ); - } - /** * @dataProvider invalidArrayProvider */ public function testNewFromArrayWithInvalidArray( array $array ) { - $this->setExpectedException( IllegalValueException::class ); + $this->expectException( IllegalValueException::class ); MonolingualTextValue::newFromArray( $array ); } @@ -74,23 +88,4 @@ public function invalidArrayProvider() { ]; } - public function testGetSortKey() { - $value = new MonolingualTextValue( 'en', 'foo' ); - $this->assertSame( 'enfoo', $value->getSortKey() ); - } - - /** - * @dataProvider instanceProvider - */ - public function testGetText( MonolingualTextValue $text, array $arguments ) { - $this->assertEquals( $arguments[1], $text->getText() ); - } - - /** - * @dataProvider instanceProvider - */ - public function testGetLanguageCode( MonolingualTextValue $text, array $arguments ) { - $this->assertEquals( $arguments[0], $text->getLanguageCode() ); - } - } diff --git a/tests/DataValues/MultilingualTextValueTest.php b/tests/DataValues/MultilingualTextValueTest.php index 89f3760..1d5b2fe 100644 --- a/tests/DataValues/MultilingualTextValueTest.php +++ b/tests/DataValues/MultilingualTextValueTest.php @@ -1,52 +1,72 @@ */ -class MultilingualTextValueTest extends DataValueTest { +class MultilingualTextValueTest extends TestCase { + + public function testGetters() { + $monolingualTextValue1 = new MonolingualTextValue( 'en', 'foo' ); + $monolingualTextValue2 = new MonolingualTextValue( 'de', 'foo' ); + $value = new MultilingualTextValue( [ $monolingualTextValue1, $monolingualTextValue2 ] ); + $this->assertSame( 'multilingualtext', $value->getType() ); + $this->assertSame( 'enfoo', $value->getSortKey() ); + $this->assertSame( + [ 'en' => $monolingualTextValue1, 'de' => $monolingualTextValue2 ], + $value->getTexts() + ); + } + + public function testGetters_empty() { + $value = new MultilingualTextValue( [] ); + $this->assertSame( '', $value->getSortKey() ); + $this->assertSame( [], $value->getTexts() ); + } + + public function testArrayAndEquals() { + $monolingualTextValue1 = new MonolingualTextValue( 'en', 'foo' ); + $monolingualTextValue2 = new MonolingualTextValue( 'de', 'foo' ); + $value = new MultilingualTextValue( [ $monolingualTextValue1, $monolingualTextValue2 ] ); + $array = $value->getArrayValue(); + $value2 = MultilingualTextValue::newFromArray( $array ); + $this->assertTrue( $value->equals( $value2 ) ); + $this->assertEquals( $value, $value2 ); + } + + public function testSerialize() { + $monolingualTextValue1 = new MonolingualTextValue( 'en', 'foo' ); + $monolingualTextValue2 = new MonolingualTextValue( 'de', 'foo' ); + $value = new MultilingualTextValue( [ $monolingualTextValue1, $monolingualTextValue2 ] ); + $serialization = serialize( $value ); + $value2 = unserialize( $serialization ); + $this->assertEquals( $value, $value2 ); + } /** - * @see DataValueTest::getClass - * - * @return string + * @dataProvider invalidConstructorArgumentsProvider */ - public function getClass() { - return MultilingualTextValue::class; - } + public function testConstructorWithInvalidArguments( $monolingualValues ) { + $this->expectException( Exception::class ); - public function validConstructorArgumentsProvider() { - return [ - [ [] ], - [ [ - new MonolingualTextValue( 'en', 'foo' ), - ] ], - [ [ - new MonolingualTextValue( 'en', 'foo' ), - new MonolingualTextValue( 'de', 'foo' ), - ] ], - [ [ - new MonolingualTextValue( 'en', 'foo' ), - new MonolingualTextValue( 'de', 'bar' ), - ] ], - [ [ - new MonolingualTextValue( 'en', 'foo' ), - new MonolingualTextValue( 'de', ' foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz ' ), - ] ], - ]; + $dataItem = new MultilingualTextValue( $monolingualValues ); } public function invalidConstructorArgumentsProvider() { @@ -81,17 +101,11 @@ public function invalidConstructorArgumentsProvider() { ]; } - public function testNewFromArray() { - $array = [ [ 'text' => 'foo', 'language' => 'en' ] ]; - $value = MultilingualTextValue::newFromArray( $array ); - $this->assertSame( $array, $value->getArrayValue() ); - } - /** * @dataProvider invalidArrayProvider */ public function testNewFromArrayWithInvalidArray( array $array ) { - $this->setExpectedException( IllegalValueException::class ); + $this->expectException( IllegalValueException::class ); MultilingualTextValue::newFromArray( $array ); } @@ -104,43 +118,4 @@ public function invalidArrayProvider() { ]; } - /** - * @dataProvider getSortKeyProvider - */ - public function testGetSortKey( array $monolingualValues, $expected ) { - $value = new MultilingualTextValue( $monolingualValues ); - $this->assertSame( $expected, $value->getSortKey() ); - } - - public function getSortKeyProvider() { - return [ - [ [], '' ], - [ [ - new MonolingualTextValue( 'en', 'foo' ), - ], 'enfoo' ], - [ [ - new MonolingualTextValue( 'en', 'foo' ), - new MonolingualTextValue( 'de', 'bar' ), - ], 'enfoo' ], - ]; - } - - /** - * @dataProvider instanceProvider - */ - public function testGetTexts( MultilingualTextValue $texts, array $arguments ) { - $actual = $texts->getTexts(); - - $this->assertInternalType( 'array', $actual ); - $this->assertContainsOnlyInstancesOf( MonolingualTextValue::class, $actual ); - $this->assertEquals( $arguments[0], array_values( $actual ) ); - } - - /** - * @dataProvider instanceProvider - */ - public function testGetValue( MultilingualTextValue $texts, array $arguments ) { - $this->assertInstanceOf( $this->getClass(), $texts->getValue() ); - } - } diff --git a/tests/ValueFormatters/Exceptions/MismatchingDataValueTypeExceptionTest.php b/tests/ValueFormatters/Exceptions/MismatchingDataValueTypeExceptionTest.php index 44af779..38f5263 100644 --- a/tests/ValueFormatters/Exceptions/MismatchingDataValueTypeExceptionTest.php +++ b/tests/ValueFormatters/Exceptions/MismatchingDataValueTypeExceptionTest.php @@ -1,22 +1,24 @@ * @author Thiemo Kreuz */ -class MismatchingDataValueTypeExceptionTest extends PHPUnit_Framework_TestCase { +class MismatchingDataValueTypeExceptionTest extends TestCase { /** * @dataProvider constructorProvider diff --git a/tests/ValueFormatters/StringFormatterTest.php b/tests/ValueFormatters/StringFormatterTest.php index bf90701..5af2df0 100644 --- a/tests/ValueFormatters/StringFormatterTest.php +++ b/tests/ValueFormatters/StringFormatterTest.php @@ -1,37 +1,31 @@ */ -class StringFormatterTest extends ValueFormatterTestBase { +class StringFormatterTest extends TestCase { - /** - * @see ValueFormatterTestBase::getInstance - * - * @param FormatterOptions|null $options - * - * @return StringFormatter - */ - protected function getInstance( FormatterOptions $options = null ) { - return new StringFormatter( $options ); + /** @dataProvider validProvider */ + public function testValidFormat( StringValue $value, string $expected ) { + $formatter = new StringFormatter(); + $this->assertSame( $expected, $formatter->format( $value ) ); } - /** - * @see ValueFormatterTestBase::validProvider - */ public function validProvider() { return [ [ new StringValue( 'ice cream' ), 'ice cream' ], @@ -47,7 +41,7 @@ public function validProvider() { */ public function testInvalidFormat( $value ) { $formatter = new StringFormatter(); - $this->setExpectedException( InvalidArgumentException::class ); + $this->expectException( InvalidArgumentException::class ); $formatter->format( $value ); } diff --git a/tests/ValueParsers/BoolParserTest.php b/tests/ValueParsers/BoolParserTest.php index e365c76..2c2bfc5 100644 --- a/tests/ValueParsers/BoolParserTest.php +++ b/tests/ValueParsers/BoolParserTest.php @@ -1,18 +1,20 @@ */ class BoolParserTest extends StringValueParserTest { diff --git a/tests/ValueParsers/DispatchingValueParserTest.php b/tests/ValueParsers/DispatchingValueParserTest.php index b328660..7d42056 100644 --- a/tests/ValueParsers/DispatchingValueParserTest.php +++ b/tests/ValueParsers/DispatchingValueParserTest.php @@ -1,51 +1,47 @@ getMock( ValueParser::class ); + private function getParser( $invocation ): ValueParser { + $mock = $this->createMock( ValueParser::class ); $mock->expects( $invocation ) ->method( 'parse' ) - ->will( $this->returnCallback( function( $value ) { + ->willReturnCallback( static function ( $value ) { if ( $value === 'invalid' ) { throw new ParseException( 'failed' ); } return $value; - } ) ); + } ); return $mock; } /** * @dataProvider invalidConstructorArgumentsProvider - * @expectedException InvalidArgumentException */ public function testGivenInvalidConstructorArguments_constructorThrowsException( $parsers, $format ) { + $this->expectException( InvalidArgumentException::class ); new DispatchingValueParser( $parsers, $format ); } @@ -81,7 +77,7 @@ public function testParseThrowsException() { 'format' ); - $this->setExpectedException( ParseException::class ); + $this->expectException( ParseException::class ); $parser->parse( 'invalid' ); } diff --git a/tests/ValueParsers/FloatParserTest.php b/tests/ValueParsers/FloatParserTest.php index c89d906..c931ed2 100644 --- a/tests/ValueParsers/FloatParserTest.php +++ b/tests/ValueParsers/FloatParserTest.php @@ -1,18 +1,20 @@ */ class FloatParserTest extends StringValueParserTest { diff --git a/tests/ValueParsers/IntParserTest.php b/tests/ValueParsers/IntParserTest.php index eab8d89..05b442b 100644 --- a/tests/ValueParsers/IntParserTest.php +++ b/tests/ValueParsers/IntParserTest.php @@ -1,18 +1,20 @@ */ class IntParserTest extends StringValueParserTest { diff --git a/tests/ValueParsers/Normalizers/NullStringNormalizerTest.php b/tests/ValueParsers/Normalizers/NullStringNormalizerTest.php index 72b9956..b568bfa 100644 --- a/tests/ValueParsers/Normalizers/NullStringNormalizerTest.php +++ b/tests/ValueParsers/Normalizers/NullStringNormalizerTest.php @@ -1,22 +1,24 @@ setExpectedException( InvalidArgumentException::class ); + $this->expectException( InvalidArgumentException::class ); $normalizer->normalize( $value ); } diff --git a/tests/ValueParsers/Normalizers/TrimmingStringNormalizerTest.php b/tests/ValueParsers/Normalizers/TrimmingStringNormalizerTest.php index 3d69681..23e267c 100644 --- a/tests/ValueParsers/Normalizers/TrimmingStringNormalizerTest.php +++ b/tests/ValueParsers/Normalizers/TrimmingStringNormalizerTest.php @@ -1,13 +1,16 @@ setExpectedException( 'InvalidArgumentException' ); + $this->expectException( InvalidArgumentException::class ); $normalizer->normalize( $value ); } diff --git a/tests/ValueParsers/NullParserTest.php b/tests/ValueParsers/NullParserTest.php index fbea499..1f1573e 100644 --- a/tests/ValueParsers/NullParserTest.php +++ b/tests/ValueParsers/NullParserTest.php @@ -1,18 +1,20 @@ */ class NullParserTest extends ValueParserTestBase { @@ -53,7 +55,7 @@ public function invalidInputProvider() { * * @dataProvider invalidInputProvider */ - public function testParseWithInvalidInputs( $value, ValueParser $parser = null ) { + public function testParseWithInvalidInputs( $value, ?ValueParser $parser = null ) { $this->markTestSkipped( 'NullParser has no invalid inputs' ); } diff --git a/tests/ValueParsers/StringParserTest.php b/tests/ValueParsers/StringParserTest.php index 47f01be..c707846 100644 --- a/tests/ValueParsers/StringParserTest.php +++ b/tests/ValueParsers/StringParserTest.php @@ -1,30 +1,34 @@ getMock( StringNormalizer::class ); + $normalizer = $this->createMock( StringNormalizer::class ); $normalizer->expects( $this->once() ) ->method( 'normalize' ) - ->will( $this->returnCallback( function( $value ) { + ->willReturnCallback( static function ( $value ) { return strtolower( trim( $value ) ); - } ) ); + } ); return [ 'simple' => [ 'hello world', null, new StringValue( 'hello world' ) ], @@ -35,7 +39,7 @@ public function provideParse() { /** * @dataProvider provideParse */ - public function testParse( $input, StringNormalizer $normalizer = null, DataValue $expected ) { + public function testParse( $input, ?StringNormalizer $normalizer, DataValue $expected ) { $parser = new StringParser( $normalizer ); $value = $parser->parse( $input ); @@ -56,7 +60,9 @@ public function nonStringProvider() { */ public function testGivenNonString_parseThrowsException( $input ) { $parser = new StringParser(); - $this->setExpectedException( 'InvalidArgumentException' ); + + $this->expectException( ParseException::class ); + $parser->parse( $input ); } diff --git a/tests/ValueParsers/StringValueParserTest.php b/tests/ValueParsers/StringValueParserTest.php index 5fcd989..b312f4e 100644 --- a/tests/ValueParsers/StringValueParserTest.php +++ b/tests/ValueParsers/StringValueParserTest.php @@ -1,17 +1,19 @@ */ abstract class StringValueParserTest extends ValueParserTestBase { diff --git a/tests/ValueParsers/ValueParserTestBase.php b/tests/ValueParsers/ValueParserTestBase.php index e19addc..cf5fdd9 100644 --- a/tests/ValueParsers/ValueParserTestBase.php +++ b/tests/ValueParsers/ValueParserTestBase.php @@ -1,10 +1,12 @@ */ -abstract class ValueParserTestBase extends PHPUnit_Framework_TestCase { +abstract class ValueParserTestBase extends TestCase { /** * @return array[] @@ -42,7 +44,7 @@ abstract protected function getInstance(); * @param mixed $expected * @param ValueParser|null $parser */ - public function testParseWithValidInputs( $value, $expected, ValueParser $parser = null ) { + public function testParseWithValidInputs( $value, $expected, ?ValueParser $parser = null ) { if ( $parser === null ) { $parser = $this->getInstance(); } @@ -75,12 +77,12 @@ private function assertSmartEquals( $expected, $actual ) { * @param mixed $value * @param ValueParser|null $parser */ - public function testParseWithInvalidInputs( $value, ValueParser $parser = null ) { + public function testParseWithInvalidInputs( $value, ?ValueParser $parser = null ) { if ( $parser === null ) { $parser = $this->getInstance(); } - $this->setExpectedException( ParseException::class ); + $this->expectException( ParseException::class ); $parser->parse( $value ); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ac82d46..f3efb2a 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,11 +1,13 @@