Completed
Pull Request — master (#899)
by Yassine
60:59
created

testAMissingPropertyWillReturnTheDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 6
loc 6
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
    public function testDatesThatShouldBeCastAsDateTimeObjectsAreDetected()
47
    {
48
        $graphNode = new GraphNode();
49
50
        // Should pass
51
        $shouldPass = $graphNode->isIso8601DateString('1985-10-26T01:21:00+0000');
52
        $this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date from Back To The Future to pass.');
53
54
        $shouldPass = $graphNode->isIso8601DateString('1999-12-31');
55
        $this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to party like it\'s 1999.');
56
57
        $shouldPass = $graphNode->isIso8601DateString('2009-05-19T14:39Z');
58
        $this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to pass.');
59
60
        $shouldPass = $graphNode->isIso8601DateString('2014-W36');
61
        $this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to pass.');
62
63
        // Should fail
64
        $shouldFail = $graphNode->isIso8601DateString('2009-05-19T14a39r');
65
        $this->assertFalse($shouldFail, 'Expected the invalid ISO 8601 format to fail.');
66
67
        $shouldFail = $graphNode->isIso8601DateString('foo_time');
68
        $this->assertFalse($shouldFail, 'Expected the invalid ISO 8601 format to fail.');
69
    }
70
71 View Code Duplication
    public function testATimeStampCanBeConvertedToADateTimeObject()
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...
72
    {
73
        $someTimeStampFromGraph = 1405547020;
74
        $graphNode = new GraphNode();
75
        $dateTime = $graphNode->castToDateTime($someTimeStampFromGraph);
76
        $prettyDate = $dateTime->format(\DateTime::RFC1036);
77
        $timeStamp = $dateTime->getTimestamp();
78
79
        $this->assertInstanceOf(\DateTime::class, $dateTime);
80
        $this->assertEquals('Wed, 16 Jul 14 23:43:40 +0200', $prettyDate);
81
        $this->assertEquals(1405547020, $timeStamp);
82
    }
83
84 View Code Duplication
    public function testAGraphDateStringCanBeConvertedToADateTimeObject()
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...
85
    {
86
        $someDateStringFromGraph = '2014-07-15T03:44:53+0000';
87
        $graphNode = new GraphNode();
88
        $dateTime = $graphNode->castToDateTime($someDateStringFromGraph);
89
        $prettyDate = $dateTime->format(\DateTime::RFC1036);
90
        $timeStamp = $dateTime->getTimestamp();
91
92
        $this->assertInstanceOf(\DateTime::class, $dateTime);
93
        $this->assertEquals('Tue, 15 Jul 14 03:44:53 +0000', $prettyDate);
94
        $this->assertEquals(1405395893, $timeStamp);
95
    }
96
97
    public function testUncastingAGraphNodeWillUncastTheDateTimeObject()
98
    {
99
        $graphNodeOne = new GraphNode(['foo', 'bar']);
100
        $graphNodeTwo = new GraphNode([
101
            'id' => '123',
102
            'date' => new \DateTime('2014-07-15T03:44:53+0000'),
103
            'some_collection' => $graphNodeOne,
104
        ]);
105
106
        $uncastArray = $graphNodeTwo->uncastFields();
107
108
        $this->assertEquals([
109
            'id' => '123',
110
            'date' => '2014-07-15T03:44:53+0000',
111
            'some_collection' => ['foo', 'bar'],
112
        ], $uncastArray);
113
    }
114
115
    public function testGettingGraphNodeAsAnArrayWillNotUncastTheDateTimeObject()
116
    {
117
        $graphNode = new GraphNode([
118
            'id' => '123',
119
            'date' => new \DateTime('2014-07-15T03:44:53+0000'),
120
        ]);
121
122
        $graphNodeAsArray = $graphNode->asArray();
123
124
        $this->assertInstanceOf(\DateTime::class, $graphNodeAsArray['date']);
125
    }
