Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: third_party/protobuf/php/tests/generated_class_test.php

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Update to new HEAD (b7632464b4) + restore GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <?php
2
3 require_once('test.pb.php');
4 require_once('test_no_namespace.pb.php');
5 require_once('test_util.php');
6
7 use Google\Protobuf\Internal\RepeatedField;
8 use Google\Protobuf\Internal\GPBType;
9 use Foo\TestEnum;
10 use Foo\TestMessage;
11 use Foo\TestMessage_Sub;
12
13 class GeneratedClassTest extends PHPUnit_Framework_TestCase
14 {
15
16 #########################################################
17 # Test field accessors.
18 #########################################################
19
20 public function testSetterGetter()
21 {
22 $m = new TestMessage();
23 $m->setOptionalInt32(1);
24 $this->assertSame(1, $m->getOptionalInt32());
25 }
26
27 #########################################################
28 # Test int32 field.
29 #########################################################
30
31 public function testInt32Field()
32 {
33 $m = new TestMessage();
34
35 // Set integer.
36 $m->setOptionalInt32(MAX_INT32);
37 $this->assertSame(MAX_INT32, $m->getOptionalInt32());
38 $m->setOptionalInt32(MIN_INT32);
39 $this->assertSame(MIN_INT32, $m->getOptionalInt32());
40
41 // Set float.
42 $m->setOptionalInt32(1.1);
43 $this->assertSame(1, $m->getOptionalInt32());
44 $m->setOptionalInt32(MAX_INT32_FLOAT);
45 $this->assertSame(MAX_INT32, $m->getOptionalInt32());
46 $m->setOptionalInt32(MIN_INT32_FLOAT);
47 $this->assertSame(MIN_INT32, $m->getOptionalInt32());
48
49 // Set string.
50 $m->setOptionalInt32('2');
51 $this->assertSame(2, $m->getOptionalInt32());
52 $m->setOptionalInt32('3.1');
53 $this->assertSame(3, $m->getOptionalInt32());
54 $m->setOptionalInt32(MAX_INT32_STRING);
55 $this->assertSame(MAX_INT32, $m->getOptionalInt32());
56 $m->setOptionalInt32(MIN_INT32_STRING);
57 $this->assertSame(MIN_INT32, $m->getOptionalInt32());
58 }
59
60 /**
61 * @expectedException PHPUnit_Framework_Error
62 */
63 public function testInt32FieldInvalidTypeFail()
64 {
65 $m = new TestMessage();
66 $m->setOptionalInt32(new TestMessage());
67 }
68
69 /**
70 * @expectedException PHPUnit_Framework_Error
71 */
72 public function testInt32FieldInvalidStringFail()
73 {
74 $m = new TestMessage();
75 $m->setOptionalInt32('abc');
76 }
77
78 #########################################################
79 # Test uint32 field.
80 #########################################################
81
82 public function testUint32Field()
83 {
84 $m = new TestMessage();
85
86 // Set integer.
87 $m->setOptionalUint32(MAX_UINT32);
88 $this->assertSame(-1, $m->getOptionalUint32());
89 $m->setOptionalUint32(-1);
90 $this->assertSame(-1, $m->getOptionalUint32());
91 $m->setOptionalUint32(MIN_UINT32);
92 $this->assertSame(MIN_INT32, $m->getOptionalUint32());
93
94 // Set float.
95 $m->setOptionalUint32(1.1);
96 $this->assertSame(1, $m->getOptionalUint32());
97 $m->setOptionalUint32(MAX_UINT32_FLOAT);
98 $this->assertSame(-1, $m->getOptionalUint32());
99 $m->setOptionalUint32(-1.0);
100 $this->assertSame(-1, $m->getOptionalUint32());
101 $m->setOptionalUint32(MIN_UINT32_FLOAT);
102 $this->assertSame(MIN_INT32, $m->getOptionalUint32());
103
104 // Set string.
105 $m->setOptionalUint32('2');
106 $this->assertSame(2, $m->getOptionalUint32());
107 $m->setOptionalUint32('3.1');
108 $this->assertSame(3, $m->getOptionalUint32());
109 $m->setOptionalUint32(MAX_UINT32_STRING);
110 $this->assertSame(-1, $m->getOptionalUint32());
111 $m->setOptionalUint32('-1.0');
112 $this->assertSame(-1, $m->getOptionalUint32());
113 $m->setOptionalUint32(MIN_UINT32_STRING);
114 $this->assertSame(MIN_INT32, $m->getOptionalUint32());
115 }
116
117 /**
118 * @expectedException PHPUnit_Framework_Error
119 */
120 public function testUint32FieldInvalidTypeFail()
121 {
122 $m = new TestMessage();
123 $m->setOptionalUint32(new TestMessage());
124 }
125
126 /**
127 * @expectedException PHPUnit_Framework_Error
128 */
129 public function testUint32FieldInvalidStringFail()
130 {
131 $m = new TestMessage();
132 $m->setOptionalUint32('abc');
133 }
134
135 #########################################################
136 # Test int64 field.
137 #########################################################
138
139 public function testInt64Field()
140 {
141 $m = new TestMessage();
142
143 // Set integer.
144 $m->setOptionalInt64(MAX_INT64);
145 $this->assertSame(MAX_INT64, $m->getOptionalInt64());
146 $m->setOptionalInt64(MIN_INT64);
147 $this->assertEquals(MIN_INT64, $m->getOptionalInt64());
148
149 // Set float.
150 $m->setOptionalInt64(1.1);
151 if (PHP_INT_SIZE == 4) {
152 $this->assertSame('1', $m->getOptionalInt64());
153 } else {
154 $this->assertSame(1, $m->getOptionalInt64());
155 }
156
157 // Set string.
158 $m->setOptionalInt64('2');
159 if (PHP_INT_SIZE == 4) {
160 $this->assertSame('2', $m->getOptionalInt64());
161 } else {
162 $this->assertSame(2, $m->getOptionalInt64());
163 }
164
165 $m->setOptionalInt64('3.1');
166 if (PHP_INT_SIZE == 4) {
167 $this->assertSame('3', $m->getOptionalInt64());
168 } else {
169 $this->assertSame(3, $m->getOptionalInt64());
170 }
171
172 $m->setOptionalInt64(MAX_INT64_STRING);
173 if (PHP_INT_SIZE == 4) {
174 $this->assertSame(MAX_INT64_STRING, $m->getOptionalInt64());
175 } else {
176 $this->assertSame(MAX_INT64, $m->getOptionalInt64());
177 }
178
179 $m->setOptionalInt64(MIN_INT64_STRING);
180 if (PHP_INT_SIZE == 4) {
181 $this->assertSame(MIN_INT64_STRING, $m->getOptionalInt64());
182 } else {
183 $this->assertSame(MIN_INT64, $m->getOptionalInt64());
184 }
185 }
186
187 /**
188 * @expectedException PHPUnit_Framework_Error
189 */
190 public function testInt64FieldInvalidTypeFail()
191 {
192 $m = new TestMessage();
193 $m->setOptionalInt64(new TestMessage());
194 }
195
196 /**
197 * @expectedException PHPUnit_Framework_Error
198 */
199 public function testInt64FieldInvalidStringFail()
200 {
201 $m = new TestMessage();
202 $m->setOptionalInt64('abc');
203 }
204
205 #########################################################
206 # Test uint64 field.
207 #########################################################
208
209 public function testUint64Field()
210 {
211 $m = new TestMessage();
212
213 // Set integer.
214 $m->setOptionalUint64(MAX_UINT64);
215 if (PHP_INT_SIZE == 4) {
216 $this->assertSame(MAX_UINT64_STRING, $m->getOptionalUint64());
217 } else {
218 $this->assertSame(MAX_UINT64, $m->getOptionalUint64());
219 }
220
221 // Set float.
222 $m->setOptionalUint64(1.1);
223 if (PHP_INT_SIZE == 4) {
224 $this->assertSame('1', $m->getOptionalUint64());
225 } else {
226 $this->assertSame(1, $m->getOptionalUint64());
227 }
228
229 // Set string.
230 $m->setOptionalUint64('2');
231 if (PHP_INT_SIZE == 4) {
232 $this->assertSame('2', $m->getOptionalUint64());
233 } else {
234 $this->assertSame(2, $m->getOptionalUint64());
235 }
236
237 $m->setOptionalUint64('3.1');
238 if (PHP_INT_SIZE == 4) {
239 $this->assertSame('3', $m->getOptionalUint64());
240 } else {
241 $this->assertSame(3, $m->getOptionalUint64());
242 }
243
244 $m->setOptionalUint64(MAX_UINT64_STRING);
245 if (PHP_INT_SIZE == 4) {
246 $this->assertSame(MAX_UINT64_STRING, $m->getOptionalUint64());
247 } else {
248 $this->assertSame(MAX_UINT64, $m->getOptionalUint64());
249 }
250 }
251
252 /**
253 * @expectedException PHPUnit_Framework_Error
254 */
255 public function testUint64FieldInvalidTypeFail()
256 {
257 $m = new TestMessage();
258 $m->setOptionalUint64(new TestMessage());
259 }
260
261 /**
262 * @expectedException PHPUnit_Framework_Error
263 */
264 public function testUint64FieldInvalidStringFail()
265 {
266 $m = new TestMessage();
267 $m->setOptionalUint64('abc');
268 }
269
270 #########################################################
271 # Test enum field.
272 #########################################################
273
274 public function testEnumField()
275 {
276 $m = new TestMessage();
277
278 // Set enum.
279 $m->setOptionalEnum(TestEnum::ONE);
280 $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
281
282 // Set integer.
283 $m->setOptionalEnum(1);
284 $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
285
286 // Set float.
287 $m->setOptionalEnum(1.1);
288 $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
289
290 // Set string.
291 $m->setOptionalEnum("1");
292 $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
293 }
294
295 #########################################################
296 # Test float field.
297 #########################################################
298
299 public function testFloatField()
300 {
301 $m = new TestMessage();
302
303 // Set integer.
304 $m->setOptionalFloat(1);
305 $this->assertEquals(1.0, $m->getOptionalFloat(), '', MAX_FLOAT_DIFF);
306
307 // Set float.
308 $m->setOptionalFloat(1.1);
309 $this->assertEquals(1.1, $m->getOptionalFloat(), '', MAX_FLOAT_DIFF);
310
311 // Set string.
312 $m->setOptionalFloat('2');
313 $this->assertEquals(2.0, $m->getOptionalFloat(), '', MAX_FLOAT_DIFF);
314 $m->setOptionalFloat('3.1');
315 $this->assertEquals(3.1, $m->getOptionalFloat(), '', MAX_FLOAT_DIFF);
316 }
317
318 /**
319 * @expectedException PHPUnit_Framework_Error
320 */
321 public function testFloatFieldInvalidTypeFail()
322 {
323 $m = new TestMessage();
324 $m->setOptionalFloat(new TestMessage());
325 }
326
327 /**
328 * @expectedException PHPUnit_Framework_Error
329 */
330 public function testFloatFieldInvalidStringFail()
331 {
332 $m = new TestMessage();
333 $m->setOptionalFloat('abc');
334 }
335
336 #########################################################
337 # Test double field.
338 #########################################################
339
340 public function testDoubleField()
341 {
342 $m = new TestMessage();
343
344 // Set integer.
345 $m->setOptionalDouble(1);
346 $this->assertEquals(1.0, $m->getOptionalDouble(), '', MAX_FLOAT_DIFF);
347
348 // Set float.
349 $m->setOptionalDouble(1.1);
350 $this->assertEquals(1.1, $m->getOptionalDouble(), '', MAX_FLOAT_DIFF);
351
352 // Set string.
353 $m->setOptionalDouble('2');
354 $this->assertEquals(2.0, $m->getOptionalDouble(), '', MAX_FLOAT_DIFF);
355 $m->setOptionalDouble('3.1');
356 $this->assertEquals(3.1, $m->getOptionalDouble(), '', MAX_FLOAT_DIFF);
357 }
358
359 /**
360 * @expectedException PHPUnit_Framework_Error
361 */
362 public function testDoubleFieldInvalidTypeFail()
363 {
364 $m = new TestMessage();
365 $m->setOptionalDouble(new TestMessage());
366 }
367
368 /**
369 * @expectedException PHPUnit_Framework_Error
370 */
371 public function testDoubleFieldInvalidStringFail()
372 {
373 $m = new TestMessage();
374 $m->setOptionalDouble('abc');
375 }
376
377 #########################################################
378 # Test bool field.
379 #########################################################
380
381 public function testBoolField()
382 {
383 $m = new TestMessage();
384
385 // Set bool.
386 $m->setOptionalBool(true);
387 $this->assertSame(true, $m->getOptionalBool());
388
389 // Set integer.
390 $m->setOptionalBool(-1);
391 $this->assertSame(true, $m->getOptionalBool());
392
393 // Set float.
394 $m->setOptionalBool(1.1);
395 $this->assertSame(true, $m->getOptionalBool());
396
397 // Set string.
398 $m->setOptionalBool('');
399 $this->assertSame(false, $m->getOptionalBool());
400 }
401
402 /**
403 * @expectedException PHPUnit_Framework_Error
404 */
405 public function testBoolFieldInvalidStringFail()
406 {
407 $m = new TestMessage();
408 $m->setOptionalBool(new TestMessage());
409 }
410
411 #########################################################
412 # Test string field.
413 #########################################################
414
415 public function testStringField()
416 {
417 $m = new TestMessage();
418
419 // Set string.
420 $m->setOptionalString('abc');
421 $this->assertSame('abc', $m->getOptionalString());
422
423 // Set integer.
424 $m->setOptionalString(1);
425 $this->assertSame('1', $m->getOptionalString());
426
427 // Set double.
428 $m->setOptionalString(1.1);
429 $this->assertSame('1.1', $m->getOptionalString());
430
431 // Set bool.
432 $m->setOptionalString(true);
433 $this->assertSame('1', $m->getOptionalString());
434 }
435
436 /**
437 * @expectedException PHPUnit_Framework_Error
438 */
439 public function testStringFieldInvalidUTF8Fail()
440 {
441 $m = new TestMessage();
442 $hex = hex2bin("ff");
443 $m->setOptionalString($hex);
444 }
445
446 #########################################################
447 # Test bytes field.
448 #########################################################
449
450 public function testBytesField()
451 {
452 $m = new TestMessage();
453
454 // Set string.
455 $m->setOptionalBytes('abc');
456 $this->assertSame('abc', $m->getOptionalBytes());
457
458 // Set integer.
459 $m->setOptionalBytes(1);
460 $this->assertSame('1', $m->getOptionalBytes());
461
462 // Set double.
463 $m->setOptionalBytes(1.1);
464 $this->assertSame('1.1', $m->getOptionalBytes());
465
466 // Set bool.
467 $m->setOptionalBytes(true);
468 $this->assertSame('1', $m->getOptionalBytes());
469 }
470
471 public function testBytesFieldInvalidUTF8Success()
472 {
473 $m = new TestMessage();
474 $hex = hex2bin("ff");
475 $m->setOptionalBytes($hex);
476 }
477
478 #########################################################
479 # Test message field.
480 #########################################################
481
482 public function testMessageField()
483 {
484 $m = new TestMessage();
485
486 $sub_m = new TestMessage_Sub();
487 $sub_m->setA(1);
488 $m->setOptionalMessage($sub_m);
489 $this->assertSame(1, $m->getOptionalMessage()->getA());
490
491 $null = null;
492 $m->setOptionalMessage($null);
493 $this->assertNull($m->getOptionalMessage());
494 }
495
496 /**
497 * @expectedException PHPUnit_Framework_Error
498 */
499 public function testMessageFieldWrongTypeFail()
500 {
501 $m = new TestMessage();
502 $a = 1;
503 $m->setOptionalMessage($a);
504 }
505
506 /**
507 * @expectedException PHPUnit_Framework_Error
508 */
509 public function testMessageFieldWrongClassFail()
510 {
511 $m = new TestMessage();
512 $m->setOptionalMessage(new TestMessage());
513 }
514
515 #########################################################
516 # Test repeated field.
517 #########################################################
518
519 public function testRepeatedField()
520 {
521 $m = new TestMessage();
522
523 $repeated_int32 = new RepeatedField(GPBType::INT32);
524 $m->setRepeatedInt32($repeated_int32);
525 $this->assertSame($repeated_int32, $m->getRepeatedInt32());
526 }
527
528 /**
529 * @expectedException PHPUnit_Framework_Error
530 */
531 public function testRepeatedFieldWrongTypeFail()
532 {
533 $m = new TestMessage();
534 $a = 1;
535 $m->setRepeatedInt32($a);
536 }
537
538 /**
539 * @expectedException PHPUnit_Framework_Error
540 */
541 public function testRepeatedFieldWrongObjectFail()
542 {
543 $m = new TestMessage();
544 $m->setRepeatedInt32($m);
545 }
546
547 /**
548 * @expectedException PHPUnit_Framework_Error
549 */
550 public function testRepeatedFieldWrongRepeatedTypeFail()
551 {
552 $m = new TestMessage();
553
554 $repeated_int32 = new RepeatedField(GPBType::UINT32);
555 $m->setRepeatedInt32($repeated_int32);
556 }
557
558 /**
559 * @expectedException PHPUnit_Framework_Error
560 */
561 public function testRepeatedFieldWrongRepeatedMessageClassFail()
562 {
563 $m = new TestMessage();
564
565 $repeated_message = new RepeatedField(GPBType::MESSAGE,
566 TestMessage::class);
567 $m->setRepeatedMessage($repeated_message);
568 }
569
570 #########################################################
571 # Test oneof field.
572 #########################################################
573
574 public function testOneofField() {
575 $m = new TestMessage();
576
577 $m->setOneofInt32(1);
578 $this->assertSame(1, $m->getOneofInt32());
579 $this->assertSame(0.0, $m->getOneofFloat());
580 $this->assertSame('', $m->getOneofString());
581 $this->assertSame(NULL, $m->getOneofMessage());
582
583 $m->setOneofFloat(2.0);
584 $this->assertSame(0, $m->getOneofInt32());
585 $this->assertSame(2.0, $m->getOneofFloat());
586 $this->assertSame('', $m->getOneofString());
587 $this->assertSame(NULL, $m->getOneofMessage());
588
589 $m->setOneofString('abc');
590 $this->assertSame(0, $m->getOneofInt32());
591 $this->assertSame(0.0, $m->getOneofFloat());
592 $this->assertSame('abc', $m->getOneofString());
593 $this->assertSame(NULL, $m->getOneofMessage());
594
595 $sub_m = new TestMessage_Sub();
596 $sub_m->setA(1);
597 $m->setOneofMessage($sub_m);
598 $this->assertSame(0, $m->getOneofInt32());
599 $this->assertSame(0.0, $m->getOneofFloat());
600 $this->assertSame('', $m->getOneofString());
601 $this->assertSame(1, $m->getOneofMessage()->getA());
602 }
603
604 #########################################################
605 # Test oneof field.
606 #########################################################
607
608 public function testMessageWithoutNamespace() {
609 $m = new NoNameSpace();
610 }
611 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698