Skip to content

Commit

Permalink
Remove array access from GraphNode
Browse files Browse the repository at this point in the history
  • Loading branch information
yguedidi committed Dec 19, 2017
1 parent c0e5c24 commit d2b7c59
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 84 deletions.
55 changes: 1 addition & 54 deletions src/GraphNode/GraphNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @package Facebook
*/
class GraphNode implements \ArrayAccess, \IteratorAggregate
class GraphNode implements \IteratorAggregate
{
/**
* @var array maps object key names to Graph object types
Expand Down Expand Up @@ -112,59 +112,6 @@ public function getIterator()
return new \ArrayIterator($this->fields);
}

/**
* Determine if an item exists at an offset.
*
* @param mixed $key
*
* @return bool
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->fields);
}

/**
* Get an item at a given offset.
*
* @param mixed $key
*
* @return mixed
*/
public function offsetGet($key)
{
return $this->fields[$key];
}

/**
* Set the item at a given offset.
*
* @param mixed $key
* @param mixed $value
*
* @return void
*/
public function offsetSet($key, $value)
{
if (is_null($key)) {
$this->fields[] = $value;
} else {
$this->fields[$key] = $value;
}
}

/**
* Unset the item at a given offset.
*
* @param string $key
*
* @return void
*/
public function offsetUnset($key)
{
unset($this->fields[$key]);
}

/**
* Convert the collection to its string representation.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/FacebookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function testPaginationReturnsProperResponse()
$nextPage = $fb->next($graphEdge);
$this->assertInstanceOf(GraphEdge::class, $nextPage);
$this->assertInstanceOf(GraphUser::class, $nextPage[0]);
$this->assertEquals('Foo', $nextPage[0]['name']);
$this->assertEquals('Foo', $nextPage[0]->getField('name'));

$lastResponse = $fb->getLastResponse();
$this->assertInstanceOf(Response::class, $lastResponse);
Expand Down
23 changes: 7 additions & 16 deletions tests/GraphNode/GraphEdgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,20 @@ public function testCanMapOverNodes()
$graphEdge = new GraphEdge(
$this->request,
[
new GraphNode(['name' => 'dummy']),
new GraphNode(['name' => 'dummy']),
new GraphNode(['name' => 'dummy1']),
new GraphNode(['name' => 'dummy2']),
],
['paging' => $this->pagination],
'/1234567890/likes'
);

$graphEdge = $graphEdge->map(function (GraphNode $node) {
$node['name'] = str_replace('dummy', 'foo', $node['name']);
return $node;
});
$output = '';

$graphEdgeToCompare = new GraphEdge(
$this->request,
[
new GraphNode(['name' => 'foo']),
new GraphNode(['name' => 'foo'])
],
['paging' => $this->pagination],
'/1234567890/likes'
);
$graphEdge->map(function (GraphNode $node) use (&$output) {
$output .= $node->getField('name');
});

$this->assertEquals($graphEdgeToCompare, $graphEdge);
$this->assertEquals('dummy1dummy2', $output);
}

public function testAnExistingPropertyCanBeAccessed()
Expand Down
22 changes: 11 additions & 11 deletions tests/GraphNode/GraphNodeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,19 @@ public function testAGraphEdgeWillBeCastRecursively()

// Story
$storyObject = $graphNode[0];
$this->assertInstanceOf(GraphNode::class, $storyObject['from']);
$this->assertInstanceOf(GraphEdge::class, $storyObject['likes']);
$this->assertInstanceOf(GraphEdge::class, $storyObject['comments']);
$this->assertInstanceOf(GraphNode::class, $storyObject->getField('from'));
$this->assertInstanceOf(GraphEdge::class, $storyObject->getField('likes'));
$this->assertInstanceOf(GraphEdge::class, $storyObject->getField('comments'));

// Story Comments
$storyComments = $storyObject['comments'];
$storyComments = $storyObject->getField('comments');
$firstStoryComment = $storyComments[0];
$this->assertInstanceOf(GraphNode::class, $firstStoryComment['from']);
$this->assertInstanceOf(GraphNode::class, $firstStoryComment->getField('from'));

// Message
$messageObject = $graphNode[1];
$this->assertInstanceOf(GraphEdge::class, $messageObject['to']);
$toUsers = $messageObject['to'];
$this->assertInstanceOf(GraphEdge::class, $messageObject->getField('to'));
$toUsers = $messageObject->getField('to');
$this->assertInstanceOf(GraphNode::class, $toUsers[0]);
}

Expand Down Expand Up @@ -422,10 +422,10 @@ public function testAGraphEdgeWillGenerateTheProperParentGraphEdges()
$factory = new GraphNodeFactory($res);
$graphEdge = $factory->makeGraphEdge();
$topGraphEdge = $graphEdge->getParentGraphEdge();
$childGraphEdgeOne = $graphEdge[0]['likes']->getParentGraphEdge();
$childGraphEdgeTwo = $graphEdge[1]['likes']->getParentGraphEdge();
$childGraphEdgeThree = $graphEdge[1]['photos']->getParentGraphEdge();
$childGraphEdgeFour = $graphEdge[1]['photos'][0]['likes']->getParentGraphEdge();
$childGraphEdgeOne = $graphEdge[0]->getField('likes')->getParentGraphEdge();
$childGraphEdgeTwo = $graphEdge[1]->getField('likes')->getParentGraphEdge();
$childGraphEdgeThree = $graphEdge[1]->getField('photos')->getParentGraphEdge();
$childGraphEdgeFour = $graphEdge[1]->getField('photos')[0]->getField('likes')->getParentGraphEdge();

$this->assertNull($topGraphEdge);
$this->assertEquals('/111/likes', $childGraphEdgeOne);
Expand Down
4 changes: 2 additions & 2 deletions tests/GraphNode/GraphNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ public function testAGraphNodeCanBeAccessedAsAnArray()
{
$graphNode = new GraphNode(['foo' => 'bar', 'faz' => 'baz']);

$this->assertEquals('bar', $graphNode['foo']);
$this->assertEquals('baz', $graphNode['faz']);
$this->assertEquals('bar', $graphNode->getField('foo'));
$this->assertEquals('baz', $graphNode->getField('faz'));
}

public function testAGraphNodeCanBeIteratedOver()
Expand Down

0 comments on commit d2b7c59

Please sign in to comment.