diff options
author | Paul Yang <TeBoring@users.noreply.github.com> | 2017-03-01 10:39:48 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-01 10:39:48 -0800 |
commit | bcbaabe53a8d661f5a473d2a157a4278ad8bf579 (patch) | |
tree | 173e7e07319f10451e2ec290c00cba89a34b647e /php/tests | |
parent | 7339fc04c49a055ec0688cd0cb24cf7ea64f7783 (diff) |
Add mergeFrom method on Message (#2766)
This method merges the contents of the specified message into the
current message. Singular fields that are set in the specified message
overwrite the corresponding fields in the current message. Repeated
fields are appended. Map fields key-value pairs are overritten.
Singular/Oneof sub-messages are recursively merged. All overritten
sub-messages are deep-copied.
Diffstat (limited to 'php/tests')
-rw-r--r-- | php/tests/generated_class_test.php | 109 | ||||
-rw-r--r-- | php/tests/memory_leak_test.php | 5 | ||||
-rw-r--r-- | php/tests/proto/test.proto | 1 | ||||
-rwxr-xr-x | php/tests/test.sh | 8 |
4 files changed, 120 insertions, 3 deletions
diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php index 83ce1139..7f8567b8 100644 --- a/php/tests/generated_class_test.php +++ b/php/tests/generated_class_test.php @@ -622,6 +622,115 @@ class GeneratedClassTest extends TestBase } ######################################################### + # Test mergeFrom method. + ######################################################### + + public function testMessageMergeFrom() + { + $m = new TestMessage(); + $this->setFields($m); + $this->expectFields($m); + $arr = $m->getOptionalMessage()->getB(); + $arr[] = 1; + + $n = new TestMessage(); + + // Singular + $n->setOptionalInt32(100); + $sub1 = new TestMessage_Sub(); + $sub1->setA(101); + $sub1->getB()[] = 102; + $n->setOptionalMessage($sub1); + + // Repeated + $n->getRepeatedInt32()[] = 200; + $n->getRepeatedString()[] = 'abc'; + $sub2 = new TestMessage_Sub(); + $sub2->setA(201); + $n->getRepeatedMessage()[] = $sub2; + + // Map + $n->getMapInt32Int32()[1] = 300; + $n->getMapInt32Int32()[-62] = 301; + $n->getMapStringString()['def'] = 'def'; + $n->getMapInt32Message()[1] = new TestMessage_Sub(); + $n->getMapInt32Message()[1]->setA(302); + $n->getMapInt32Message()[2] = new TestMessage_Sub(); + $n->getMapInt32Message()[2]->setA(303); + + $m->mergeFrom($n); + + $this->assertSame(100, $m->getOptionalInt32()); + $this->assertSame(42, $m->getOptionalUint32()); + $this->assertSame(101, $m->getOptionalMessage()->getA()); + $this->assertSame(2, count($m->getOptionalMessage()->getB())); + $this->assertSame(1, $m->getOptionalMessage()->getB()[0]); + $this->assertSame(102, $m->getOptionalMessage()->getB()[1]); + + $this->assertSame(3, count($m->getRepeatedInt32())); + $this->assertSame(200, $m->getRepeatedInt32()[2]); + $this->assertSame(2, count($m->getRepeatedUint32())); + $this->assertSame(3, count($m->getRepeatedString())); + $this->assertSame('abc', $m->getRepeatedString()[2]); + $this->assertSame(3, count($m->getRepeatedMessage())); + $this->assertSame(201, $m->getRepeatedMessage()[2]->getA()); + + $this->assertSame(2, count($m->getMapInt32Int32())); + $this->assertSame(300, $m->getMapInt32Int32()[1]); + $this->assertSame(301, $m->getMapInt32Int32()[-62]); + $this->assertSame(1, count($m->getMapUint32Uint32())); + $this->assertSame(2, count($m->getMapStringString())); + $this->assertSame('def', $m->getMapStringString()['def']); + + $this->assertSame(2, count($m->getMapInt32Message())); + $this->assertSame(302, $m->getMapInt32Message()[1]->getA()); + $this->assertSame(303, $m->getMapInt32Message()[2]->getA()); + + $this->assertSame("", $m->getMyOneof()); + + // Check sub-messages are copied by value. + $n->getOptionalMessage()->setA(-101); + $this->assertSame(101, $m->getOptionalMessage()->getA()); + $n->getRepeatedMessage()[0]->setA(-201); + $this->assertSame(201, $m->getRepeatedMessage()[2]->getA()); + $n->getMapInt32Message()[1]->setA(-302); + $this->assertSame(302, $m->getMapInt32Message()[1]->getA()); + + // Test merge oneof. + $m = new TestMessage(); + + $n = new TestMessage(); + $n->setOneofInt32(1); + $m->mergeFrom($n); + $this->assertSame(1, $m->getOneofInt32()); + + $sub = new TestMessage_Sub(); + $n->setOneofMessage($sub); + $n->getOneofMessage()->setA(400); + $m->mergeFrom($n); + $this->assertSame(400, $m->getOneofMessage()->getA()); + $n->getOneofMessage()->setA(-400); + $this->assertSame(400, $m->getOneofMessage()->getA()); + + // Test all fields + $m = new TestMessage(); + $n = new TestMessage(); + $this->setFields($m); + $n->mergeFrom($m); + $this->expectFields($n); + } + + /** + * @expectedException PHPUnit_Framework_Error + */ + public function testMessageMergeFromInvalidTypeFail() + { + $m = new TestMessage(); + $n = new TestMessage_Sub(); + $m->mergeFrom($n); + } + + ######################################################### # Test message/enum without namespace. ######################################################### diff --git a/php/tests/memory_leak_test.php b/php/tests/memory_leak_test.php index af327273..d4776d6f 100644 --- a/php/tests/memory_leak_test.php +++ b/php/tests/memory_leak_test.php @@ -79,3 +79,8 @@ $data = $m->encode(); $n = new TestMessage(); $n->decode($data); assert(1 === $n->getOneofMessage()->getA()); + +$from = new TestMessage(); +$to = new TestMessage(); +TestUtil::setTestMessage($from); +$to->mergeFrom($from); diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto index 594aee4d..c971df21 100644 --- a/php/tests/proto/test.proto +++ b/php/tests/proto/test.proto @@ -90,6 +90,7 @@ message TestMessage { message Sub { int32 a = 1; + repeated int32 b = 2; } // Reserved for non-existing field test. diff --git a/php/tests/test.sh b/php/tests/test.sh index 3635d86c..fc3f0186 100755 --- a/php/tests/test.sh +++ b/php/tests/test.sh @@ -19,7 +19,9 @@ do echo "" done -# Make sure to run the memory test in debug mode. -php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php +# # Make sure to run the memory test in debug mode. +# php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php -USE_ZEND_ALLOC=0 valgrind --leak-check=yes php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php +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 |