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