1--TEST--
2PHPC-545: Update does not serialize embedded Persistable's __pclass field
3--SKIPIF--
4<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5<?php skip_if_not_live(); ?>
6<?php skip_if_not_clean(); ?>
7--FILE--
8<?php
9require_once __DIR__ . "/../utils/basic.inc";
10
11class Book implements MongoDB\BSON\Persistable
12{
13    public function bsonSerialize()
14    {
15        $data = get_object_vars($this);
16        return $data;
17    }
18
19    public function bsonUnserialize(array $data)
20    {
21        foreach ($data as $name => $value) {
22            $this->{$name} = $value;
23        }
24    }
25}
26
27class Page implements MongoDB\BSON\Persistable
28{
29    public function bsonSerialize()
30    {
31        $data = get_object_vars($this);
32        return $data;
33    }
34
35    public function bsonUnserialize(array $data)
36    {
37        foreach ($data as $name => $value) {
38            $this->{$name} = $value;
39        }
40    }
41}
42
43// Aux
44$manager = new MongoDB\Driver\Manager(URI);
45$wc = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY);
46
47// Create
48$book = new Book();
49$book->title = 'Unnameable';
50$book->pages = [];
51$page1 = new Page();
52$page1->content = 'Lorem ipsum';
53$book->pages[] = $page1;
54$bulk = new MongoDB\Driver\BulkWrite;
55$bulk->insert($book);
56$result = $manager->executeBulkWrite(NS, $bulk, $wc);
57printf("Inserted %d document(s)\n", $result->getInsertedCount());
58
59// Read
60$query = new MongoDB\Driver\Query(['title' => $book->title]);
61$cursor = $manager->executeQuery(NS, $query);
62$bookAfterInsert = $cursor->toArray()[0];
63
64// Update
65$bookAfterInsert->description = 'An interesting document';
66$page2 = new Page();
67$page2->content = 'Dolor sit amet';
68$bookAfterInsert->pages[] = $page2;
69$bulk = new MongoDB\Driver\BulkWrite;
70$bulk->update(['title' => $bookAfterInsert->title], $bookAfterInsert);
71$result = $manager->executeBulkWrite(NS, $bulk, $wc);
72printf("Modified %d document(s)\n", $result->getModifiedCount());
73
74// Read (again)
75$query = new MongoDB\Driver\Query(['title' => $bookAfterInsert->title]);
76$cursor = $manager->executeQuery(NS, $query);
77$bookAfterUpdate = $cursor->toArray()[0];
78var_dump($bookAfterUpdate);
79
80?>
81===DONE===
82<?php exit(0); ?>
83--EXPECTF--
84Inserted 1 document(s)
85Modified 1 document(s)
86object(Book)#%d (%d) {
87  ["_id"]=>
88  object(MongoDB\BSON\ObjectId)#%d (%d) {
89    ["oid"]=>
90    string(24) "%s"
91  }
92  ["__pclass"]=>
93  object(MongoDB\BSON\Binary)#%d (%d) {
94    ["data"]=>
95    string(4) "Book"
96    ["type"]=>
97    int(%d)
98  }
99  ["title"]=>
100  string(10) "Unnameable"
101  ["pages"]=>
102  array(2) {
103    [0]=>
104    object(Page)#%d (%d) {
105      ["__pclass"]=>
106      object(MongoDB\BSON\Binary)#%d (%d) {
107        ["data"]=>
108        string(4) "Page"
109        ["type"]=>
110        int(%d)
111      }
112      ["content"]=>
113      string(11) "Lorem ipsum"
114    }
115    [1]=>
116    object(Page)#%d (%d) {
117      ["__pclass"]=>
118      object(MongoDB\BSON\Binary)#%d (%d) {
119        ["data"]=>
120        string(4) "Page"
121        ["type"]=>
122        int(%d)
123      }
124      ["content"]=>
125      string(14) "Dolor sit amet"
126    }
127  }
128  ["description"]=>
129  string(23) "An interesting document"
130}
131===DONE===
132