Added proto3 presence support for PHP (#7724)
* WIP.
* Added proto3 presence support for PHP.
* Added compatibility code for old generated code.
diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php
index 5442f50..ea8bd65 100644
--- a/php/tests/encode_decode_test.php
+++ b/php/tests/encode_decode_test.php
@@ -326,6 +326,24 @@
}
+ public function testEncodeDecodeOptional()
+ {
+ $m = new TestMessage();
+ $this->assertFalse($m->hasTrueOptionalInt32());
+ $data = $m->serializeToString();
+ $this->assertSame("", $data);
+
+ $m->setTrueOptionalInt32(0);
+ $this->assertTrue($m->hasTrueOptionalInt32());
+ $data = $m->serializeToString();
+ $this->assertNotSame("", $data);
+
+ $m2 = new TestMessage();
+ $m2->mergeFromString($data);
+ $this->assertTrue($m2->hasTrueOptionalInt32());
+ $this->assertSame(0, $m2->getTrueOptionalInt32());
+ }
+
public function testJsonEncodeDecodeOneof()
{
$m = new TestMessage();
diff --git a/php/tests/generate_protos.sh b/php/tests/generate_protos.sh
index 0c2a555..e83c3c1 100755
--- a/php/tests/generate_protos.sh
+++ b/php/tests/generate_protos.sh
@@ -7,10 +7,10 @@
rm -rf generated
mkdir -p generated
-find proto -type f -name "*.proto"| xargs ../../src/protoc --php_out=generated -I../../src -I.
+find proto -type f -name "*.proto"| xargs ../../src/protoc --experimental_allow_proto3_optional --php_out=generated -I../../src -I.
if [ "$1" = "--aggregate_metadata" ]; then
# Overwrite some of the files to use aggregation.
AGGREGATED_FILES="proto/test.proto proto/test_include.proto proto/test_import_descriptor_proto.proto"
- ../../src/protoc --php_out=aggregate_metadata=foo#bar:generated -I../../src -I. $AGGREGATED_FILES
+ ../../src/protoc --experimental_allow_proto3_optional --php_out=aggregate_metadata=foo#bar:generated -I../../src -I. $AGGREGATED_FILES
fi
diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php
index 053697d..f49c4e9 100644
--- a/php/tests/generated_class_test.php
+++ b/php/tests/generated_class_test.php
@@ -72,6 +72,28 @@
}
#########################################################
+ # Test optional int32 field.
+ #########################################################
+
+ public function testOptionalInt32Field()
+ {
+ $m = new TestMessage();
+
+ $this->assertFalse($m->hasTrueOptionalInt32());
+ $this->assertSame(0, $m->getTrueOptionalInt32());
+
+ // Set integer.
+ $m->setTrueOptionalInt32(MAX_INT32);
+ $this->assertTrue($m->hasTrueOptionalInt32());
+ $this->assertSame(MAX_INT32, $m->getTrueOptionalInt32());
+
+ // Clear integer.
+ $m->clearTrueOptionalInt32();
+ $this->assertFalse($m->hasTrueOptionalInt32());
+ $this->assertSame(0, $m->getTrueOptionalInt32());
+ }
+
+ #########################################################
# Test uint32 field.
#########################################################
diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto
index 9505709..368b19e 100644
--- a/php/tests/proto/test.proto
+++ b/php/tests/proto/test.proto
@@ -34,6 +34,27 @@
bar.TestInclude optional_included_message = 18;
TestMessage recursive = 19;
+ // True optional
+ optional int32 true_optional_int32 = 201;
+ optional int64 true_optional_int64 = 202;
+ optional uint32 true_optional_uint32 = 203;
+ optional uint64 true_optional_uint64 = 204;
+ optional sint32 true_optional_sint32 = 205;
+ optional sint64 true_optional_sint64 = 206;
+ optional fixed32 true_optional_fixed32 = 207;
+ optional fixed64 true_optional_fixed64 = 208;
+ optional sfixed32 true_optional_sfixed32 = 209;
+ optional sfixed64 true_optional_sfixed64 = 210;
+ optional float true_optional_float = 211;
+ optional double true_optional_double = 212;
+ optional bool true_optional_bool = 213;
+ optional string true_optional_string = 214;
+ optional bytes true_optional_bytes = 215;
+
+ optional TestEnum true_optional_enum = 216;
+ optional Sub true_optional_message = 217;
+ optional bar.TestInclude true_optional_included_message = 218;
+
// Repeated
repeated int32 repeated_int32 = 31;
repeated int64 repeated_int64 = 32;
diff --git a/php/tests/test.sh b/php/tests/test.sh
index b10b57f..4beeed5 100755
--- a/php/tests/test.sh
+++ b/php/tests/test.sh
@@ -51,8 +51,8 @@
export ZEND_DONT_UNLOAD_MODULES=1
export USE_ZEND_ALLOC=0
-valgrind --leak-check=yes php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php
-valgrind --leak-check=yes php -d protobuf.keep_descriptor_pool_after_request=1 -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php
+valgrind --suppressions=valgrind.supp --leak-check=yes php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php
+valgrind --suppressions=valgrind.supp --leak-check=yes php -d protobuf.keep_descriptor_pool_after_request=1 -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php
# TODO(teboring): Only for debug (phpunit has memory leak which blocks this beging used by
# regresssion test.)
diff --git a/php/tests/valgrind.supp b/php/tests/valgrind.supp
new file mode 100644
index 0000000..e83b0a3
--- /dev/null
+++ b/php/tests/valgrind.supp
@@ -0,0 +1,12 @@
+{
+ PHP_Equal_Val
+ Memcheck:Cond
+ fun:zend_string_equal_val
+}
+
+{
+ PHP_ScanDir_Tail
+ Memcheck:Cond
+ obj:/usr/bin/php7.3
+ fun:__scandir64_tail
+}