Completed
Push — master ( fd3679...f7d236 )
by
unknown
171:43 queued 171:39
created

testAGraphNodeCanBeConvertedToAString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
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\GraphNode\Birthday;
26
use Facebook\GraphNode\GraphNode;
27
use PHPUnit\Framework\TestCase;
28
29
class GraphNodeTest extends TestCase
30
{
31
    public function testAnEmptyBaseGraphNodeCanInstantiate()
32
    {
33
        $graphNode = new GraphNode();
34
        $backingData = $graphNode->asArray();
35
36
        $this->assertEquals([], $backingData);
37
    }
38
39
    public function testAGraphNodeCanInstantiateWithData()
40
    {
41
        $graphNode = new GraphNode(['foo' => 'bar']);
42
        $backingData = $graphNode->asArray();
43
44
        $this->assertEquals(['foo' => 'bar'], $backingData);
45
    }
46
47
    /**
48
     * @dataProvider provideDateTimeFieldNames
49
     */
50
    public function testCastDateTimeFieldsToDateTime($fieldName)
51
    {
52
        $graphNode = new GraphNode([$fieldName => '1989-11-02']);
53
54
        $this->assertInstanceOf(\DateTime::class, $graphNode->getField($fieldName));
55
    }
56
57
    public static function provideDateTimeFieldNames()
58
    {
59
        yield ['created_time'];
60
        yield ['updated_time'];
61
        yield ['start_time'];
62
        yield ['stop_time'];
63
        yield ['end_time'];
64
        yield ['backdated_time'];
65
        yield ['issued_at'];
66
        yield ['expires_at'];
67
        yield ['publish_time'];
68
    }
69
70
    /**
71
     * @dataProvider provideValidDateTimeFieldValues
72
     */
73
    public function testCastDateTimeFieldValueToDateTime($value, $message, $prettyDate = null)
74
    {
75
        $graphNode = new GraphNode(['created_time' => $value]);
76
77
        $this->assertInstanceOf(\DateTime::class, $graphNode->getField('created_time'), $message);
78
79
        if ($prettyDate !== null) {
80
            $this->assertEquals($prettyDate, $graphNode->getField('created_time')->format(\DateTime::RFC1036));
81
        }
82
    }
83
84
    public static function provideValidDateTimeFieldValues()
85
    {
86
        yield ['1985-10-26T01:21:00+0000', 'Expected the valid ISO 8601 formatted date from Back To The Future to pass.'];
87
        yield ['2014-07-15T03:44:53+0000', 'Expected the valid ISO 8601 formatted date to pass.', 'Tue, 15 Jul 14 03:44:53 +0000'];
88
        yield ['1999-12-31', 'Expected the valid ISO 8601 formatted date to party like it\'s 1999.'];
89
        yield ['2009-05-19T14:39Z', 'Expected the valid ISO 8601 formatted date to pass.'];
90
        yield ['2014-W36', 'Expected the valid ISO 8601 formatted date to pass.'];
91
        yield [1405547020, 'Expected the valid timestamp to pass.', 'Wed, 16 Jul 14 23:43:40 +0200'];
92
    }
93
94
    /**
95
     * @dataProvider provideInvalidDateTimeFieldValues
96
     */
97
    public function testNotCastDateTimeFieldValueToDateTime($value, $message)
98
    {
99
        $graphNode = new GraphNode(['created_time' => $value]);
100
101
        $this->assertNotInstanceOf(\DateTime::class, $graphNode->getField('created_time'), $message);
102
    }
103
104
    public static function provideInvalidDateTimeFieldValues()
105
    {
106
        yield ['2009-05-19T14a39r', 'Expected the invalid ISO 8601 format to fail.'];
107
        yield ['foo_time', 'Expected the invalid ISO 8601 format to fail.'];
108
    }
109
110
    public function testCastBirthdayFieldValueToBirthday()
111
    {
112
        $graphNode = new GraphNode(['birthday' => '11/02/1989']);
113
114
        $this->assertInstanceOf(Birthday::class, $graphNode->getField('birthday'));
115
    }
116
117
    public function testGettingGraphNodeAsAnArrayWillNotUncastTheDateTimeObject()
118
    {
119
        $graphNode = new GraphNode([
120
            'id' => '123',
121
            'created_time' => '2014-07-15T03:44:53+0000',
122
        ]);
123
124
        $graphNodeAsArray = $graphNode->asArray();
125
126
        $this->assertInstanceOf(\DateTime::class, $graphNodeAsArray['created_time']);
127
    }
128
129 View Code Duplication
    public function testGettingAGraphNodeAsAStringWillSafelyRepresentDateTimes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $graphNode = new GraphNode([
132
            'id' => '123',
133
            'created_time' => '2014-07-15T03:44:53+0000',
134
        ]);
135
136
        $graphNodeAsString = (string) $graphNode;
137
138
        $this->assertEquals('{"id":"123","created_time":"2014-07-15T03:44:53+0000"}', $graphNodeAsString);
139
    }
140
141 View Code Duplication
    public function testAnExistingFieldCanBeAccessed()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $graphNode = new GraphNode(['foo' => 'bar']);
144
145
        $field = $graphNode->getField('foo');
146
        $this->assertEquals('bar', $field);
147
    }
148
149 View Code Duplication
    public function testAMissingFieldWillReturnNull()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $graphNode = new GraphNode(['foo' => 'bar']);
152
        $field = $graphNode->getField('baz');
153
154
        $this->assertNull($field, 'Expected the property to return null.');
155
    }
156
157 View Code Duplication
    public function testAMissingFieldWillReturnTheDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $graphNode = new GraphNode(['foo' => 'bar']);
160
161
        $field = $graphNode->getField('baz', 'faz');
162
        $this->assertEquals('faz', $field);
163
    }
164
165 View Code Duplication
    public function testFalseDefaultsWillReturnSameType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        $graphNode = new GraphNode(['foo' => 'bar']);
168
169
        $field = $graphNode->getField('baz', '');
170
        $this->assertSame('', $field);
171
172
        $field = $graphNode->getField('baz', 0);
173
        $this->assertSame(0, $field);
174
175
        $field = $graphNode->getField('baz', false);
176
        $this->assertFalse($field);
177
    }
178
179
    public function testTheFieldsFromTheGraphNodeCanBeReturned()
180
    {
181
        $graphNode = new GraphNode([
182
            'field1' => 'foo',
183
            'field2' => 'bar',
184
            'field3' => 'baz',
185
        ]);
186
187
        $fieldNames = $graphNode->getFieldNames();
188
        $this->assertEquals(['field1', 'field2', 'field3'], $fieldNames);
189
    }
190
191 View Code Duplication
    public function testAGraphNodeCanBeConvertedToAString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    {
193
        $graphNode = new GraphNode(['foo', 'bar', 123]);
194
195
        $graphNodeAsString = (string) $graphNode;
196
197
        $this->assertEquals('["foo","bar",123]', $graphNodeAsString);
198
    }
199
}
200