well_known_test.php 10.6 KB
Newer Older
1 2
<?php

3 4 5
require_once('test_base.php');
require_once('test_util.php');

6
use Foo\TestMessage;
7
use Google\Protobuf\Any;
8 9 10 11
use Google\Protobuf\Api;
use Google\Protobuf\BoolValue;
use Google\Protobuf\BytesValue;
use Google\Protobuf\DoubleValue;
12
use Google\Protobuf\Duration;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
use Google\Protobuf\Enum;
use Google\Protobuf\EnumValue;
use Google\Protobuf\Field;
use Google\Protobuf\FieldMask;
use Google\Protobuf\Field_Cardinality;
use Google\Protobuf\Field_Kind;
use Google\Protobuf\FloatValue;
use Google\Protobuf\GPBEmpty;
use Google\Protobuf\Int32Value;
use Google\Protobuf\Int64Value;
use Google\Protobuf\ListValue;
use Google\Protobuf\Method;
use Google\Protobuf\Mixin;
use Google\Protobuf\NullValue;
use Google\Protobuf\Option;
use Google\Protobuf\SourceContext;
use Google\Protobuf\StringValue;
use Google\Protobuf\Struct;
use Google\Protobuf\Syntax;
32
use Google\Protobuf\Timestamp;
33 34 35 36
use Google\Protobuf\Type;
use Google\Protobuf\UInt32Value;
use Google\Protobuf\UInt64Value;
use Google\Protobuf\Value;
37 38

class NotMessage {}
39

40
class WellKnownTest extends TestBase {
41

42
    public function testEmpty()
43 44 45 46 47 48 49
    {
        $msg = new GPBEmpty();
    }

    public function testImportDescriptorProto()
    {
        $msg = new TestImportDescriptorProto();
50 51
    }

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    public function testAny()
    {
        // Create embed message
        $embed = new TestMessage();
        $this->setFields($embed);
        $data = $embed->serializeToString();

        // Set any via normal setter.
        $any = new Any();

        $this->assertSame(
            $any, $any->setTypeUrl("type.googleapis.com/foo.TestMessage"));
        $this->assertSame("type.googleapis.com/foo.TestMessage",
                          $any->getTypeUrl());

        $this->assertSame($any, $any->setValue($data));
        $this->assertSame($data, $any->getValue());

        // Test unpack.
        $msg = $any->unpack();
        $this->assertTrue($msg instanceof TestMessage);
        $this->expectFields($msg);

        // Test pack.
        $any = new Any();
        $any->pack($embed);
        $this->assertSame($data, $any->getValue());
        $this->assertSame("type.googleapis.com/foo.TestMessage", $any->getTypeUrl());

        // Test is.
        $this->assertTrue($any->is(TestMessage::class));
        $this->assertFalse($any->is(Any::class));
    }

    /**
     * @expectedException Exception
     */
    public function testAnyUnpackInvalidTypeUrl()
    {
        $any = new Any();
        $any->setTypeUrl("invalid");
        $any->unpack();
    }

    /**
     * @expectedException Exception
     */
    public function testAnyUnpackMessageNotAdded()
    {
        $any = new Any();
        $any->setTypeUrl("type.googleapis.com/MessageNotAdded");
        $any->unpack();
    }

    /**
     * @expectedException Exception
     */
    public function testAnyUnpackDecodeError()
    {
        $any = new Any();
        $any->setTypeUrl("type.googleapis.com/foo.TestMessage");
        $any->setValue("abc");
        $any->unpack();
    }
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    public function testApi()
    {
        $m = new Api();

        $m->setName("a");
        $this->assertSame("a", $m->getName());

        $m->setMethods([new Method()]);
        $this->assertSame(1, count($m->getMethods()));

        $m->setOptions([new Option()]);
        $this->assertSame(1, count($m->getOptions()));

        $m->setVersion("a");
        $this->assertSame("a", $m->getVersion());

        $m->setSourceContext(new SourceContext());
        $this->assertFalse(is_null($m->getSourceContext()));

        $m->setMixins([new Mixin()]);
        $this->assertSame(1, count($m->getMixins()));

        $m->setSyntax(Syntax::SYNTAX_PROTO2);
        $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax());

        $m = new Method();

        $m->setName("a");
        $this->assertSame("a", $m->getName());

        $m->setRequestTypeUrl("a");
        $this->assertSame("a", $m->getRequestTypeUrl());

        $m->setRequestStreaming(true);
        $this->assertSame(true, $m->getRequestStreaming());

        $m->setResponseTypeUrl("a");
        $this->assertSame("a", $m->getResponseTypeUrl());

        $m->setResponseStreaming(true);
        $this->assertSame(true, $m->getResponseStreaming());

        $m->setOptions([new Option()]);
        $this->assertSame(1, count($m->getOptions()));

        $m = new Mixin();

        $m->setName("a");
        $this->assertSame("a", $m->getName());

