aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/tests/well_known_test.php
blob: 4441b13b70269b6bf557da33a564ace891005716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
<?php

require_once('test_base.php');
require_once('test_util.php');

use Google\Protobuf\GPBEmpty;
use Google\Protobuf\Any;
use Google\Protobuf\Timestamp;

use Foo\TestMessage;

class NotMessage {}

class WellKnownTest extends TestBase {

    public function testNone()
    {
        $msg = new GPBEmpty();
    }

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

    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();
    }

    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'));
    }
}