Skip to content

Commit e40362f

Browse files
authored
PHPC-2347: Deprecate BSON functions (#1607)
* PHPC-2347: Deprecate BSON functions * Remove deprecated functionality from BSON corpus tests * Change test-only BSON functions to use BSON document class * Fix changed test expectations * Account for deprecations in BSON function tests * Don't use deprecated functions in tests where possible * Suggest replacement methods with deprecations
1 parent 164a56d commit e40362f

File tree

966 files changed

+1545
-10773
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

966 files changed

+1545
-10773
lines changed

scripts/convert-bson-corpus-tests.php

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
require_once __DIR__ . '/../tests/utils/basic.inc';
44

55
$expectedFailures = [
6-
'Int64 type: -1' => 'PHP encodes integers as 32-bit if range allows',
7-
'Int64 type: 0' => 'PHP encodes integers as 32-bit if range allows',
8-
'Int64 type: 1' => 'PHP encodes integers as 32-bit if range allows',
96
'Javascript Code: Embedded nulls' => 'Embedded null in code string is not supported in libbson (CDRIVER-1879)',
107
'Javascript Code with Scope: Unicode and embedded null in code string, empty scope' => 'Embedded null in code string is not supported in libbson (CDRIVER-1879)',
11-
'Multiple types within the same document: All BSON types' => 'PHP encodes integers as 32-bit if range allows',
128
'Top-level document validity: Bad $date (number, not string or hash)' => 'Legacy extended JSON $date syntax uses numbers (CDRIVER-2223)',
139
];
1410

@@ -147,85 +143,49 @@ function getParamsForValid(array $test, array $case)
147143
$code .= sprintf('$convertedExtJson = %s;', var_export($convertedExtJson, true)) . "\n";
148144
}
149145

150-
$code .= "\n// Canonical BSON -> Native -> Canonical BSON\n";
151-
$code .= 'echo bin2hex(fromPHP(toPHP($canonicalBson))), "\n";' . "\n";
152-
$expect .= $expectedCanonicalBson . "\n";
153-
154146
$code .= "\n// Canonical BSON -> BSON object -> Canonical BSON\n";
155147
$code .= 'echo bin2hex((string) MongoDB\BSON\Document::fromBSON($canonicalBson)), "\n";' . "\n";
156148
$expect .= $expectedCanonicalBson . "\n";
157149

158-
$code .= "\n// Canonical BSON -> Canonical extJSON\n";
159-
$code .= 'echo json_canonicalize(toCanonicalExtendedJSON($canonicalBson)), "\n";' . "\n";
160-
$expect .= $expectedCanonicalExtJson . "\n";
161-
162150
$code .= "\n// Canonical BSON -> BSON object -> Canonical extJSON\n";
163151
$code .= 'echo json_canonicalize(MongoDB\BSON\Document::fromBSON($canonicalBson)->toCanonicalExtendedJSON()), "\n";' . "\n";
164152
$expect .= $expectedCanonicalExtJson . "\n";
165153

166154
if (isset($relaxedExtJson)) {
167-
$code .= "\n// Canonical BSON -> Relaxed extJSON\n";
168-
$code .= 'echo json_canonicalize(toRelaxedExtendedJSON($canonicalBson)), "\n";' . "\n";
169-
$expect .= $expectedRelaxedExtJson . "\n";
170-
171155
$code .= "\n// Canonical BSON -> BSON object -> Relaxed extJSON\n";
172156
$code .= 'echo json_canonicalize(MongoDB\BSON\Document::fromBSON($canonicalBson)->toRelaxedExtendedJSON()), "\n";' . "\n";
173157
$expect .= $expectedRelaxedExtJson . "\n";
174158
}
175159

176160
if (!$lossy) {
177-
$code .= "\n// Canonical extJSON -> Canonical BSON\n";
178-
$code .= 'echo bin2hex(fromJSON($canonicalExtJson)), "\n";' . "\n";
179-
$expect .= $expectedCanonicalBson . "\n";
180-
181161
$code .= "\n// Canonical extJSON -> BSON object -> Canonical BSON\n";
182162
$code .= 'echo bin2hex((string) MongoDB\BSON\Document::fromJSON($canonicalExtJson)), "\n";' . "\n";
183163
$expect .= $expectedCanonicalBson . "\n";
184164
}
185165

186166
if (isset($degenerateBson)) {
187-
$code .= "\n// Degenerate BSON -> Native -> Canonical BSON\n";
188-
$code .= 'echo bin2hex(fromPHP(toPHP($degenerateBson))), "\n";' . "\n";
189-
$expect .= $expectedCanonicalBson . "\n";
190-
191167
$code .= "\n// Degenerate BSON -> BSON object -> Canonical BSON\n";
192-
$code .= 'echo bin2hex(fromPHP(MongoDB\BSON\Document::fromBSON($degenerateBson)->toPHP())), "\n";' . "\n";
168+
$code .= 'echo bin2hex((string) MongoDB\BSON\Document::fromPHP(MongoDB\BSON\Document::fromBSON($degenerateBson)->toPHP())), "\n";' . "\n";
193169
$expect .= $expectedCanonicalBson . "\n";
194170

195-
$code .= "\n// Degenerate BSON -> Canonical extJSON\n";
196-
$code .= 'echo json_canonicalize(toCanonicalExtendedJSON($degenerateBson)), "\n";' . "\n";
197-
$expect .= $expectedCanonicalExtJson . "\n";
198-
199171
$code .= "\n// Degenerate BSON -> BSON object -> Canonical extJSON\n";
200172
$code .= 'echo json_canonicalize(MongoDB\BSON\Document::fromBSON($degenerateBson)->toCanonicalExtendedJSON()), "\n";' . "\n";
201173
$expect .= $expectedCanonicalExtJson . "\n";
202174

203175
if (isset($relaxedExtJson)) {
204-
$code .= "\n// Degenerate BSON -> Relaxed extJSON\n";
205-
$code .= 'echo json_canonicalize(toRelaxedExtendedJSON($degenerateBson)), "\n";' . "\n";
206-
$expect .= $expectedRelaxedExtJson . "\n";
207-
208176
$code .= "\n// Degenerate BSON -> BSON object -> Relaxed extJSON\n";
209177
$code .= 'echo json_canonicalize(MongoDB\BSON\Document::fromBSON($degenerateBson)->toRelaxedExtendedJSON()), "\n";' . "\n";
210178
$expect .= $expectedRelaxedExtJson . "\n";
211179
}
212180
}
213181

