|
| 1 | +--TEST-- |
| 2 | +PHPC-545: Update does not serialize embedded Persistable's __pclass field |
| 3 | +--SKIPIF-- |
| 4 | +<?php if (!defined("HHVM_VERSION_ID")) exit("skip PHPC encodes __pclass first"); ?> |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +define('NS', 'demo.test'); |
| 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(); |
| 43 | +$wc = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY); |
| 44 | + |
| 45 | +// Cleanup |
| 46 | +$c = new MongoDB\Driver\Command( [ 'drop' => 'test'] ); |
| 47 | +try { |
| 48 | + $manager->executeCommand( 'demo', $c ); |
| 49 | +} |
| 50 | +catch ( MongoDB\Driver\Exception\RuntimeException $e ) |
| 51 | +{ |
| 52 | + // Ignore "ns not found" errors |
| 53 | + if ( $e->getCode() == 59 ) { |
| 54 | + throw $e; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Create |
| 59 | +$book = new Book(); |
| 60 | +$book->title = 'Unnameable'; |
| 61 | +$book->pages = []; |
| 62 | +$page1 = new Page(); |
| 63 | +$page1->content = 'Lorem ipsum'; |
| 64 | +$book->pages[] = $page1; |
| 65 | +$bulk = new MongoDB\Driver\BulkWrite; |
| 66 | +$bulk->insert($book); |
| 67 | +$result = $manager->executeBulkWrite(NS, $bulk, $wc); |
| 68 | +printf("Inserted %d document(s)\n", $result->getInsertedCount()); |
| 69 | + |
| 70 | +// Read |
| 71 | +$query = new MongoDB\Driver\Query(['title' => $book->title]); |
| 72 | +$cursor = $manager->executeQuery(NS, $query); |
| 73 | +$bookAfterInsert = $cursor->toArray()[0]; |
| 74 | + |
| 75 | +// Update |
| 76 | +$bookAfterInsert->description = 'An interesting document'; |
| 77 | +$page2 = new Page(); |
| 78 | +$page2->content = 'Dolor sit amet'; |
| 79 | +$bookAfterInsert->pages[] = $page2; |
| 80 | +$bulk = new MongoDB\Driver\BulkWrite; |
| 81 | +$bulk->update(['title' => $bookAfterInsert->title], $bookAfterInsert); |
| 82 | +$result = $manager->executeBulkWrite(NS, $bulk, $wc); |
| 83 | +printf("Modified %d document(s)\n", $result->getModifiedCount()); |
| 84 | + |
| 85 | +// Read (again) |
| 86 | +$query = new MongoDB\Driver\Query(['title' => $bookAfterInsert->title]); |
| 87 | +$cursor = $manager->executeQuery(NS, $query); |
| 88 | +$bookAfterUpdate = $cursor->toArray()[0]; |
| 89 | +var_dump($bookAfterUpdate); |
| 90 | + |
| 91 | +?> |
| 92 | +===DONE=== |
| 93 | +<?php exit(0); ?> |
| 94 | +--EXPECTF-- |
| 95 | +Inserted 1 document(s) |
| 96 | +Modified 1 document(s) |
| 97 | +object(Book)#%d (%d) { |
| 98 | + ["_id"]=> |
| 99 | + object(MongoDB\BSON\ObjectID)#%d (%d) { |
| 100 | + ["oid"]=> |
| 101 | + string(24) "%s" |
| 102 | + } |
| 103 | + ["title"]=> |
| 104 | + string(10) "Unnameable" |
| 105 | + ["pages"]=> |
| 106 | + array(2) { |
| 107 | + [0]=> |
| 108 | + object(Page)#%d (%d) { |
| 109 | + ["content"]=> |
| 110 | + string(11) "Lorem ipsum" |
| 111 | + ["__pclass"]=> |
| 112 | + object(MongoDB\BSON\Binary)#%d (%d) { |
| 113 | + ["data"]=> |
| 114 | + string(4) "Page" |
| 115 | + ["type"]=> |
| 116 | + int(%d) |
| 117 | + } |
| 118 | + } |
| 119 | + [1]=> |
| 120 | + object(Page)#%d (%d) { |
| 121 | + ["content"]=> |
| 122 | + string(14) "Dolor sit amet" |
| 123 | + ["__pclass"]=> |
| 124 | + object(MongoDB\BSON\Binary)#%d (%d) { |
| 125 | + ["data"]=> |
| 126 | + string(4) "Page" |
| 127 | + ["type"]=> |
| 128 | + int(%d) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + ["__pclass"]=> |
| 133 | + object(MongoDB\BSON\Binary)#%d (%d) { |
| 134 | + ["data"]=> |
| 135 | + string(4) "Book" |
| 136 | + ["type"]=> |
| 137 | + int(%d) |
| 138 | + } |
| 139 | + ["description"]=> |
| 140 | + string(23) "An interesting document" |
| 141 | +} |
| 142 | +===DONE=== |
0 commit comments