diff --git a/composer.json b/composer.json index 231ab583e..08406fc41 100644 --- a/composer.json +++ b/composer.json @@ -44,10 +44,10 @@ "jackalope/jackalope-doctrine-dbal": "^1.3", "ocramius/proxy-manager": "^1.0 || ^2.0", "phpbench/phpbench": "^1.0", - "phpstan/phpstan": "^1.0.2", - "phpunit/phpunit": "^9.0 || ^10.0", + "phpstan/phpstan": "^1.10.57", + "phpunit/phpunit": "^9.0 || ^10.0 || ^11.0", "psr/container": "^1.0 || ^2.0", - "rector/rector": "^0.19.0", + "rector/rector": "^1.0.0", "symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0", "symfony/expression-language": "^5.4 || ^6.0 || ^7.0", "symfony/filesystem": "^5.4 || ^6.0 || ^7.0", diff --git a/phpstan/ignore-by-php-version.neon.php b/phpstan/ignore-by-php-version.neon.php index d5d5a0520..b3ef4e83b 100644 --- a/phpstan/ignore-by-php-version.neon.php +++ b/phpstan/ignore-by-php-version.neon.php @@ -1,5 +1,8 @@ = 80000) { $includes[] = __DIR__ . '/ignore-missing-attribute.neon'; + if(!class_exists(DataProvider::class)) { + $includes[] = __DIR__ . '/no-phpunit-attributes.neon'; + } } if (PHP_VERSION_ID >= 80100 && PHP_VERSION_ID < 80200) { $includes[] = __DIR__ . '/php-81.neon'; } -if(!class_exists(Symfony\Component\Uid\UuidV7::class)) { +if(!class_exists(UuidV7::class)) { $includes[] = __DIR__ . '/no-uuid-7.neon'; } + $config = []; $config['includes'] = $includes; $config['parameters']['phpVersion'] = PHP_VERSION_ID; diff --git a/phpstan/no-phpunit-attributes.neon b/phpstan/no-phpunit-attributes.neon new file mode 100644 index 000000000..a66b213b9 --- /dev/null +++ b/phpstan/no-phpunit-attributes.neon @@ -0,0 +1,3 @@ +parameters: + ignoreErrors: + - '~Attribute class PHPUnit\\Framework\\Attributes\\.* does not exist~' diff --git a/rector.php b/rector.php index 6cc8584b1..154c683ee 100644 --- a/rector.php +++ b/rector.php @@ -9,5 +9,5 @@ $rectorConfig->paths([ __DIR__ . '/src', ]); - $rectorConfig->sets([LevelSetList::UP_TO_PHP_74]); + $rectorConfig->phpVersion(74); }; diff --git a/tests/Deserializer/BaseDeserializationTest.php b/tests/Deserializer/BaseDeserializationTest.php index 8aaf6b71a..736b8ef83 100644 --- a/tests/Deserializer/BaseDeserializationTest.php +++ b/tests/Deserializer/BaseDeserializationTest.php @@ -11,6 +11,7 @@ use JMS\Serializer\Tests\Fixtures\GroupsObject; use JMS\Serializer\Tests\Fixtures\Price; use JMS\Serializer\Tests\Fixtures\Publisher; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class BaseDeserializationTest extends TestCase @@ -18,6 +19,7 @@ class BaseDeserializationTest extends TestCase /** * @dataProvider dataTypeCannotBeCasted */ + #[DataProvider('dataTypeCannotBeCasted')] public function testDeserializationInvalidDataCausesException($data, string $type): void { $serializer = SerializerBuilder::create()->build(); @@ -48,6 +50,7 @@ public static function dataTypeCannotBeCasted(): iterable /** * @dataProvider dataDeserializerGroupExclusion */ + #[DataProvider('dataDeserializerGroupExclusion')] public function testDeserializerGroupExclusion(array $data, array $groups, array $expected): void { $serializer = SerializerBuilder::create()->build(); diff --git a/tests/Exclusion/DisjunctExclusionStrategyTest.php b/tests/Exclusion/DisjunctExclusionStrategyTest.php index 5b25d59d9..4ad965d48 100644 --- a/tests/Exclusion/DisjunctExclusionStrategyTest.php +++ b/tests/Exclusion/DisjunctExclusionStrategyTest.php @@ -25,7 +25,7 @@ public function testShouldSkipClassShortCircuiting() $first->expects($this->once()) ->method('shouldSkipClass') ->with($metadata, $context) - ->will($this->returnValue(true)); + ->willReturn(true); $last->expects($this->never()) ->method('shouldSkipClass'); @@ -46,12 +46,12 @@ public function testShouldSkipClassDisjunctBehavior() $first->expects($this->once()) ->method('shouldSkipClass') ->with($metadata, $context) - ->will($this->returnValue(false)); + ->willReturn(false); $last->expects($this->once()) ->method('shouldSkipClass') ->with($metadata, $context) - ->will($this->returnValue(true)); + ->willReturn(true); self::assertTrue($strat->shouldSkipClass($metadata, $context)); } @@ -69,12 +69,12 @@ public function testShouldSkipClassReturnsFalseIfNoPredicateMatched() $first->expects($this->once()) ->method('shouldSkipClass') ->with($metadata, $context) - ->will($this->returnValue(false)); + ->willReturn(false); $last->expects($this->once()) ->method('shouldSkipClass') ->with($metadata, $context) - ->will($this->returnValue(false)); + ->willReturn(false); self::assertFalse($strat->shouldSkipClass($metadata, $context)); } @@ -92,7 +92,7 @@ public function testShouldSkipPropertyShortCircuiting() $first->expects($this->once()) ->method('shouldSkipProperty') ->with($metadata, $context) - ->will($this->returnValue(true)); + ->willReturn(true); $last->expects($this->never()) ->method('shouldSkipProperty'); @@ -113,12 +113,12 @@ public function testShouldSkipPropertyDisjunct() $first->expects($this->once()) ->method('shouldSkipProperty') ->with($metadata, $context) - ->will($this->returnValue(false)); + ->willReturn(false); $last->expects($this->once()) ->method('shouldSkipProperty') ->with($metadata, $context) - ->will($this->returnValue(true)); + ->willReturn(true); self::assertTrue($strat->shouldSkipProperty($metadata, $context)); } @@ -136,12 +136,12 @@ public function testShouldSkipPropertyReturnsFalseIfNoPredicateMatches() $first->expects($this->once()) ->method('shouldSkipProperty') ->with($metadata, $context) - ->will($this->returnValue(false)); + ->willReturn(false); $last->expects($this->once()) ->method('shouldSkipProperty') ->with($metadata, $context) - ->will($this->returnValue(false)); + ->willReturn(false); self::assertFalse($strat->shouldSkipProperty($metadata, $context)); } diff --git a/tests/Exclusion/GroupsExclusionStrategyTest.php b/tests/Exclusion/GroupsExclusionStrategyTest.php index 02ea11069..05f4c29d6 100644 --- a/tests/Exclusion/GroupsExclusionStrategyTest.php +++ b/tests/Exclusion/GroupsExclusionStrategyTest.php @@ -7,18 +7,16 @@ use JMS\Serializer\Exclusion\GroupsExclusionStrategy; use JMS\Serializer\Metadata\StaticPropertyMetadata; use JMS\Serializer\SerializationContext; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class GroupsExclusionStrategyTest extends TestCase { /** - * @param array $propertyGroups - * @param array $groups - * @param bool $exclude - * * @dataProvider getExclusionRules */ - public function testUninitializedContextIsWorking(array $propertyGroups, array $groups, $exclude) + #[DataProvider('getExclusionRules')] + public function testUninitializedContextIsWorking(array $propertyGroups, array $groups, bool $exclude) { $metadata = new StaticPropertyMetadata('stdClass', 'prop', 'propVal'); $metadata->groups = $propertyGroups; @@ -57,6 +55,7 @@ public static function getExclusionRules() /** * @dataProvider getGroupsFor */ + #[DataProvider('getGroupsFor')] public function testGroupsFor(array $groups, array $propsVisited, array $resultingGroups) { $exclusion = new GroupsExclusionStrategy($groups); diff --git a/tests/Handler/ArrayCollectionDepthTest.php b/tests/Handler/ArrayCollectionDepthTest.php index d0ddcf143..1c924635a 100644 --- a/tests/Handler/ArrayCollectionDepthTest.php +++ b/tests/Handler/ArrayCollectionDepthTest.php @@ -11,6 +11,7 @@ use JMS\Serializer\SerializationContext; use JMS\Serializer\Serializer as JMSSerializer; use JMS\Serializer\SerializerBuilder; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ArrayCollectionDepthTest extends TestCase @@ -28,6 +29,7 @@ protected function setUp(): void * * @dataProvider getCollections */ + #[DataProvider('getCollections')] public function testDepth($collection) { $context = SerializationContext::create() diff --git a/tests/Handler/ArrayCollectionHandlerTest.php b/tests/Handler/ArrayCollectionHandlerTest.php index 3fc13e34c..00053fd6f 100644 --- a/tests/Handler/ArrayCollectionHandlerTest.php +++ b/tests/Handler/ArrayCollectionHandlerTest.php @@ -11,6 +11,7 @@ use JMS\Serializer\Tests\Fixtures\ExclusionStrategy\AlwaysExcludeExclusionStrategy; use JMS\Serializer\Visitor\SerializationVisitorInterface; use Metadata\MetadataFactoryInterface; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; use PHPUnit\Framework\TestCase; class ArrayCollectionHandlerTest extends TestCase @@ -18,6 +19,7 @@ class ArrayCollectionHandlerTest extends TestCase /** * @doesNotPerformAssertions */ + #[DoesNotPerformAssertions] public function testSerializeArray() { $handler = new ArrayCollectionHandler(); @@ -36,6 +38,7 @@ public function testSerializeArray() /** * @doesNotPerformAssertions */ + #[DoesNotPerformAssertions] public function testSerializeArraySkipByExclusionStrategy() { $handler = new ArrayCollectionHandler(false); diff --git a/tests/Handler/DateHandlerTest.php b/tests/Handler/DateHandlerTest.php index 17b00c0ac..86d4955da 100644 --- a/tests/Handler/DateHandlerTest.php +++ b/tests/Handler/DateHandlerTest.php @@ -9,6 +9,8 @@ use JMS\Serializer\SerializationContext; use JMS\Serializer\Visitor\DeserializationVisitorInterface; use JMS\Serializer\Visitor\SerializationVisitorInterface; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; use PHPUnit\Framework\TestCase; class DateHandlerTest extends TestCase @@ -39,11 +41,11 @@ public static function getParams() } /** - * @param array $params - * * @doesNotPerformAssertions * @dataProvider getParams */ + #[DataProvider('getParams')] + #[DoesNotPerformAssertions] public function testSerializeDate(array $params) { $context = $this->getMockBuilder(SerializationContext::class)->getMock(); @@ -57,12 +59,10 @@ public function testSerializeDate(array $params) } /** - * @param string $dateInterval - * @param \DateTime $expected - * * @dataProvider getDeserializeDateInterval */ - public function testDeserializeDateInterval($dateInterval, $expected) + #[DataProvider('getDeserializeDateInterval')] + public function testDeserializeDateInterval(string $dateInterval, array $expected) { $visitor = $this->getMockBuilder(DeserializationVisitorInterface::class)->getMock(); $visitor->method('visitString')->with('2017-06-18'); diff --git a/tests/Handler/IteratorHandlerTest.php b/tests/Handler/IteratorHandlerTest.php index bc2affafd..d49f32b90 100644 --- a/tests/Handler/IteratorHandlerTest.php +++ b/tests/Handler/IteratorHandlerTest.php @@ -11,6 +11,7 @@ use JMS\Serializer\SerializationContext; use JMS\Serializer\Visitor\DeserializationVisitorInterface; use JMS\Serializer\Visitor\SerializationVisitorInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; final class IteratorHandlerTest extends TestCase @@ -40,6 +41,7 @@ public static function iteratorsProvider(): array /** * @dataProvider iteratorsProvider */ + #[DataProvider('iteratorsProvider')] public function testSerialize(\Iterator $iterator): void { $type = get_class($iterator); diff --git a/tests/Handler/SymfonyUidHandlerTest.php b/tests/Handler/SymfonyUidHandlerTest.php index 1a5a2f41a..9c7cf1b59 100644 --- a/tests/Handler/SymfonyUidHandlerTest.php +++ b/tests/Handler/SymfonyUidHandlerTest.php @@ -10,6 +10,7 @@ use JMS\Serializer\Handler\SymfonyUidHandler; use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Symfony\Component\Uid\AbstractUid; use Symfony\Component\Uid\Ulid; @@ -46,6 +47,7 @@ public static function dataUid(): \Generator /** * @dataProvider dataUid */ + #[DataProvider('dataUid')] public function testSerializeUidToJson(AbstractUid $uid): void { self::assertJsonStringEqualsJsonString( @@ -57,6 +59,7 @@ public function testSerializeUidToJson(AbstractUid $uid): void /** * @dataProvider dataUid */ + #[DataProvider('dataUid')] public function testSerializeUidToXmlWithCData(AbstractUid $uid): void { self::assertXmlStringEqualsXmlString( @@ -68,6 +71,7 @@ public function testSerializeUidToXmlWithCData(AbstractUid $uid): void /** * @dataProvider dataUid */ + #[DataProvider('dataUid')] public function testSerializeUidToXmlWithoutCData(AbstractUid $uid): void { self::assertXmlStringEqualsXmlString( @@ -117,6 +121,7 @@ public function testSerializeUidRejectsInvalidFormat(): void /** * @dataProvider dataUid */ + #[DataProvider('dataUid')] public function testDeserializeUidFromJson(AbstractUid $uid): void { self::assertTrue($uid->equals($this->createSerializer()->deserialize(sprintf('"%s"', (string) $uid), \get_class($uid), 'json'))); @@ -125,6 +130,7 @@ public function testDeserializeUidFromJson(AbstractUid $uid): void /** * @dataProvider dataUid */ + #[DataProvider('dataUid')] public function testDeserializeUidFromXml(AbstractUid $uid): void { self::assertTrue($uid->equals($this->createSerializer()->deserialize(sprintf('%s', (string) $uid), \get_class($uid), 'xml'))); diff --git a/tests/Metadata/ClassMetadataTest.php b/tests/Metadata/ClassMetadataTest.php index 6b523cfeb..eb31e4954 100644 --- a/tests/Metadata/ClassMetadataTest.php +++ b/tests/Metadata/ClassMetadataTest.php @@ -4,8 +4,10 @@ namespace JMS\Serializer\Tests\Metadata; +use JMS\Serializer\Exception\InvalidMetadataException; use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ClassMetadataTest extends TestCase @@ -23,14 +25,14 @@ public static function getAccessOrderCases() public function testSerialization() { - $meta = new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'b'); + $meta = new PropertyMetadata(PropertyMetadataOrder::class, 'b'); $restoredMeta = unserialize(serialize($meta)); self::assertEquals($meta, $restoredMeta); } public function testSerializationClass() { - $meta = new ClassMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder'); + $meta = new ClassMetadata(PropertyMetadataOrder::class); $meta->xmlRootPrefix = 'foo'; $meta->xmlDiscriminatorCData = true; $meta->xmlDiscriminatorAttribute = false; @@ -43,11 +45,12 @@ public function testSerializationClass() /** * @dataProvider getAccessOrderCases */ + #[DataProvider('getAccessOrderCases')] public function testSetAccessorOrderCustom(array $order, array $expected) { - $metadata = new ClassMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder'); - $metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'b')); - $metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'a')); + $metadata = new ClassMetadata(PropertyMetadataOrder::class); + $metadata->addPropertyMetadata(new PropertyMetadata(PropertyMetadataOrder::class, 'b')); + $metadata->addPropertyMetadata(new PropertyMetadata(PropertyMetadataOrder::class, 'a')); self::assertEquals(['b', 'a'], array_keys($metadata->propertyMetadata)); $metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_CUSTOM, $order); @@ -56,9 +59,9 @@ public function testSetAccessorOrderCustom(array $order, array $expected) public function testSetAccessorOrderAlphabetical() { - $metadata = new ClassMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder'); - $metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'b')); - $metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'a')); + $metadata = new ClassMetadata(PropertyMetadataOrder::class); + $metadata->addPropertyMetadata(new PropertyMetadata(PropertyMetadataOrder::class, 'b')); + $metadata->addPropertyMetadata(new PropertyMetadata(PropertyMetadataOrder::class, 'a')); self::assertEquals(['b', 'a'], array_keys($metadata->propertyMetadata)); $metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_ALPHABETICAL); @@ -68,6 +71,7 @@ public function testSetAccessorOrderAlphabetical() /** * @dataProvider providerPublicMethodData */ + #[DataProvider('providerPublicMethodData')] public function testAccessorTypePublicMethod($property, $getterInit, $setterInit, $getterName, $setterName) { $object = new PropertyMetadataPublicMethod(); @@ -82,9 +86,10 @@ public function testAccessorTypePublicMethod($property, $getterInit, $setterInit /** * @dataProvider providerPublicMethodException */ + #[DataProvider('providerPublicMethodException')] public function testAccessorTypePublicMethodException($getter, $setter, $message) { - $this->expectException('\JMS\Serializer\Exception\InvalidMetadataException'); + $this->expectException(InvalidMetadataException::class); $this->expectExceptionMessage($message); $object = new PropertyMetadataPublicMethod(); diff --git a/tests/Metadata/Driver/DoctrineDriverTest.php b/tests/Metadata/Driver/DoctrineDriverTest.php index 976f02b91..bfaf44f90 100644 --- a/tests/Metadata/Driver/DoctrineDriverTest.php +++ b/tests/Metadata/Driver/DoctrineDriverTest.php @@ -174,7 +174,7 @@ protected function getDoctrineDriver() $registry = $this->getMockBuilder(ManagerRegistry::class)->getMock(); $registry->expects($this->atLeastOnce()) ->method('getManagerForClass') - ->will($this->returnValue($this->getEntityManager())); + ->willReturn($this->getEntityManager()); return new DoctrineTypeDriver( $this->getMetadataDriver(), diff --git a/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php b/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php index b05ac295d..37c92e7a7 100644 --- a/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php +++ b/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php @@ -12,6 +12,11 @@ use JMS\Serializer\Metadata\Driver\AnnotationDriver; use JMS\Serializer\Metadata\Driver\DoctrinePHPCRTypeDriver; use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; +use JMS\Serializer\Tests\Fixtures\BlogPost; +use JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\Author; +use JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\BlogPost as BlogPostPHPCR; +use JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\Comment; +use PHPCR\SessionInterface; use PHPUnit\Framework\TestCase; class DoctrinePHPCRDriverTest extends TestCase @@ -25,7 +30,7 @@ protected function setUp(): void public function getMetadata() { - $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\BlogPost'); + $refClass = new \ReflectionClass(BlogPostPHPCR::class); return $this->getDoctrinePHPCRDriver()->loadMetadataForClass($refClass); } @@ -43,7 +48,7 @@ public function testSingleValuedAssociationIsProperlyHinted() { $metadata = $this->getMetadata(); self::assertEquals( - ['name' => 'JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\Author', 'params' => []], + ['name' => Author::class, 'params' => []], $metadata->propertyMetadata['author']->type, ); } @@ -56,7 +61,7 @@ public function testMultiValuedAssociationIsProperlyHinted() [ 'name' => 'ArrayCollection', 'params' => [ - ['name' => 'JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\Comment', 'params' => []], + ['name' => Comment::class, 'params' => []], ], ], $metadata->propertyMetadata['comments']->type, @@ -78,7 +83,7 @@ public function testNonDoctrineDocumentClassIsNotModified() { // Note: Using regular BlogPost fixture here instead of Doctrine fixture // because it has no Doctrine metadata. - $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'); + $refClass = new \ReflectionClass(BlogPost::class); $plainMetadata = $this->getAnnotationDriver()->loadMetadataForClass($refClass); $doctrineMetadata = $this->getDoctrinePHPCRDriver()->loadMetadataForClass($refClass); @@ -100,7 +105,7 @@ protected function getDocumentManager() new DoctrinePHPCRDriver(new AnnotationReader(), __DIR__ . '/../../Fixtures/DoctrinePHPCR'), ); - $session = $this->getMockBuilder('PHPCR\SessionInterface')->getMock(); + $session = $this->getMockBuilder(SessionInterface::class)->getMock(); return DocumentManager::create($session, $config); } @@ -115,7 +120,7 @@ protected function getDoctrinePHPCRDriver() $registry = $this->getMockBuilder(ManagerRegistry::class)->getMock(); $registry->expects($this->atLeastOnce()) ->method('getManagerForClass') - ->will($this->returnValue($this->getDocumentManager())); + ->willReturn($this->getDocumentManager()); return new DoctrinePHPCRTypeDriver( $this->getAnnotationDriver(), diff --git a/tests/Ordering/CustomPropertyOrderingStrategyTest.php b/tests/Ordering/CustomPropertyOrderingStrategyTest.php index 73d5be866..b1062bd04 100644 --- a/tests/Ordering/CustomPropertyOrderingStrategyTest.php +++ b/tests/Ordering/CustomPropertyOrderingStrategyTest.php @@ -6,6 +6,7 @@ use JMS\Serializer\Metadata\PropertyMetadata; use JMS\Serializer\Ordering\CustomPropertyOrderingStrategy; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class CustomPropertyOrderingStrategyTest extends TestCase @@ -13,6 +14,7 @@ class CustomPropertyOrderingStrategyTest extends TestCase /** * @dataProvider dataOrder */ + #[DataProvider('dataOrder')] public function testOrder(array $ordering, array $keysToSort, array $expectedResult): void { $strategy = new CustomPropertyOrderingStrategy(array_flip($ordering)); diff --git a/tests/Serializer/ArrayTest.php b/tests/Serializer/ArrayTest.php index d548ac96f..a59c03a51 100644 --- a/tests/Serializer/ArrayTest.php +++ b/tests/Serializer/ArrayTest.php @@ -4,12 +4,14 @@ namespace JMS\Serializer\Tests\Serializer; +use JMS\Serializer\Exception\RuntimeException; use JMS\Serializer\SerializerBuilder; use JMS\Serializer\Tests\Fixtures\Author; use JMS\Serializer\Tests\Fixtures\AuthorList; use JMS\Serializer\Tests\Fixtures\Order; use JMS\Serializer\Tests\Fixtures\Price; use JMS\Serializer\Tests\Fixtures\TypedProperties\ConstructorPromotion\DefaultValuesAndAccessors; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ArrayTest extends TestCase @@ -38,9 +40,10 @@ public function testToArray() /** * @dataProvider scalarValues */ + #[DataProvider('scalarValues')] public function testToArrayWithScalar($input) { - $this->expectException('JMS\Serializer\Exception\RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage(sprintf( 'The input data of type "%s" did not convert to an array, but got a result of type "%s".', gettype($input), @@ -68,7 +71,7 @@ public function testFromArray() ]; $expected = new Order(new Price(2.5)); - $result = $this->serializer->fromArray($data, 'JMS\Serializer\Tests\Fixtures\Order'); + $result = $this->serializer->fromArray($data, Order::class); self::assertEquals($expected, $result); } diff --git a/tests/Serializer/BaseSerializationTestCase.php b/tests/Serializer/BaseSerializationTestCase.php index 7400151ff..d9acd9574 100644 --- a/tests/Serializer/BaseSerializationTestCase.php +++ b/tests/Serializer/BaseSerializationTestCase.php @@ -5,6 +5,7 @@ namespace JMS\Serializer\Tests\Serializer; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Version; use JMS\Serializer\Construction\UnserializeObjectConstructor; use JMS\Serializer\Context; use JMS\Serializer\DeserializationContext; @@ -62,6 +63,7 @@ use JMS\Serializer\Tests\Fixtures\Discriminator\Post; use JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\ExtendedUser; use JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\User; +use JMS\Serializer\Tests\Fixtures\Discriminator\Vehicle; use JMS\Serializer\Tests\Fixtures\DiscriminatorGroup\Car as DiscriminatorGroupCar; use JMS\Serializer\Tests\Fixtures\DocBlockType\UnionTypedDocBLockProperty; use JMS\Serializer\Tests\Fixtures\ExclusionStrategy\AlwaysExcludeExclusionStrategy; @@ -127,9 +129,14 @@ use JMS\Serializer\Tests\Fixtures\VehicleInterfaceGarage; use JMS\Serializer\Visitor\DeserializationVisitorInterface; use JMS\Serializer\Visitor\SerializationVisitorInterface; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; +use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\ExpressionLanguage\ExpressionFunction; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; +use Symfony\Component\Form\DataMapperInterface; use Symfony\Component\Form\Extension\Core\Type\ButtonType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Form; @@ -269,6 +276,7 @@ public function testDeserializeNullObject() /** * @dataProvider getTypes */ + #[DataProvider('getTypes')] public function testNull($type) { if ($this->hasDeserializer()) { @@ -286,6 +294,7 @@ public function testNull($type) /** * @dataProvider getTypes */ + #[DataProvider('getTypes')] public function testNullAllowed($type) { $context = SerializationContext::create()->setSerializeNull(true); @@ -551,6 +560,7 @@ public static function expressionFunctionProvider() * * @dataProvider expressionFunctionProvider */ + #[DataProvider('expressionFunctionProvider')] public function testExpressionExclusion($person, ExpressionFunction $function, $json) { $language = new ExpressionLanguage(); @@ -566,6 +576,7 @@ public function testExpressionExclusion($person, ExpressionFunction $function, $ /** * @dataProvider getBooleans */ + #[DataProvider('getBooleans')] public function testBooleans($strBoolean, $boolean) { self::assertEquals(static::getContent('boolean_' . $strBoolean), $this->serialize($boolean)); @@ -583,6 +594,7 @@ public static function getBooleans() /** * @dataProvider getNumerics */ + #[DataProvider('getNumerics')] public function testNumerics($key, $value, $type) { self::assertSame(static::getContent($key), $this->serialize($value)); @@ -816,9 +828,6 @@ public function testNamedDateTimeArrays() } } - /** - * @group datetime - */ public function testNamedDateTimeImmutableArrays() { $data = [ @@ -857,6 +866,7 @@ public function testArrayMixed() * @dataProvider getDateTime * @group datetime */ + #[DataProvider('getDateTime')] public function testDateTime($key, $value, $type) { self::assertEquals(static::getContent($key), $this->serialize($value)); @@ -882,6 +892,7 @@ public static function getDateTime() * @dataProvider getDateTimeImmutable * @group datetime */ + #[DataProvider('getDateTimeImmutable')] public function testDateTimeImmutable($key, $value, $type) { self::assertEquals(static::getContent($key), $this->serialize($value)); @@ -1245,13 +1256,14 @@ public function testFormErrors() /** * @dataProvider initialFormTypeProvider */ + #[DataProvider('initialFormTypeProvider')] public function testNestedFormErrors($type) { - $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); + $dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); $formConfigBuilder = new FormConfigBuilder('foo', null, $dispatcher); $formConfigBuilder->setCompound(true); - $formConfigBuilder->setDataMapper($this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock()); + $formConfigBuilder->setDataMapper($this->getMockBuilder(DataMapperInterface::class)->getMock()); $fooConfig = $formConfigBuilder->getFormConfig(); $form = new Form($fooConfig); @@ -1273,9 +1285,11 @@ public function testNestedFormErrors($type) * @doesNotPerformAssertions * @dataProvider initialFormTypeProvider */ + #[DataProvider('initialFormTypeProvider')] + #[DoesNotPerformAssertions] public function testFormErrorsWithNonFormComponents($type) { - $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); + $dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); $factoryBuilder = new FormFactoryBuilder(); $factoryBuilder->addType(new SubmitType()); @@ -1285,7 +1299,7 @@ public function testFormErrorsWithNonFormComponents($type) $formConfigBuilder = new FormConfigBuilder('foo', null, $dispatcher); $formConfigBuilder->setFormFactory($factory); $formConfigBuilder->setCompound(true); - $formConfigBuilder->setDataMapper($this->getMockBuilder('Symfony\Component\Form\DataMapperInterface')->getMock()); + $formConfigBuilder->setDataMapper($this->getMockBuilder(DataMapperInterface::class)->getMock()); $fooConfig = $formConfigBuilder->getFormConfig(); $form = new Form($fooConfig); @@ -1327,7 +1341,7 @@ public function testConstraintViolationList() public function testDoctrineProxy() { - if (!class_exists('Doctrine\ORM\Version')) { + if (!class_exists(Version::class)) { $this->markTestSkipped('Doctrine is not available.'); } @@ -1338,7 +1352,7 @@ public function testDoctrineProxy() public function testInitializedDoctrineProxy() { - if (!class_exists('Doctrine\ORM\Version')) { + if (!class_exists(Version::class)) { $this->markTestSkipped('Doctrine is not available.'); } @@ -1362,7 +1376,7 @@ public function testMixedAccessTypes() self::assertEquals(static::getContent('mixed_access_types'), $this->serialize($object)); if ($this->hasDeserializer()) { - $object = $this->deserialize(static::getContent('mixed_access_types'), 'JMS\Serializer\Tests\Fixtures\GetSetObject'); + $object = $this->deserialize(static::getContent('mixed_access_types'), GetSetObject::class); self::assertSame(1, $this->getField($object, 'id')); self::assertSame('Johannes', $this->getField($object, 'name')); self::assertSame(42, $this->getField($object, 'readOnlyProperty')); @@ -1592,6 +1606,7 @@ public function testUninitializedTypedProperties() /** * @doesNotPerformAssertions */ + #[DoesNotPerformAssertions] public function testCustomHandlerVisitingNull() { $handler = static function ($visitor, $attachment, array $type, Context $context) { @@ -1615,9 +1630,6 @@ public function testObjectWithEmptyHash() self::assertEquals(static::getContent('hash_empty'), $this->serializer->serialize(new ObjectWithEmptyHash(), $this->getFormat())); } - /** - * @group null - */ public function testSerializeObjectWhenNull() { self::assertEquals( @@ -1631,9 +1643,6 @@ public function testSerializeObjectWhenNull() ); } - /** - * @group polymorphic - */ public function testPolymorphicObjectsWithGroup() { $context = SerializationContext::create(); @@ -1663,6 +1672,7 @@ public static function getDiscrimatorObjectsSamples(): array * * @dataProvider getDiscrimatorObjectsSamples */ + #[DataProvider('getDiscrimatorObjectsSamples')] public function testDiscrimatorObjects($data, $contentId) { $context = SerializationContext::create()->setGroups(['entity.identification']); @@ -1672,9 +1682,6 @@ public function testDiscrimatorObjects($data, $contentId) ); } - /** - * @group polymorphic - */ public function testPolymorphicObjects() { self::assertEquals( @@ -1695,7 +1702,7 @@ public function testPolymorphicObjects() new Car(5), $this->deserialize( static::getContent('car'), - 'JMS\Serializer\Tests\Fixtures\Discriminator\Car', + Car::class, ), 'Class is resolved correctly when concrete sub-class is used.', ); @@ -1704,7 +1711,7 @@ public function testPolymorphicObjects() new Car(5), $this->deserialize( static::getContent('car'), - 'JMS\Serializer\Tests\Fixtures\Discriminator\Vehicle', + Vehicle::class, ), 'Class is resolved correctly when least supertype is used.', ); @@ -1713,7 +1720,7 @@ public function testPolymorphicObjects() new Car(5), $this->deserialize( static::getContent('car_without_type'), - 'JMS\Serializer\Tests\Fixtures\Discriminator\Car', + Car::class, ), 'Class is resolved correctly when concrete sub-class is used and no type is defined.', ); @@ -1722,7 +1729,7 @@ public function testPolymorphicObjects() new Post('Post Title'), $this->deserialize( static::getContent('post'), - 'JMS\Serializer\Tests\Fixtures\Discriminator\Post', + Post::class, ), 'Class is resolved correctly when parent class is used and type is set.', ); @@ -1731,7 +1738,7 @@ public function testPolymorphicObjects() new ImagePost('Image Post Title'), $this->deserialize( static::getContent('image_post'), - 'JMS\Serializer\Tests\Fixtures\Discriminator\Post', + Post::class, ), 'Class is resolved correctly when least supertype is used.', ); @@ -1740,16 +1747,13 @@ public function testPolymorphicObjects() new ImagePost('Image Post Title'), $this->deserialize( static::getContent('image_post'), - 'JMS\Serializer\Tests\Fixtures\Discriminator\ImagePost', + ImagePost::class, ), 'Class is resolved correctly when concrete sub-class is used and no type is defined.', ); } } - /** - * @group polymorphic - */ public function testNestedPolymorphicObjects() { $garage = new Garage([new Car(3), new Moped(1)]); @@ -1763,15 +1767,12 @@ public function testNestedPolymorphicObjects() $garage, $this->deserialize( static::getContent('garage'), - 'JMS\Serializer\Tests\Fixtures\Garage', + Garage::class, ), ); } } - /** - * @group polymorphic - */ public function testNestedPolymorphicInterfaces() { $garage = new VehicleInterfaceGarage([new Car(3), new Moped(1)]); @@ -1785,15 +1786,12 @@ public function testNestedPolymorphicInterfaces() $garage, $this->deserialize( static::getContent('garage'), - 'JMS\Serializer\Tests\Fixtures\VehicleInterfaceGarage', + VehicleInterfaceGarage::class, ), ); } } - /** - * @group polymorphic - */ public function testPolymorphicObjectsInvalidDeserialization() { $this->expectException(\LogicException::class); @@ -1804,7 +1802,7 @@ public function testPolymorphicObjectsInvalidDeserialization() $this->deserialize( static::getContent('car_without_type'), - 'JMS\Serializer\Tests\Fixtures\Discriminator\Vehicle', + Vehicle::class, ); } @@ -1896,6 +1894,7 @@ public function testObjectWithNullableArrays() /** * @dataProvider getSerializeNullCases */ + #[DataProvider('getSerializeNullCases')] public function testSerializeNullArrayObjectWithExclusionStrategy(bool $serializeNull) { $arr = [ @@ -1948,6 +1947,7 @@ public static function getFirstClassListCollectionsValues() * * @dataProvider getFirstClassListCollectionsValues */ + #[DataProvider('getFirstClassListCollectionsValues')] public function testFirstClassListCollections($items, $expected, ?FirstClassListCollection $expectedDeserializatrion = null) { $collection = new FirstClassListCollection($items); @@ -2143,7 +2143,7 @@ static function (DeserializationVisitorInterface $visitor, $data, $type, Context 'name' => 'array', 'params' => [ ['name' => 'integer', 'params' => []], - ['name' => 'JMS\Serializer\Tests\Fixtures\Author', 'params' => []], + ['name' => Author::class, 'params' => []], ], ]; diff --git a/tests/Serializer/ContextTest.php b/tests/Serializer/ContextTest.php index b70227985..c5fe9078f 100644 --- a/tests/Serializer/ContextTest.php +++ b/tests/Serializer/ContextTest.php @@ -5,6 +5,7 @@ namespace JMS\Serializer\Tests\Serializer; use JMS\Serializer\Exception\LogicException; +use JMS\Serializer\Exclusion\ExclusionStrategyInterface; use JMS\Serializer\GraphNavigator; use JMS\Serializer\Metadata\ClassMetadata; use JMS\Serializer\Metadata\PropertyMetadata; @@ -18,6 +19,7 @@ use JMS\Serializer\Tests\Fixtures\VersionedObject; use JMS\Serializer\VisitorInterface; use Metadata\MetadataFactoryInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ContextTest extends TestCase @@ -32,7 +34,7 @@ public function testSerializationContextPathAndDepth() $self = $this; - $exclusionStrategy = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(); + $exclusionStrategy = $this->getMockBuilder(ExclusionStrategyInterface::class)->getMock(); $exclusionStrategy->expects($this->any()) ->method('shouldSkipClass') ->with($this->anything(), $this->callback(static function (SerializationContext $context) use ($self, $objects) { @@ -40,7 +42,7 @@ public function testSerializationContextPathAndDepth() if ($context->getObject() === $objects[0]) { $expectedDepth = 1; - $expectedPath = 'JMS\Serializer\Tests\Fixtures\Node'; + $expectedPath = Node::class; } elseif ($context->getObject() === $objects[1]) { $expectedDepth = 2; $expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node'; @@ -57,7 +59,7 @@ public function testSerializationContextPathAndDepth() return true; })) - ->will($this->returnValue(false)); + ->willReturn(false); $exclusionStrategy->expects($this->any()) ->method('shouldSkipProperty') @@ -66,7 +68,7 @@ public function testSerializationContextPathAndDepth() if ($context->getObject() === $objects[0]) { $expectedDepth = 1; - $expectedPath = 'JMS\Serializer\Tests\Fixtures\Node'; + $expectedPath = Node::class; } elseif ($context->getObject() === $objects[1]) { $expectedDepth = 2; $expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node'; @@ -83,7 +85,7 @@ public function testSerializationContextPathAndDepth() return true; })) - ->will($this->returnValue(false)); + ->willReturn(false); $serializer = SerializerBuilder::create()->build(); @@ -97,10 +99,10 @@ public function testSerializationMetadataStack() ]); $self = $this; - $exclusionStrategy = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(); - $exclusionStrategy->expects($this->any()) + $exclusionStrategy = $this->getMockBuilder(ExclusionStrategyInterface::class)->getMock(); + $exclusionStrategy ->method('shouldSkipClass') - ->will($this->returnCallback(static function (ClassMetadata $classMetadata, SerializationContext $context) use ($self, $object, $child) { + ->willReturnCallback(static function (ClassMetadata $classMetadata, SerializationContext $context) use ($self, $object, $child) { $stack = $context->getMetadataStack(); if ($object === $context->getObject()) { @@ -109,32 +111,32 @@ public function testSerializationMetadataStack() if ($child === $context->getObject()) { $self->assertEquals(2, $stack->count()); - $self->assertEquals('JMS\Serializer\Tests\Fixtures\Node', $stack[1]->name); + $self->assertEquals(Node::class, $stack[1]->name); $self->assertEquals('children', $stack[0]->name); } return false; - })); + }); - $exclusionStrategy->expects($this->any()) + $exclusionStrategy ->method('shouldSkipProperty') - ->will($this->returnCallback(static function (PropertyMetadata $propertyMetadata, SerializationContext $context) use ($self) { + ->willReturnCallback(static function (PropertyMetadata $propertyMetadata, SerializationContext $context) use ($self) { $stack = $context->getMetadataStack(); if ('JMS\Serializer\Tests\Fixtures\Node' === $propertyMetadata->class && 'children' === $propertyMetadata->name) { $self->assertEquals(1, $stack->count()); - $self->assertEquals('JMS\Serializer\Tests\Fixtures\Node', $stack[0]->name); + $self->assertEquals(Node::class, $stack[0]->name); } if ('JMS\Serializer\Tests\Fixtures\InlineChild' === $propertyMetadata->class) { $self->assertEquals(3, $stack->count()); - $self->assertEquals('JMS\Serializer\Tests\Fixtures\Node', $stack[2]->name); + $self->assertEquals(Node::class, $stack[2]->name); $self->assertEquals('children', $stack[1]->name); - $self->assertEquals('JMS\Serializer\Tests\Fixtures\InlineChild', $stack[0]->name); + $self->assertEquals(InlineChild::class, $stack[0]->name); } return false; - })); + }); $serializer = SerializerBuilder::create()->build(); $serializer->serialize($object, 'json', SerializationContext::create()->addExclusionStrategy($exclusionStrategy)); @@ -153,6 +155,7 @@ public static function getScalars() /** * @dataProvider getScalars */ + #[DataProvider('getScalars')] public function testCanVisitScalars($scalar) { $context = SerializationContext::create(); diff --git a/tests/Serializer/Doctrine/ObjectConstructorTest.php b/tests/Serializer/Doctrine/ObjectConstructorTest.php index 6faf53ead..fad75ccaf 100644 --- a/tests/Serializer/Doctrine/ObjectConstructorTest.php +++ b/tests/Serializer/Doctrine/ObjectConstructorTest.php @@ -54,6 +54,7 @@ use LogicException; use Metadata\Driver\AdvancedDriverInterface; use Metadata\MetadataFactoryInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use ReflectionClass; use RuntimeException; @@ -305,7 +306,8 @@ public static function dataProviderPersistendCollectionIsNotReplaced(): array /** * @dataProvider dataProviderPersistendCollectionIsNotReplaced */ - public function testPersistendCollectionIsNotReplaced(string $data, string $type): void + #[DataProvider('dataProviderPersistendCollectionIsNotReplaced')] + public function testPersistendCollectionIsNotReplaced(string $serializedData, string $type): void { $serializer = $this->createSerializerWithDoctrineObjectConstructor(); @@ -322,7 +324,7 @@ public function testPersistendCollectionIsNotReplaced(string $data, string $type $em->flush(); $em->clear(); - $smartPhoneDeserialized = $serializer->deserialize($data, SmartPhone::class, $type); + $smartPhoneDeserialized = $serializer->deserialize($serializedData, SmartPhone::class, $type); self::assertInstanceOf(SmartPhone::class, $smartPhoneDeserialized); self::assertSame( diff --git a/tests/Serializer/GraphNavigatorTest.php b/tests/Serializer/GraphNavigatorTest.php index 8425be6d5..7b8de3708 100644 --- a/tests/Serializer/GraphNavigatorTest.php +++ b/tests/Serializer/GraphNavigatorTest.php @@ -29,6 +29,7 @@ use JMS\Serializer\VisitorInterface; use Metadata\Driver\DriverChain; use Metadata\MetadataFactory; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; use PHPUnit\Framework\TestCase; class GraphNavigatorTest extends TestCase @@ -60,29 +61,29 @@ public function testNavigatorPassesInstanceOnSerialization() $metadata = $this->metadataFactory->getMetadataForClass(get_class($object)); $self = $this; - $this->context = $this->getMockBuilder(SerializationContext::class)->getMock(); + $this->context = $this->createMock(SerializationContext::class); $context = $this->context; - $exclusionStrategy = $this->getMockBuilder('JMS\Serializer\Exclusion\ExclusionStrategyInterface')->getMock(); + $exclusionStrategy = $this->createMock(ExclusionStrategyInterface::class); $exclusionStrategy->expects($this->once()) ->method('shouldSkipClass') - ->will($this->returnCallback(static function ($passedMetadata, $passedContext) use ($metadata, $context, $self) { + ->willReturnCallback(static function ($passedMetadata, $passedContext) use ($metadata, $context, $self) { $self->assertSame($metadata, $passedMetadata); $self->assertSame($context, $passedContext); return false; - })); + }); $exclusionStrategy->expects($this->once()) ->method('shouldSkipProperty') - ->will($this->returnCallback(static function ($propertyMetadata, $passedContext) use ($context, $metadata, $self) { + ->willReturnCallback(static function ($propertyMetadata, $passedContext) use ($context, $metadata, $self) { $self->assertSame($metadata->propertyMetadata['foo'], $propertyMetadata); $self->assertSame($context, $passedContext); return false; - })); + }); $this->context->expects($this->once()) ->method('getExclusionStrategy') - ->will($this->returnValue($exclusionStrategy)); + ->willReturn($exclusionStrategy); $navigator = new SerializationGraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->accessor, $this->dispatcher); $navigator->initialize($this->serializationVisitor, $this->context); @@ -112,7 +113,7 @@ public function testNavigatorPassesNullOnDeserialization() $this->context->expects($this->once()) ->method('getExclusionStrategy') - ->will($this->returnValue($exclusionStrategy)); + ->willReturn($exclusionStrategy); $navigator = new DeserializationGraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->objectConstructor, $this->accessor, $this->dispatcher); $navigator->initialize($this->deserializationVisitor, $this->context); @@ -122,6 +123,7 @@ public function testNavigatorPassesNullOnDeserialization() /** * @doesNotPerformAssertions */ + #[DoesNotPerformAssertions] public function testNavigatorChangeTypeOnSerialization() { $object = new SerializableClass(); @@ -184,6 +186,7 @@ public function testHandlerIsExecutedOnSerialization() /** * @doesNotPerformAssertions */ + #[DoesNotPerformAssertions] public function testFilterableHandlerIsSkippedOnSerialization() { $object = new SerializableClass(); @@ -217,6 +220,7 @@ public function testFilterableHandlerIsNotSkippedOnSerialization() /** * @doesNotPerformAssertions */ + #[DoesNotPerformAssertions] public function testNavigatorDoesNotCrashWhenObjectConstructorReturnsNull() { $objectConstructor = $this->getMockBuilder(ObjectConstructorInterface::class)->getMock(); diff --git a/tests/Serializer/JsonSerializationTest.php b/tests/Serializer/JsonSerializationTest.php index 4c7bb0302..4a2fcd1d9 100644 --- a/tests/Serializer/JsonSerializationTest.php +++ b/tests/Serializer/JsonSerializationTest.php @@ -16,9 +16,11 @@ use JMS\Serializer\Tests\Fixtures\FirstClassMapCollection; use JMS\Serializer\Tests\Fixtures\ObjectWithEmptyArrayAndHash; use JMS\Serializer\Tests\Fixtures\ObjectWithInlineArray; +use JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty; use JMS\Serializer\Tests\Fixtures\Tag; use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory; use JMS\Serializer\Visitor\SerializationVisitorInterface; +use PHPUnit\Framework\Attributes\DataProvider; class JsonSerializationTest extends BaseSerializationTestCase { @@ -170,12 +172,10 @@ public static function getFirstClassMapCollectionsValues() } /** - * @param array $items - * @param array $expected - * * @dataProvider getFirstClassMapCollectionsValues */ - public function testFirstClassMapCollections($items, $expected): void + #[DataProvider('getFirstClassMapCollectionsValues')] + public function testFirstClassMapCollections(array $items, string $expected): void { $collection = new FirstClassMapCollection($items); @@ -190,17 +190,17 @@ public function testAddLinksToOutput() { $this->dispatcher->addListener('serializer.post_serialize', static function (Event $event) { self::assertFalse($event->getVisitor()->hasData('_links')); - }, 'JMS\Serializer\Tests\Fixtures\Author', 'json'); + }, Author::class, 'json'); $this->dispatcher->addSubscriber(new LinkAddingSubscriber()); $this->dispatcher->addListener('serializer.post_serialize', static function (Event $event) { self::assertTrue($event->getVisitor()->hasData('_links')); - }, 'JMS\Serializer\Tests\Fixtures\Author', 'json'); + }, Author::class, 'json'); $this->handlerRegistry->registerHandler( GraphNavigatorInterface::DIRECTION_SERIALIZATION, - 'JMS\Serializer\Tests\Fixtures\AuthorList', + AuthorList::class, 'json', static function (SerializationVisitorInterface $visitor, AuthorList $data, array $type, Context $context) { return $visitor->visitArray(iterator_to_array($data), $type); @@ -219,7 +219,7 @@ public function testReplaceNameInOutput() $this->dispatcher->addSubscriber(new ReplaceNameSubscriber()); $this->handlerRegistry->registerHandler( GraphNavigatorInterface::DIRECTION_SERIALIZATION, - 'JMS\Serializer\Tests\Fixtures\AuthorList', + AuthorList::class, 'json', static function (SerializationVisitorInterface $visitor, AuthorList $data, array $type, Context $context) { return $visitor->visitArray(iterator_to_array($data), $type); @@ -240,15 +240,15 @@ public function testDeserializingObjectWithObjectPropertyWithNoArrayToObject() $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Invalid data "baz" (string), expected "JMS\Serializer\Tests\Fixtures\Author".'); - $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty'); + $this->deserialize($content, ObjectWithObjectProperty::class); } public function testDeserializingObjectWithObjectProperty() { $content = self::getContent('object_with_object_property'); - $object = $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty'); + $object = $this->deserialize($content, ObjectWithObjectProperty::class); self::assertEquals('bar', $object->getFoo()); - self::assertInstanceOf('JMS\Serializer\Tests\Fixtures\Author', $object->getAuthor()); + self::assertInstanceOf(Author::class, $object->getAuthor()); self::assertEquals('baz', $object->getAuthor()->getName()); } @@ -277,29 +277,24 @@ public static function getPrimitiveTypes() /** * @dataProvider getPrimitiveTypes */ - public function testPrimitiveTypes($primitiveType, $data) + #[DataProvider('getPrimitiveTypes')] + public function testPrimitiveTypes(string $type, $data) { $navigator = $this->getMockBuilder(GraphNavigatorInterface::class)->getMock(); $factory = new JsonSerializationVisitorFactory(); $visitor = $factory->getVisitor(); $visitor->setNavigator($navigator); - $functionToCall = 'visit' . ucfirst($primitiveType); + $functionToCall = 'visit' . ucfirst($type); $result = $visitor->$functionToCall($data, [], $this->getMockBuilder(SerializationContext::class)->getMock()); - self::{'assertIs' . (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float'][$primitiveType] ?? $primitiveType)}($result); + self::{'assertIs' . (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float'][$type] ?? $type)}($result); } - /** - * @group empty-object - */ public function testSerializeEmptyObject() { self::assertEquals('{}', $this->serialize(new Author(null))); } - /** - * @group encoding - */ public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOff() { ini_set('display_errors', '1'); @@ -310,9 +305,6 @@ public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOff() $this->serialize(['foo' => 'bar', 'bar' => pack('H*', 'c32e')]); } - /** - * @group encoding - */ public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOn() { ini_set('display_errors', '0'); @@ -394,13 +386,10 @@ public static function getTypeHintedArrays() } /** - * @param array $array - * @param string $expected - * @param SerializationContext|null $context - * * @dataProvider getTypeHintedArrays */ - public function testTypeHintedArraySerialization(array $array, $expected, $context = null) + #[DataProvider('getTypeHintedArrays')] + public function testTypeHintedArraySerialization(array $array, string $expected, ?SerializationContext $context = null) { self::assertEquals($expected, $this->serialize($array, $context)); } @@ -435,13 +424,10 @@ public static function getTypeHintedArraysAndStdClass() } /** - * @param array $array - * @param string $expected - * @param SerializationContext|null $context - * * @dataProvider getTypeHintedArraysAndStdClass */ - public function testTypeHintedArrayAncdtdClassSerialization(array $array, $expected, $context = null) + #[DataProvider('getTypeHintedArraysAndStdClass')] + public function testTypeHintedArrayAncdtdClassSerialization(array $array, string $expected, ?SerializationContext $context = null) { self::assertEquals($expected, $this->serialize($array, $context)); } @@ -467,7 +453,7 @@ public function onPostSerialize(ObjectEvent $event) public static function getSubscribedEvents() { return [ - ['event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => 'JMS\Serializer\Tests\Fixtures\Author'], + ['event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => Author::class], ]; } } @@ -482,7 +468,7 @@ public function onPostSerialize(Event $event) public static function getSubscribedEvents() { return [ - ['event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => 'JMS\Serializer\Tests\Fixtures\Author'], + ['event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => Author::class], ]; } } diff --git a/tests/Serializer/JsonStrictSerializationTest.php b/tests/Serializer/JsonStrictSerializationTest.php index 8bc0562bf..c0227fd28 100644 --- a/tests/Serializer/JsonStrictSerializationTest.php +++ b/tests/Serializer/JsonStrictSerializationTest.php @@ -6,6 +6,7 @@ use JMS\Serializer\SerializerBuilder; use JMS\Serializer\Visitor\Factory\JsonDeserializationVisitorFactory; +use PHPUnit\Framework\Attributes\DataProvider; class JsonStrictSerializationTest extends JsonSerializationTest { @@ -17,12 +18,10 @@ protected function extendBuilder(SerializerBuilder $builder): void } /** - * @param array $items - * @param array $expected - * * @dataProvider getFirstClassMapCollectionsValues */ - public function testFirstClassMapCollections($items, $expected): void + #[DataProvider('getFirstClassMapCollectionsValues')] + public function testFirstClassMapCollections(array $items, string $expected): void { self::markTestIncomplete('Fixtures are broken'); } diff --git a/tests/Serializer/Naming/CamelCaseNamingStrategyTest.php b/tests/Serializer/Naming/CamelCaseNamingStrategyTest.php index 1f73c8c9c..d2f58b7af 100644 --- a/tests/Serializer/Naming/CamelCaseNamingStrategyTest.php +++ b/tests/Serializer/Naming/CamelCaseNamingStrategyTest.php @@ -5,6 +5,7 @@ namespace JMS\Serializer\Tests\Serializer\Naming; use JMS\Serializer\Naming\CamelCaseNamingStrategy; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class CamelCaseNamingStrategyTest extends TestCase @@ -20,6 +21,7 @@ public static function providePropertyNames() /** * @dataProvider providePropertyNames */ + #[DataProvider('providePropertyNames')] public function testCamelCaseNamingHandlesMultipleUppercaseLetters($propertyName, $expected) { $mockProperty = $this->getMockBuilder('JMS\Serializer\Metadata\PropertyMetadata')->disableOriginalConstructor()->getMock(); diff --git a/tests/Serializer/Naming/IdenticalPropertyNamingStrategyTest.php b/tests/Serializer/Naming/IdenticalPropertyNamingStrategyTest.php index be1f68d84..e5b39e08e 100644 --- a/tests/Serializer/Naming/IdenticalPropertyNamingStrategyTest.php +++ b/tests/Serializer/Naming/IdenticalPropertyNamingStrategyTest.php @@ -5,6 +5,7 @@ namespace JMS\Serializer\Tests\Serializer\Naming; use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class IdenticalPropertyNamingStrategyTest extends TestCase @@ -21,6 +22,7 @@ public static function providePropertyNames() /** * @dataProvider providePropertyNames */ + #[DataProvider('providePropertyNames')] public function testTranslateName($propertyName) { $mockProperty = $this->getMockBuilder('JMS\Serializer\Metadata\PropertyMetadata')->disableOriginalConstructor()->getMock(); diff --git a/tests/Serializer/SerializationContextFactoryTest.php b/tests/Serializer/SerializationContextFactoryTest.php index bb2d079b3..6f12b95c3 100644 --- a/tests/Serializer/SerializationContextFactoryTest.php +++ b/tests/Serializer/SerializationContextFactoryTest.php @@ -6,6 +6,8 @@ use Doctrine\Common\Annotations\AnnotationReader; use JMS\Serializer\Construction\UnserializeObjectConstructor; +use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface; +use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface; use JMS\Serializer\DeserializationContext; use JMS\Serializer\Handler\HandlerRegistry; use JMS\Serializer\Metadata\Driver\AnnotationDriver; @@ -42,14 +44,14 @@ protected function setUp(): void public function testSerializeUseProvidedSerializationContext() { - $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\SerializationContextFactoryInterface'); + $contextFactoryMock = $this->createMock(SerializationContextFactoryInterface::class); $context = new SerializationContext(); $context->setSerializeNull(true); $contextFactoryMock ->expects($this->once()) ->method('createSerializationContext') - ->will($this->returnValue($context)); + ->willReturn($context); $builder = SerializerBuilder::create(); $builder->setSerializationContextFactory($contextFactoryMock); @@ -62,13 +64,13 @@ public function testSerializeUseProvidedSerializationContext() public function testDeserializeUseProvidedDeserializationContext() { - $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\DeserializationContextFactoryInterface'); + $contextFactoryMock = $this->createMock(DeserializationContextFactoryInterface::class); $context = new DeserializationContext(); $contextFactoryMock ->expects($this->once()) ->method('createDeserializationContext') - ->will($this->returnValue($context)); + ->willReturn($context); $builder = SerializerBuilder::create(); $builder->setDeserializationContextFactory($contextFactoryMock); @@ -81,14 +83,14 @@ public function testDeserializeUseProvidedDeserializationContext() public function testToArrayUseProvidedSerializationContext() { - $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\SerializationContextFactoryInterface'); + $contextFactoryMock = $this->createMock(SerializationContextFactoryInterface::class); $context = new SerializationContext(); $context->setSerializeNull(true); $contextFactoryMock ->expects($this->once()) ->method('createSerializationContext') - ->will($this->returnValue($context)); + ->willReturn($context); $builder = SerializerBuilder::create(); $builder->setSerializationContextFactory($contextFactoryMock); @@ -101,13 +103,13 @@ public function testToArrayUseProvidedSerializationContext() public function testFromArrayUseProvidedDeserializationContext() { - $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\DeserializationContextFactoryInterface'); + $contextFactoryMock = $this->createMock(DeserializationContextFactoryInterface::class); $context = new DeserializationContext(); $contextFactoryMock ->expects($this->once()) ->method('createDeserializationContext') - ->will($this->returnValue($context)); + ->willReturn($context); $builder = SerializerBuilder::create(); $builder->setDeserializationContextFactory($contextFactoryMock); diff --git a/tests/Serializer/Type/ParserTest.php b/tests/Serializer/Type/ParserTest.php index eecdab5ab..65c7d74d9 100644 --- a/tests/Serializer/Type/ParserTest.php +++ b/tests/Serializer/Type/ParserTest.php @@ -7,6 +7,7 @@ use JMS\Serializer\Type\Exception\SyntaxError; use JMS\Serializer\Type\Parser; use JMS\Serializer\Type\ParserInterface; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class ParserTest extends TestCase @@ -22,6 +23,7 @@ protected function setUp(): void /** * @dataProvider validTypesProvider */ + #[DataProvider('validTypesProvider')] public function testParse(string $sourceType, array $expectedType): void { self::assertSame( @@ -149,6 +151,7 @@ public static function validTypesProvider(): iterable /** * @dataProvider wrongSyntax */ + #[DataProvider('wrongSyntax')] public function testSyntaxError($value): void { $this->expectException(SyntaxError::class); diff --git a/tests/Serializer/XmlSerializationTest.php b/tests/Serializer/XmlSerializationTest.php index 6fcb01c77..201a748cf 100644 --- a/tests/Serializer/XmlSerializationTest.php +++ b/tests/Serializer/XmlSerializationTest.php @@ -46,6 +46,9 @@ use JMS\Serializer\Visitor\Factory\XmlDeserializationVisitorFactory; use JMS\Serializer\Visitor\Factory\XmlSerializationVisitorFactory; use JMS\Serializer\XmlSerializationVisitor; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; +use PHPUnit\Framework\Attributes\Group; class XmlSerializationTest extends BaseSerializationTestCase { @@ -61,6 +64,7 @@ public function testInvalidUsageOfXmlValue() /** * @dataProvider getXMLBooleans */ + #[DataProvider('getXMLBooleans')] public function testXMLBooleans($xmlBoolean, $boolean) { if ($this->hasDeserializer()) { @@ -143,6 +147,7 @@ public function testDocumentTypesAreNotAllowed() /** * @doesNotPerformAssertions */ + #[DoesNotPerformAssertions] public function testWhitelistedDocumentTypesAreAllowed() { $xmlVisitor = new XmlDeserializationVisitorFactory(); @@ -302,6 +307,7 @@ public function testDeserializeTypedAndNestedArrayKeyValues() * @dataProvider getDateTime * @group datetime */ + #[DataProvider('getDateTime')] public function testDateTimeNoCData($key, $value, $type) { $builder = SerializerBuilder::create(); @@ -317,6 +323,7 @@ public function testDateTimeNoCData($key, $value, $type) * @dataProvider getDateTimeImmutable * @group datetime */ + #[DataProvider('getDateTimeImmutable')] public function testDateTimeImmutableNoCData($key, $value, $type) { $builder = SerializerBuilder::create(); diff --git a/tests/SerializerBuilderTest.php b/tests/SerializerBuilderTest.php index 232bcfdd3..73fb575f9 100644 --- a/tests/SerializerBuilderTest.php +++ b/tests/SerializerBuilderTest.php @@ -4,6 +4,8 @@ namespace JMS\Serializer\Tests; +use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface; +use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface; use JMS\Serializer\DeserializationContext; use JMS\Serializer\Exception\UnsupportedFormatException; use JMS\Serializer\Expression\ExpressionEvaluator; @@ -16,6 +18,7 @@ use JMS\Serializer\Tests\Fixtures\PersonSecretWithVariables; use JMS\Serializer\Type\ParserInterface; use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Constraint\FileExists; use PHPUnit\Framework\Constraint\LogicalNot; use PHPUnit\Framework\TestCase; @@ -136,14 +139,14 @@ public function testIncludeInterfaceMetadata() public function testSetSerializationContext() { - $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\SerializationContextFactoryInterface'); + $contextFactoryMock = $this->createMock(SerializationContextFactoryInterface::class); $context = new SerializationContext(); $context->setSerializeNull(true); $contextFactoryMock ->expects($this->once()) ->method('createSerializationContext') - ->will($this->returnValue($context)); + ->willReturn($context); $this->builder->setSerializationContextFactory($contextFactoryMock); @@ -156,13 +159,13 @@ public function testSetSerializationContext() public function testSetDeserializationContext() { - $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\DeserializationContextFactoryInterface'); + $contextFactoryMock = $this->createMock(DeserializationContextFactoryInterface::class); $context = new DeserializationContext(); $contextFactoryMock ->expects($this->once()) ->method('createDeserializationContext') - ->will($this->returnValue($context)); + ->willReturn($context); $this->builder->setDeserializationContextFactory($contextFactoryMock); @@ -229,6 +232,7 @@ public static function expressionFunctionProvider() * * @dataProvider expressionFunctionProvider */ + #[DataProvider('expressionFunctionProvider')] public function testExpressionEngine(ExpressionFunction $function, $json) { $language = new ExpressionLanguage();