From f53211981bb14ced6a351acbf6b5d9b787393770 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 23 Aug 2023 09:25:57 +0200 Subject: [PATCH] Prohibit specifying codec and typeMap options to Watch --- src/Operation/Watch.php | 4 ++++ tests/Operation/WatchTest.php | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Operation/Watch.php b/src/Operation/Watch.php index 2caab579c..075c86d70 100644 --- a/src/Operation/Watch.php +++ b/src/Operation/Watch.php @@ -210,6 +210,10 @@ public function __construct(Manager $manager, ?string $databaseName, ?string $co throw InvalidArgumentException::invalidType('"codec" option', $options['codec'], DocumentCodec::class); } + if (isset($options['codec']) && isset($options['typeMap'])) { + throw InvalidArgumentException::cannotCombineCodecAndTypeMap(); + } + if (array_key_exists('fullDocument', $options) && ! is_string($options['fullDocument'])) { throw InvalidArgumentException::invalidType('"fullDocument" option', $options['fullDocument'], 'string'); } diff --git a/tests/Operation/WatchTest.php b/tests/Operation/WatchTest.php index 8b4f3e0d6..fd97a1c5e 100644 --- a/tests/Operation/WatchTest.php +++ b/tests/Operation/WatchTest.php @@ -4,6 +4,7 @@ use MongoDB\Exception\InvalidArgumentException; use MongoDB\Operation\Watch; +use MongoDB\Tests\Fixtures\Codec\TestDocumentCodec; use stdClass; /** @@ -57,6 +58,14 @@ public function provideInvalidConstructorOptions() ]); } + public function testConstructorRejectsCodecAndTypemap(): void + { + $this->expectExceptionObject(InvalidArgumentException::cannotCombineCodecAndTypeMap()); + + $options = ['codec' => new TestDocumentCodec(), 'typeMap' => ['root' => 'array']]; + new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $options); + } + private function getInvalidTimestampValues() { return [123, 3.14, 'foo', true, [], new stdClass()];