function testSerializeComplexMessage()
 {
     $book = new Tests\AddressBook();
     $person = new Tests\Person();
     $person->name = 'John Doe';
     $person->id = 2051;
     $person->email = '*****@*****.**';
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '1231231212';
     $phone->type = Tests\Person\PhoneType::HOME;
     $person->addPhone($phone);
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '55512321312';
     $phone->type = Tests\Person\PhoneType::MOBILE;
     $person->addPhone($phone);
     $book->addPerson($person);
     $person = new Tests\Person();
     $person->name = 'Iván Montes';
     $person->id = 23;
     $person->email = '*****@*****.**';
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '3493123123';
     $phone->type = Tests\Person\PhoneType::WORK;
     $person->addPhone($phone);
     $book->addPerson($person);
     $xml = Protobuf::encode($book);
     $xml = simplexml_load_string($xml);
     $this->assertEquals($xml->person[0]->name, "John Doe");
     $this->assertEquals($xml->person[0]->phone[1]->number, "55512321312");
     $this->assertEquals((int) $xml->person[1]->id, 23);
     $this->assertEquals((int) $xml->person[1]->phone[0]->type, 2);
 }
 public function flush()
 {
     $message = new Msg();
     $message->ok = true;
     $message->events = $this->events;
     $this->socketClient->open();
     $this->socketClient->write(Protobuf::encode($message));
     $this->socketClient->close();
 }
 function testSerializeComplexMessage()
 {
     $book = new Tests\AddressBook();
     $person = new Tests\Person();
     $person->name = 'John Doe';
     $person->id = 2051;
     $person->email = '*****@*****.**';
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '1231231212';
     $phone->type = Tests\Person\PhoneType::HOME;
     $person->addPhone($phone);
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '55512321312';
     $phone->type = Tests\Person\PhoneType::MOBILE;
     $person->addPhone($phone);
     $book->addPerson($person);
     $person = new Tests\Person();
     $person->name = 'Iván Montes';
     $person->id = 23;
     $person->email = '*****@*****.**';
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '3493123123';
     $phone->type = Tests\Person\PhoneType::WORK;
     $person->addPhone($phone);
     $book->addPerson($person);
     $txt = Protobuf::encode($book);
     $txt = str_replace(' ', '', $txt);
     $txt = trim($txt);
     $expected = '
         person {
             name: "John Doe"
             id: 2051
             email: "*****@*****.**"
             phone {
                 number: "1231231212"
                 type: 1
             }
             phone {
                 number: "55512321312"
                 type: 0
             }
         }
         person {
             name: "Iv\\u00e1n Montes"
             id: 23
             email: "*****@*****.**"
             phone {
                 number: "3493123123"
                 type: 2
             }
         }
     ';
     $expected = str_replace(' ', '', $expected);
     $expected = trim($expected);
     $this->assertEquals($txt, $expected);
 }
 /**
  * @test
  */
 public function itShouldSendEvents()
 {
     $anEvent = new Event();
     $anotherEvent = new Event();
     $message = $this->aMessage(array($anEvent, $anotherEvent));
     $socketClient = $this->socketClientMock();
     $socketClient->expects($this->once())->method('write')->with(Protobuf::encode($message));
     $client = new Client($socketClient, $this->eventBuilderFactoryMock());
     $client->sendEvent($anEvent);
     $client->sendEvent($anotherEvent);
     $client->flush();
 }
 function testSerializeComplexMessage()
 {
     $book = new Tests\AddressBook();
     $person = new Tests\Person();
     $person->name = 'John Doe';
     $person->id = 2051;
     $person->email = '*****@*****.**';
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '1231231212';
     $phone->type = Tests\Person\PhoneType::HOME;
     $person->addPhone($phone);
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '55512321312';
     $phone->type = Tests\Person\PhoneType::MOBILE;
     $person->addPhone($phone);
     $book->addPerson($person);
     $person = new Tests\Person();
     $person->name = 'Iván Montes';
     $person->id = 23;
     $person->email = '*****@*****.**';
     $phone = new Tests\Person\PhoneNumber();
     $phone->number = '3493123123';
     $phone->type = Tests\Person\PhoneType::WORK;
     $person->addPhone($phone);
     $book->addPerson($person);
     $json = Protobuf::encode($book);
     $expected = '[
          "1",
          [
              [
                  "1234",
                  "John Doe",
                  2051,
                  "*****@*****.**",
                  [
                      ["12","1231231212",1],
                      ["12","55512321312",0]
                  ]
              ],
              [
                  "1234",
                  "Iv\\u00e1n Montes",
                  23,
                  "*****@*****.**",
                  [
                     ["12","3493123123",2]
                  ]
              ]
          ]
      ]';
     $expected = preg_replace('/\\n\\s*/', '', $expected);
     $this->assertEquals($json, $expected);
 }
 function testSerializeAnnotatedMessageWithRepeatedFields()
 {
     $repeated = new \Tests\Annotated\Repeated();
     $repeated->string = array('one', 'two', 'three');
     $bin = Protobuf::encode($repeated);
     $this->assertEquals($bin, '{"string":["one","two","three"]}');
     $repeated = new Tests\Annotated\Repeated();
     $repeated->int = array(1, 2, 3);
     $bin = Protobuf::encode($repeated);
     $this->assertEquals($bin, '{"int":[1,2,3]}');
     $repeated = new Tests\Annotated\Repeated();
     $repeated->nested = array();
     $nested = new Tests\Annotated\RepeatedNested();
     $nested->id = 1;
     $repeated->nested[] = $nested;
     $nested = new Tests\Annotated\RepeatedNested();
     $nested->id = 2;
     $repeated->nested[] = $nested;
     $nested = new Tests\Annotated\RepeatedNested();
     $nested->id = 3;
     $repeated->nested[] = $nested;
     $json = Protobuf::encode($repeated);
     $this->assertEquals($json, '{"nested":[{"id":1},{"id":2},{"id":3}]}');
 }