• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2
3require_once('test_base.php');
4require_once('test_util.php');
5
6use Foo\TestWrapperSetters;
7use Google\Protobuf\BoolValue;
8use Google\Protobuf\BytesValue;
9use Google\Protobuf\DoubleValue;
10use Google\Protobuf\FloatValue;
11use Google\Protobuf\Int32Value;
12use Google\Protobuf\Int64Value;
13use Google\Protobuf\StringValue;
14use Google\Protobuf\UInt32Value;
15use Google\Protobuf\UInt64Value;
16
17class WrapperTypeSettersTest extends TestBase
18{
19    /**
20     * @dataProvider gettersAndSettersDataProvider
21     */
22    public function testGettersAndSetters(
23        $class,
24        $wrapperClass,
25        $setter,
26        $valueSetter,
27        $getter,
28        $valueGetter,
29        $sequence
30    ) {
31        $oldSetterMsg = new $class();
32        $newSetterMsg = new $class();
33        foreach ($sequence as list($value, $expectedValue)) {
34            // Manually wrap the value to pass to the old setter
35            $wrappedValue = is_null($value) ? $value : new $wrapperClass(['value' => $value]);
36
37            // Set values using new and old setters
38            $oldSetterMsg->$setter($wrappedValue);
39            $newSetterMsg->$valueSetter($value);
40
41            // Get expected values old getter
42            $expectedValue = $oldSetterMsg->$getter();
43
44            // Check that old getter returns the same value after using the
45            // new setter
46            $actualValue = $newSetterMsg->$getter();
47            $this->assertEquals($expectedValue, $actualValue);
48
49            // Check that new getter returns the unwrapped value from
50            // $expectedValue
51            $actualValueNewGetter = $newSetterMsg->$valueGetter();
52            if (is_null($expectedValue)) {
53                $this->assertNull($actualValueNewGetter);
54            } else {
55                $this->assertEquals($expectedValue->getValue(), $actualValueNewGetter);
56            }
57        }
58    }
59
60    public function gettersAndSettersDataProvider()
61    {
62        return [
63            [TestWrapperSetters::class, DoubleValue::class, "setDoubleValue", "setDoubleValueUnwrapped", "getDoubleValue", "getDoubleValueUnwrapped", [
64                [1.1, new DoubleValue(["value" => 1.1])],
65                [2.2, new DoubleValue(["value" => 2.2])],
66                [null, null],
67                [0, new DoubleValue()],
68            ]],
69            [TestWrapperSetters::class, FloatValue::class, "setFloatValue", "setFloatValueUnwrapped", "getFloatValue", "getFloatValueUnwrapped", [
70                [1.1, new FloatValue(["value" => 1.1])],
71                [2.2, new FloatValue(["value" => 2.2])],
72                [null, null],
73                [0, new FloatValue()],
74            ]],
75            [TestWrapperSetters::class, Int64Value::class, "setInt64Value", "setInt64ValueUnwrapped", "getInt64Value", "getInt64ValueUnwrapped", [
76                [123, new Int64Value(["value" => 123])],
77                [-789, new Int64Value(["value" => -789])],
78                [null, null],
79                [0, new Int64Value()],
80                [5.5, new Int64Value(["value" => 5])], // Test conversion from float to int
81            ]],
82            [TestWrapperSetters::class, UInt64Value::class, "setUInt64Value", "setUInt64ValueUnwrapped", "getUInt64Value", "getUInt64ValueUnwrapped", [
83                [123, new UInt64Value(["value" => 123])],
84                [789, new UInt64Value(["value" => 789])],
85                [null, null],
86                [0, new UInt64Value()],
87                [5.5, new UInt64Value(["value" => 5])], // Test conversion from float to int
88                [-7, new UInt64Value(["value" => -7])], // Test conversion from -ve to +ve
89            ]],
90            [TestWrapperSetters::class, Int32Value::class, "setInt32Value", "setInt32ValueUnwrapped", "getInt32Value", "getInt32ValueUnwrapped", [
91                [123, new Int32Value(["value" => 123])],
92                [-789, new Int32Value(["value" => -789])],
93                [null, null],
94                [0, new Int32Value()],
95                [5.5, new Int32Value(["value" => 5])], // Test conversion from float to int
96            ]],
97            [TestWrapperSetters::class, UInt32Value::class, "setUInt32Value", "setUInt32ValueUnwrapped", "getUInt32Value", "getUInt32ValueUnwrapped", [
98                [123, new UInt32Value(["value" => 123])],
99                [789, new UInt32Value(["value" => 789])],
100                [null, null],
101                [0, new UInt32Value()],
102                [5.5, new UInt32Value(["value" => 5])], // Test conversion from float to int
103                [-7, new UInt32Value(["value" => -7])], // Test conversion from -ve to +ve
104            ]],
105            [TestWrapperSetters::class, BoolValue::class, "setBoolValue", "setBoolValueUnwrapped", "getBoolValue", "getBoolValueUnwrapped", [
106                [true, new BoolValue(["value" => true])],
107                [false, new BoolValue(["value" => false])],
108                [null, null],
109            ]],
110            [TestWrapperSetters::class, StringValue::class, "setStringValue", "setStringValueUnwrapped", "getStringValue", "getStringValueUnwrapped", [
111                ["asdf", new StringValue(["value" => "asdf"])],
112                ["", new StringValue(["value" => ""])],
113                [null, null],
114                ["", new StringValue()],
115                [5, new StringValue(["value" => "5"])], // Test conversion from number to string
116                [5.5, new StringValue(["value" => "5.5"])], // Test conversion from number to string
117                [-7, new StringValue(["value" => "-7"])], // Test conversion from number to string
118                [-7.5, new StringValue(["value" => "-7.5"])], // Test conversion from number to string
119            ]],
120            [TestWrapperSetters::class, BytesValue::class, "setBytesValue", "setBytesValueUnwrapped", "getBytesValue", "getBytesValueUnwrapped", [
121                ["asdf", new BytesValue(["value" => "asdf"])],
122                ["", new BytesValue(["value" => ""])],
123                [null, null],
124                ["", new BytesValue()],
125                [5, new BytesValue(["value" => "5"])], // Test conversion from number to bytes
126                [5.5, new BytesValue(["value" => "5.5"])], // Test conversion from number to bytes
127                [-7, new BytesValue(["value" => "-7"])], // Test conversion from number to bytes
128                [-7.5, new BytesValue(["value" => "-7.5"])], // Test conversion from number to bytes
129            ]],
130            [TestWrapperSetters::class, DoubleValue::class, "setDoubleValueOneof", "setDoubleValueOneofUnwrapped", "getDoubleValueOneof", "getDoubleValueOneofUnwrapped", [
131                [1.1, new DoubleValue(["value" => 1.1])],
132                [2.2, new DoubleValue(["value" => 2.2])],
133                [null, null],
134                [0, new DoubleValue()],
135            ]],
136            [TestWrapperSetters::class, StringValue::class, "setStringValueOneof", "setStringValueOneofUnwrapped", "getStringValueOneof", "getStringValueOneofUnwrapped", [
137                ["asdf", new StringValue(["value" => "asdf"])],
138                ["", new StringValue(["value" => ""])],
139                [null, null],
140                ["", new StringValue()],
141                [5, new StringValue(["value" => "5"])], // Test conversion from number to string
142                [5.5, new StringValue(["value" => "5.5"])], // Test conversion from number to string
143                [-7, new StringValue(["value" => "-7"])], // Test conversion from number to string
144                [-7.5, new StringValue(["value" => "-7.5"])], // Test conversion from number to string
145            ]],
146        ];
147    }
148
149    /**
150     * @dataProvider invalidSettersDataProvider
151     * @expectedException \Exception
152     */
153    public function testInvalidSetters($class, $setter, $value)
154    {
155        (new $class())->$setter($value);
156    }
157
158    public function invalidSettersDataProvider()
159    {
160        return [
161            [TestWrapperSetters::class, "setDoubleValueUnwrapped", "abc"],
162            [TestWrapperSetters::class, "setDoubleValueUnwrapped", []],
163            [TestWrapperSetters::class, "setDoubleValueUnwrapped", new stdClass()],
164            [TestWrapperSetters::class, "setDoubleValueUnwrapped", new DoubleValue()],
165
166            [TestWrapperSetters::class, "setFloatValueUnwrapped", "abc"],
167            [TestWrapperSetters::class, "setFloatValueUnwrapped", []],
168            [TestWrapperSetters::class, "setFloatValueUnwrapped", new stdClass()],
169            [TestWrapperSetters::class, "setFloatValueUnwrapped", new FloatValue()],
170
171            [TestWrapperSetters::class, "setInt64ValueUnwrapped", "abc"],
172            [TestWrapperSetters::class, "setInt64ValueUnwrapped", []],
173            [TestWrapperSetters::class, "setInt64ValueUnwrapped", new stdClass()],
174            [TestWrapperSetters::class, "setInt64ValueUnwrapped", new Int64Value()],
175
176            [TestWrapperSetters::class, "setUInt64ValueUnwrapped", "abc"],
177            [TestWrapperSetters::class, "setUInt64ValueUnwrapped", []],
178            [TestWrapperSetters::class, "setUInt64ValueUnwrapped", new stdClass()],
179            [TestWrapperSetters::class, "setUInt64ValueUnwrapped", new UInt64Value()],
180
181            [TestWrapperSetters::class, "setInt32ValueUnwrapped", "abc"],
182            [TestWrapperSetters::class, "setInt32ValueUnwrapped", []],
183            [TestWrapperSetters::class, "setInt32ValueUnwrapped", new stdClass()],
184            [TestWrapperSetters::class, "setInt32ValueUnwrapped", new Int32Value()],
185
186            [TestWrapperSetters::class, "setUInt32ValueUnwrapped", "abc"],
187            [TestWrapperSetters::class, "setUInt32ValueUnwrapped", []],
188            [TestWrapperSetters::class, "setUInt32ValueUnwrapped", new stdClass()],
189            [TestWrapperSetters::class, "setUInt32ValueUnwrapped", new UInt32Value()],
190
191            [TestWrapperSetters::class, "setBoolValueUnwrapped", []],
192            [TestWrapperSetters::class, "setBoolValueUnwrapped", new stdClass()],
193            [TestWrapperSetters::class, "setBoolValueUnwrapped", new BoolValue()],
194
195            [TestWrapperSetters::class, "setStringValueUnwrapped", []],
196            [TestWrapperSetters::class, "setStringValueUnwrapped", new stdClass()],
197            [TestWrapperSetters::class, "setStringValueUnwrapped", new StringValue()],
198
199            [TestWrapperSetters::class, "setBytesValueUnwrapped", []],
200            [TestWrapperSetters::class, "setBytesValueUnwrapped", new stdClass()],
201            [TestWrapperSetters::class, "setBytesValueUnwrapped", new BytesValue()],
202        ];
203    }
204
205    /**
206     * @dataProvider constructorWithWrapperTypeDataProvider
207     */
208    public function testConstructorWithWrapperType($class, $wrapperClass, $wrapperField, $getter, $value)
209    {
210        $actualInstance = new $class([$wrapperField => $value]);
211        $expectedInstance = new $class([$wrapperField => new $wrapperClass(['value' => $value])]);
212        $this->assertEquals($expectedInstance->$getter()->getValue(), $actualInstance->$getter()->getValue());
213    }
214
215    public function constructorWithWrapperTypeDataProvider()
216    {
217        return [
218            [TestWrapperSetters::class, DoubleValue::class, 'double_value', 'getDoubleValue', 1.1],
219            [TestWrapperSetters::class, FloatValue::class, 'float_value', 'getFloatValue', 2.2],
220            [TestWrapperSetters::class, Int64Value::class, 'int64_value', 'getInt64Value', 3],
221            [TestWrapperSetters::class, UInt64Value::class, 'uint64_value', 'getUInt64Value', 4],
222            [TestWrapperSetters::class, Int32Value::class, 'int32_value', 'getInt32Value', 5],
223            [TestWrapperSetters::class, UInt32Value::class, 'uint32_value', 'getUInt32Value', 6],
224            [TestWrapperSetters::class, BoolValue::class, 'bool_value', 'getBoolValue', true],
225            [TestWrapperSetters::class, StringValue::class, 'string_value', 'getStringValue', "eight"],
226            [TestWrapperSetters::class, BytesValue::class, 'bytes_value', 'getBytesValue', "nine"],
227        ];
228    }
229
230    /**
231     * @dataProvider constructorWithRepeatedWrapperTypeDataProvider
232     */
233    public function testConstructorWithRepeatedWrapperType($wrapperField, $getter, $value)
234    {
235        $actualInstance = new TestWrapperSetters([$wrapperField => $value]);
236        foreach ($actualInstance->$getter() as $key => $actualWrapperValue) {
237            $actualInnerValue = $actualWrapperValue->getValue();
238            $expectedElement = $value[$key];
239            if (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\StringValue')) {
240                $expectedInnerValue = $expectedElement->getValue();
241            } else {
242                $expectedInnerValue = $expectedElement;
243            }
244            $this->assertEquals($expectedInnerValue, $actualInnerValue);
245        }
246    }
247
248    public function constructorWithRepeatedWrapperTypeDataProvider()
249    {
250        $sv7 = new StringValue(['value' => 'seven']);
251        $sv8 = new StringValue(['value' => 'eight']);
252
253        $testWrapperSetters = new TestWrapperSetters();
254        $testWrapperSetters->setRepeatedStringValue([$sv7, $sv8]);
255        $repeatedField = $testWrapperSetters->getRepeatedStringValue();
256
257        return [
258            ['repeated_string_value', 'getRepeatedStringValue', []],
259            ['repeated_string_value', 'getRepeatedStringValue', [$sv7]],
260            ['repeated_string_value', 'getRepeatedStringValue', [$sv7, $sv8]],
261            ['repeated_string_value', 'getRepeatedStringValue', ['seven']],
262            ['repeated_string_value', 'getRepeatedStringValue', [7]],
263            ['repeated_string_value', 'getRepeatedStringValue', [7.7]],
264            ['repeated_string_value', 'getRepeatedStringValue', ['seven', 'eight']],
265            ['repeated_string_value', 'getRepeatedStringValue', [$sv7, 'eight']],
266            ['repeated_string_value', 'getRepeatedStringValue', ['seven', $sv8]],
267            ['repeated_string_value', 'getRepeatedStringValue', $repeatedField],
268        ];
269    }
270
271    /**
272     * @dataProvider constructorWithMapWrapperTypeDataProvider
273     */
274    public function testConstructorWithMapWrapperType($wrapperField, $getter, $value)
275    {
276        $actualInstance = new TestWrapperSetters([$wrapperField => $value]);
277        foreach ($actualInstance->$getter() as $key => $actualWrapperValue) {
278            $actualInnerValue = $actualWrapperValue->getValue();
279            $expectedElement = $value[$key];
280            if (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\StringValue')) {
281                $expectedInnerValue = $expectedElement->getValue();
282            } elseif (is_object($expectedElement) && is_a($expectedElement, '\Google\Protobuf\Internal\MapEntry')) {
283                $expectedInnerValue = $expectedElement->getValue()->getValue();
284            } else {
285                $expectedInnerValue = $expectedElement;
286            }
287            $this->assertEquals($expectedInnerValue, $actualInnerValue);
288        }
289    }
290
291    public function constructorWithMapWrapperTypeDataProvider()
292    {
293        $sv7 = new StringValue(['value' => 'seven']);
294        $sv8 = new StringValue(['value' => 'eight']);
295
296        $testWrapperSetters = new TestWrapperSetters();
297        $testWrapperSetters->setMapStringValue(['key' => $sv7, 'key2' => $sv8]);
298        $mapField = $testWrapperSetters->getMapStringValue();
299
300        return [
301            ['map_string_value', 'getMapStringValue', []],
302            ['map_string_value', 'getMapStringValue', ['key' => $sv7]],
303            ['map_string_value', 'getMapStringValue', ['key' => $sv7, 'key2' => $sv8]],
304            ['map_string_value', 'getMapStringValue', ['key' => 'seven']],
305            ['map_string_value', 'getMapStringValue', ['key' => 7]],
306            ['map_string_value', 'getMapStringValue', ['key' => 7.7]],
307            ['map_string_value', 'getMapStringValue', ['key' => 'seven', 'key2' => 'eight']],
308            ['map_string_value', 'getMapStringValue', ['key' => $sv7, 'key2' => 'eight']],
309            ['map_string_value', 'getMapStringValue', ['key' => 'seven', 'key2' => $sv8]],
310            ['map_string_value', 'getMapStringValue', $mapField],
311        ];
312    }
313}
314