From 9925bbd5c21cde134d0d2b5e07a39f6087c332fa Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Mon, 31 Jul 2023 14:35:15 +0200 Subject: [PATCH] Trigger warning when calling CodecCursor::setTypeMap --- src/Model/CodecCursor.php | 5 ++ tests/Model/CodecCursorFunctionalTest.php | 66 +++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 tests/Model/CodecCursorFunctionalTest.php diff --git a/src/Model/CodecCursor.php b/src/Model/CodecCursor.php index c4c9e9b97..b3b5061ae 100644 --- a/src/Model/CodecCursor.php +++ b/src/Model/CodecCursor.php @@ -27,6 +27,10 @@ use function assert; use function iterator_to_array; +use function sprintf; +use function trigger_error; + +use const E_USER_WARNING; /** * @template TValue of object @@ -104,6 +108,7 @@ public function rewind(): void public function setTypeMap(array $typemap): void { // Not supported + trigger_error(sprintf('Discarding type map for %s', __METHOD__), E_USER_WARNING); } /** @return array */ diff --git a/tests/Model/CodecCursorFunctionalTest.php b/tests/Model/CodecCursorFunctionalTest.php new file mode 100644 index 000000000..47d301e04 --- /dev/null +++ b/tests/Model/CodecCursorFunctionalTest.php @@ -0,0 +1,66 @@ +dropCollection($this->getDatabaseName(), $this->getCollectionName()); + } + + public function testSetTypeMap(): void + { + $collection = self::createTestClient()->selectCollection($this->getDatabaseName(), $this->getCollectionName()); + $cursor = $collection->find(); + + $codecCursor = CodecCursor::fromCursor($cursor, $this->createCodec()); + + $this->expectWarning(); + $this->expectWarningMessage('Discarding type map for MongoDB\Model\CodecCursor::setTypeMap'); + + $codecCursor->setTypeMap(['root' => 'array']); + } + + private function createCodec(): DocumentCodec + { + return new class implements DocumentCodec { + public function canDecode($value): bool + { + return false; + } + + public function canEncode($value): bool + { + return false; + } + + public function decode($value): object + { + return (object) []; + } + + public function decodeIfSupported($value): object + { + return (object) []; + } + + public function encode($value): Document + { + return Document::fromPHP([]); + } + + public function encodeIfSupported($value): Document + { + return Document::fromPHP([]); + } + }; + } +}