• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2
3# phpunit has memory leak by itself. Thus, it cannot be used to test memory leak.
4
5require_once('generated/NoNamespaceEnum.php');
6require_once('generated/NoNamespaceMessage.php');
7require_once('generated/NoNamespaceMessage/NestedEnum.php');
8require_once('generated/NoNamespaceMessage/NestedMessage.php');
9require_once('generated/PrefixEmpty.php');
10require_once('generated/PrefixTestPrefix.php');
11require_once('generated/PrefixTestPrefix/PrefixNestedEnum.php');
12require_once('generated/PrefixTestPrefix/PrefixNestedMessage.php');
13require_once('generated/TestEmptyNamespace.php');
14require_once('generated/TestEmptyNamespace/NestedEnum.php');
15require_once('generated/TestEmptyNamespace/NestedMessage.php');
16require_once('generated/Bar/TestInclude.php');
17require_once('generated/Bar/TestLegacyMessage.php');
18require_once('generated/Bar/TestLegacyMessage/NestedEnum.php');
19require_once('generated/Bar/TestLegacyMessage/NestedMessage.php');
20require_once('generated/Foo/PBARRAY.php');
21require_once('generated/Foo/PBEmpty.php');
22require_once('generated/Foo/TestAny.php');
23require_once('generated/Foo/TestBoolValue.php');
24require_once('generated/Foo/TestBytesValue.php');
25require_once('generated/Foo/TestEnum.php');
26require_once('generated/Foo/TestIncludeNamespaceMessage.php');
27require_once('generated/Foo/TestIncludePrefixMessage.php');
28require_once('generated/Foo/TestInt32Value.php');
29require_once('generated/Foo/TestInt64Value.php');
30require_once('generated/Foo/TestMessage.php');
31require_once('generated/Foo/TestMessage/PBEmpty.php');
32require_once('generated/Foo/TestMessage/NestedEnum.php');
33require_once('generated/Foo/TestMessage/Sub.php');
34require_once('generated/Foo/TestPackedMessage.php');
35require_once('generated/Foo/TestPhpDoc.php');
36require_once('generated/Foo/TestRandomFieldOrder.php');
37require_once('generated/Foo/TestReverseFieldOrder.php');
38require_once('generated/Foo/TestStringValue.php');
39require_once('generated/Foo/TestUInt32Value.php');
40require_once('generated/Foo/TestUInt64Value.php');
41require_once('generated/Foo/TestUnpackedMessage.php');
42require_once('generated/Foo/testLowerCaseMessage.php');
43require_once('generated/Foo/testLowerCaseEnum.php');
44require_once('generated/GPBMetadata/Proto/Test.php');
45require_once('generated/TestEmptyPhpNamespace.php');
46require_once('generated/GPBMetadata/Proto/TestInclude.php');
47require_once('generated/TestNoNamespace.php');
48require_once('generated/Metadata/Php/Test/TestPhpNamespace.php');
49require_once('generated/GPBMetadata/Proto/TestPrefix.php');
50require_once('generated/Php/Test/TestNamespace.php');
51require_once('generated/Php/Test/TestNamespace/PBEmpty.php');
52require_once('generated/Php/Test/TestNamespace/PBEmpty/NestedEnum.php');
53require_once('generated/Php/Test/TestNamespace/PBEmpty/NestedMessage.php');
54require_once('generated/Php/Test/TestNamespace/NestedEnum.php');
55require_once('generated/Php/Test/TestNamespace/NestedMessage.php');
56require_once('test_util.php');
57
58use Google\Protobuf\Internal\RepeatedField;
59use Google\Protobuf\Internal\GPBType;
60use Foo\TestAny;
61use Foo\TestMessage;
62use Foo\TestMessage\Sub;
63
64$from = new TestMessage();
65TestUtil::setTestMessage($from);
66TestUtil::assertTestMessage($from);
67
68$data = $from->serializeToString();
69
70$to = new TestMessage();
71$to->mergeFromString($data);
72
73TestUtil::assertTestMessage($to);
74
75$from = new TestMessage();
76TestUtil::setTestMessage2($from);
77
78$data = $from->serializeToString();
79
80$to->mergeFromString($data);
81
82// TODO(teboring): This causes following tests fail in php7.
83# $from->setRecursive($from);
84
85$arr = new RepeatedField(GPBType::MESSAGE, TestMessage::class);
86$arr[] = new TestMessage;
87$arr[0]->SetRepeatedRecursive($arr);
88
89// Test oneof fields.
90$m = new TestMessage();
91
92$m->setOneofInt32(1);
93assert(1 === $m->getOneofInt32());
94assert(0.0 === $m->getOneofFloat());
95assert('' === $m->getOneofString());
96assert(NULL === $m->getOneofMessage());
97$data = $m->serializeToString();
98$n = new TestMessage();
99$n->mergeFromString($data);
100assert(1 === $n->getOneofInt32());
101
102$m->setOneofFloat(2.0);
103assert(0 === $m->getOneofInt32());
104assert(2.0 === $m->getOneofFloat());
105assert('' === $m->getOneofString());
106assert(NULL === $m->getOneofMessage());
107$data = $m->serializeToString();
108$n = new TestMessage();
109$n->mergeFromString($data);
110assert(2.0 === $n->getOneofFloat());
111
112$m->setOneofString('abc');
113assert(0 === $m->getOneofInt32());
114assert(0.0 === $m->getOneofFloat());
115assert('abc' === $m->getOneofString());
116assert(NULL === $m->getOneofMessage());
117$data = $m->serializeToString();
118$n = new TestMessage();
119$n->mergeFromString($data);
120assert('abc' === $n->getOneofString());
121
122$sub_m = new Sub();
123$sub_m->setA(1);
124$m->setOneofMessage($sub_m);
125assert(0 === $m->getOneofInt32());
126assert(0.0 === $m->getOneofFloat());
127assert('' === $m->getOneofString());
128assert(1 === $m->getOneofMessage()->getA());
129$data = $m->serializeToString();
130$n = new TestMessage();
131$n->mergeFromString($data);
132assert(1 === $n->getOneofMessage()->getA());
133
134$m = new TestMessage();
135$m->mergeFromString(hex2bin('F80601'));
136assert('f80601' === bin2hex($m->serializeToString()));
137
138// Test create repeated field via array.
139$str_arr = array("abc");
140$m = new TestMessage();
141$m->setRepeatedString($str_arr);
142
143// Test create map field via array.
144$str_arr = array("abc"=>"abc");
145$m = new TestMessage();
146$m->setMapStringString($str_arr);
147
148// Test unset
149$from = new TestMessage();
150TestUtil::setTestMessage($from);
151unset($from);
152
153// Test wellknown
154$from = new \Google\Protobuf\Timestamp();
155$from->setSeconds(1);
156assert(1, $from->getSeconds());
157
158$timestamp = new \Google\Protobuf\Timestamp();
159
160date_default_timezone_set('UTC');
161$from = new DateTime('2011-01-01T15:03:01.012345UTC');
162$timestamp->fromDateTime($from);
163assert($from->format('U') == $timestamp->getSeconds());
164assert(1000 * $from->format('u') == $timestamp->getNanos());
165
166$to = $timestamp->toDateTime();
167assert(\DateTime::class == get_class($to));
168assert($from->format('U') == $to->format('U'));
169
170$from = new \Google\Protobuf\Value();
171$from->setNumberValue(1);
172assert(1, $from->getNumberValue());
173
174// Test discard unknown in message.
175$m = new TestMessage();
176$from = hex2bin('F80601');
177$m->mergeFromString($from);
178$m->discardUnknownFields();
179$to = $m->serializeToString();
180assert("" === bin2hex($to));
181
182// Test clear
183$m = new TestMessage();
184TestUtil::setTestMessage($m);
185$m->clear();
186
187// Test unset map element
188$m = new TestMessage();
189$map = $m->getMapStringString();
190$map[1] = 1;
191unset($map[1]);
192
193// Test descriptor
194$pool = \Google\Protobuf\DescriptorPool::getGeneratedPool();
195$desc = $pool->getDescriptorByClassName("\Foo\TestMessage");
196$field = $desc->getField(1);
197
198$from = new TestMessage();
199$to = new TestMessage();
200TestUtil::setTestMessage($from);
201$to->mergeFrom($from);
202TestUtil::assertTestMessage($to);
203
204// Test decode Any
205// Make sure packed message has been created at least once.
206$packed = new TestMessage();
207
208$m = new TestAny();
209$m->mergeFromJsonString(
210    "{\"any\":" .
211    "  {\"@type\":\"type.googleapis.com/foo.TestMessage\"," .
212    "   \"optionalInt32\":1}}");
213assert("type.googleapis.com/foo.TestMessage" ===
214       $m->getAny()->getTypeUrl());
215assert("0801" === bin2hex($m->getAny()->getValue()));
216