From 0934bf15b5024c64bb30cbeea7feb26a55dc4ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 23 Oct 2026 18:15:45 +0200 Subject: [PATCH 1/3] PHPC-2791: Add tests for duplicate BSON keys Document how Document handles duplicate keys when decoding and when round-tripping, following the BSON corpus spec prose tests 2.2 and 2.3. --- .../bson-document-duplicate-keys-001.phpt | 61 +++++++++++++++++++ .../bson-document-duplicate-keys-002.phpt | 42 +++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/bson/bson-document-duplicate-keys-001.phpt create mode 100644 tests/bson/bson-document-duplicate-keys-002.phpt diff --git a/tests/bson/bson-document-duplicate-keys-001.phpt b/tests/bson/bson-document-duplicate-keys-001.phpt new file mode 100644 index 000000000..b3cabb3a5 --- /dev/null +++ b/tests/bson/bson-document-duplicate-keys-001.phpt @@ -0,0 +1,61 @@ +--TEST-- +MongoDB\BSON\Document: Decoding BSON with duplicate keys +--DESCRIPTION-- +BSON corpus spec prose test #2.2 (decoding) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#22-test-decoding +--FILE-- + $value) { + var_dump($key, $value); +} + +echo "\nget() and offsetGet() return the first value\n"; +var_dump($document->get('foo')); +var_dump($document['foo']); +var_dump($document->has('foo')); + +echo "\ntoPHP() returns the last value\n"; +var_dump($document->toPHP()); +var_dump($document->toPHP(['root' => 'array'])); + +echo "\nExtended JSON preserves duplicates\n"; +echo $document->toRelaxedExtendedJSON(), "\n"; +echo $document->toCanonicalExtendedJSON(), "\n"; + +?> +===DONE=== + +--EXPECT-- +Iteration returns both elements in order +string(3) "foo" +int(1) +string(3) "foo" +int(2) + +get() and offsetGet() return the first value +int(1) +int(1) +bool(true) + +toPHP() returns the last value +object(stdClass)#3 (1) { + ["foo"]=> + int(2) +} +array(1) { + ["foo"]=> + int(2) +} + +Extended JSON preserves duplicates +{ "foo" : 1, "foo" : 2 } +{ "foo" : { "$numberInt" : "1" }, "foo" : { "$numberInt" : "2" } } +===DONE=== diff --git a/tests/bson/bson-document-duplicate-keys-002.phpt b/tests/bson/bson-document-duplicate-keys-002.phpt new file mode 100644 index 000000000..fea1e62e5 --- /dev/null +++ b/tests/bson/bson-document-duplicate-keys-002.phpt @@ -0,0 +1,42 @@ +--TEST-- +MongoDB\BSON\Document: Round-trip of BSON with duplicate keys +--DESCRIPTION-- +BSON corpus spec prose test #2.3 (round-trip) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#23-test-round-trip +--FILE-- +toRelaxedExtendedJSON()); +echo $fromJson->toRelaxedExtendedJSON(), "\n"; +var_dump((string) $fromJson === $bson); + +echo "\nRound-trip through PHP keeps the last value\n"; +$roundTrip = MongoDB\BSON\Document::fromPHP($document->toPHP()); +echo $roundTrip->toRelaxedExtendedJSON(), "\n"; +var_dump((string) $roundTrip === $bson); + +?> +===DONE=== + +--EXPECT-- +Raw BSON is preserved +bool(true) + +Round-trip through Extended JSON preserves duplicates +{ "foo" : 1, "foo" : 2 } +bool(true) + +Round-trip through PHP keeps the last value +{ "foo" : 2 } +bool(false) +===DONE=== From 0b530cbefdb747c9acf5a186c60b884f583d0c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 23 Oct 2026 18:30:32 +0200 Subject: [PATCH 2/3] PHPC-2791: Add prose test for duplicate keys in Extended JSON --- .../bson-document-duplicate-keys-003.phpt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/bson/bson-document-duplicate-keys-003.phpt diff --git a/tests/bson/bson-document-duplicate-keys-003.phpt b/tests/bson/bson-document-duplicate-keys-003.phpt new file mode 100644 index 000000000..8dcf68298 --- /dev/null +++ b/tests/bson/bson-document-duplicate-keys-003.phpt @@ -0,0 +1,27 @@ +--TEST-- +MongoDB\BSON\Document: Parsing Extended JSON with duplicate keys +--DESCRIPTION-- +BSON corpus spec prose test #2.4 (parsing Extended JSON) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#24-test-parsing-extended-json +--FILE-- +toRelaxedExtendedJSON(), "\n"; +var_dump((string) $document === $expected); + +?> +===DONE=== + +--EXPECT-- +Duplicate keys are preserved in order +{ "foo" : 1, "foo" : 2 } +bool(true) +===DONE=== From ef29c5dc9edd76ff9ceb7e44875df15c15edb7c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 23 Oct 2026 18:31:47 +0200 Subject: [PATCH 3/3] PHPC-2791: Add encoding prose test and renumber duplicate key tests --- .../bson-document-duplicate-keys-001.phpt | 56 +++---------- .../bson-document-duplicate-keys-002.phpt | 83 ++++++++++++++----- .../bson-document-duplicate-keys-003.phpt | 33 ++++++-- .../bson-document-duplicate-keys-004.phpt | 27 ++++++ 4 files changed, 122 insertions(+), 77 deletions(-) create mode 100644 tests/bson/bson-document-duplicate-keys-004.phpt diff --git a/tests/bson/bson-document-duplicate-keys-001.phpt b/tests/bson/bson-document-duplicate-keys-001.phpt index b3cabb3a5..46ffc3f3e 100644 --- a/tests/bson/bson-document-duplicate-keys-001.phpt +++ b/tests/bson/bson-document-duplicate-keys-001.phpt @@ -1,61 +1,27 @@ --TEST-- -MongoDB\BSON\Document: Decoding BSON with duplicate keys +MongoDB\BSON\Document: Encoding duplicate keys --DESCRIPTION-- -BSON corpus spec prose test #2.2 (decoding) -https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#22-test-decoding +BSON corpus spec prose test #2.1 (encoding) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#21-test-encoding --FILE-- 1, 'foo' => 2]); -echo "Iteration returns both elements in order\n"; -foreach ($document as $key => $value) { - var_dump($key, $value); -} - -echo "\nget() and offsetGet() return the first value\n"; -var_dump($document->get('foo')); -var_dump($document['foo']); -var_dump($document->has('foo')); - -echo "\ntoPHP() returns the last value\n"; -var_dump($document->toPHP()); -var_dump($document->toPHP(['root' => 'array'])); - -echo "\nExtended JSON preserves duplicates\n"; +echo "PHP collapses duplicate keys, keeping the last value\n"; echo $document->toRelaxedExtendedJSON(), "\n"; -echo $document->toCanonicalExtendedJSON(), "\n"; +var_dump((string) $document === hex2bin('0e00000010666f6f000200000000')); ?> ===DONE=== --EXPECT-- -Iteration returns both elements in order -string(3) "foo" -int(1) -string(3) "foo" -int(2) - -get() and offsetGet() return the first value -int(1) -int(1) +PHP collapses duplicate keys, keeping the last value +{ "foo" : 2 } bool(true) - -toPHP() returns the last value -object(stdClass)#3 (1) { - ["foo"]=> - int(2) -} -array(1) { - ["foo"]=> - int(2) -} - -Extended JSON preserves duplicates -{ "foo" : 1, "foo" : 2 } -{ "foo" : { "$numberInt" : "1" }, "foo" : { "$numberInt" : "2" } } ===DONE=== diff --git a/tests/bson/bson-document-duplicate-keys-002.phpt b/tests/bson/bson-document-duplicate-keys-002.phpt index fea1e62e5..7b642b9d7 100644 --- a/tests/bson/bson-document-duplicate-keys-002.phpt +++ b/tests/bson/bson-document-duplicate-keys-002.phpt @@ -1,42 +1,79 @@ --TEST-- -MongoDB\BSON\Document: Round-trip of BSON with duplicate keys +MongoDB\BSON\Document: Decoding BSON with duplicate keys --DESCRIPTION-- -BSON corpus spec prose test #2.3 (round-trip) -https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#23-test-round-trip +BSON corpus spec prose test #2.2 (decoding) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#22-test-decoding --FILE-- $value) { + var_dump($key, $value); +} -echo "\nRound-trip through Extended JSON preserves duplicates\n"; -$fromJson = MongoDB\BSON\Document::fromJSON($document->toRelaxedExtendedJSON()); -echo $fromJson->toRelaxedExtendedJSON(), "\n"; -var_dump((string) $fromJson === $bson); +echo "\nget() and offsetGet() return the first value\n"; +var_dump($document->get('foo')); +var_dump($document['foo']); +var_dump($document->has('foo')); -echo "\nRound-trip through PHP keeps the last value\n"; -$roundTrip = MongoDB\BSON\Document::fromPHP($document->toPHP()); -echo $roundTrip->toRelaxedExtendedJSON(), "\n"; -var_dump((string) $roundTrip === $bson); +echo "\ntoPHP() keeps the last value for object type maps\n"; +var_dump($document->toPHP()); +var_dump($document->toPHP(['root' => 'object'])); + +echo "\ntoPHP() keeps the last value for array type maps\n"; +var_dump($document->toPHP(['root' => 'array'])); + +echo "\ntoPHP() with the bson type map preserves duplicates\n"; +$bsonDocument = $document->toPHP(['root' => 'bson']); +var_dump($bsonDocument instanceof MongoDB\BSON\Document); +echo $bsonDocument->toRelaxedExtendedJSON(), "\n"; + +echo "\nExtended JSON preserves duplicates\n"; +echo $document->toRelaxedExtendedJSON(), "\n"; +echo $document->toCanonicalExtendedJSON(), "\n"; ?> ===DONE=== ---EXPECT-- -Raw BSON is preserved +--EXPECTF-- +Iteration returns both elements in order +string(3) "foo" +int(1) +string(3) "foo" +int(2) + +get() and offsetGet() return the first value +int(1) +int(1) bool(true) -Round-trip through Extended JSON preserves duplicates -{ "foo" : 1, "foo" : 2 } +toPHP() keeps the last value for object type maps +object(stdClass)#%d (1) { + ["foo"]=> + int(2) +} +object(stdClass)#%d (1) { + ["foo"]=> + int(2) +} + +toPHP() keeps the last value for array type maps +array(1) { + ["foo"]=> + int(2) +} + +toPHP() with the bson type map preserves duplicates bool(true) +{ "foo" : 1, "foo" : 2 } -Round-trip through PHP keeps the last value -{ "foo" : 2 } -bool(false) +Extended JSON preserves duplicates +{ "foo" : 1, "foo" : 2 } +{ "foo" : { "$numberInt" : "1" }, "foo" : { "$numberInt" : "2" } } ===DONE=== diff --git a/tests/bson/bson-document-duplicate-keys-003.phpt b/tests/bson/bson-document-duplicate-keys-003.phpt index 8dcf68298..fea1e62e5 100644 --- a/tests/bson/bson-document-duplicate-keys-003.phpt +++ b/tests/bson/bson-document-duplicate-keys-003.phpt @@ -1,27 +1,42 @@ --TEST-- -MongoDB\BSON\Document: Parsing Extended JSON with duplicate keys +MongoDB\BSON\Document: Round-trip of BSON with duplicate keys --DESCRIPTION-- -BSON corpus spec prose test #2.4 (parsing Extended JSON) -https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#24-test-parsing-extended-json +BSON corpus spec prose test #2.3 (round-trip) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#23-test-round-trip --FILE-- toRelaxedExtendedJSON(), "\n"; -var_dump((string) $document === $expected); +echo "\nRound-trip through Extended JSON preserves duplicates\n"; +$fromJson = MongoDB\BSON\Document::fromJSON($document->toRelaxedExtendedJSON()); +echo $fromJson->toRelaxedExtendedJSON(), "\n"; +var_dump((string) $fromJson === $bson); + +echo "\nRound-trip through PHP keeps the last value\n"; +$roundTrip = MongoDB\BSON\Document::fromPHP($document->toPHP()); +echo $roundTrip->toRelaxedExtendedJSON(), "\n"; +var_dump((string) $roundTrip === $bson); ?> ===DONE=== --EXPECT-- -Duplicate keys are preserved in order +Raw BSON is preserved +bool(true) + +Round-trip through Extended JSON preserves duplicates { "foo" : 1, "foo" : 2 } bool(true) + +Round-trip through PHP keeps the last value +{ "foo" : 2 } +bool(false) ===DONE=== diff --git a/tests/bson/bson-document-duplicate-keys-004.phpt b/tests/bson/bson-document-duplicate-keys-004.phpt new file mode 100644 index 000000000..8dcf68298 --- /dev/null +++ b/tests/bson/bson-document-duplicate-keys-004.phpt @@ -0,0 +1,27 @@ +--TEST-- +MongoDB\BSON\Document: Parsing Extended JSON with duplicate keys +--DESCRIPTION-- +BSON corpus spec prose test #2.4 (parsing Extended JSON) +https://github.com/mongodb/specifications/blob/master/source/bson-corpus/bson-corpus.md#24-test-parsing-extended-json +--FILE-- +toRelaxedExtendedJSON(), "\n"; +var_dump((string) $document === $expected); + +?> +===DONE=== + +--EXPECT-- +Duplicate keys are preserved in order +{ "foo" : 1, "foo" : 2 } +bool(true) +===DONE===