From ed57a2b4ece8a20bc3c3c64e50e42aea98beb7a6 Mon Sep 17 00:00:00 2001 From: Marcin Czarnecki Date: Sun, 6 Apr 2025 20:30:11 +0200 Subject: [PATCH] Fix warnings in a test code --- tests/Expression/ExpressionEvaluatorTest.php | 3 --- tests/Fixtures/IndexedCommentsBlogPost.php | 2 +- tests/Fixtures/ObjectWithEmptyArrayAndHash.php | 2 +- .../TypedProperties/UnionTypedProperties.php | 2 +- tests/Handler/FormErrorHandlerTest.php | 8 ++++---- tests/Metadata/Driver/BaseDriverTestCase.php | 6 +++++- tests/Metadata/Driver/DoctrinePHPCRDriverTest.php | 4 ++-- tests/Serializer/BaseSerializationTestCase.php | 12 +++--------- tests/Serializer/ContextTest.php | 4 ++-- tests/Serializer/DateIntervalFormatTest.php | 10 +++++----- .../Serializer/Doctrine/ObjectConstructorTest.php | 14 +++++++------- .../Subscriber/DoctrineProxySubscriberTest.php | 1 - .../SymfonyValidatorValidatorSubscriberTest.php | 8 ++++---- tests/Serializer/XmlSerializationTest.php | 4 ++-- tests/Util/DeprecationLogger.php | 2 +- 15 files changed, 38 insertions(+), 44 deletions(-) diff --git a/tests/Expression/ExpressionEvaluatorTest.php b/tests/Expression/ExpressionEvaluatorTest.php index 8af379c6c..dfc370609 100644 --- a/tests/Expression/ExpressionEvaluatorTest.php +++ b/tests/Expression/ExpressionEvaluatorTest.php @@ -4,7 +4,6 @@ namespace JMS\Serializer\Tests\Expression; -use JMS\Serializer\Expression\Expression; use JMS\Serializer\Expression\ExpressionEvaluator; use PHPUnit\Framework\TestCase; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; @@ -29,8 +28,6 @@ public function testEvaluate() public function testParse() { $parsed = $this->evaluator->parse('a + b', ['a', 'b']); - self::assertInstanceOf(Expression::class, $parsed); - $evaluated = $this->evaluator->evaluateParsed($parsed, ['a' => 1, 'b' => 2]); self::assertSame(3, $evaluated); } diff --git a/tests/Fixtures/IndexedCommentsBlogPost.php b/tests/Fixtures/IndexedCommentsBlogPost.php index d8526e97d..902a528d1 100644 --- a/tests/Fixtures/IndexedCommentsBlogPost.php +++ b/tests/Fixtures/IndexedCommentsBlogPost.php @@ -59,6 +59,6 @@ class IndexedCommentsList public function addComment(Comment $comment) { $this->comments[] = $comment; - $this->count += 1; + ++$this->count; } } diff --git a/tests/Fixtures/ObjectWithEmptyArrayAndHash.php b/tests/Fixtures/ObjectWithEmptyArrayAndHash.php index a8a5a528f..0120b1c50 100644 --- a/tests/Fixtures/ObjectWithEmptyArrayAndHash.php +++ b/tests/Fixtures/ObjectWithEmptyArrayAndHash.php @@ -28,7 +28,7 @@ class ObjectWithEmptyArrayAndHash * @Serializer\SkipWhenEmpty() */ #[Serializer\SkipWhenEmpty] - private $object = []; + private $object; public function __construct() { diff --git a/tests/Fixtures/TypedProperties/UnionTypedProperties.php b/tests/Fixtures/TypedProperties/UnionTypedProperties.php index 9ec0ff480..ef55c995b 100644 --- a/tests/Fixtures/TypedProperties/UnionTypedProperties.php +++ b/tests/Fixtures/TypedProperties/UnionTypedProperties.php @@ -8,7 +8,7 @@ class UnionTypedProperties { public bool|float|string|array|int $data; - private int|bool|float|string|null $nullableData = null; + private int|bool|float|string|null $nullableData; private string|false $valueTypedUnion; diff --git a/tests/Handler/FormErrorHandlerTest.php b/tests/Handler/FormErrorHandlerTest.php index 353b6c20a..e3c2d3bf7 100644 --- a/tests/Handler/FormErrorHandlerTest.php +++ b/tests/Handler/FormErrorHandlerTest.php @@ -282,12 +282,12 @@ protected function getMockForm($name = 'name'): MockObject $form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock(); $config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock(); - $form->expects($this->any()) + $form ->method('getName') - ->will($this->returnValue($name)); - $form->expects($this->any()) + ->willReturn($name); + $form ->method('getConfig') - ->will($this->returnValue($config)); + ->willReturn($config); return $form; } diff --git a/tests/Metadata/Driver/BaseDriverTestCase.php b/tests/Metadata/Driver/BaseDriverTestCase.php index 12787eba2..876fd24b0 100644 --- a/tests/Metadata/Driver/BaseDriverTestCase.php +++ b/tests/Metadata/Driver/BaseDriverTestCase.php @@ -162,7 +162,11 @@ public function testVirtualProperty() self::assertArrayHasKey('virtualSerializedValue', $m->propertyMetadata); self::assertArrayHasKey('typedVirtualProperty', $m->propertyMetadata); - self::assertEquals($m->propertyMetadata['virtualSerializedValue']->serializedName, 'test', 'Serialized name is missing'); + self::assertEquals( + 'test', + $m->propertyMetadata['virtualSerializedValue']->serializedName, + 'Serialized name is missing', + ); $p = new VirtualPropertyMetadata($m->name, 'virtualValue'); $p->getter = 'getVirtualValue'; diff --git a/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php b/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php index 349e49a82..a48b4e887 100644 --- a/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php +++ b/tests/Metadata/Driver/DoctrinePHPCRDriverTest.php @@ -122,9 +122,9 @@ public function getAnnotationDriver() { if (class_exists(DoctrinePHPCRDriver::class)) { return new AnnotationDriver(new AnnotationReader(), new IdenticalPropertyNamingStrategy()); - } else { - return new AnnotationOrAttributeDriver(new IdenticalPropertyNamingStrategy()); } + + return new AnnotationOrAttributeDriver(new IdenticalPropertyNamingStrategy()); } protected function getDoctrinePHPCRDriver() diff --git a/tests/Serializer/BaseSerializationTestCase.php b/tests/Serializer/BaseSerializationTestCase.php index db8866628..be1a3484c 100644 --- a/tests/Serializer/BaseSerializationTestCase.php +++ b/tests/Serializer/BaseSerializationTestCase.php @@ -975,7 +975,7 @@ public function testDeserializingNull() $builder->setObjectConstructor($objectConstructor); $this->serializer = $builder->build(); - $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), new Publisher('Bar Foo')); + $post = new BlogPost('This is a nice title.', new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), new Publisher('Bar Foo')); $this->setField($post, 'author', null); $this->setField($post, 'publisher', null); @@ -1309,11 +1309,7 @@ public function testFormErrorsWithNonFormComponents($type) $context = SerializationContext::create(); $context->setInitialType($type); - try { - $this->serialize($form, $context); - } catch (\Throwable $e) { - self::assertTrue(false, 'Serialization should not throw an exception'); - } + $this->serialize($form, $context); } public static function initialFormTypeProvider() @@ -1998,9 +1994,7 @@ public function testThrowingExceptionWhenDeserializingUnionDocBlockTypes() } $this->expectException(RuntimeException::class); - - $object = new UnionTypedDocBlockProperty(10000); - $deserialized = $this->deserialize(static::getContent('data_integer'), UnionTypedDocBlockProperty::class); + $this->deserialize(static::getContent('data_integer'), UnionTypedDocBlockProperty::class); } public function testIterable(): void diff --git a/tests/Serializer/ContextTest.php b/tests/Serializer/ContextTest.php index c5fe9078f..98ff1859d 100644 --- a/tests/Serializer/ContextTest.php +++ b/tests/Serializer/ContextTest.php @@ -35,7 +35,7 @@ public function testSerializationContextPathAndDepth() $self = $this; $exclusionStrategy = $this->getMockBuilder(ExclusionStrategyInterface::class)->getMock(); - $exclusionStrategy->expects($this->any()) + $exclusionStrategy ->method('shouldSkipClass') ->with($this->anything(), $this->callback(static function (SerializationContext $context) use ($self, $objects) { $expectedDepth = $expectedPath = null; @@ -61,7 +61,7 @@ public function testSerializationContextPathAndDepth() })) ->willReturn(false); - $exclusionStrategy->expects($this->any()) + $exclusionStrategy ->method('shouldSkipProperty') ->with($this->anything(), $this->callback(static function (SerializationContext $context) use ($self, $objects) { $expectedDepth = $expectedPath = null; diff --git a/tests/Serializer/DateIntervalFormatTest.php b/tests/Serializer/DateIntervalFormatTest.php index 6ab22db73..07843ff60 100644 --- a/tests/Serializer/DateIntervalFormatTest.php +++ b/tests/Serializer/DateIntervalFormatTest.php @@ -14,21 +14,21 @@ public function testFormat() $dtf = new DateHandler(); $ATOMDateIntervalString = $dtf->format(new \DateInterval('P0D')); - self::assertEquals($ATOMDateIntervalString, 'P0DT0S'); + self::assertEquals('P0DT0S', $ATOMDateIntervalString); $ATOMDateIntervalString = $dtf->format(new \DateInterval('P0DT0S')); - self::assertEquals($ATOMDateIntervalString, 'P0DT0S'); + self::assertEquals('P0DT0S', $ATOMDateIntervalString); $ATOMDateIntervalString = $dtf->format(new \DateInterval('PT45M')); - self::assertEquals($ATOMDateIntervalString, 'PT45M'); + self::assertEquals('PT45M', $ATOMDateIntervalString); $ATOMDateIntervalString = $dtf->format(new \DateInterval('P2YT45M')); - self::assertEquals($ATOMDateIntervalString, 'P2YT45M'); + self::assertEquals('P2YT45M', $ATOMDateIntervalString); $ATOMDateIntervalString = $dtf->format(new \DateInterval('P2Y4DT6H8M16S')); - self::assertEquals($ATOMDateIntervalString, 'P2Y4DT6H8M16S'); + self::assertEquals('P2Y4DT6H8M16S', $ATOMDateIntervalString); } } diff --git a/tests/Serializer/Doctrine/ObjectConstructorTest.php b/tests/Serializer/Doctrine/ObjectConstructorTest.php index dd9fd1edf..6156a9edf 100644 --- a/tests/Serializer/Doctrine/ObjectConstructorTest.php +++ b/tests/Serializer/Doctrine/ObjectConstructorTest.php @@ -364,7 +364,7 @@ public function testFallbackOnEmbeddableClassWithXmlDriver() $connection = $this->createConnection(); $entityManager = $this->createXmlEntityManager($connection); - $this->registry = $registry = new SimpleBaseManagerRegistry( + $this->registry = new SimpleBaseManagerRegistry( static function ($id) use ($connection, $entityManager) { switch ($id) { case 'default_connection': @@ -394,7 +394,7 @@ public function testFallbackOnEmbeddableClassWithXmlDriverAndXmlData() $connection = $this->createConnection(); $entityManager = $this->createXmlEntityManager($connection); - $this->registry = $registry = new SimpleBaseManagerRegistry( + $this->registry = new SimpleBaseManagerRegistry( static function ($id) use ($connection, $entityManager) { switch ($id) { case 'default_connection': @@ -426,17 +426,17 @@ public function testArrayKeyExistsOnObject(): void $ormClassMetadata = $this->createMock(DoctrineClassMetadata::class); $ormClassMetadata - ->expects(self::atLeastOnce()) + ->expects($this->atLeastOnce()) ->method('getIdentifierFieldNames') ->willReturn(['id']); $om = $this->createMock(ObjectManager::class); $om - ->expects(self::atLeastOnce()) + ->expects($this->atLeastOnce()) ->method('getMetadataFactory') ->willReturn($metadataFactory); $om - ->expects(self::atLeastOnce()) + ->expects($this->atLeastOnce()) ->method('getClassMetadata') ->willReturnMap([ [BlogPostSeo::class, $ormClassMetadata], @@ -444,7 +444,7 @@ public function testArrayKeyExistsOnObject(): void $registry = $this->createMock(ManagerRegistry::class); $registry - ->expects(self::atLeastOnce()) + ->expects($this->atLeastOnce()) ->method('getManagerForClass') ->willReturnMap([ [BlogPostSeo::class, $om], @@ -452,7 +452,7 @@ public function testArrayKeyExistsOnObject(): void $fallback = $this->createMock(ObjectConstructorInterface::class); $fallback - ->expects(self::once()) + ->expects($this->once()) ->method('construct'); $type = ['name' => BlogPostSeo::class, 'params' => []]; diff --git a/tests/Serializer/EventDispatcher/Subscriber/DoctrineProxySubscriberTest.php b/tests/Serializer/EventDispatcher/Subscriber/DoctrineProxySubscriberTest.php index 1def2747a..5dc1a24a2 100644 --- a/tests/Serializer/EventDispatcher/Subscriber/DoctrineProxySubscriberTest.php +++ b/tests/Serializer/EventDispatcher/Subscriber/DoctrineProxySubscriberTest.php @@ -116,7 +116,6 @@ public function testListenersCanChangeType() { $proxy = new SimpleObjectProxy('foo', 'bar'); - $realClassEventTriggered1 = false; $this->dispatcher->addListener('serializer.pre_serialize', static function (PreSerializeEvent $event) { $event->setType('foo', ['bar']); }, get_parent_class($proxy)); diff --git a/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php b/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php index 68f9cf020..80352f64c 100644 --- a/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php +++ b/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php @@ -28,7 +28,7 @@ public function testValidate(): void { $obj = new stdClass(); - $this->validator->expects(self::once()) + $this->validator->expects($this->once()) ->method('validate') ->with($obj, null, ['foo']) ->willReturn(new ConstraintViolationList()); @@ -42,7 +42,7 @@ public function testValidateThrowsExceptionWhenListIsNotEmpty(): void { $obj = new stdClass(); - $this->validator->expects(self::once()) + $this->validator->expects($this->once()) ->method('validate') ->with($obj, null, ['foo']) ->willReturn(new ConstraintViolationList([new ConstraintViolation('foo', 'foo', [], 'a', 'b', 'c')])); @@ -57,7 +57,7 @@ public function testValidateThrowsExceptionWhenListIsNotEmpty(): void public function testValidatorIsNotCalledWhenNoGroupsAreSet(): void { - $this->validator->expects(self::never()) + $this->validator->expects($this->never()) ->method('validate'); $this->subscriber->onPostDeserialize(new ObjectEvent(DeserializationContext::create(), new stdClass(), [])); @@ -65,7 +65,7 @@ public function testValidatorIsNotCalledWhenNoGroupsAreSet(): void public function testValidationIsOnlyPerformedOnRootObject(): void { - $this->validator->expects(self::once()) + $this->validator->expects($this->once()) ->method('validate') ->with(self::isInstanceOf(AuthorList::class), null, ['Foo']) ->willReturn(new ConstraintViolationList()); diff --git a/tests/Serializer/XmlSerializationTest.php b/tests/Serializer/XmlSerializationTest.php index 464790aba..3119d924f 100644 --- a/tests/Serializer/XmlSerializationTest.php +++ b/tests/Serializer/XmlSerializationTest.php @@ -209,7 +209,7 @@ public function testUnserializeMissingArray() { $xml = ''; $object = $this->serializer->deserialize($xml, 'JMS\Serializer\Tests\Fixtures\ObjectWithAbsentXmlListNode', 'xml'); - self::assertEquals($object->absentAndNs, []); + self::assertEquals([], $object->absentAndNs); $xml = ' @@ -217,7 +217,7 @@ public function testUnserializeMissingArray() '; $object = $this->serializer->deserialize($xml, 'JMS\Serializer\Tests\Fixtures\ObjectWithAbsentXmlListNode', 'xml'); - self::assertEquals($object->absentAndNs, ['foo']); + self::assertEquals(['foo'], $object->absentAndNs); } public function testObjectWithNamespacesAndList() diff --git a/tests/Util/DeprecationLogger.php b/tests/Util/DeprecationLogger.php index 786e98a7f..e19f2dd04 100644 --- a/tests/Util/DeprecationLogger.php +++ b/tests/Util/DeprecationLogger.php @@ -30,7 +30,7 @@ public function __destruct() } else { sort($this->errors); array_map(static function (array $m) { - [$errno, $errstr, $errfile, $errline] = $m; + [, $errstr, $errfile, $errline] = $m; echo '- ' . sprintf('%s in %s:%s', $errstr, $errfile, $errline) . PHP_EOL; }, $this->errors); }