diff options
Diffstat (limited to 'php/tests/encode_decode_test.php')
-rw-r--r-- | php/tests/encode_decode_test.php | 263 |
1 files changed, 263 insertions, 0 deletions
diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index d36b883e..fdd142f4 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -11,9 +11,156 @@ use Foo\TestMessage\Sub; use Foo\TestPackedMessage; use Foo\TestRandomFieldOrder; use Foo\TestUnpackedMessage; +use Google\Protobuf\DoubleValue; +use Google\Protobuf\FloatValue; +use Google\Protobuf\Int32Value; +use Google\Protobuf\UInt32Value; +use Google\Protobuf\Int64Value; +use Google\Protobuf\UInt64Value; +use Google\Protobuf\BoolValue; +use Google\Protobuf\StringValue; +use Google\Protobuf\BytesValue; +use Google\Protobuf\Value; +use Google\Protobuf\ListValue; +use Google\Protobuf\Struct; class EncodeDecodeTest extends TestBase { + public function testDecodeJsonSimple() + { + $m = new TestMessage(); + $m->mergeFromJsonString("{\"optionalInt32\":1}"); + } + + public function testDecodeTopLevelBoolValue() + { + $m = new BoolValue(); + + $m->mergeFromJsonString("true"); + $this->assertEquals(true, $m->getValue()); + + $m->mergeFromJsonString("false"); + $this->assertEquals(false, $m->getValue()); + } + + public function testEncodeTopLevelBoolValue() + { + $m = new BoolValue(); + $m->setValue(true); + $this->assertSame("true", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelDoubleValue() + { + $m = new DoubleValue(); + $m->mergeFromJsonString("1.5"); + $this->assertEquals(1.5, $m->getValue()); + } + + public function testEncodeTopLevelDoubleValue() + { + $m = new DoubleValue(); + $m->setValue(1.5); + $this->assertSame("1.5", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelFloatValue() + { + $m = new FloatValue(); + $m->mergeFromJsonString("1.5"); + $this->assertEquals(1.5, $m->getValue()); + } + + public function testEncodeTopLevelFloatValue() + { + $m = new FloatValue(); + $m->setValue(1.5); + $this->assertSame("1.5", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelInt32Value() + { + $m = new Int32Value(); + $m->mergeFromJsonString("1"); + $this->assertEquals(1, $m->getValue()); + } + + public function testEncodeTopLevelInt32Value() + { + $m = new Int32Value(); + $m->setValue(1); + $this->assertSame("1", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelUInt32Value() + { + $m = new UInt32Value(); + $m->mergeFromJsonString("1"); + $this->assertEquals(1, $m->getValue()); + } + + public function testEncodeTopLevelUInt32Value() + { + $m = new UInt32Value(); + $m->setValue(1); + $this->assertSame("1", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelInt64Value() + { + $m = new Int64Value(); + $m->mergeFromJsonString("1"); + $this->assertEquals(1, $m->getValue()); + } + + # public function testEncodeTopLevelInt64Value() + # { + # $m = new Int64Value(); + # $m->setValue(1); + # $this->assertSame("\"1\"", $m->serializeToJsonString()); + # } + + public function testDecodeTopLevelUInt64Value() + { + $m = new UInt64Value(); + $m->mergeFromJsonString("1"); + $this->assertEquals(1, $m->getValue()); + } + + # public function testEncodeTopLevelUInt64Value() + # { + # $m = new UInt64Value(); + # $m->setValue(1); + # $this->assertSame("\"1\"", $m->serializeToJsonString()); + # } + + public function testDecodeTopLevelStringValue() + { + $m = new StringValue(); + $m->mergeFromJsonString("\"a\""); + $this->assertSame("a", $m->getValue()); + } + + public function testEncodeTopLevelStringValue() + { + $m = new StringValue(); + $m->setValue("a"); + $this->assertSame("\"a\"", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelBytesValue() + { + $m = new BytesValue(); + $m->mergeFromJsonString("\"YQ==\""); + $this->assertSame("a", $m->getValue()); + } + + public function testEncodeTopLevelBytesValue() + { + $m = new BytesValue(); + $m->setValue("a"); + $this->assertSame("\"YQ==\"", $m->serializeToJsonString()); + } public function testEncode() { @@ -522,4 +669,120 @@ class EncodeDecodeTest extends TestBase $to->mergeFromJsonString($data); $this->expectFields($to); } + + public function testDecodeDuration() + { + $m = new Google\Protobuf\Duration(); + $m->mergeFromJsonString("\"1234.5678s\""); + $this->assertEquals(1234, $m->getSeconds()); + $this->assertEquals(567800000, $m->getNanos()); + } + + public function testEncodeDuration() + { + $m = new Google\Protobuf\Duration(); + $m->setSeconds(1234); + $m->setNanos(999999999); + $this->assertEquals("\"1234.999999999s\"", $m->serializeToJsonString()); + } + + public function testDecodeTimestamp() + { + $m = new Google\Protobuf\Timestamp(); + $m->mergeFromJsonString("\"2000-01-01T00:00:00.123456789Z\""); + $this->assertEquals(946684800, $m->getSeconds()); + $this->assertEquals(123456789, $m->getNanos()); + } + + public function testEncodeTimestamp() + { + $m = new Google\Protobuf\Timestamp(); + $m->setSeconds(946684800); + $m->setNanos(123456789); + $this->assertEquals("\"2000-01-01T00:00:00.123456789Z\"", + $m->serializeToJsonString()); + } + + public function testDecodeTopLevelValue() + { + $m = new Value(); + $m->mergeFromJsonString("\"a\""); + $this->assertSame("a", $m->getStringValue()); + + $m = new Value(); + $m->mergeFromJsonString("1.5"); + $this->assertSame(1.5, $m->getNumberValue()); + + $m = new Value(); + $m->mergeFromJsonString("true"); + $this->assertSame(true, $m->getBoolValue()); + + $m = new Value(); + $m->mergeFromJsonString("null"); + $this->assertSame("null_value", $m->getKind()); + + $m = new Value(); + $m->mergeFromJsonString("[1]"); + $this->assertSame("list_value", $m->getKind()); + + $m = new Value(); + $m->mergeFromJsonString("{\"a\":1}"); + $this->assertSame("struct_value", $m->getKind()); + } + + public function testEncodeTopLevelValue() + { + $m = new Value(); + $m->setStringValue("a"); + $this->assertSame("\"a\"", $m->serializeToJsonString()); + + $m = new Value(); + $m->setNumberValue(1.5); + $this->assertSame("1.5", $m->serializeToJsonString()); + + $m = new Value(); + $m->setBoolValue(true); + $this->assertSame("true", $m->serializeToJsonString()); + + $m = new Value(); + $m->setNullValue(0); + $this->assertSame("null", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelListValue() + { + $m = new ListValue(); + $m->mergeFromJsonString("[1]"); + $this->assertSame(1.0, $m->getValues()[0]->getNumberValue()); + } + + public function testEncodeTopLevelListValue() + { + $m = new ListValue(); + $arr = $m->getValues(); + $sub = new Value(); + $sub->setNumberValue(1.5); + $arr[] = $sub; + $this->assertSame("[1.5]", $m->serializeToJsonString()); + } + + public function testDecodeTopLevelStruct() + { + $m = new Struct(); + $m->mergeFromJsonString("{\"a\":{\"b\":1}}"); + $this->assertSame(1.0, $m->getFields()["a"] + ->getStructValue() + ->getFields()["b"]->getNumberValue()); + } + + public function testEncodeTopLevelStruct() + { + $m = new Struct(); + $map = $m->getFields(); + $sub = new Value(); + $sub->setNumberValue(1.5); + $map["a"] = $sub; + $this->assertSame("{\"a\":1.5}", $m->serializeToJsonString()); + } + } |