aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/tests/test_base.php
blob: dc5e73f579fad1c4bc452aa680919f9c9eeb3a2c (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php

use Foo\TestMessage;
use Foo\TestEnum;
use Foo\TestMessage_Sub;

class TestBase extends PHPUnit_Framework_TestCase
{

    public function setFields(TestMessage $m)
    {
        TestUtil::setTestMessage($m);
    }

    public function setFields2(TestMessage $m)
    {
        TestUtil::setTestMessage2($m);
    }

    public function expectFields(TestMessage $m)
    {
        $this->assertSame(-42,  $m->getOptionalInt32());
        $this->assertSame(42,  $m->getOptionalUint32());
        $this->assertSame(-44,  $m->getOptionalSint32());
        $this->assertSame(46,   $m->getOptionalFixed32());
        $this->assertSame(-46,  $m->getOptionalSfixed32());
        $this->assertSame(1.5,  $m->getOptionalFloat());
        $this->assertSame(1.6,  $m->getOptionalDouble());
        $this->assertSame(true, $m->getOptionalBool());
        $this->assertSame('a',  $m->getOptionalString());
        $this->assertSame('b',  $m->getOptionalBytes());
        $this->assertSame(TestEnum::ONE, $m->getOptionalEnum());
        $this->assertSame(33,   $m->getOptionalMessage()->getA());
        if (PHP_INT_SIZE == 4) {
            $this->assertSame('-43',  $m->getOptionalInt64());
            $this->assertSame('43',   $m->getOptionalUint64());
            $this->assertSame('-45',  $m->getOptionalSint64());
            $this->assertSame('47',   $m->getOptionalFixed64());
            $this->assertSame('-47',  $m->getOptionalSfixed64());
        } else {
            $this->assertSame(-43,  $m->getOptionalInt64());
            $this->assertSame(43,   $m->getOptionalUint64());
            $this->assertSame(-45,  $m->getOptionalSint64());
            $this->assertSame(47,   $m->getOptionalFixed64());
            $this->assertSame(-47,  $m->getOptionalSfixed64());
        }

        $this->assertEquals(-42,  $m->getRepeatedInt32()[0]);
        $this->assertEquals(42,   $m->getRepeatedUint32()[0]);
        $this->assertEquals(-43,  $m->getRepeatedInt64()[0]);
        $this->assertEquals(43,   $m->getRepeatedUint64()[0]);
        $this->assertEquals(-44,  $m->getRepeatedSint32()[0]);
        $this->assertEquals(-45,  $m->getRepeatedSint64()[0]);
        $this->assertEquals(46,   $m->getRepeatedFixed32()[0]);
        $this->assertEquals(47,   $m->getRepeatedFixed64()[0]);
        $this->assertEquals(-46,  $m->getRepeatedSfixed32()[0]);
        $this->assertEquals(-47,  $m->getRepeatedSfixed64()[0]);
        $this->assertEquals(1.5,  $m->getRepeatedFloat()[0]);
        $this->assertEquals(1.6,  $m->getRepeatedDouble()[0]);
        $this->assertEquals(true, $m->getRepeatedBool()[0]);
        $this->assertEquals('a',  $m->getRepeatedString()[0]);
        $this->assertEquals('b',  $m->getRepeatedBytes()[0]);
        $this->assertEquals(34,   $m->getRepeatedMessage()[0]->GetA());

        $this->assertEquals(-52,   $m->getRepeatedInt32()[1]);
        $this->assertEquals(52,    $m->getRepeatedUint32()[1]);
        $this->assertEquals(-53,   $m->getRepeatedInt64()[1]);
        $this->assertEquals(53,    $m->getRepeatedUint64()[1]);
        $this->assertEquals(-54,   $m->getRepeatedSint32()[1]);
        $this->assertEquals(-55,   $m->getRepeatedSint64()[1]);
        $this->assertEquals(56,    $m->getRepeatedFixed32()[1]);
        $this->assertEquals(57,    $m->getRepeatedFixed64()[1]);
        $this->assertEquals(-56,   $m->getRepeatedSfixed32()[1]);
        $this->assertEquals(-57,   $m->getRepeatedSfixed64()[1]);
        $this->assertEquals(2.5,   $m->getRepeatedFloat()[1]);
        $this->assertEquals(2.6,   $m->getRepeatedDouble()[1]);
        $this->assertEquals(false, $m->getRepeatedBool()[1]);
        $this->assertEquals('c',   $m->getRepeatedString()[1]);
        $this->assertEquals('d',   $m->getRepeatedBytes()[1]);
        $this->assertEquals(35,    $m->getRepeatedMessage()[1]->GetA());

        if (PHP_INT_SIZE == 4) {
            $this->assertEquals('-63', $m->getMapInt64Int64()['-63']);
            $this->assertEquals('63',  $m->getMapUint64Uint64()['63']);
            $this->assertEquals('-65', $m->getMapSint64Sint64()['-65']);
            $this->assertEquals('67',  $m->getMapFixed64Fixed64()['67']);
            $this->assertEquals('-69',  $m->getMapSfixed64Sfixed64()['-69']);
        } else {
            $this->assertEquals(-63, $m->getMapInt64Int64()[-63]);
            $this->assertEquals(63,  $m->getMapUint64Uint64()[63]);
            $this->assertEquals(-65, $m->getMapSint64Sint64()[-65]);
            $this->assertEquals(67,  $m->getMapFixed64Fixed64()[67]);
            $this->assertEquals(-69,  $m->getMapSfixed64Sfixed64()[-69]);
        }
        $this->assertEquals(-62, $m->getMapInt32Int32()[-62]);
        $this->assertEquals(62,  $m->getMapUint32Uint32()[62]);
        $this->assertEquals(-64, $m->getMapSint32Sint32()[-64]);
        $this->assertEquals(66,  $m->getMapFixed32Fixed32()[66]);
        $this->assertEquals(-68,  $m->getMapSfixed32Sfixed32()[-68]);
        $this->assertEquals(3.5, $m->getMapInt32Float()[1]);
        $this->assertEquals(3.6, $m->getMapInt32Double()[1]);
        $this->assertEquals(true , $m->getMapBoolBool()[true]);
        $this->assertEquals('e', $m->getMapStringString()['e']);
        $this->assertEquals('f', $m->getMapInt32Bytes()[1]);
        $this->assertEquals(TestEnum::ONE, $m->getMapInt32Enum()[1]);
        $this->assertEquals(36, $m->getMapInt32Message()[1]->GetA());
    }

    // Test message merged from setFields and setFields2.
    public function expectFieldsMerged(TestMessage $m)
    {
        $this->assertSame(-144,  $m->getOptionalSint32());
        $this->assertSame(146,   $m->getOptionalFixed32());
        $this->assertSame(-146,  $m->getOptionalSfixed32());
        $this->assertSame(11.5,  $m->getOptionalFloat());
        $this->assertSame(11.6,  $m->getOptionalDouble());
        $this->assertSame(true, $m->getOptionalBool());
        $this->assertSame('aa',  $m->getOptionalString());
        $this->assertSame('bb',  $m->getOptionalBytes());
        $this->assertSame(133,   $m->getOptionalMessage()->getA());
        if (PHP_INT_SIZE == 4) {
            $this->assertSame('-143',  $m->getOptionalInt64());
            $this->assertSame('143',   $m->getOptionalUint64());
            $this->assertSame('-145',  $m->getOptionalSint64());
            $this->assertSame('147',   $m->getOptionalFixed64());
            $this->assertSame('-147',  $m->getOptionalSfixed64());
        } else {
            $this->assertSame(-143,  $m->getOptionalInt64());
            $this->assertSame(143,   $m->getOptionalUint64());
            $this->assertSame(-145,  $m->getOptionalSint64());
            $this->assertSame(147,   $m->getOptionalFixed64());
            $this->assertSame(-147,  $m->getOptionalSfixed64());
        }

        $this->assertEquals(-42,  $m->getRepeatedInt32()[0]);
        $this->assertEquals(42,   $m->getRepeatedUint32()[0]);
        $this->assertEquals(-43,  $m->getRepeatedInt64()[0]);
        $this->assertEquals(43,   $m->getRepeatedUint64()[0]);
        $this->assertEquals(-44,  $m->getRepeatedSint32()[0]);
        $this->assertEquals(-45,  $m->getRepeatedSint64()[0]);
        $this->assertEquals(46,   $m->getRepeatedFixed32()[0]);
        $this->assertEquals(47,   $m->getRepeatedFixed64()[0]);
        $this->assertEquals(-46,  $m->getRepeatedSfixed32()[0]);
        $this->assertEquals(-47,  $m->getRepeatedSfixed64()[0]);
        $this->assertEquals(1.5,  $m->getRepeatedFloat()[0]);
        $this->assertEquals(1.6,  $m->getRepeatedDouble()[0]);
        $this->assertEquals(true, $m->getRepeatedBool()[0]);
        $this->assertEquals('a',  $m->getRepeatedString()[0]);
        $this->assertEquals('b',  $m->getRepeatedBytes()[0]);
        $this->assertEquals(TestEnum::ZERO,  $m->getRepeatedEnum()[0]);
        $this->assertEquals(34,   $m->getRepeatedMessage()[0]->GetA());

        $this->assertEquals(-52,   $m->getRepeatedInt32()[1]);
        $this->assertEquals(52,    $m->getRepeatedUint32()[1]);
        $this->assertEquals(-53,   $m->getRepeatedInt64()[1]);
        $this->assertEquals(53,    $m->getRepeatedUint64()[1]);
        $this->assertEquals(-54,   $m->getRepeatedSint32()[1]);
        $this->assertEquals(-55,   $m->getRepeatedSint64()[1]);
        $this->assertEquals(56,    $m->getRepeatedFixed32()[1]);
        $this->assertEquals(57,    $m->getRepeatedFixed64()[1]);
        $this->assertEquals(-56,   $m->getRepeatedSfixed32()[1]);
        $this->assertEquals(-57,   $m->getRepeatedSfixed64()[1]);
        $this->assertEquals(2.5,   $m->getRepeatedFloat()[1]);
        $this->assertEquals(2.6,   $m->getRepeatedDouble()[1]);
        $this->assertEquals(false, $m->getRepeatedBool()[1]);
        $this->assertEquals('c',   $m->getRepeatedString()[1]);
        $this->assertEquals('d',   $m->getRepeatedBytes()[1]);
        $this->assertEquals(TestEnum::ONE,  $m->getRepeatedEnum()[1]);
        $this->assertEquals(35,    $m->getRepeatedMessage()[1]->GetA());

        $this->assertEquals(-142,  $m->getRepeatedInt32()[2]);
        $this->assertEquals(142,   $m->getRepeatedUint32()[2]);
        $this->assertEquals(-143,  $m->getRepeatedInt64()[2]);
        $this->assertEquals(143,   $m->getRepeatedUint64()[2]);
        $this->assertEquals(-144,  $m->getRepeatedSint32()[2]);
        $this->assertEquals(-145,  $m->getRepeatedSint64()[2]);
        $this->assertEquals(146,   $m->getRepeatedFixed32()[2]);
        $this->assertEquals(147,   $m->getRepeatedFixed64()[2]);
        $this->assertEquals(-146,  $m->getRepeatedSfixed32()[2]);
        $this->assertEquals(-147,  $m->getRepeatedSfixed64()[2]);
        $this->assertEquals(11.5,  $m->getRepeatedFloat()[2]);
        $this->assertEquals(11.6,  $m->getRepeatedDouble()[2]);
        $this->assertEquals(false, $m->getRepeatedBool()[2]);
        $this->assertEquals('aa',  $m->getRepeatedString()[2]);
        $this->assertEquals('bb',  $m->getRepeatedBytes()[2]);
        $this->assertEquals(TestEnum::TWO,  $m->getRepeatedEnum()[2]);
        $this->assertEquals(134,   $m->getRepeatedMessage()[2]->GetA());

        if (PHP_INT_SIZE == 4) {
            $this->assertEquals('-163', $m->getMapInt64Int64()['-63']);
            $this->assertEquals('163',  $m->getMapUint64Uint64()['63']);
            $this->assertEquals('-165', $m->getMapSint64Sint64()['-65']);
            $this->assertEquals('167',  $m->getMapFixed64Fixed64()['67']);
            $this->assertEquals('-169',  $m->getMapSfixed64Sfixed64()['-69']);
        } else {
            $this->assertEquals(-163, $m->getMapInt64Int64()[-63]);
            $this->assertEquals(163,  $m->getMapUint64Uint64()[63]);
            $this->assertEquals(-165, $m->getMapSint64Sint64()[-65]);
            $this->assertEquals(167,  $m->getMapFixed64Fixed64()[67]);
            $this->assertEquals(-169,  $m->getMapSfixed64Sfixed64()[-69]);
        }
        $this->assertEquals(-162, $m->getMapInt32Int32()[-62]);
        $this->assertEquals(162,  $m->getMapUint32Uint32()[62]);
        $this->assertEquals(-164, $m->getMapSint32Sint32()[-64]);
        $this->assertEquals(166,  $m->getMapFixed32Fixed32()[66]);
        $this->assertEquals(-168,  $m->getMapSfixed32Sfixed32()[-68]);
        $this->assertEquals(13.5, $m->getMapInt32Float()[1]);
        $this->assertEquals(13.6, $m->getMapInt32Double()[1]);
        $this->assertEquals(false , $m->getMapBoolBool()[true]);
        $this->assertEquals('ee', $m->getMapStringString()['e']);
        $this->assertEquals('ff', $m->getMapInt32Bytes()[1]);
        $this->assertEquals(TestEnum::TWO, $m->getMapInt32Enum()[1]);
        $this->assertEquals(136, $m->getMapInt32Message()[1]->GetA());

        if (PHP_INT_SIZE == 4) {
            $this->assertEquals('-163', $m->getMapInt64Int64()['-163']);
            $this->assertEquals('163',  $m->getMapUint64Uint64()['163']);
            $this->assertEquals('-165', $m->getMapSint64Sint64()['-165']);
            $this->assertEquals('167',  $m->getMapFixed64Fixed64()['167']);
            $this->assertEquals('-169',  $m->getMapSfixed64Sfixed64()['-169']);
        } else {
            $this->assertEquals(-163, $m->getMapInt64Int64()[-163]);
            $this->assertEquals(163,  $m->getMapUint64Uint64()[163]);
            $this->assertEquals(-165, $m->getMapSint64Sint64()[-165]);
            $this->assertEquals(167,  $m->getMapFixed64Fixed64()[167]);
            $this->assertEquals(-169,  $m->getMapSfixed64Sfixed64()[-169]);
        }
        $this->assertEquals(-162, $m->getMapInt32Int32()[-162]);
        $this->assertEquals(162,  $m->getMapUint32Uint32()[162]);
        $this->assertEquals(-164, $m->getMapSint32Sint32()[-164]);
        $this->assertEquals(166,  $m->getMapFixed32Fixed32()[166]);
        $this->assertEquals(-168,  $m->getMapSfixed32Sfixed32()[-168]);
        $this->assertEquals(13.5, $m->getMapInt32Float()[2]);
        $this->assertEquals(13.6, $m->getMapInt32Double()[2]);
        $this->assertEquals(false , $m->getMapBoolBool()[false]);
        $this->assertEquals('ee', $m->getMapStringString()['ee']);
        $this->assertEquals('ff', $m->getMapInt32Bytes()[2]);
        $this->assertEquals(TestEnum::TWO, $m->getMapInt32Enum()[2]);
        $this->assertEquals(136, $m->getMapInt32Message()[2]->GetA());
    }

    public function expectEmptyFields(TestMessage $m)
    {
        $this->assertSame(0,   $m->getOptionalInt32());
        $this->assertSame(0,   $m->getOptionalUint32());
        $this->assertSame(0,   $m->getOptionalSint32());
        $this->assertSame(0,   $m->getOptionalFixed32());
        $this->assertSame(0,   $m->getOptionalSfixed32());
        $this->assertSame(0.0, $m->getOptionalFloat());
        $this->assertSame(0.0, $m->getOptionalDouble());
        $this->assertSame(false, $m->getOptionalBool());
        $this->assertSame('',  $m->getOptionalString());
        $this->assertSame('',  $m->getOptionalBytes());
        $this->assertSame(0, $m->getOptionalEnum());
        $this->assertNull($m->getOptionalMessage());
        $this->assertNull($m->getOptionalIncludedMessage());
        $this->assertNull($m->getRecursive());
        if (PHP_INT_SIZE == 4) {
            $this->assertSame("0", $m->getOptionalInt64());
            $this->assertSame("0", $m->getOptionalUint64());
            $this->assertSame("0", $m->getOptionalSint64());
            $this->assertSame("0", $m->getOptionalFixed64());
            $this->assertSame("0", $m->getOptionalSfixed64());
        } else {
            $this->assertSame(0, $m->getOptionalInt64());
            $this->assertSame(0, $m->getOptionalUint64());
            $this->assertSame(0, $m->getOptionalSint64());
            $this->assertSame(0, $m->getOptionalFixed64());
            $this->assertSame(0, $m->getOptionalSfixed64());
        }

        $this->assertEquals(0, count($m->getRepeatedInt32()));
        $this->assertEquals(0, count($m->getRepeatedUint32()));
        $this->assertEquals(0, count($m->getRepeatedInt64()));
        $this->assertEquals(0, count($m->getRepeatedUint64()));
        $this->assertEquals(0, count($m->getRepeatedSint32()));
        $this->assertEquals(0, count($m->getRepeatedSint64()));
        $this->assertEquals(0, count($m->getRepeatedFixed32()));
        $this->assertEquals(0, count($m->getRepeatedFixed64()));
        $this->assertEquals(0, count($m->getRepeatedSfixed32()));
        $this->assertEquals(0, count($m->getRepeatedSfixed64()));
        $this->assertEquals(0, count($m->getRepeatedFloat()));
        $this->assertEquals(0, count($m->getRepeatedDouble()));
        $this->assertEquals(0, count($m->getRepeatedBool()));
        $this->assertEquals(0, count($m->getRepeatedString()));
        $this->assertEquals(0, count($m->getRepeatedBytes()));
        $this->assertEquals(0, count($m->getRepeatedEnum()));
        $this->assertEquals(0, count($m->getRepeatedMessage()));
        $this->assertEquals(0, count($m->getRepeatedRecursive()));

        $this->assertSame("", $m->getMyOneof());
        $this->assertSame(0,   $m->getOneofInt32());
        $this->assertSame(0,   $m->getOneofUint32());
        $this->assertSame(0,   $m->getOneofSint32());
        $this->assertSame(0,   $m->getOneofFixed32());
        $this->assertSame(0,   $m->getOneofSfixed32());
        $this->assertSame(0.0, $m->getOneofFloat());
        $this->assertSame(0.0, $m->getOneofDouble());
        $this->assertSame(false, $m->getOneofBool());
        $this->assertSame('',  $m->getOneofString());
        $this->assertSame('',  $m->getOneofBytes());
        $this->assertSame(0, $m->getOneofEnum());
        $this->assertNull($m->getOptionalMessage());
        if (PHP_INT_SIZE == 4) {
            $this->assertSame("0", $m->getOneofInt64());
            $this->assertSame("0", $m->getOneofUint64());
            $this->assertSame("0", $m->getOneofSint64());
            $this->assertSame("0", $m->getOneofFixed64());
            $this->assertSame("0", $m->getOneofSfixed64());
        } else {
            $this->assertSame(0, $m->getOneofInt64());
            $this->assertSame(0, $m->getOneofUint64());
            $this->assertSame(0, $m->getOneofSint64());
            $this->assertSame(0, $m->getOneofFixed64());
            $this->assertSame(0, $m->getOneofSfixed64());
        }

        $this->assertEquals(0, count($m->getMapInt64Int64()));
        $this->assertEquals(0, count($m->getMapUint64Uint64()));
        $this->assertEquals(0, count($m->getMapSint64Sint64()));
        $this->assertEquals(0, count($m->getMapFixed64Fixed64()));
        $this->assertEquals(0, count($m->getMapInt32Int32()));
        $this->assertEquals(0, count($m->getMapUint32Uint32()));
        $this->assertEquals(0, count($m->getMapSint32Sint32()));
        $this->assertEquals(0, count($m->getMapFixed32Fixed32()));
        $this->assertEquals(0, count($m->getMapSfixed32Sfixed32()));
        $this->assertEquals(0, count($m->getMapSfixed64Sfixed64()));
        $this->assertEquals(0, count($m->getMapInt32Float()));
        $this->assertEquals(0, count($m->getMapInt32Double()));
        $this->assertEquals(0, count($m->getMapBoolBool()));
        $this->assertEquals(0, count($m->getMapStringString()));
        $this->assertEquals(0, count($m->getMapInt32Bytes()));
        $this->assertEquals(0, count($m->getMapInt32Enum()));
        $this->assertEquals(0, count($m->getMapInt32Message()));
        $this->assertEquals(0, count($m->getMapRecursive()));
    }

  // This test is to avoid the warning of no test by php unit.
  public function testNone()
  {
  }
}