File: mongoupdatebatch-execute-001.phpt

package info (click to toggle)
php-mongo 1.5.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,040 kB
  • ctags: 2,802
  • sloc: ansic: 17,632; xml: 2,195; php: 1,630; pascal: 330; makefile: 52; sh: 39
file content (191 lines) | stat: -rw-r--r-- 4,484 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
--TEST--
MongoUpdateBatch::execute() Updating one and multiple documents in separate batches
--DESCRIPTION--
This tests batches consisting of single update operations.
--SKIPIF--
<?php $needs = "2.5.5"; ?>
<?php if (!MONGO_STREAMS) { echo "skip This test requires streams support"; } ?>
<?php if ( ! class_exists('MongoWriteBatch')) { exit('skip This test requires MongoWriteBatch classes'); } ?>
<?php require_once "tests/utils/standalone.inc" ?>
--FILE--
<?php
require_once "tests/utils/server.inc";

$numBatches = 0;

function log_write_batch($server, $write_options, $batch, $protocol_options) {
  global $numBatches;

  $numBatches += 1;
}

$ctx = stream_context_create(array(
  'mongodb' => array('log_write_batch' => 'log_write_batch'),
));

$host = MongoShellServer::getStandaloneInfo();
$mc = new MongoClient($host, array(), array('context' => $ctx));

$collection = $mc->selectCollection(dbname(), collname(__FILE__));
$collection->drop();

$collection->insert(array('_id' => 1, 'foo' => 'bar'));
$collection->insert(array('_id' => 2, 'foo' => 'bar'));
$collection->insert(array('_id' => 3, 'foo' => 'bar'));
$collection->insert(array('_id' => 4, 'foo' => 'bar'));

printf("Updating one document (multi unspecified) in a single batch\n");

$update = array(
  'q' => array('foo' => 'bar'),
  'u' => array('$set' => array('foo' => 'abc')),
);

$batch = new MongoUpdateBatch($collection);
var_dump($batch->add($update));
$res = $batch->execute(array('w' => 1));
dump_these_keys($res, array('nMatched', 'nModified', 'nUpserted', 'ok'));

printf("Updating one document (multi = false) in a single batch\n");

$update = array(
  'q' => array('foo' => 'bar'),
  'u' => array('$set' => array('foo' => 'def')),
  'multi' => false,
);

$batch = new MongoUpdateBatch($collection);
var_dump($batch->add($update));
$res = $batch->execute(array('w' => 1));
dump_these_keys($res, array('nMatched', 'nModified', 'nUpserted', 'ok'));

printf("Updating multiple documents (multi = true) in a single batch\n");

$update = array(
  'q' => array('foo' => 'bar'),
  'u' => array('$set' => array('foo' => 'ghi')),
  'multi' => true,
);

$batch = new MongoUpdateBatch($collection);
var_dump($batch->add($update));
$res = $batch->execute(array('w' => 1));
dump_these_keys($res, array('nMatched', 'nModified', 'nUpserted', 'ok'));

printf("Upserting one document (with _id) in a single batch\n");

$update = array(
  'q' => array('_id' => 5),
  'u' => array('$set' => array('x' => 2)),
  'upsert' => true,
);

$batch = new MongoUpdateBatch($collection);
var_dump($batch->add($update));
$res = $batch->execute(array('w' => 1));
dump_these_keys($res, array('upserted', 'nMatched', 'nModified', 'nUpserted', 'ok'));

printf("Upserting one document (_id unspecified) in a single batch\n");

$update = array(
  'q' => array('foo' => 'bar'),
  'u' => array('$set' => array('x' => 1)),
  'upsert' => true,
);

$batch = new MongoUpdateBatch($collection);
var_dump($batch->add($update));
$res = $batch->execute(array('w' => 1));
dump_these_keys($res, array('upserted', 'nMatched', 'nModified', 'nUpserted', 'ok'));

printf("Total batches sent to server: %d\n", $numBatches);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Updating one document (multi unspecified) in a single batch
bool(true)
array(4) {
  ["nMatched"]=>
  int(1)
  ["nModified"]=>
  int(1)
  ["nUpserted"]=>
  int(0)
  ["ok"]=>
  bool(true)
}
Updating one document (multi = false) in a single batch
bool(true)
array(4) {
  ["nMatched"]=>
  int(1)
  ["nModified"]=>
  int(1)
  ["nUpserted"]=>
  int(0)
  ["ok"]=>
  bool(true)
}
Updating multiple documents (multi = true) in a single batch
bool(true)
array(4) {
  ["nMatched"]=>
  int(2)
  ["nModified"]=>
  int(2)
  ["nUpserted"]=>
  int(0)
  ["ok"]=>
  bool(true)
}
Upserting one document (with _id) in a single batch
bool(true)
array(5) {
  ["upserted"]=>
  array(1) {
    [0]=>
    array(2) {
      ["index"]=>
      int(0)
      ["_id"]=>
      int(5)
    }
  }
  ["nMatched"]=>
  int(0)
  ["nModified"]=>
  int(0)
  ["nUpserted"]=>
  int(1)
  ["ok"]=>
  bool(true)
}
Upserting one document (_id unspecified) in a single batch
bool(true)
array(5) {
  ["upserted"]=>
  array(1) {
    [0]=>
    array(2) {
      ["index"]=>
      int(0)
      ["_id"]=>
      object(MongoId)#%d (1) {
        ["$id"]=>
        string(24) "%s"
      }
    }
  }
  ["nMatched"]=>
  int(0)
  ["nModified"]=>
  int(0)
  ["nUpserted"]=>
  int(1)
  ["ok"]=>
  bool(true)
}
Total batches sent to server: 5
===DONE===