GraphUserTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testBirthdaysGetCastToBirthday() 0 19 1
A testPicturePropertiesWillGetCastAsGraphPictureObjects() 0 24 1
A testPagePropertiesWillGetCastAsGraphPageObjects() 0 24 1
A testDatesGetCastToDateTime() 0 13 1
A testUserPropertiesWillGetCastAsGraphUserObjects() 0 18 1
A testBirthdayCastHandlesDateWithoutYear() 0 15 1
A testBirthdayCastHandlesYearWithoutDate() 0 15 1
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
namespace Facebook\Tests\GraphNode;
24
25
use Facebook\Response;
26
use Facebook\GraphNode\GraphNodeFactory;
27
use Facebook\GraphNode\GraphPicture;
28
use Facebook\GraphNode\GraphUser;
29
use Facebook\GraphNode\GraphPage;
30
use Facebook\GraphNode\Birthday;
31
use PHPUnit\Framework\TestCase;
32
use Prophecy\Prophecy\ObjectProphecy;
33
34
class GraphUserTest extends TestCase
35
{
36
    /**
37
     * @var ObjectProphecy|Response
38
     */
39
    protected $responseMock;
40
41
    protected function setUp()
42
    {
43
        $this->responseMock = $this->prophesize(Response::class);
44
    }
45
46
    public function testDatesGetCastToDateTime()
47
    {
48
        $dataFromGraph = [
49
            'updated_time' => '2016-04-26 13:22:05',
50
        ];
51
52
        $this->responseMock->getDecodedBody()->willReturn($dataFromGraph);
53
        $factory = new GraphNodeFactory($this->responseMock->reveal());
54
        $graphNode = $factory->makeGraphUser();
55
56
        $updatedTime = $graphNode->getField('updated_time');
57
58
        $this->assertInstanceOf(\DateTime::class, $updatedTime);
59
    }
60
61
    public function testBirthdaysGetCastToBirthday()
62
    {
63
        $dataFromGraph = [
64
            'birthday' => '1984/01/01',
65
        ];
66
67
        $this->responseMock->getDecodedBody()->willReturn($dataFromGraph);
68
        $factory = new GraphNodeFactory($this->responseMock->reveal());
69
        $graphNode = $factory->makeGraphUser();
70
71
        $birthday = $graphNode->getBirthday();
72
73
        // Test to ensure BC
74
        $this->assertInstanceOf(\DateTime::class, $birthday);
75
76
        $this->assertInstanceOf(Birthday::class, $birthday);
77
        $this->assertTrue($birthday->hasDate());
78
        $this->assertTrue($birthday->hasYear());
79
        $this->assertEquals('1984/01/01', $birthday->format('Y/m/d'));
80
    }
81
82
    public function testBirthdayCastHandlesDateWithoutYear()
83
    {
84
        $dataFromGraph = [
85
            'birthday' => '03/21',
86
        ];
87
88
        $this->responseMock->getDecodedBody()->willReturn($dataFromGraph);
89
        $factory = new GraphNodeFactory($this->responseMock->reveal());
90
        $graphNode = $factory->makeGraphUser();
91
92
        $birthday = $graphNode->getBirthday();
93
94
        $this->assertTrue($birthday->hasDate());
95
        $this->assertFalse($birthday->hasYear());
96
        $this->assertEquals('03/21', $birthday->format('m/d'));
97
    }
98
99
    public function testBirthdayCastHandlesYearWithoutDate()
100
    {
101
        $dataFromGraph = [
102
            'birthday' => '1984',
103
        ];
104
105
        $this->responseMock->getDecodedBody()->willReturn($dataFromGraph);
106
        $factory = new GraphNodeFactory($this->responseMock->reveal());
107
        $graphNode = $factory->makeGraphUser();
108
109
        $birthday = $graphNode->getBirthday();
110
111
        $this->assertTrue($birthday->hasYear());
112
        $this->assertFalse($birthday->hasDate());
113
        $this->assertEquals('1984', $birthday->format('Y'));
114
    }
115
116
    public function testPagePropertiesWillGetCastAsGraphPageObjects()
117
    {
118
        $dataFromGraph = [
119
            'id' => '123',
120
            'name' => 'Foo User',
121
            'hometown' => [
122
                'id' => '1',
123
                'name' => 'Foo Place',
124
            ],
125
            'location' => [
126
                'id' => '2',
127
                'name' => 'Bar Place',
128
            ],
129
        ];
130
131
        $this->responseMock->getDecodedBody()->willReturn($dataFromGraph);
132
        $factory = new GraphNodeFactory($this->responseMock->reveal());
133
        $graphNode = $factory->makeGraphUser();
134
135
        $hometown = $graphNode->getHometown();
136
        $location = $graphNode->getLocation();
137
138
        $this->assertInstanceOf(GraphPage::class, $hometown);
139
        $this->assertInstanceOf(GraphPage::class, $location);
140
    }
141
142
    public function testUserPropertiesWillGetCastAsGraphUserObjects()
143
    {
144
        $dataFromGraph = [
145
            'id' => '123',
146
            'name' => 'Foo User',
147
            'significant_other' => [
148
                'id' => '1337',
149
                'name' => 'Bar User',
150
            ],
151
        ];
152
153
        $this->responseMock->getDecodedBody()->willReturn($dataFromGraph);
154
        $factory = new GraphNodeFactory($this->responseMock->reveal());
155
        $graphNode = $factory->makeGraphUser();
156
157
        $significantOther = $graphNode->getSignificantOther();
158
159
        $this->assertInstanceOf(GraphUser::class, $significantOther);
160
    }
161
162
    public function testPicturePropertiesWillGetCastAsGraphPictureObjects()
163
    {
164
        $dataFromGraph = [
165
            'id' => '123',
166
            'name' => 'Foo User',
167
            'picture' => [
168
                'is_silhouette' => true,
169
                'url' => 'http://foo.bar',
170
                'width' => 200,
171
                'height' => 200,
172
            ],
173
        ];
174
175
        $this->responseMock->getDecodedBody()->willReturn($dataFromGraph);
176
        $factory = new GraphNodeFactory($this->responseMock->reveal());
177
        $graphNode = $factory->makeGraphUser();
178
179
        $Picture = $graphNode->getPicture();
180
181
        $this->assertInstanceOf(GraphPicture::class, $Picture);
182
        $this->assertTrue($Picture->isSilhouette());
183
        $this->assertEquals(200, $Picture->getWidth());
184
        $this->assertEquals(200, $Picture->getHeight());
185
        $this->assertEquals('http://foo.bar', $Picture->getUrl());
186
    }
187
}
188