        $m->setRoot("a");
        $this->assertSame("a", $m->getRoot());
    }

    public function testEnum()
    {
        $m = new Enum();

        $m->setName("a");
        $this->assertSame("a", $m->getName());

        $m->setEnumvalue([new EnumValue()]);
        $this->assertSame(1, count($m->getEnumvalue()));

        $m->setOptions([new Option()]);
        $this->assertSame(1, count($m->getOptions()));

        $m->setSourceContext(new SourceContext());
        $this->assertFalse(is_null($m->getSourceContext()));

        $m->setSyntax(Syntax::SYNTAX_PROTO2);
        $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax());
    }

    public function testEnumValue()
    {
        $m = new EnumValue();

        $m->setName("a");
        $this->assertSame("a", $m->getName());

        $m->setNumber(1);
        $this->assertSame(1, $m->getNumber());

        $m->setOptions([new Option()]);
        $this->assertSame(1, count($m->getOptions()));
    }

    public function testField()
    {
        $m = new Field();

        $m->setKind(Field_Kind::TYPE_DOUBLE);
        $this->assertSame(Field_Kind::TYPE_DOUBLE, $m->getKind());

        $m->setCardinality(Field_Cardinality::CARDINALITY_OPTIONAL);
        $this->assertSame(Field_Cardinality::CARDINALITY_OPTIONAL, $m->getCardinality());

        $m->setNumber(1);
        $this->assertSame(1, $m->getNumber());

        $m->setName("a");
        $this->assertSame("a", $m->getName());

        $m->setTypeUrl("a");
        $this->assertSame("a", $m->getTypeUrl());

        $m->setOneofIndex(1);
        $this->assertSame(1, $m->getOneofIndex());

        $m->setPacked(true);
        $this->assertSame(true, $m->getPacked());

        $m->setOptions([new Option()]);
        $this->assertSame(1, count($m->getOptions()));

        $m->setJsonName("a");
        $this->assertSame("a", $m->getJsonName());

        $m->setDefaultValue("a");
        $this->assertSame("a", $m->getDefaultValue());
    }

    public function testFieldMask()
    {
        $m = new FieldMask();
        $m->setPaths(["a"]);
        $this->assertSame(1, count($m->getPaths()));
    }

    public function testOption()
    {
        $m = new Option();

        $m->setName("a");
        $this->assertSame("a", $m->getName());

        $m->setValue(new Any());
        $this->assertFalse(is_null($m->getValue()));
    }

    public function testSourceContext()
    {
        $m = new SourceContext();
        $m->setFileName("a");
        $this->assertSame("a", $m->getFileName());
    }

    public function testStruct()
    {
        $m = new ListValue();
        $m->setValues([new Value()]);
        $this->assertSame(1, count($m->getValues()));

        $m = new Value();

        $m->setNullValue(NullValue::NULL_VALUE);
        $this->assertSame(NullValue::NULL_VALUE, $m->getNullValue());
        $this->assertSame("null_value", $m->getKind());

        $m->setNumberValue(1.0);
        $this->assertSame(1.0, $m->getNumberValue());
        $this->assertSame("number_value", $m->getKind());

        $m->setStringValue("a");
        $this->assertSame("a", $m->getStringValue());
        $this->assertSame("string_value", $m->getKind());

        $m->setBoolValue(true);
        $this->assertSame(true, $m->getBoolValue());
        $this->assertSame("bool_value", $m->getKind());

        $m->setStructValue(new Struct());
        $this->assertFalse(is_null($m->getStructValue()));
        $this->assertSame("struct_value", $m->getKind());

        $m->setListValue(new ListValue());
        $this->assertFalse(is_null($m->getListValue()));
        $this->assertSame("list_value", $m->getKind());

        $m = new Struct();
        $m->setFields(array("a"=>new Value()));
        $this->assertSame(1, count($m->getFields()));
    }

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    public function testTimestamp()
    {
        $timestamp = new Timestamp();

        $timestamp->setSeconds(1);
        $timestamp->setNanos(2);
        $this->assertEquals(1, $timestamp->getSeconds());
        $this->assertSame(2, $timestamp->getNanos());

        date_default_timezone_set('UTC');
        $from = new DateTime('2011-01-01T15:03:01.012345UTC');
        $timestamp->fromDateTime($from);
        $this->assertEquals($from->format('U'), $timestamp->getSeconds());
        $this->assertSame(0, $timestamp->getNanos());

        $to = $timestamp->toDateTime();
        $this->assertSame(\DateTime::class, get_class($to));
        $this->assertSame($from->format('U'), $to->format('U'));
    }
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    public function testType()
    {
        $m = new Type();

        $m->setName("a");
        $this->assertSame("a", $m->getName());

        $m->setFields([new Field()]);
        $this->assertSame(1, count($m->getFields()));

        $m->setOneofs(["a"]);
        $this->assertSame(1, count($m->getOneofs()));

        $m->setOptions([new Option()]);
        $this->assertSame(1, count($m->getOptions()));

        $m->setSourceContext(new SourceContext());
        $this->assertFalse(is_null($m->getSourceContext()));

        $m->setSyntax(Syntax::SYNTAX_PROTO2);
        $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax());
    }

345 346 347 348 349 350 351 352 353
    public function testDuration()
    {
        $duration = new Duration();

        $duration->setSeconds(1);
        $duration->setNanos(2);
        $this->assertEquals(1, $duration->getSeconds());
        $this->assertSame(2, $duration->getNanos());
    }
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

    public function testWrappers()
    {
        $m = new DoubleValue();
        $m->setValue(1.0);
        $this->assertSame(1.0, $m->getValue());

        $m = new FloatValue();
        $m->setValue(1.0);
        $this->assertSame(1.0, $m->getValue());

        $m = new Int64Value();
        $m->setValue(1);
        $this->assertEquals(1, $m->getValue());

        $m = new UInt64Value();
        $m->setValue(1);
        $this->assertEquals(1, $m->getValue());

        $m = new Int32Value();
        $m->setValue(1);
        $this->assertSame(1, $m->getValue());

        $m = new UInt32Value();
        $m->setValue(1);
        $this->assertSame(1, $m->getValue());

        $m = new BoolValue();
        $m->setValue(true);
        $this->assertSame(true, $m->getValue());

        $m = new StringValue();
        $m->setValue("a");
        $this->assertSame("a", $m->getValue());

        $m = new BytesValue();
        $m->setValue("a");
        $this->assertSame("a", $m->getValue());
    }
393
}