1--TEST--
2SPL: ArrayObject::exchangeArray() basic usage with object as underlying data store.
3--FILE--
4<?php
5
6#[AllowDynamicProperties]
7class C {
8    public $pub1 = 'public1';
9}
10
11echo "--> exchangeArray() with objects:\n";
12$original = new C;
13$ao = new ArrayObject($original);
14$swapIn = new C;
15try {
16    $copy = $ao->exchangeArray($swapIn);
17    $copy['addedToCopy'] = 'added To Copy';
18} catch (Exception $e) {
19    echo "Exception:" . $e->getMessage() . "\n";
20}
21$swapIn->addedToSwapIn = 'added To Swap-In';
22$original->addedToOriginal = 'added To Original';
23var_dump($ao, $original, $swapIn, $copy);
24
25
26echo "\n\n--> exchangeArray() with no arg:\n";
27unset($original, $ao, $swapIn, $copy);
28$original = new C;
29$ao = new ArrayObject($original);
30try {
31    $copy = $ao->exchangeArray();
32    $copy['addedToCopy'] = 'added To Copy';
33} catch (TypeError $e) {
34    echo "Exception: " . $e->getMessage() . "\n";
35}
36$original->addedToOriginal = 'added To Original';
37var_dump($ao, $original, $copy);
38
39echo "\n\n--> exchangeArray() with bad arg type:\n";
40unset($original, $ao, $swapIn, $copy);
41$original = new C;
42$ao = new ArrayObject($original);
43try {
44    $copy = $ao->exchangeArray(null);
45    $copy['addedToCopy'] = 'added To Copy';
46} catch (TypeError $e) {
47    echo $e->getMessage() . "\n";
48}
49$original->addedToOriginal = 'added To Original';
50var_dump($ao, $original, $copy);
51
52?>
53--EXPECTF--
54--> exchangeArray() with objects:
55object(ArrayObject)#2 (1) {
56  ["storage":"ArrayObject":private]=>
57  object(C)#3 (2) {
58    ["pub1"]=>
59    string(7) "public1"
60    ["addedToSwapIn"]=>
61    string(16) "added To Swap-In"
62  }
63}
64object(C)#1 (2) {
65  ["pub1"]=>
66  string(7) "public1"
67  ["addedToOriginal"]=>
68  string(17) "added To Original"
69}
70object(C)#3 (2) {
71  ["pub1"]=>
72  string(7) "public1"
73  ["addedToSwapIn"]=>
74  string(16) "added To Swap-In"
75}
76array(2) {
77  ["pub1"]=>
78  string(7) "public1"
79  ["addedToCopy"]=>
80  string(13) "added To Copy"
81}
82
83
84--> exchangeArray() with no arg:
85Exception: ArrayObject::exchangeArray() expects exactly 1 argument, 0 given
86
87Warning: Undefined variable $copy in %s on line %d
88object(ArrayObject)#2 (1) {
89  ["storage":"ArrayObject":private]=>
90  object(C)#3 (2) {
91    ["pub1"]=>
92    string(7) "public1"
93    ["addedToOriginal"]=>
94    string(17) "added To Original"
95  }
96}
97object(C)#3 (2) {
98  ["pub1"]=>
99  string(7) "public1"
100  ["addedToOriginal"]=>
101  string(17) "added To Original"
102}
103NULL
104
105
106--> exchangeArray() with bad arg type:
107ArrayObject::exchangeArray(): Argument #1 ($array) must be of type array, null given
108
109Warning: Undefined variable $copy in %s on line %d
110object(ArrayObject)#3 (1) {
111  ["storage":"ArrayObject":private]=>
112  object(C)#2 (2) {
113    ["pub1"]=>
114    string(7) "public1"
115    ["addedToOriginal"]=>
116    string(17) "added To Original"
117  }
118}
119object(C)#2 (2) {
120  ["pub1"]=>
121  string(7) "public1"
122  ["addedToOriginal"]=>
123  string(17) "added To Original"
124}
125NULL
126