126
127 View Code Duplication
    public function testReturningACollectionAsJasonWillSafelyRepresentDateTimes()
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...
128
    {
129
        $graphNode = new GraphNode([
130
            'id' => '123',
131
            'date' => new \DateTime('2014-07-15T03:44:53+0000'),
132
        ]);
133
134
        $graphNodeAsString = $graphNode->asJson();
135
136
        $this->assertEquals('{"id":"123","date":"2014-07-15T03:44:53+0000"}', $graphNodeAsString);
137
    }
138
139 View Code Duplication
    public function testAnExistingPropertyCanBeAccessed()
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...
140
    {
141
        $graphNode = new GraphNode(['foo' => 'bar']);
142
143
        $field = $graphNode->getField('foo');
144
        $this->assertEquals('bar', $field);
145
    }
146
147 View Code Duplication
    public function testAMissingPropertyWillReturnNull()
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...
148
    {
149
        $graphNode = new GraphNode(['foo' => 'bar']);
150
        $field = $graphNode->getField('baz');
151
152
        $this->assertNull($field, 'Expected the property to return null.');
153
    }
154
155 View Code Duplication
    public function testAMissingPropertyWillReturnTheDefault()
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...
156
    {
157
        $graphNode = new GraphNode(['foo' => 'bar']);
158
159
        $field = $graphNode->getField('baz', 'faz');
160
        $this->assertEquals('faz', $field);
161
    }
162
163 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...
164
    {
165
        $graphNode = new GraphNode(['foo' => 'bar']);
166
167
        $field = $graphNode->getField('baz', '');
168
        $this->assertSame('', $field);
169
170
        $field = $graphNode->getField('baz', 0);
171
        $this->assertSame(0, $field);
172
173
        $field = $graphNode->getField('baz', false);
174
        $this->assertFalse($field);
175
    }
176
177
    public function testTheKeysFromTheCollectionCanBeReturned()
178
    {
179
        $graphNode = new GraphNode([
180
            'key1' => 'foo',
181
            'key2' => 'bar',
182
            'key3' => 'baz',
183
        ]);
184
185
        $fieldNames = $graphNode->getFieldNames();
186
        $this->assertEquals(['key1', 'key2', 'key3'], $fieldNames);
187
    }
188
189
    public function testAnArrayCanBeInjectedViaTheConstructor()
190
    {
191
        $graphNode = new GraphNode(['foo', 'bar']);
192
        $this->assertEquals(['foo', 'bar'], $graphNode->asArray());
193
    }
194
195 View Code Duplication
    public function testACollectionCanBeConvertedToProperJson()
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...
196
    {
197
        $graphNode = new GraphNode(['foo', 'bar', 123]);
198
199
        $graphNodeAsString = $graphNode->asJson();
200
201
        $this->assertEquals('["foo","bar",123]', $graphNodeAsString);
202
    }
203
204
    public function testACollectionCanBeCounted()
205
    {
206
        $graphNode = new GraphNode(['foo', 'bar', 'baz']);
207
208
        $graphNodeCount = count($graphNode);
209
210
        $this->assertEquals(3, $graphNodeCount);
211
    }
212
213
    public function testACollectionCanBeAccessedAsAnArray()
214
    {
215
        $graphNode = new GraphNode(['foo' => 'bar', 'faz' => 'baz']);
216
217
        $this->assertEquals('bar', $graphNode['foo']);
218
        $this->assertEquals('baz', $graphNode['faz']);
219
    }
220
221 View Code Duplication
    public function testACollectionCanBeIteratedOver()
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...
222
    {
223
        $graphNode = new GraphNode(['foo' => 'bar', 'faz' => 'baz']);
224
225
        $this->assertInstanceOf(\IteratorAggregate::class, $graphNode);
226
227
        $newArray = [];
228
229
        foreach ($graphNode as $k => $v) {
230
            $newArray[$k] = $v;
231
        }
232
233
        $this->assertEquals(['foo' => 'bar', 'faz' => 'baz'], $newArray);
234
    }
235
}
236