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