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

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

Powered by Google App Engine
This is Rietveld 408576698