214182
if (isset($degenerateExtJson) && !$lossy) {
215-
$code .= "\n// Degenerate extJSON -> Canonical BSON\n";
216-
$code .= 'echo bin2hex(fromJSON($degenerateExtJson)), "\n";' . "\n";
217-
$expect .= $expectedCanonicalBson . "\n";
218-
219183
$code .= "\n// Degenerate extJSON -> BSON object -> Canonical BSON\n";
220184
$code .= 'echo bin2hex((string) MongoDB\BSON\Document::fromJSON($degenerateExtJson)), "\n";' . "\n";
221185
$expect .= $expectedCanonicalBson . "\n";
222186
}
223187

224188
if (isset($relaxedExtJson)) {
225-
$code .= "\n// Relaxed extJSON -> BSON -> Relaxed extJSON\n";
226-
$code .= 'echo json_canonicalize(toRelaxedExtendedJSON(fromJSON($relaxedExtJson))), "\n";' . "\n";
227-
$expect .= $expectedRelaxedExtJson . "\n";
228-
229189
$code .= "\n// Relaxed extJSON -> BSON object -> Relaxed extJSON\n";
230190
$code .= 'echo json_canonicalize(MongoDB\BSON\Document::fromJSON($relaxedExtJson)->toRelaxedExtendedJSON()), "\n";' . "\n";
231191
$expect .= $expectedRelaxedExtJson . "\n";
@@ -248,7 +208,7 @@ function getParamsForDecodeError(array $test, array $case)
248208

249209
$code = sprintf('$bson = hex2bin(%s);', var_export($case['bson'], true)) . "\n\n";
250210
$code .= "throws(function() use (\$bson) {\n";
251-
$code .= " var_dump(toPHP(\$bson));\n";
211+
$code .= " MongoDB\\BSON\\Document::fromBSON(\$bson)->toPHP();\n";
252212
$code .= "}, 'MongoDB\Driver\Exception\UnexpectedValueException');";
253213

254214
/* We do not test for the exception message, since that may differ based on
@@ -277,7 +237,7 @@ function getParamsForParseError(array $test, array $case)
277237
case '0x00': // Top-level document
278238
case '0x05': // Binary
279239
$code = "throws(function() {\n";
280-
$code .= sprintf(" fromJSON(%s);\n", var_export($case['string'], true));
240+
$code .= sprintf(" MongoDB\\BSON\\Document::fromJSON(%s);\n", var_export($case['string'], true));
281241
$code .= "}, 'MongoDB\Driver\Exception\UnexpectedValueException');";
282242

283243
/* We do not test for the exception message, since that may differ

src/functions.stub.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,38 @@
33
/** @generate-function-entries */
44

55
namespace MongoDB\BSON {
6+
/** @deprecated use MongoDB\BSON\Document::fromJSON instead */
67
function fromJSON(string $json): string {}
78

89
#if PHP_VERSION_ID >= 80000
10+
/** @deprecated use MongoDB\BSON\Document::fromPHP instead */
911
function fromPHP(array|object $value): string {}
1012
#else
11-
/** @param array|object $value */
13+
/**
14+
* @param array|object $value
15+
* @deprecated use MongoDB\BSON\Document::fromPHP instead
16+
*/
1217
function fromPHP($value): string {}
1318
#endif
1419

20+
/** @deprecated use MongoDB\BSON\Document::toCanonicalExtendedJSON instead */
1521
function toCanonicalExtendedJSON(string $bson): string {}
1622

23+
/** @deprecated */
1724
function toJSON(string $bson): string {}
1825

1926
#if PHP_VERSION_ID >= 80000
27+
/** @deprecated use MongoDB\BSON\Document::toPHP instead */
2028
function toPHP(string $bson, ?array $typemap = null): array|object {}
2129
#else
22-
/** @return array|object */
30+
/**
31+
* @return array|object
32+
* @deprecated use MongoDB\BSON\Document::toPHP instead
33+
*/
2334
function toPHP(string $bson, ?array $typemap = null) {}
2435
#endif
2536

37+
/** @deprecated use MongoDB\BSON\Document::toRelaxedExtendedJSON instead */
2638
function toRelaxedExtendedJSON(string $bson): string {}
2739
}
2840

src/functions_arginfo.h

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bson-corpus/array-decodeError-001.phpt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bson-corpus/array-decodeError-002.phpt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bson-corpus/array-decodeError-003.phpt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bson-corpus/array-valid-001.phpt

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bson-corpus/array-valid-002.phpt

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bson-corpus/array-valid-003.phpt

Lines changed: 1 addition & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/bson-corpus/array-valid-004.phpt

Lines changed: 1 addition & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)