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/generated_class_test.php | |
parent | 7339fc04c49a055ec0688cd0cb24cf7ea64f7783 (diff) | |
download | protobuf-bcbaabe53a8d661f5a473d2a157a4278ad8bf579.tar.gz protobuf-bcbaabe53a8d661f5a473d2a157a4278ad8bf579.tar.bz2 protobuf-bcbaabe53a8d661f5a473d2a157a4278ad8bf579.zip |
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/generated_class_test.php')
-rw-r--r-- | php/tests/generated_class_test.php | 109 |
1 files changed, 109 insertions, 0 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. ######################################################### |