From 82a5953d94c05344f1b05f95ca332d97ff601d06 Mon Sep 17 00:00:00 2001 From: Eduardo Weiland Date: Wed, 1 Jul 2020 09:38:14 -0300 Subject: [PATCH] Add workaround example --- README.md | 8 +++- test/KeyValueArraySubscriber.php | 64 ++++++++++++++++++++++++++++++++ test/ReportWithWorkaround.php | 21 +++++++++++ test/WorkaroundTest.php | 59 +++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 test/KeyValueArraySubscriber.php create mode 100644 test/ReportWithWorkaround.php create mode 100644 test/WorkaroundTest.php diff --git a/README.md b/README.md index 16e6e27..a42d648 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,14 @@ Install composer dependencies composer install ``` -And run test with phpunit +Failing test using `@Type("array")` ``` vendor/bin/phpunit test/SerializerTest.php ``` + +Working test with workaround using `@Type("KeyValueArray")` + +``` +vendor/bin/phpunit test/WorkaroundTest.php +``` diff --git a/test/KeyValueArraySubscriber.php b/test/KeyValueArraySubscriber.php new file mode 100644 index 0000000..6a0a3ce --- /dev/null +++ b/test/KeyValueArraySubscriber.php @@ -0,0 +1,64 @@ + GraphNavigator::DIRECTION_SERIALIZATION, + 'format' => 'json', + 'type' => 'KeyValueArray', + 'method' => 'handle', + ], + [ + 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, + 'format' => 'json', + 'type' => 'KeyValueArray', + 'method' => 'handle', + ], + ]; + } + + /** + * @param mixed $visitor + * @param mixed $map + * @param array $type + * @param Context $context + * @return array + */ + public function handle($visitor, $map, array $type, Context $context): array + { + if (!is_iterable($map)) { + return []; + } + + if (count($type['params']) !== 2) { + throw new Exception('KeyValueArray requires key and value types'); + } + + $keyType = $type['params'][0]; + $valueType = $type['params'][1]; + $navigator = $context->getNavigator(); + $output = []; + + foreach ($map as $key => $value) { + $outputKey = $navigator->accept($key, $keyType); + $outputValue = $navigator->accept($value, $valueType); + $output[$outputKey] = $outputValue; + } + + return $output; + } +} + diff --git a/test/ReportWithWorkaround.php b/test/ReportWithWorkaround.php new file mode 100644 index 0000000..fee815b --- /dev/null +++ b/test/ReportWithWorkaround.php @@ -0,0 +1,21 @@ + + * @JMS\Type("KeyValueArray") + */ + public array $items = []; + + public function addItem(int $id, string $value): void + { + $this->items[$id] = $value; + } +} diff --git a/test/WorkaroundTest.php b/test/WorkaroundTest.php new file mode 100644 index 0000000..9e3a62e --- /dev/null +++ b/test/WorkaroundTest.php @@ -0,0 +1,59 @@ +serializer = SerializerBuilder::create() + ->addDefaultHandlers() + ->configureHandlers(function(HandlerRegistry $registry) { + $hashids = new Hashids('', 6); + $hashidsHandler = new HashidsSubscriber($hashids); + $keyValueHandler = new KeyValueArraySubscriber($hashids); + + $registry->registerSubscribingHandler($hashidsHandler); + $registry->registerSubscribingHandler($keyValueHandler); + }) + ->build(); + } + + public function testDeserialize(): void + { + $deserialized = $this->serializer->deserialize(self::JSON, ReportWithWorkaround::class, 'json'); + + $this->assertInstanceOf(ReportWithWorkaround::class, $deserialized); + $this->assertCount(2, $deserialized->items); + + $this->assertArrayHasKey(42, $deserialized->items); + $this->assertEquals('99.80', $deserialized->items[42]); + + $this->assertArrayHasKey(191, $deserialized->items); + $this->assertEquals('154.00', $deserialized->items[191]); + } + + public function testSerialize(): void + { + $report = new ReportWithWorkaround(); + $report->addItem(42, '99.80'); + $report->addItem(191, '154.00'); + + $serialized = $this->serializer->serialize($report, 'json'); + + // This works + $this->assertJsonStringEqualsJsonString(self::JSON, $serialized); + } +}