From 3b3010c6f2538fb92b16d0fad99eb83677af2efe Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Sat, 31 Jan 2026 00:06:16 +0100 Subject: [PATCH 1/2] Use ClassMetadata::(get|set)FieldValue instead of getting the property reflection --- phpstan-baseline.neon | 24 ---- .../MetadataFactory/CustomDriverTest.php | 3 +- .../MetadataFactory/ForcedMetadataTest.php | 3 +- .../Extension/Encoder/EncoderListener.php | 9 +- .../Gedmo/Sluggable/SluggablePositionTest.php | 10 +- tests/Gedmo/Tree/ConcurrencyTest.php | 8 +- tests/Gedmo/Tree/InMemoryUpdatesTest.php | 18 +-- tests/Gedmo/Tree/MultiInheritanceTest.php | 2 +- tests/Gedmo/Tree/RepositoryTest.php | 52 ++++---- tests/Gedmo/Tree/TreeTest.php | 112 +++++++++--------- 10 files changed, 110 insertions(+), 131 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2a8058ceca..cb42bc7b19 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1038,24 +1038,6 @@ parameters: count: 2 path: tests/Gedmo/DoctrineExtensionsTest.php - - - message: '#^Property Gedmo\\Tests\\Mapping\\Fixture\\Attribute\\TranslatableModel\:\:\$title \(string\|null\) is never assigned string so it can be removed from the property type\.$#' - identifier: property.unusedType - count: 1 - path: tests/Gedmo/Mapping/Fixture/Attribute/TranslatableModel.php - - - - message: '#^Property Gedmo\\Tests\\Mapping\\Fixture\\Attribute\\TranslatableModel\:\:\$titleFallbackFalse \(string\|null\) is never assigned string so it can be removed from the property type\.$#' - identifier: property.unusedType - count: 1 - path: tests/Gedmo/Mapping/Fixture/Attribute/TranslatableModel.php - - - - message: '#^Property Gedmo\\Tests\\Mapping\\Fixture\\Attribute\\TranslatableModel\:\:\$titleFallbackTrue \(string\|null\) is never assigned string so it can be removed from the property type\.$#' - identifier: property.unusedType - count: 1 - path: tests/Gedmo/Mapping/Fixture/Attribute/TranslatableModel.php - - message: '#^Class Gedmo\\Tests\\Translatable\\Fixture\\CategoryTranslation not found\.$#' identifier: class.notFound @@ -1116,12 +1098,6 @@ parameters: count: 1 path: tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php - - - message: '#^Call to an undefined method Doctrine\\Persistence\\Mapping\\ClassMetadata\\:\:getReflectionProperty\(\)\.$#' - identifier: method.notFound - count: 2 - path: tests/Gedmo/Mapping/Mock/Extension/Encoder/EncoderListener.php - - message: '#^Call to an undefined method Doctrine\\Persistence\\ObjectManager\:\:getUnitOfWork\(\)\.$#' identifier: method.notFound diff --git a/tests/Gedmo/Mapping/MetadataFactory/CustomDriverTest.php b/tests/Gedmo/Mapping/MetadataFactory/CustomDriverTest.php index 47fea572c2..eaa21b0583 100644 --- a/tests/Gedmo/Mapping/MetadataFactory/CustomDriverTest.php +++ b/tests/Gedmo/Mapping/MetadataFactory/CustomDriverTest.php @@ -89,8 +89,7 @@ public function testShouldWork(): void $id = $this->em ->getClassMetadata(Timestampable::class) - ->getReflectionProperty('id') - ->getValue($test) + ->getFieldValue($test, 'id') ; static::assertNotEmpty($id); } diff --git a/tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php b/tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php index 80f19d7162..940a3aaad2 100644 --- a/tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php +++ b/tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php @@ -94,8 +94,7 @@ public function testShouldWork(): void $id = $this->em ->getClassMetadata(Timestampable::class) - ->getReflectionProperty('id') - ->getValue($test) + ->getFieldValue($test, 'id') ; static::assertNotEmpty($id); } diff --git a/tests/Gedmo/Mapping/Mock/Extension/Encoder/EncoderListener.php b/tests/Gedmo/Mapping/Mock/Extension/Encoder/EncoderListener.php index 2ad5e3be44..a785897e82 100644 --- a/tests/Gedmo/Mapping/Mock/Extension/Encoder/EncoderListener.php +++ b/tests/Gedmo/Mapping/Mock/Extension/Encoder/EncoderListener.php @@ -83,11 +83,16 @@ private function encode(EventAdapterInterface $ea, object $object, array $config $om = $ea->getObjectManager(); $meta = $om->getClassMetadata(get_class($object)); $uow = $om->getUnitOfWork(); + + if (!method_exists($meta, 'getFieldValue') || !method_exists($meta, 'setFieldValue')) { + throw new \RuntimeException('ClassMetadata does not have (get|set)FieldValue() methods: '.get_class($meta)); + } + foreach ($config['encode'] as $field => $options) { - $value = $meta->getReflectionProperty($field)->getValue($object); + $value = $meta->getFieldValue($object, $field); $method = $options['type']; $encoded = $method($options['secret'].$value); - $meta->getReflectionProperty($field)->setValue($object, $encoded); + $meta->setFieldValue($object, $field, $encoded); } // recalculate changeset $ea->recomputeSingleObjectChangeSet($uow, $meta, $object); diff --git a/tests/Gedmo/Sluggable/SluggablePositionTest.php b/tests/Gedmo/Sluggable/SluggablePositionTest.php index bf4df9ca3c..8722ed88e2 100644 --- a/tests/Gedmo/Sluggable/SluggablePositionTest.php +++ b/tests/Gedmo/Sluggable/SluggablePositionTest.php @@ -40,7 +40,7 @@ public function testPositionedSlugOrder(): void $repo = $this->em->getRepository(Position::class); $object = $repo->find(1); - $slug = $meta->getReflectionProperty('slug')->getValue($object); + $slug = $meta->getFieldValue($object, 'slug'); static::assertSame('code-other-title-prop', $slug); } @@ -55,10 +55,10 @@ private function populate(): void { $meta = $this->em->getClassMetadata(Position::class); $object = new Position(); - $meta->getReflectionProperty('title')->setValue($object, 'title'); - $meta->getReflectionProperty('prop')->setValue($object, 'prop'); - $meta->getReflectionProperty('code')->setValue($object, 'code'); - $meta->getReflectionProperty('other')->setValue($object, 'other'); + $meta->setFieldValue($object, 'title', 'title'); + $meta->setFieldValue($object, 'prop', 'prop'); + $meta->setFieldValue($object, 'code', 'code'); + $meta->setFieldValue($object, 'other', 'other'); $this->em->persist($object); $this->em->flush(); diff --git a/tests/Gedmo/Tree/ConcurrencyTest.php b/tests/Gedmo/Tree/ConcurrencyTest.php index 8c18a8fe4e..a6bbc3b770 100644 --- a/tests/Gedmo/Tree/ConcurrencyTest.php +++ b/tests/Gedmo/Tree/ConcurrencyTest.php @@ -78,15 +78,15 @@ public function testConcurrentEntitiesInOneFlush(): void $meta = $this->em->getClassMetadata(Category::class); $sport = $repo->findOneBy(['title' => 'Sport']); - $left = $meta->getReflectionProperty('lft')->getValue($sport); - $right = $meta->getReflectionProperty('rgt')->getValue($sport); + $left = $meta->getFieldValue($sport, 'lft'); + $right = $meta->getFieldValue($sport, 'rgt'); static::assertSame(9, $left); static::assertSame(16, $right); $skiing = $repo->findOneBy(['title' => 'Skiing']); - $left = $meta->getReflectionProperty('lft')->getValue($skiing); - $right = $meta->getReflectionProperty('rgt')->getValue($skiing); + $left = $meta->getFieldValue($skiing, 'lft'); + $right = $meta->getFieldValue($skiing, 'rgt'); static::assertSame(10, $left); static::assertSame(13, $right); diff --git a/tests/Gedmo/Tree/InMemoryUpdatesTest.php b/tests/Gedmo/Tree/InMemoryUpdatesTest.php index 1d9536b388..6bddf8d2f5 100644 --- a/tests/Gedmo/Tree/InMemoryUpdatesTest.php +++ b/tests/Gedmo/Tree/InMemoryUpdatesTest.php @@ -64,29 +64,29 @@ public function testInMemoryTreeInserts(): void $this->em->clear(); $node = $repo->find(2); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(2, $left); static::assertSame(5, $right); $node = $repo->find(3); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(6, $left); static::assertSame(7, $right); $node = $repo->find(4); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(3, $left); static::assertSame(4, $right); /*print "Tree:\n"; for ($i=1; $i < 5; $i++) { $node = $this->em->getRepository(Category::class)->find($i); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); - $level = $meta->getReflectionProperty('level')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); + $level = $meta->getFieldValue($node, 'level'); print $node->getTitle()." - $left - $right - $level\n"; } print "\n\n";*/ diff --git a/tests/Gedmo/Tree/MultiInheritanceTest.php b/tests/Gedmo/Tree/MultiInheritanceTest.php index 6ed11052d9..f9f6ce7d02 100644 --- a/tests/Gedmo/Tree/MultiInheritanceTest.php +++ b/tests/Gedmo/Tree/MultiInheritanceTest.php @@ -38,7 +38,7 @@ public function testInheritance(): void $repo = $this->em->getRepository(Node::class); $food = $repo->findOneBy(['identifier' => 'food']); - $left = $meta->getReflectionProperty('lft')->getValue($food); + $left = $meta->getFieldValue($food, 'lft'); static::assertSame(1, $left); static::assertNotNull($food->getCreated()); diff --git a/tests/Gedmo/Tree/RepositoryTest.php b/tests/Gedmo/Tree/RepositoryTest.php index cd7f6629e7..bb7ee6b225 100644 --- a/tests/Gedmo/Tree/RepositoryTest.php +++ b/tests/Gedmo/Tree/RepositoryTest.php @@ -177,8 +177,8 @@ public function testAdvancedFunctions(): void $repo = $this->em->getRepository(Category::class); $meta = $this->em->getClassMetadata(Category::class); - $left = $meta->getReflectionProperty('lft')->getValue($onions); - $right = $meta->getReflectionProperty('rgt')->getValue($onions); + $left = $meta->getFieldValue($onions, 'lft'); + $right = $meta->getFieldValue($onions, 'rgt'); static::assertSame(11, $left); static::assertSame(12, $right); @@ -187,8 +187,8 @@ public function testAdvancedFunctions(): void $repo->moveUp($onions, 1); - $left = $meta->getReflectionProperty('lft')->getValue($onions); - $right = $meta->getReflectionProperty('rgt')->getValue($onions); + $left = $meta->getFieldValue($onions, 'lft'); + $right = $meta->getFieldValue($onions, 'rgt'); static::assertSame(9, $left); static::assertSame(10, $right); @@ -196,8 +196,8 @@ public function testAdvancedFunctions(): void // move down onions by one position $repo->moveDown($onions, 1); - $left = $meta->getReflectionProperty('lft')->getValue($onions); - $right = $meta->getReflectionProperty('rgt')->getValue($onions); + $left = $meta->getFieldValue($onions, 'lft'); + $right = $meta->getFieldValue($onions, 'rgt'); static::assertSame(11, $left); static::assertSame(12, $right); @@ -206,8 +206,8 @@ public function testAdvancedFunctions(): void $repo->moveUp($onions, true); - $left = $meta->getReflectionProperty('lft')->getValue($onions); - $right = $meta->getReflectionProperty('rgt')->getValue($onions); + $left = $meta->getFieldValue($onions, 'lft'); + $right = $meta->getFieldValue($onions, 'rgt'); static::assertSame(5, $left); static::assertSame(6, $right); @@ -220,32 +220,32 @@ public function testAdvancedFunctions(): void $node = $this->em->getRepository(Category::class) ->findOneBy(['title' => 'Cabbages']); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(5, $left); static::assertSame(6, $right); $node = $this->em->getRepository(Category::class) ->findOneBy(['title' => 'Carrots']); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(7, $left); static::assertSame(8, $right); $node = $this->em->getRepository(Category::class) ->findOneBy(['title' => 'Onions']); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(9, $left); static::assertSame(10, $right); $node = $this->em->getRepository(Category::class) ->findOneBy(['title' => 'Potatoes']); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(11, $left); static::assertSame(12, $right); @@ -266,8 +266,8 @@ public function testAdvancedFunctions(): void $node = $this->em->getRepository(Category::class) ->findOneBy(['title' => 'Fruits']); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(2, $left); static::assertSame(3, $right); @@ -275,8 +275,8 @@ public function testAdvancedFunctions(): void $node = $this->em->getRepository(Category::class) ->findOneBy(['title' => 'Cabbages']); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(4, $left); static::assertSame(5, $right); @@ -297,16 +297,16 @@ public function testRootRemoval(): void static::assertNull($food); $node = $repo->findOneBy(['title' => 'Fruits']); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(1, $left); static::assertSame(2, $right); static::assertNull($node->getParent()); $node = $repo->findOneBy(['title' => 'Vegitables']); - $left = $meta->getReflectionProperty('lft')->getValue($node); - $right = $meta->getReflectionProperty('rgt')->getValue($node); + $left = $meta->getFieldValue($node, 'lft'); + $right = $meta->getFieldValue($node, 'rgt'); static::assertSame(3, $left); static::assertSame(12, $right); @@ -367,8 +367,8 @@ public function testMoveRootNode(): void $meta = $this->em->getClassMetadata(Category::class); - $left = $meta->getReflectionProperty('lft')->getValue($food); - $right = $meta->getReflectionProperty('rgt')->getValue($food); + $left = $meta->getFieldValue($food, 'lft'); + $right = $meta->getFieldValue($food, 'rgt'); static::assertSame(3, $left); static::assertSame(12, $right); diff --git a/tests/Gedmo/Tree/TreeTest.php b/tests/Gedmo/Tree/TreeTest.php index bde7da3ee8..2cf1813658 100644 --- a/tests/Gedmo/Tree/TreeTest.php +++ b/tests/Gedmo/Tree/TreeTest.php @@ -46,8 +46,8 @@ public function testTheTree(): void $this->em->clear(); $root = $this->em->getRepository(Category::class)->find(1); - $left = $meta->getReflectionProperty('lft')->getValue($root); - $right = $meta->getReflectionProperty('rgt')->getValue($root); + $left = $meta->getFieldValue($root, 'lft'); + $right = $meta->getFieldValue($root, 'rgt'); static::assertSame(1, $left); static::assertSame(2, $right); @@ -61,18 +61,18 @@ public function testTheTree(): void $this->em->clear(); $root = $this->em->getRepository(Category::class)->find(1); - $left = $meta->getReflectionProperty('lft')->getValue($root); - $right = $meta->getReflectionProperty('rgt')->getValue($root); - $level = $meta->getReflectionProperty('level')->getValue($root); + $left = $meta->getFieldValue($root, 'lft'); + $right = $meta->getFieldValue($root, 'rgt'); + $level = $meta->getFieldValue($root, 'level'); static::assertSame(1, $left); static::assertSame(4, $right); static::assertSame(0, $level); $child = $this->em->getRepository(Category::class)->find(2); - $left = $meta->getReflectionProperty('lft')->getValue($child); - $right = $meta->getReflectionProperty('rgt')->getValue($child); - $level = $meta->getReflectionProperty('level')->getValue($child); + $left = $meta->getFieldValue($child, 'lft'); + $right = $meta->getFieldValue($child, 'rgt'); + $level = $meta->getFieldValue($child, 'level'); static::assertSame(2, $left); static::assertSame(3, $right); @@ -87,18 +87,18 @@ public function testTheTree(): void $this->em->clear(); $root = $this->em->getRepository(Category::class)->find(1); - $left = $meta->getReflectionProperty('lft')->getValue($root); - $right = $meta->getReflectionProperty('rgt')->getValue($root); - $level = $meta->getReflectionProperty('level')->getValue($root); + $left = $meta->getFieldValue($root, 'lft'); + $right = $meta->getFieldValue($root, 'rgt'); + $level = $meta->getFieldValue($root, 'level'); static::assertSame(1, $left); static::assertSame(6, $right); static::assertSame(0, $level); $child2 = $this->em->getRepository(Category::class)->find(3); - $left = $meta->getReflectionProperty('lft')->getValue($child2); - $right = $meta->getReflectionProperty('rgt')->getValue($child2); - $level = $meta->getReflectionProperty('level')->getValue($child2); + $left = $meta->getFieldValue($child2, 'lft'); + $right = $meta->getFieldValue($child2, 'rgt'); + $level = $meta->getFieldValue($child2, 'level'); static::assertSame(4, $left); static::assertSame(5, $right); @@ -113,15 +113,15 @@ public function testTheTree(): void $this->em->clear(); $child2 = $this->em->getRepository(Category::class)->find(3); - $left = $meta->getReflectionProperty('lft')->getValue($child2); - $right = $meta->getReflectionProperty('rgt')->getValue($child2); - $level = $meta->getReflectionProperty('level')->getValue($child2); + $left = $meta->getFieldValue($child2, 'lft'); + $right = $meta->getFieldValue($child2, 'rgt'); + $level = $meta->getFieldValue($child2, 'level'); static::assertSame(4, $left); static::assertSame(7, $right); static::assertSame(1, $level); - $level = $meta->getReflectionProperty('level')->getValue($childsChild); + $level = $meta->getFieldValue($childsChild, 'level'); static::assertSame(2, $level); @@ -137,9 +137,9 @@ public function testTheTree(): void $this->em->clear(); $child = $this->em->getRepository(Category::class)->find(2); - $left = $meta->getReflectionProperty('lft')->getValue($child); - $right = $meta->getReflectionProperty('rgt')->getValue($child); - $level = $meta->getReflectionProperty('level')->getValue($child); + $left = $meta->getFieldValue($child, 'lft'); + $right = $meta->getFieldValue($child, 'rgt'); + $level = $meta->getFieldValue($child, 'level'); static::assertSame(2, $left); static::assertSame(5, $right); @@ -152,8 +152,8 @@ public function testTheTree(): void $this->em->clear(); $root = $this->em->getRepository(Category::class)->find(1); - $left = $meta->getReflectionProperty('lft')->getValue($root); - $right = $meta->getReflectionProperty('rgt')->getValue($root); + $left = $meta->getFieldValue($root, 'lft'); + $right = $meta->getFieldValue($root, 'rgt'); static::assertSame(1, $left); static::assertSame(4, $right); @@ -167,9 +167,9 @@ public function testTheTree(): void $this->em->flush(); $this->em->clear(); - $left = $meta->getReflectionProperty('lft')->getValue($yetAnotherChild); - $right = $meta->getReflectionProperty('rgt')->getValue($yetAnotherChild); - $level = $meta->getReflectionProperty('level')->getValue($yetAnotherChild); + $left = $meta->getFieldValue($yetAnotherChild, 'lft'); + $right = $meta->getFieldValue($yetAnotherChild, 'rgt'); + $level = $meta->getFieldValue($yetAnotherChild, 'level'); static::assertSame(4, $left); static::assertSame(5, $right); @@ -212,14 +212,14 @@ public function testIssue33(): void $meta = $this->em->getClassMetadata(Category::class); $subNode = $repo->findOneBy(['title' => 'sub-node']); - $left = $meta->getReflectionProperty('lft')->getValue($subNode); - $right = $meta->getReflectionProperty('rgt')->getValue($subNode); + $left = $meta->getFieldValue($subNode, 'lft'); + $right = $meta->getFieldValue($subNode, 'rgt'); static::assertSame(3, $left); static::assertSame(4, $right); $node1 = $repo->findOneBy(['title' => 'node1']); - $left = $meta->getReflectionProperty('lft')->getValue($node1); - $right = $meta->getReflectionProperty('rgt')->getValue($node1); + $left = $meta->getFieldValue($node1, 'lft'); + $right = $meta->getFieldValue($node1, 'rgt'); static::assertSame(2, $left); static::assertSame(5, $right); } @@ -237,8 +237,8 @@ public function testIssue273(): void $this->em->clear(); $root = $this->em->getRepository(CategoryUuid::class)->find($rootId); - $left = $meta->getReflectionProperty('lft')->getValue($root); - $right = $meta->getReflectionProperty('rgt')->getValue($root); + $left = $meta->getFieldValue($root, 'lft'); + $right = $meta->getFieldValue($root, 'rgt'); static::assertSame(1, $left); static::assertSame(2, $right); @@ -253,18 +253,18 @@ public function testIssue273(): void $this->em->clear(); $root = $this->em->getRepository(CategoryUuid::class)->find($rootId); - $left = $meta->getReflectionProperty('lft')->getValue($root); - $right = $meta->getReflectionProperty('rgt')->getValue($root); - $level = $meta->getReflectionProperty('level')->getValue($root); + $left = $meta->getFieldValue($root, 'lft'); + $right = $meta->getFieldValue($root, 'rgt'); + $level = $meta->getFieldValue($root, 'level'); static::assertSame(1, $left); static::assertSame(4, $right); static::assertSame(0, $level); $child = $this->em->getRepository(CategoryUuid::class)->find($childId); - $left = $meta->getReflectionProperty('lft')->getValue($child); - $right = $meta->getReflectionProperty('rgt')->getValue($child); - $level = $meta->getReflectionProperty('level')->getValue($child); + $left = $meta->getFieldValue($child, 'lft'); + $right = $meta->getFieldValue($child, 'rgt'); + $level = $meta->getFieldValue($child, 'level'); static::assertSame(2, $left); static::assertSame(3, $right); @@ -280,18 +280,18 @@ public function testIssue273(): void $this->em->clear(); $root = $this->em->getRepository(CategoryUuid::class)->find($rootId); - $left = $meta->getReflectionProperty('lft')->getValue($root); - $right = $meta->getReflectionProperty('rgt')->getValue($root); - $level = $meta->getReflectionProperty('level')->getValue($root); + $left = $meta->getFieldValue($root, 'lft'); + $right = $meta->getFieldValue($root, 'rgt'); + $level = $meta->getFieldValue($root, 'level'); static::assertSame(1, $left); static::assertSame(6, $right); static::assertSame(0, $level); $child2 = $this->em->getRepository(CategoryUuid::class)->find($child2Id); - $left = $meta->getReflectionProperty('lft')->getValue($child2); - $right = $meta->getReflectionProperty('rgt')->getValue($child2); - $level = $meta->getReflectionProperty('level')->getValue($child2); + $left = $meta->getFieldValue($child2, 'lft'); + $right = $meta->getFieldValue($child2, 'rgt'); + $level = $meta->getFieldValue($child2, 'level'); static::assertSame(4, $left); static::assertSame(5, $right); @@ -307,15 +307,15 @@ public function testIssue273(): void $this->em->clear(); $child2 = $this->em->getRepository(CategoryUuid::class)->find($child2Id); - $left = $meta->getReflectionProperty('lft')->getValue($child2); - $right = $meta->getReflectionProperty('rgt')->getValue($child2); - $level = $meta->getReflectionProperty('level')->getValue($child2); + $left = $meta->getFieldValue($child2, 'lft'); + $right = $meta->getFieldValue($child2, 'rgt'); + $level = $meta->getFieldValue($child2, 'level'); static::assertSame(4, $left); static::assertSame(7, $right); static::assertSame(1, $level); - $level = $meta->getReflectionProperty('level')->getValue($childsChild); + $level = $meta->getFieldValue($childsChild, 'level'); static::assertSame(2, $level); @@ -331,9 +331,9 @@ public function testIssue273(): void $this->em->clear(); $child = $this->em->getRepository(CategoryUuid::class)->find($childId); - $left = $meta->getReflectionProperty('lft')->getValue($child); - $right = $meta->getReflectionProperty('rgt')->getValue($child); - $level = $meta->getReflectionProperty('level')->getValue($child); + $left = $meta->getFieldValue($child, 'lft'); + $right = $meta->getFieldValue($child, 'rgt'); + $level = $meta->getFieldValue($child, 'level'); static::assertSame(2, $left); static::assertSame(5, $right); @@ -346,8 +346,8 @@ public function testIssue273(): void $this->em->clear(); $root = $this->em->getRepository(CategoryUuid::class)->find($rootId); - $left = $meta->getReflectionProperty('lft')->getValue($root); - $right = $meta->getReflectionProperty('rgt')->getValue($root); + $left = $meta->getFieldValue($root, 'lft'); + $right = $meta->getFieldValue($root, 'rgt'); static::assertSame(1, $left); static::assertSame(4, $right); @@ -361,9 +361,9 @@ public function testIssue273(): void $this->em->flush(); $this->em->clear(); - $left = $meta->getReflectionProperty('lft')->getValue($yetAnotherChild); - $right = $meta->getReflectionProperty('rgt')->getValue($yetAnotherChild); - $level = $meta->getReflectionProperty('level')->getValue($yetAnotherChild); + $left = $meta->getFieldValue($yetAnotherChild, 'lft'); + $right = $meta->getFieldValue($yetAnotherChild, 'rgt'); + $level = $meta->getFieldValue($yetAnotherChild, 'level'); static::assertSame(4, $left); static::assertSame(5, $right); From f0960aca3d46611869c20b4388a9441007799a89 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Sat, 31 Jan 2026 18:52:51 +0100 Subject: [PATCH 2/2] Try to fix test --- .../Timestampable/TimestampableDocumentTest.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/Gedmo/Timestampable/TimestampableDocumentTest.php b/tests/Gedmo/Timestampable/TimestampableDocumentTest.php index a3547f6518..59d763693f 100644 --- a/tests/Gedmo/Timestampable/TimestampableDocumentTest.php +++ b/tests/Gedmo/Timestampable/TimestampableDocumentTest.php @@ -16,6 +16,7 @@ use Gedmo\Tests\Timestampable\Fixture\Document\Type; use Gedmo\Tests\Tool\BaseTestCaseMongoODM; use Gedmo\Timestampable\TimestampableListener; +use MongoDB\BSON\Timestamp; /** * These are tests for Timestampable behavior ODM implementation @@ -41,7 +42,11 @@ public function testTimestampable(): void $date = new \DateTime(); $now = time(); - $created = $article->getCreated()->getTimestamp(); + $created = $article->getCreated(); + if ($created instanceof Timestamp) { + $created = $created->getTimestamp(); + } + static::assertTrue($created > $now - 5 && $created < $now + 5); // 5 seconds interval if lag static::assertSame( $date->format('Y-m-d H:i'), @@ -80,10 +85,12 @@ public function testForcedValues(): void $repo = $this->dm->getRepository(Article::class); $sport = $repo->findOneBy(['title' => 'sport forced']); - static::assertSame( - $created, - $sport->getCreated()->getTimestamp() - ); + $createdField = $sport->getCreated(); + if ($createdField instanceof Timestamp) { + $createdField = $createdField->getTimestamp(); + } + + static::assertSame($created, $createdField); static::assertSame( '2000-01-01 12:00:00', $sport->getUpdated()->format('Y-m-d H:i:s')