1+ --TEST--
2+ PHPC-2505: Setting and unsetting a property may interfere with using foreach to iterate objects
3+ --FILE--
4+ <?php
5+ require_once __DIR__ . "/../utils/basic.inc " ;
6+
7+ $ tests = [
8+ [ 'binary ' => new MongoDB \BSON \Binary ('foo ' , MongoDB \BSON \Binary::TYPE_GENERIC ) ],
9+ [ 'dbpointer ' => createDBPointer () ],
10+ [ 'decimal128 ' => new MongoDB \BSON \Decimal128 ('1234.5678 ' ) ],
11+ [ 'int64 ' => new MongoDB \BSON \Int64 ('9223372036854775807 ' ) ],
12+ // JavaScript w/ scope may not be necessary (same code path as w/o scope), but we'll test it anyway
13+ [ 'javascript ' => new MongoDB \BSON \Javascript ('function() { return 1; } ' ) ],
14+ // The context is recreated every time with a different object ID
15+ //[ 'javascript_ws' => new MongoDB\BSON\Javascript('function() { return a; }', ['a' => 1]) ],
16+ // MaxKey and MinKey don't have get_properties or get_gc handlers, but we'll test them anyway
17+ [ 'maxkey ' => new MongoDB \BSON \MaxKey ],
18+ [ 'minkey ' => new MongoDB \BSON \MinKey ],
19+ [ 'objectid ' => new MongoDB \BSON \ObjectId ],
20+ [ 'regex ' => new MongoDB \BSON \Regex ('pattern ' , 'i ' ) ],
21+ [ 'symbol ' => createSymbol () ],
22+ [ 'timestamp ' => new MongoDB \BSON \Timestamp (1234 , 5678 ) ],
23+ [ 'utcdatetime ' => new MongoDB \BSON \UTCDateTime ],
24+ ];
25+
26+ ob_start ();
27+ foreach ($ tests as $ test ) {
28+ echo key ($ test ), "\n" ;
29+ $ test = reset ($ test );
30+
31+ foreach ($ test as $ k => $ v ) {
32+ var_dump ($ k , $ v );
33+ }
34+ }
35+ $ buf1 = ob_get_clean ();
36+ if ($ buf1 === false ) {
37+ throw new \AssertionError ("Could not flush buffer " );
38+ }
39+
40+ foreach ($ tests as $ test ) {
41+ $ test = reset ($ test );
42+ $ t = &$ test ->a ;
43+ $ t = 'test ' ;
44+ unset($ test ->a , $ t );
45+ }
46+
47+ ob_start ();
48+ foreach ($ tests as $ test ) {
49+ echo key ($ test ), "\n" ;
50+ $ test = reset ($ test );
51+
52+ foreach ($ test as $ k => $ v ) {
53+ var_dump ($ k , $ v );
54+ }
55+ }
56+ $ buf2 = ob_get_clean ();
57+ if ($ buf2 === false ) {
58+ throw new \AssertionError ("Could not flush buffer " );
59+ }
60+
61+ if ($ buf1 === $ buf2 ) {
62+ echo "OK! \n" ;
63+ exit (0 );
64+ } else {
65+ echo ("buf1 != buf2: $ buf1 \n!= \n$ buf2 \n" );
66+ }
67+
68+ ?>
69+ --EXPECT--
70+ OK!
0 commit comments