Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit e721c56

Browse files
committed
add proxy class resolver
1 parent 4700dba commit e721c56

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/SnapshotDriver/SnapshotJsonObjectDriver.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace PhpArsenal\SymfonyMongoTestingFoundation\SnapshotDriver;
44

5+
use App\General\Doctrine\AbstractDocument;
56
use PHPUnit\Framework\Assert;
7+
use ProxyManager\Configuration;
8+
use ProxyManager\Inflector\ClassNameInflector;
69
use Spatie\Snapshots\Driver;
710
use Symfony\Component\Serializer\Encoder\JsonEncoder;
811
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
@@ -14,6 +17,8 @@
1417

1518
class SnapshotJsonObjectDriver implements Driver
1619
{
20+
private static ?ClassNameInflector $classNameInflector = null;
21+
1722
public function __construct(
1823
private array $ignoredProperties = [],
1924
private array $redactedProperties = []
@@ -32,12 +37,12 @@ public function serialize($data): string
3237

3338
$normalizedData = $serializer->normalize($data, 'array', [
3439
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
35-
return sprintf('CIRCULAR_%s', array_reverse(explode('\\', get_class($object)))[0]);
40+
return sprintf('CIRCULAR_%s', array_reverse(explode('\\', self::resolveFromObject($object)))[0]);
3641
},
3742
AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true,
3843
AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true,
3944
AbstractObjectNormalizer::MAX_DEPTH_HANDLER => function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
40-
return sprintf('MAX_DEPTH_%s', array_reverse(explode('\\', get_class($object)))[0]);
45+
return sprintf('MAX_DEPTH_%s', array_reverse(explode('\\', self::resolveFromObject($innerObject)))[0]);
4146
},
4247
AbstractNormalizer::IGNORED_ATTRIBUTES => $this->ignoredProperties,
4348
]);
@@ -85,4 +90,23 @@ private function redactKeys(array &$values, array $redactedKeys): void
8590
}
8691
}
8792
}
93+
94+
private static function resolve(string $proxyObjectClass): string
95+
{
96+
return self::getClassNameInflector()->getUserClassName($proxyObjectClass);
97+
}
98+
99+
private static function resolveFromObject(object $object): string
100+
{
101+
return self::resolve($object::class);
102+
}
103+
104+
private static function getClassNameInflector(): ClassNameInflector
105+
{
106+
if (!self::$classNameInflector) {
107+
self::$classNameInflector = (new Configuration())->getClassNameInflector();
108+
}
109+
110+
return self::$classNameInflector;
111+
}
88112
}

0 commit comments

Comments
 (0)