From 81220611b45a58b7f4ee19853218f59e276a96c9 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Wed, 26 Jun 2024 11:30:29 +0200 Subject: [PATCH 1/3] Make `SimpleNormalizer` extendable + readonly --- CHANGELOG.md | 7 +++++++ src/Normalizer/SimpleNormalizer.php | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae4e156..2dd5338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +1.3.0 +===== + +* (feature) Make `SimpleNormalizer` extendable (for testing). +* (improvement) Make `SimpleNormalizer` readonly. + + 1.2.2 ===== diff --git a/src/Normalizer/SimpleNormalizer.php b/src/Normalizer/SimpleNormalizer.php index 10cb654..bb7a180 100644 --- a/src/Normalizer/SimpleNormalizer.php +++ b/src/Normalizer/SimpleNormalizer.php @@ -8,12 +8,15 @@ use Torr\SimpleNormalizer\Exception\ObjectTypeNotSupportedException; use Torr\SimpleNormalizer\Exception\UnsupportedTypeException; -final class SimpleNormalizer +/** + * @final + */ +readonly class SimpleNormalizer { /** */ public function __construct ( - private readonly ServiceLocator $objectNormalizers, + private ServiceLocator $objectNormalizers, ) {} /** From 4687b76246520f3adc02428f56799c9b66490049 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Wed, 26 Jun 2024 11:31:46 +0200 Subject: [PATCH 2/3] Update CI --- .github/workflows/ci.yml | 3 - composer.json | 120 +++++++++++++++--------------- phpstan.neon | 11 ++- phpunit.xml | 7 +- vendor-bin/c-norm/composer.json | 17 +++-- vendor-bin/cs-fixer/composer.json | 7 +- vendor-bin/phpstan/composer.json | 21 ++++++ vendor-bin/test/composer.json | 11 --- 8 files changed, 105 insertions(+), 92 deletions(-) create mode 100644 vendor-bin/phpstan/composer.json delete mode 100644 vendor-bin/test/composer.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce975a1..408e2f2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,9 +2,6 @@ name: CI on: [ push ] -env: - SYMFONY_PHPUNIT_DIR: "$HOME/symfony-bridge/.phpunit" - jobs: build-test: runs-on: ubuntu-latest diff --git a/composer.json b/composer.json index 4ed152f..dcd40bc 100644 --- a/composer.json +++ b/composer.json @@ -1,62 +1,62 @@ { - "name": "21torr/simple-normalizer", - "description": "A simple and fast normalizer bundle for Symfony.", - "license": "MIT", - "type": "symfony-bundle", - "authors": [ - { - "name": "21TORR", - "homepage": "https://www.21torr.com/" - } - ], - "homepage": "https://github.com/21TORR/simple-normalizer", - "require": { - "php": ">= 8.3", - "21torr/bundle-helpers": "^2.1", - "symfony/dependency-injection": "^6.4 || ^7.0", - "symfony/http-kernel": "^6.4 || ^7.0" - }, - "require-dev": { - "bamarni/composer-bin-plugin": "^1.8", - "doctrine/common": "^3.4", - "phpunit/phpunit": "^9.6", - "roave/security-advisories": "dev-latest", - "symfony/phpunit-bridge": "^7.0" - }, - "autoload": { - "psr-4": { - "Torr\\SimpleNormalizer\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Tests\\Torr\\SimpleNormalizer\\": "tests/" - } - }, - "config": { - "allow-plugins": { - "bamarni/composer-bin-plugin": true - }, - "sort-packages": true - }, - "extra": { - "bamarni-bin": { - "bin-links": false, - "forward-command": true - } - }, - "scripts": { - "fix-lint": [ - "vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi", - "@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --ansi" - ], - "lint": [ - "@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --dry-run --ansi", - "vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --dry-run --no-interaction --ansi" - ], - "test": [ - "phpunit", - "vendor-bin/test/vendor/bin/phpstan analyze -c phpstan.neon . --ansi" - ] - } + "name": "21torr/simple-normalizer", + "description": "A simple and fast normalizer bundle for Symfony.", + "license": "MIT", + "type": "symfony-bundle", + "authors": [ + { + "name": "21TORR", + "homepage": "https://www.21torr.com/" + } + ], + "homepage": "https://github.com/21TORR/simple-normalizer", + "require": { + "php": ">= 8.3", + "21torr/bundle-helpers": "^2.1", + "symfony/dependency-injection": "^6.4 || ^7.0", + "symfony/http-kernel": "^6.4 || ^7.0" + }, + "require-dev": { + "21torr/janus": "^1.3", + "bamarni/composer-bin-plugin": "^1.8", + "doctrine/common": "^3.4", + "roave/security-advisories": "dev-latest", + "symfony/phpunit-bridge": "^7.0" + }, + "autoload": { + "psr-4": { + "Torr\\SimpleNormalizer\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\Torr\\SimpleNormalizer\\": "tests/" + } + }, + "config": { + "allow-plugins": { + "bamarni/composer-bin-plugin": true + }, + "sort-packages": true + }, + "extra": { + "bamarni-bin": { + "bin-links": false, + "forward-command": true + } + }, + "scripts": { + "fix-lint": [ + "@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --ansi", + "vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi" + ], + "lint": [ + "@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --dry-run --ansi", + "vendor-bin/cs-fixer/vendor/bin/php-cs-fixer check --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi" + ], + "test": [ + "simple-phpunit", + "vendor-bin/phpstan/vendor/bin/phpstan analyze -c phpstan.neon . --ansi -v" + ] + } } diff --git a/phpstan.neon b/phpstan.neon index 9841b71..cd0577a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,8 @@ -parameters: - level: 9 - includes: - - vendor-bin/test/vendor/21torr/php-cs/phpstan/lib.neon + - vendor/21torr/janus/phpstan/lib.neon + +# If you use simple-phpunit, you need to uncomment the following line. +# Always make sure to first run simple-phpunit and then PHPStan. +# parameters: +# bootstrapFiles: +# - vendor/bin/.phpunit/phpunit/vendor/autoload.php diff --git a/phpunit.xml b/phpunit.xml index e7c0d26..829d78d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,14 +1,15 @@ + diff --git a/vendor-bin/c-norm/composer.json b/vendor-bin/c-norm/composer.json index c806c98..29d96fe 100644 --- a/vendor-bin/c-norm/composer.json +++ b/vendor-bin/c-norm/composer.json @@ -1,10 +1,11 @@ { - "require": { - "ergebnis/composer-normalize": "^2.28" - }, - "config": { - "allow-plugins": { - "ergebnis/composer-normalize": true - } - } + "require-dev": { + "ergebnis/composer-normalize": "^2.42", + "roave/security-advisories": "dev-latest" + }, + "config": { + "allow-plugins": { + "ergebnis/composer-normalize": true + } + } } diff --git a/vendor-bin/cs-fixer/composer.json b/vendor-bin/cs-fixer/composer.json index 05755b0..ceadfce 100644 --- a/vendor-bin/cs-fixer/composer.json +++ b/vendor-bin/cs-fixer/composer.json @@ -1,5 +1,6 @@ { - "require": { - "21torr/php-cs-fixer": "^1.0" - } + "require-dev": { + "21torr/php-cs-fixer": "^1.1.1", + "roave/security-advisories": "dev-latest" + } } diff --git a/vendor-bin/phpstan/composer.json b/vendor-bin/phpstan/composer.json new file mode 100644 index 0000000..cf165ff --- /dev/null +++ b/vendor-bin/phpstan/composer.json @@ -0,0 +1,21 @@ +{ + "require": { + "php": "^8.3" + }, + "require-dev": { + "phpstan/extension-installer": "^1.3.1", + "phpstan/phpstan": "^1.11", + "phpstan/phpstan-deprecation-rules": "^1.2", + "phpstan/phpstan-doctrine": "^1.4", + "phpstan/phpstan-phpunit": "^1.4", + "phpstan/phpstan-symfony": "^1.4", + "roave/security-advisories": "dev-latest", + "staabm/phpstan-todo-by": "^0.1.25" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "phpstan/extension-installer": true + } + } +} diff --git a/vendor-bin/test/composer.json b/vendor-bin/test/composer.json deleted file mode 100644 index a686f64..0000000 --- a/vendor-bin/test/composer.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "require": { - "21torr/php-cs": "^4.0" - }, - "config": { - "allow-plugins": { - "phpstan/extension-installer": true, - "ergebnis/composer-normalize": true - } - } -} From c737bedc03dd8bf5d783944f0e0dd186805bf5c6 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Wed, 26 Jun 2024 11:34:16 +0200 Subject: [PATCH 3/3] Update CI --- composer.json | 120 +++++++++--------- phpstan.neon | 6 +- src/Normalizer/SimpleNormalizer.php | 1 + tests/Normalizer/ArrayNormalizerTest.php | 15 ++- tests/Normalizer/BasicTypesNormalizerTest.php | 9 +- tests/Normalizer/MapNormalizerTest.php | 8 +- tests/Normalizer/ObjectNormalizationTest.php | 11 +- .../ValueWithContextNormalizerTest.php | 7 +- 8 files changed, 96 insertions(+), 81 deletions(-) diff --git a/composer.json b/composer.json index dcd40bc..6c9c11e 100644 --- a/composer.json +++ b/composer.json @@ -1,62 +1,62 @@ { - "name": "21torr/simple-normalizer", - "description": "A simple and fast normalizer bundle for Symfony.", - "license": "MIT", - "type": "symfony-bundle", - "authors": [ - { - "name": "21TORR", - "homepage": "https://www.21torr.com/" - } - ], - "homepage": "https://github.com/21TORR/simple-normalizer", - "require": { - "php": ">= 8.3", - "21torr/bundle-helpers": "^2.1", - "symfony/dependency-injection": "^6.4 || ^7.0", - "symfony/http-kernel": "^6.4 || ^7.0" - }, - "require-dev": { - "21torr/janus": "^1.3", - "bamarni/composer-bin-plugin": "^1.8", - "doctrine/common": "^3.4", - "roave/security-advisories": "dev-latest", - "symfony/phpunit-bridge": "^7.0" - }, - "autoload": { - "psr-4": { - "Torr\\SimpleNormalizer\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Tests\\Torr\\SimpleNormalizer\\": "tests/" - } - }, - "config": { - "allow-plugins": { - "bamarni/composer-bin-plugin": true - }, - "sort-packages": true - }, - "extra": { - "bamarni-bin": { - "bin-links": false, - "forward-command": true - } - }, - "scripts": { - "fix-lint": [ - "@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --ansi", - "vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi" - ], - "lint": [ - "@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --dry-run --ansi", - "vendor-bin/cs-fixer/vendor/bin/php-cs-fixer check --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi" - ], - "test": [ - "simple-phpunit", - "vendor-bin/phpstan/vendor/bin/phpstan analyze -c phpstan.neon . --ansi -v" - ] - } + "name": "21torr/simple-normalizer", + "description": "A simple and fast normalizer bundle for Symfony.", + "license": "MIT", + "type": "symfony-bundle", + "authors": [ + { + "name": "21TORR", + "homepage": "https://www.21torr.com/" + } + ], + "homepage": "https://github.com/21TORR/simple-normalizer", + "require": { + "php": ">= 8.3", + "21torr/bundle-helpers": "^2.1", + "symfony/dependency-injection": "^6.4 || ^7.0", + "symfony/http-kernel": "^6.4 || ^7.0" + }, + "require-dev": { + "21torr/janus": "^1.3", + "bamarni/composer-bin-plugin": "^1.8", + "doctrine/common": "^3.4", + "roave/security-advisories": "dev-latest", + "symfony/phpunit-bridge": "^7.0" + }, + "autoload": { + "psr-4": { + "Torr\\SimpleNormalizer\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\Torr\\SimpleNormalizer\\": "tests/" + } + }, + "config": { + "allow-plugins": { + "bamarni/composer-bin-plugin": true + }, + "sort-packages": true + }, + "extra": { + "bamarni-bin": { + "bin-links": false, + "forward-command": true + } + }, + "scripts": { + "fix-lint": [ + "@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --ansi", + "vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi" + ], + "lint": [ + "@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --dry-run --ansi", + "vendor-bin/cs-fixer/vendor/bin/php-cs-fixer check --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi" + ], + "test": [ + "simple-phpunit", + "vendor-bin/phpstan/vendor/bin/phpstan analyze -c phpstan.neon . --ansi -v" + ] + } } diff --git a/phpstan.neon b/phpstan.neon index cd0577a..c97c69f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,6 +3,6 @@ includes: # If you use simple-phpunit, you need to uncomment the following line. # Always make sure to first run simple-phpunit and then PHPStan. -# parameters: -# bootstrapFiles: -# - vendor/bin/.phpunit/phpunit/vendor/autoload.php +parameters: + bootstrapFiles: + - vendor/bin/.phpunit/phpunit/vendor/autoload.php diff --git a/src/Normalizer/SimpleNormalizer.php b/src/Normalizer/SimpleNormalizer.php index bb7a180..38ac9b6 100644 --- a/src/Normalizer/SimpleNormalizer.php +++ b/src/Normalizer/SimpleNormalizer.php @@ -14,6 +14,7 @@ readonly class SimpleNormalizer { /** + * @param ServiceLocator $objectNormalizers */ public function __construct ( private ServiceLocator $objectNormalizers, diff --git a/tests/Normalizer/ArrayNormalizerTest.php b/tests/Normalizer/ArrayNormalizerTest.php index d07425f..3908473 100644 --- a/tests/Normalizer/ArrayNormalizerTest.php +++ b/tests/Normalizer/ArrayNormalizerTest.php @@ -5,6 +5,9 @@ use PHPUnit\Framework\TestCase; use Torr\SimpleNormalizer\Test\SimpleNormalizerTestTrait; +/** + * @internal + */ final class ArrayNormalizerTest extends TestCase { use SimpleNormalizerTestTrait; @@ -12,7 +15,7 @@ final class ArrayNormalizerTest extends TestCase /** * */ - public function provideListArray () : iterable + public static function provideListArray () : iterable { yield [[1, 2, 3, 4], [1, 2, 3, 4]]; yield [[1, null, 3, null], [1, 3]]; @@ -28,14 +31,14 @@ public function testListArray (array $input, array $expected) : void $normalizer = $this->createNormalizer(); $actual = $normalizer->normalize($input); - self::assertEquals($expected, $actual); - self::assertTrue(\array_is_list($actual)); + self::assertSame($expected, $actual); + self::assertTrue(array_is_list($actual)); } /** * */ - public function provideAssociativeArray () : iterable + public static function provideAssociativeArray () : iterable { yield [["a" => 1, "b" => 2, "c" => 3, "d" => 4], ["a" => 1, "b" => 2, "c" => 3, "d" => 4]]; yield [["a" => 1, "b" => null, "c" => 3, "d" => null], ["a" => 1, "b" => null, "c" => 3, "d" => null]]; @@ -50,7 +53,7 @@ public function testAssociativeArray (array $input, array $expected) : void $normalizer = $this->createNormalizer(); $actual = $normalizer->normalize($input); - self::assertEquals($expected, $actual); - self::assertFalse(\array_is_list($actual)); + self::assertSame($expected, $actual); + self::assertFalse(array_is_list($actual)); } } diff --git a/tests/Normalizer/BasicTypesNormalizerTest.php b/tests/Normalizer/BasicTypesNormalizerTest.php index deef57c..1a4ba93 100644 --- a/tests/Normalizer/BasicTypesNormalizerTest.php +++ b/tests/Normalizer/BasicTypesNormalizerTest.php @@ -3,9 +3,12 @@ namespace Tests\Torr\SimpleNormalizer\Normalizer; use PHPUnit\Framework\TestCase; -use Torr\SimpleNormalizer\Test\SimpleNormalizerTestTrait; use Torr\SimpleNormalizer\Exception\UnsupportedTypeException; +use Torr\SimpleNormalizer\Test\SimpleNormalizerTestTrait; +/** + * @internal + */ final class BasicTypesNormalizerTest extends TestCase { use SimpleNormalizerTestTrait; @@ -13,7 +16,7 @@ final class BasicTypesNormalizerTest extends TestCase /** * */ - public function provideBasicValue () : iterable + public static function provideBasicValue () : iterable { yield [null, null]; yield [5, 5]; @@ -34,7 +37,7 @@ public function testBasicValue (mixed $input, mixed $expected) : void /** * */ - public function provideInvalidValue () : iterable + public static function provideInvalidValue () : iterable { yield [fopen("php://memory", "rb")]; } diff --git a/tests/Normalizer/MapNormalizerTest.php b/tests/Normalizer/MapNormalizerTest.php index 0a79866..c55e412 100644 --- a/tests/Normalizer/MapNormalizerTest.php +++ b/tests/Normalizer/MapNormalizerTest.php @@ -6,6 +6,9 @@ use Symfony\Component\DependencyInjection\ServiceLocator; use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer; +/** + * @internal + */ final class MapNormalizerTest extends TestCase { /** @@ -13,13 +16,12 @@ final class MapNormalizerTest extends TestCase */ public function testNonEmptyMap () : void { - $normalizer = new SimpleNormalizer(new ServiceLocator([])); $result = $normalizer->normalizeMap([ "o" => "hai", ], []); - self::assertSame('{"o":"hai"}', \json_encode($result)); + self::assertSame('{"o":"hai"}', json_encode($result)); } /** @@ -30,6 +32,6 @@ public function testEmptyMap () : void $normalizer = new SimpleNormalizer(new ServiceLocator([])); $result = $normalizer->normalizeMap([], []); - self::assertSame('{}', \json_encode($result)); + self::assertSame('{}', json_encode($result)); } } diff --git a/tests/Normalizer/ObjectNormalizationTest.php b/tests/Normalizer/ObjectNormalizationTest.php index 7a35034..f15f87c 100644 --- a/tests/Normalizer/ObjectNormalizationTest.php +++ b/tests/Normalizer/ObjectNormalizationTest.php @@ -4,11 +4,14 @@ use PHPUnit\Framework\TestCase; use Tests\Torr\SimpleNormalizer\Fixture\DummyVO; -use Torr\SimpleNormalizer\Test\SimpleNormalizerTestTrait; use Torr\SimpleNormalizer\Exception\ObjectTypeNotSupportedException; use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer; use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface; +use Torr\SimpleNormalizer\Test\SimpleNormalizerTestTrait; +/** + * @internal + */ final class ObjectNormalizationTest extends TestCase { use SimpleNormalizerTestTrait; @@ -18,11 +21,11 @@ final class ObjectNormalizationTest extends TestCase */ public function testNormalizeObject () : void { - $dummyNormalizer = new class implements SimpleObjectNormalizerInterface - { + $dummyNormalizer = new class() implements SimpleObjectNormalizerInterface { public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed { \assert($value instanceof DummyVO); + return [ "id" => $value->id, ]; @@ -37,7 +40,7 @@ public static function getNormalizedType () : string $value = new DummyVO(42); $normalizer = $this->createNormalizer($dummyNormalizer); - self::assertEquals(["id" => 42], $normalizer->normalize($value)); + self::assertSame(["id" => 42], $normalizer->normalize($value)); } /** diff --git a/tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php b/tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php index d27a351..2187885 100644 --- a/tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php +++ b/tests/Normalizer/ObjectNormalizer/ValueWithContextNormalizerTest.php @@ -10,6 +10,9 @@ use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer; use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface; +/** + * @internal + */ final class ValueWithContextNormalizerTest extends TestCase { /** @@ -37,8 +40,8 @@ public function testContextPassing () : void ); $normalizer = new SimpleNormalizer(new ServiceLocator([ - ValueWithContext::class => fn () => new ValueWithContextNormalizer(), - DummyVO::class => fn () => $dummyNormalizer, + ValueWithContext::class => static fn () => new ValueWithContextNormalizer(), + DummyVO::class => static fn () => $dummyNormalizer, ])); $normalizer->normalize($value, [