Passed
Pull Request — master (#899)
by Yassine
33:24
created

testTheFieldsFromTheGraphNodeCanBeReturned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
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\GraphNode;
26
use PHPUnit\Framework\TestCase;
27
28
class GraphNodeTest extends TestCase
29
{
30
    public function testAnEmptyBaseGraphNodeCanInstantiate()
31
    {
32
        $graphNode = new GraphNode();
33
        $backingData = $graphNode->asArray();
34
35
        $this->assertEquals([], $backingData);
36
    }
37
38
    public function testAGraphNodeCanInstantiateWithData()
39
    {
40
        $graphNode = new GraphNode(['foo' => 'bar']);
41
        $backingData = $graphNode->asArray();
42
43
        $this->assertEquals(['foo' => 'bar'], $backingData);
44
    }
45
46
    /**
47
     * @dataProvider provideDateTimeFieldNames
48
     */
49
    public function testCastDateTimeFieldsToDateTime($fieldName)
50
    {
51
        $graphNode = new GraphNode([$fieldName => '1989-11-02']);
52
53
        $this->assertInstanceOf(\DateTime::class, $graphNode->getField($fieldName));
54
    }
55
56
    public static function provideDateTimeFieldNames()
57
    {
58
        yield ['created_time'];
59
        yield ['updated_time'];
60
        yield ['start_time'];
61
        yield ['stop_time'];
62
        yield ['end_time'];
63
        yield ['backdated_time'];
64
        yield ['issued_at'];
65
        yield ['expires_at'];
66
        yield ['publish_time'];
67
    }
68
69
    /**
70
     * @dataProvider provideValidDateTimeFieldValues
71
     */
72
    public function testCastDateTimeFieldValueToDateTime($value, $message, $prettyDate = null)
73
    {
74
        $graphNode = new GraphNode(['created_time' => $value]);
75
76
        $this->assertInstanceOf(\DateTime::class, $graphNode->getField('created_time'), $message);
77
78
        if ($prettyDate !== null) {
79
            $this->assertEquals($prettyDate, $graphNode->getField('created_time')->format(\DateTime::RFC1036));
80
        }
81
    }
82
83
    public static function provideValidDateTimeFieldValues()
84
    {
85
        yield ['1985-10-26T01:21:00+0000', 'Expected the valid ISO 8601 formatted date from Back To The Future to pass.'];
86
        yield ['2014-07-15T03:44:53+0000', 'Expected the valid ISO 8601 formatted date to pass.', 'Tue, 15 Jul 14 03:44:53 +0000'];
87
        yield ['1999-12-31', 'Expected the valid ISO 8601 formatted date to party like it\'s 1999.'];
88
        yield ['2009-05-19T14:39Z', 'Expected the valid ISO 8601 formatted date to pass.'];
89
        yield ['2014-W36', 'Expected the valid ISO 8601 formatted date to pass.'];
90
        yield [1405547020, 'Expected the valid timestamp to pass.', 'Wed, 16 Jul 14 23:43:40 +0200'];
91
    }
92
93
    /**
94
     * @dataProvider provideInvalidDateTimeFieldValues
95
     */
96
    public function testNotCastDateTimeFieldValueToDateTime($value, $message)
97
    {
98
        $graphNode = new GraphNode(['created_time' => $value]);
99
100
        $this->assertNotInstanceOf(\DateTime::class, $graphNode->getField('created_time'), $message);
101
    }
102
103
    public static function provideInvalidDateTimeFieldValues()
104
    {
105
        yield ['2009-05-19T14a39r', 'Expected the invalid ISO 8601 format to fail.'];
106
        yield ['foo_time', 'Expected the invalid ISO 8601 format to fail.'];
107
    }
108
109
    public function testGettingGraphNodeAsAnArrayWillNotUncastTheDateTimeObject()
110
    {
111
        $graphNode = new GraphNode([
112
            'id' => '123',
113
            'created_time' => '2014-07-15T03:44:53+0000',
114
        ]);
115
116
        $graphNodeAsArray = $graphNode->asArray();
117
118
        $this->assertInstanceOf(\DateTime::class, $graphNodeAsArray['created_time']);
119
    }
120
121 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...
122
    {
123
        $graphNode = new GraphNode([
124
            'id' => '123',
125
            'created_time' => '2014-07-15T03:44:53+0000',
126
        ]);
127
128
        $graphNodeAsString = (string) $graphNode;
129
130
        $this->assertEquals('{"id":"123","created_time":"2014-07-15T03:44:53+0000"}', $graphNodeAsString);
131
    }
132
133 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...
134
    {
135
        $graphNode = new GraphNode(['foo' => 'bar']);
136
137
        $field = $graphNode->getField('foo');
138
        $this->assertEquals('bar', $field);
139
    }
140
141 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...
142
    {
143
        $graphNode = new GraphNode(['foo' => 'bar']);
144
        $field = $graphNode->getField('baz');
145
146
        $this->assertNull($field, 'Expected the property to return null.');
147
    }
148
149 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...
150
    {
151
        $graphNode = new GraphNode(['foo' => 'bar']);
152
153
        $field = $graphNode->getField('baz', 'faz');
154
        $this->assertEquals('faz', $field);
155
    }
156
157 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...
158
    {
159
        $graphNode = new GraphNode(['foo' => 'bar']);
160
161
        $field = $graphNode->getField('baz', '');
162
        $this->assertSame('', $field);
163
164
        $field = $graphNode->getField('baz', 0);
165
        $this->assertSame(0, $field);
166
167
        $field = $graphNode->getField('baz', false);
168
        $this->assertFalse($field);
169
    }
170
171
    public function testTheFieldsFromTheGraphNodeCanBeReturned()
172
    {
173
        $graphNode = new GraphNode([
174
            'field1' => 'foo',
175
            'field2' => 'bar',
176
            'field3' => 'baz',
177
        ]);
178
179
        $fieldNames = $graphNode->getFieldNames();
180
        $this->assertEquals(['field1', 'field2', 'field3'], $fieldNames);
181
    }
182
183 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...
184
    {
185
        $graphNode = new GraphNode(['foo', 'bar', 123]);
186
187
        $graphNodeAsString = (string) $graphNode;
188
189
        $this->assertEquals('["foo","bar",123]', $graphNodeAsString);
190
    }
191
192
    public function testAGraphNodeCanBeAccessedAsAnArray()
193
    {
194
        $graphNode = new GraphNode(['foo' => 'bar', 'faz' => 'baz']);
195
196
        $this->assertEquals('bar', $graphNode['foo']);
197
        $this->assertEquals('baz', $graphNode['faz']);
198
    }
199
200 View Code Duplication
    public function testAGraphNodeCanBeIteratedOver()
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...
201
    {
202
        $graphNode = new GraphNode(['foo' => 'bar', 'faz' => 'baz']);
203
204
        $this->assertInstanceOf(\IteratorAggregate::class, $graphNode);
205
206
        $newArray = [];
207
208
        foreach ($graphNode as $k => $v) {
209
            $newArray[$k] = $v;
210
        }
211
212
        $this->assertEquals(['foo' => 'bar', 'faz' => 'baz'], $newArray);
213
    }
214
}
215