File: bug0545.phpt

package info (click to toggle)
php-mongodb 1.2.3-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,908 kB
  • ctags: 5,983
  • sloc: ansic: 57,609; xml: 887; php: 871; sh: 65; pascal: 11; makefile: 2
file content (130 lines) | stat: -rw-r--r-- 3,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--TEST--
PHPC-545: Update does not serialize embedded Persistable's __pclass field
--SKIPIF--
<?php if (defined("HHVM_VERSION_ID")) exit("skip HHVM encodes __pclass last"); ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE)?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

class Book implements MongoDB\BSON\Persistable
{
    public function bsonSerialize()
    {
        $data = get_object_vars($this);
        return $data;
    }

    public function bsonUnserialize(array $data)
    {
        foreach ($data as $name => $value) {
            $this->{$name} = $value;
        }
    }
}

class Page implements MongoDB\BSON\Persistable
{
    public function bsonSerialize()
    {
        $data = get_object_vars($this);
        return $data;
    }

    public function bsonUnserialize(array $data)
    {
        foreach ($data as $name => $value) {
            $this->{$name} = $value;
        }
    }
}

// Aux
$manager = new MongoDB\Driver\Manager(STANDALONE);
$wc = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY);

// Create
$book = new Book();
$book->title = 'Unnameable';
$book->pages = [];
$page1 = new Page();
$page1->content = 'Lorem ipsum';
$book->pages[] = $page1;
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert($book);
$result = $manager->executeBulkWrite(NS, $bulk, $wc);
printf("Inserted %d document(s)\n", $result->getInsertedCount());

// Read
$query = new MongoDB\Driver\Query(['title' => $book->title]);
$cursor = $manager->executeQuery(NS, $query);
$bookAfterInsert = $cursor->toArray()[0];

// Update
$bookAfterInsert->description = 'An interesting document';
$page2 = new Page();
$page2->content = 'Dolor sit amet';
$bookAfterInsert->pages[] = $page2;
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(['title' => $bookAfterInsert->title], $bookAfterInsert);
$result = $manager->executeBulkWrite(NS, $bulk, $wc);
printf("Modified %d document(s)\n", $result->getModifiedCount());

// Read (again)
$query = new MongoDB\Driver\Query(['title' => $bookAfterInsert->title]);
$cursor = $manager->executeQuery(NS, $query);
$bookAfterUpdate = $cursor->toArray()[0];
var_dump($bookAfterUpdate);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Inserted 1 document(s)
Modified 1 document(s)
object(Book)#%d (%d) {
  ["_id"]=>
  object(MongoDB\BSON\ObjectID)#%d (%d) {
    ["oid"]=>
    string(24) "%s"
  }
  ["__pclass"]=>
  object(MongoDB\BSON\Binary)#%d (%d) {
    ["data"]=>
    string(4) "Book"
    ["type"]=>
    int(%d)
  }
  ["title"]=>
  string(10) "Unnameable"
  ["pages"]=>
  array(2) {
    [0]=>
    object(Page)#%d (%d) {
      ["__pclass"]=>
      object(MongoDB\BSON\Binary)#%d (%d) {
        ["data"]=>
        string(4) "Page"
        ["type"]=>
        int(%d)
      }
      ["content"]=>
      string(11) "Lorem ipsum"
    }
    [1]=>
    object(Page)#%d (%d) {
      ["__pclass"]=>
      object(MongoDB\BSON\Binary)#%d (%d) {
        ["data"]=>
        string(4) "Page"
        ["type"]=>
        int(%d)
      }
      ["content"]=>
      string(14) "Dolor sit amet"
    }
  }
  ["description"]=>
  string(23) "An interesting document"
}
===DONE===