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