示例#1
0
 public function testFormatComplexMessage()
 {
     $book = new AddressBook();
     $person = new Person();
     $person->setId(2051);
     $person->setName('John Doe');
     $person->setEmail('*****@*****.**');
     $phone = new Person\PhoneNumber();
     $phone->setNumber('1231231212');
     $phone->setType(Person\PhoneType::HOME());
     $person->addPhone($phone);
     $phone = new Person\PhoneNumber();
     $phone->setNumber('55512321312');
     $phone->setType(Person\PhoneType::MOBILE());
     $person->addPhone($phone);
     $book->addPerson($person);
     $person = new Person();
     $person->setId(23);
     $person->setName('Iván Montes');
     $person->setEmail('*****@*****.**');
     $phone = new Person\PhoneNumber();
     $phone->setNumber('3493123123');
     $phone->setType(Person\PhoneType::WORK());
     $person->addPhone($phone);
     $book->addPerson($person);
     $expected = $this->getProtoContent('addressbook.txt');
     $actual = $this->textFormat->encodeMessage($book);
     $this->assertEquals($expected, (string) $actual);
 }
 public function testReadComplexMessage()
 {
     $binary = $this->getProtoContent('addressbook.bin');
     $complex = AddressBook::fromStream($binary);
     $this->assertInstanceOf(AddressBook::CLASS, $complex);
     $this->assertCount(2, $complex->getPersonList());
     $person1 = $complex->getPersonList()[0];
     $person2 = $complex->getPersonList()[1];
     $this->assertInstanceOf(Person::CLASS, $person1);
     $this->assertInstanceOf(Person::CLASS, $person2);
     $this->assertEquals($person1->getId(), 2051);
     $this->assertEquals($person1->getName(), 'John Doe');
     $this->assertEquals($person2->getId(), 23);
     $this->assertEquals($person2->getName(), 'Iván Montes');
     $this->assertCount(2, $person1->getPhoneList());
     $this->assertCount(1, $person2->getPhoneList());
     $this->assertEquals($person1->getPhoneList()[0]->getNumber(), '1231231212');
     $this->assertEquals($person1->getPhoneList()[0]->getType(), PhoneType::HOME());
     $this->assertEquals($person1->getPhoneList()[1]->getNumber(), '55512321312');
     $this->assertEquals($person1->getPhoneList()[1]->getType(), PhoneType::MOBILE());
     $this->assertEquals($person2->getPhoneList()[0]->getNumber(), '3493123123');
     $this->assertEquals($person2->getPhoneList()[0]->getType(), PhoneType::WORK());
 }