|
| 1 | +--TEST-- |
| 2 | +BSON\toPHP(): __pclass must be both instantiatable and Persistable |
| 3 | +--SKIPIF-- |
| 4 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"?> |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +use MongoDB\BSON as BSON; |
| 8 | + |
| 9 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 10 | + |
| 11 | +abstract class MyAbstractDocument implements BSON\Persistable |
| 12 | +{ |
| 13 | +} |
| 14 | + |
| 15 | +class MyDocument implements BSON\Unserializable |
| 16 | +{ |
| 17 | + public function bsonUnserialize(array $data) |
| 18 | + { |
| 19 | + $this->unserialized = true; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// Create base64-encoded class names for __pclass field's binary data |
| 24 | +$bMyAbstractDocument = base64_encode('MyAbstractDocument'); |
| 25 | +$bMyDocument = base64_encode('MyDocument'); |
| 26 | +$bUnserializable = base64_encode(BSON_NAMESPACE . '\Unserializable'); |
| 27 | +$bPersistable = base64_encode(BSON_NAMESPACE . '\Persistable'); |
| 28 | + |
| 29 | +$tests = array( |
| 30 | + '{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "' . $bMyAbstractDocument . '" } }', |
| 31 | + '{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "' . $bMyDocument . '" } }', |
| 32 | + '{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "' . $bUnserializable . '" } }', |
| 33 | + '{ "foo": "yes", "__pclass": { "$type" : "44", "$binary" : "' . $bPersistable . '" } }', |
| 34 | +); |
| 35 | + |
| 36 | +foreach ($tests as $test) { |
| 37 | + echo $test, "\n"; |
| 38 | + var_dump(toPHP(fromJSON($test))); |
| 39 | + echo "\n"; |
| 40 | +} |
| 41 | + |
| 42 | +?> |
| 43 | +===DONE=== |
| 44 | +<?php exit(0); ?> |
| 45 | +--EXPECTF-- |
| 46 | +{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "TXlBYnN0cmFjdERvY3VtZW50" } } |
| 47 | +object(stdClass)#%d (2) { |
| 48 | + ["foo"]=> |
| 49 | + string(3) "yes" |
| 50 | + ["__pclass"]=> |
| 51 | + object(MongoDB\BSON\Binary)#%d (2) { |
| 52 | + ["data"]=> |
| 53 | + string(18) "MyAbstractDocument" |
| 54 | + ["subtype"]=> |
| 55 | + int(128) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "TXlEb2N1bWVudA==" } } |
| 60 | +object(stdClass)#%d (2) { |
| 61 | + ["foo"]=> |
| 62 | + string(3) "yes" |
| 63 | + ["__pclass"]=> |
| 64 | + object(MongoDB\BSON\Binary)#%d (2) { |
| 65 | + ["data"]=> |
| 66 | + string(10) "MyDocument" |
| 67 | + ["subtype"]=> |
| 68 | + int(128) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +{ "foo": "yes", "__pclass": { "$type" : "80", "$binary" : "TW9uZ29EQlxCU09OXFVuc2VyaWFsaXphYmxl" } } |
| 73 | +object(stdClass)#%d (2) { |
| 74 | + ["foo"]=> |
| 75 | + string(3) "yes" |
| 76 | + ["__pclass"]=> |
| 77 | + object(MongoDB\BSON\Binary)#%d (2) { |
| 78 | + ["data"]=> |
| 79 | + string(27) "MongoDB\BSON\Unserializable" |
| 80 | + ["subtype"]=> |
| 81 | + int(128) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +{ "foo": "yes", "__pclass": { "$type" : "44", "$binary" : "TW9uZ29EQlxCU09OXFBlcnNpc3RhYmxl" } } |
| 86 | +object(stdClass)#%d (2) { |
| 87 | + ["foo"]=> |
| 88 | + string(3) "yes" |
| 89 | + ["__pclass"]=> |
| 90 | + object(MongoDB\BSON\Binary)#%d (2) { |
| 91 | + ["data"]=> |
| 92 | + string(24) "MongoDB\BSON\Persistable" |
| 93 | + ["subtype"]=> |
| 94 | + int(68) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +===DONE=== |
0 commit comments