diff options
Diffstat (limited to 'lib/facebook-graph-sdk/tests/GraphNodes')
13 files changed, 1692 insertions, 0 deletions
diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/AbstractGraphNode.php b/lib/facebook-graph-sdk/tests/GraphNodes/AbstractGraphNode.php new file mode 100644 index 0000000..1c4ce15 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/AbstractGraphNode.php @@ -0,0 +1,51 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Mockery as m; +use Facebook\GraphNodes\GraphNodeFactory; + +abstract class AbstractGraphNode extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Facebook\FacebookResponse|\Mockery\MockInterface + */ + protected $responseMock; + + public function setUp() + { + parent::setUp(); + $this->responseMock = m::mock('\Facebook\FacebookResponse'); + } + + protected function makeFactoryWithData($data) + { + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($data); + + return new GraphNodeFactory($this->responseMock); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/CollectionTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/CollectionTest.php new file mode 100755 index 0000000..af3eba8 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/CollectionTest.php @@ -0,0 +1,125 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Facebook\GraphNodes\Collection; + +class CollectionTest extends \PHPUnit_Framework_TestCase +{ + + public function testAnExistingPropertyCanBeAccessed() + { + $graphNode = new Collection(['foo' => 'bar']); + + $field = $graphNode->getField('foo'); + $this->assertEquals('bar', $field); + + // @todo v6: Remove this assertion + $property = $graphNode->getProperty('foo'); + $this->assertEquals('bar', $property); + } + + public function testAMissingPropertyWillReturnNull() + { + $graphNode = new Collection(['foo' => 'bar']); + $field = $graphNode->getField('baz'); + + $this->assertNull($field, 'Expected the property to return null.'); + } + + public function testAMissingPropertyWillReturnTheDefault() + { + $graphNode = new Collection(['foo' => 'bar']); + + $field = $graphNode->getField('baz', 'faz'); + $this->assertEquals('faz', $field); + + // @todo v6: Remove this assertion + $property = $graphNode->getProperty('baz', 'faz'); + $this->assertEquals('faz', $property); + } + + public function testTheKeysFromTheCollectionCanBeReturned() + { + $graphNode = new Collection([ + 'key1' => 'foo', + 'key2' => 'bar', + 'key3' => 'baz', + ]); + + $fieldNames = $graphNode->getFieldNames(); + $this->assertEquals(['key1', 'key2', 'key3'], $fieldNames); + + // @todo v6: Remove this assertion + $propertyNames = $graphNode->getPropertyNames(); + $this->assertEquals(['key1', 'key2', 'key3'], $propertyNames); + } + + public function testAnArrayCanBeInjectedViaTheConstructor() + { + $collection = new Collection(['foo', 'bar']); + $this->assertEquals(['foo', 'bar'], $collection->asArray()); + } + + public function testACollectionCanBeConvertedToProperJson() + { + $collection = new Collection(['foo', 'bar', 123]); + + $collectionAsString = $collection->asJson(); + + $this->assertEquals('["foo","bar",123]', $collectionAsString); + } + + public function testACollectionCanBeCounted() + { + $collection = new Collection(['foo', 'bar', 'baz']); + + $collectionCount = count($collection); + + $this->assertEquals(3, $collectionCount); + } + + public function testACollectionCanBeAccessedAsAnArray() + { + $collection = new Collection(['foo' => 'bar', 'faz' => 'baz']); + + $this->assertEquals('bar', $collection['foo']); + $this->assertEquals('baz', $collection['faz']); + } + + public function testACollectionCanBeIteratedOver() + { + $collection = new Collection(['foo' => 'bar', 'faz' => 'baz']); + + $this->assertInstanceOf('IteratorAggregate', $collection); + + $newArray = []; + + foreach ($collection as $k => $v) { + $newArray[$k] = $v; + } + + $this->assertEquals(['foo' => 'bar', 'faz' => 'baz'], $newArray); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphAchievementTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphAchievementTest.php new file mode 100644 index 0000000..d5e276e --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphAchievementTest.php @@ -0,0 +1,117 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +class GraphAchievementTest extends AbstractGraphNode +{ + + public function testIdIsString() + { + $dataFromGraph = [ + 'id' => '1337' + ]; + + $factory = $this->makeFactoryWithData($dataFromGraph); + $graphNode = $factory->makeGraphAchievement(); + + $id = $graphNode->getId(); + + $this->assertEquals($dataFromGraph['id'], $id); + } + + public function testTypeIsAlwaysString() + { + $dataFromGraph = [ + 'id' => '1337' + ]; + + $factory = $this->makeFactoryWithData($dataFromGraph); + $graphNode = $factory->makeGraphAchievement(); + + $type = $graphNode->getType(); + + $this->assertEquals('game.achievement', $type); + } + + public function testNoFeedStoryIsBoolean() + { + $dataFromGraph = [ + 'no_feed_story' => (rand(0, 1) == 1) + ]; + + $factory = $this->makeFactoryWithData($dataFromGraph); + $graphNode = $factory->makeGraphAchievement(); + + $isNoFeedStory = $graphNode->isNoFeedStory(); + + $this->assertTrue(is_bool($isNoFeedStory)); + } + + public function testDatesGetCastToDateTime() + { + $dataFromGraph = [ + 'publish_time' => '2014-07-15T03:54:34+0000' + ]; + + $factory = $this->makeFactoryWithData($dataFromGraph); + $graphNode = $factory->makeGraphAchievement(); + + $publishTime = $graphNode->getPublishTime(); + + $this->assertInstanceOf('DateTime', $publishTime); + } + + public function testFromGetsCastAsGraphUser() + { + $dataFromGraph = [ + 'from' => [ + 'id' => '1337', + 'name' => 'Foo McBar' + ] + ]; + + $factory = $this->makeFactoryWithData($dataFromGraph); + $graphNode = $factory->makeGraphAchievement(); + + $from = $graphNode->getFrom(); + + $this->assertInstanceOf('\Facebook\GraphNodes\GraphUser', $from); + } + + public function testApplicationGetsCastAsGraphApplication() + { + $dataFromGraph = [ + 'application' => [ + 'id' => '1337' + ] + ]; + + $factory = $this->makeFactoryWithData($dataFromGraph); + $graphNode = $factory->makeGraphAchievement(); + + $app = $graphNode->getApplication(); + + $this->assertInstanceOf('\Facebook\GraphNodes\GraphApplication', $app); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphAlbumTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphAlbumTest.php new file mode 100644 index 0000000..f7a5521 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphAlbumTest.php @@ -0,0 +1,109 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Mockery as m; +use Facebook\GraphNodes\GraphNodeFactory; + +class GraphAlbumTest extends \PHPUnit_Framework_TestCase +{ + + /** + * @var \Facebook\FacebookResponse + */ + protected $responseMock; + + public function setUp() + { + $this->responseMock = m::mock('\\Facebook\\FacebookResponse'); + } + + public function testDatesGetCastToDateTime() + { + $dataFromGraph = [ + 'created_time' => '2014-07-15T03:54:34+0000', + 'updated_time' => '2014-07-12T01:24:09+0000', + 'id' => '123', + 'name' => 'Bar', + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphAlbum(); + + $createdTime = $graphNode->getCreatedTime(); + $updatedTime = $graphNode->getUpdatedTime(); + + $this->assertInstanceOf('DateTime', $createdTime); + $this->assertInstanceOf('DateTime', $updatedTime); + } + + public function testFromGetsCastAsGraphUser() + { + $dataFromGraph = [ + 'id' => '123', + 'from' => [ + 'id' => '1337', + 'name' => 'Foo McBar', + ], + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphAlbum(); + + $from = $graphNode->getFrom(); + + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphUser', $from); + } + + public function testPlacePropertyWillGetCastAsGraphPageObject() + { + $dataFromGraph = [ + 'id' => '123', + 'name' => 'Foo Album', + 'place' => [ + 'id' => '1', + 'name' => 'For Bar Place', + ] + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphAlbum(); + + $place = $graphNode->getPlace(); + + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphPage', $place); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphEdgeTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphEdgeTest.php new file mode 100644 index 0000000..4e70f52 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphEdgeTest.php @@ -0,0 +1,120 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Facebook\FacebookApp; +use Facebook\FacebookRequest; +use Facebook\GraphNodes\GraphEdge; + +class GraphEdgeTest extends \PHPUnit_Framework_TestCase +{ + + /** + * @var \Facebook\FacebookRequest + */ + protected $request; + + protected $basePagination = [ + 'next' => 'https://graph.facebook.com/v7.12/998899/photos?pretty=0&limit=25&after=foo_after_cursor', + 'previous' => 'https://graph.facebook.com/v7.12/998899/photos?pretty=0&limit=25&before=foo_before_cursor', + ]; + protected $cursorPagination = [ + 'cursors' => [ + 'after' => 'bar_after_cursor', + 'before' => 'bar_before_cursor', + ], + ]; + + public function setUp() + { + $app = new FacebookApp('123', 'foo_app_secret'); + $this->request = new FacebookRequest( + $app, + 'foo_token', + 'GET', + '/me/photos?keep=me', + ['foo' => 'bar'], + 'foo_eTag', + 'v1337' + ); + } + + /** + * @expectedException \Facebook\Exceptions\FacebookSDKException + */ + public function testNonGetRequestsWillThrow() + { + $this->request->setMethod('POST'); + $graphEdge = new GraphEdge($this->request); + $graphEdge->validateForPagination(); + } + + public function testCanReturnGraphGeneratedPaginationEndpoints() + { + $graphEdge = new GraphEdge( + $this->request, + [], + ['paging' => $this->basePagination] + ); + $nextPage = $graphEdge->getPaginationUrl('next'); + $prevPage = $graphEdge->getPaginationUrl('previous'); + + $this->assertEquals('/998899/photos?pretty=0&limit=25&after=foo_after_cursor', $nextPage); + $this->assertEquals('/998899/photos?pretty=0&limit=25&before=foo_before_cursor', $prevPage); + } + + public function testCanGeneratePaginationEndpointsFromACursor() + { + $graphEdge = new GraphEdge( + $this->request, + [], + ['paging' => $this->cursorPagination], + '/1234567890/likes' + ); + $nextPage = $graphEdge->getPaginationUrl('next'); + $prevPage = $graphEdge->getPaginationUrl('previous'); + + $this->assertEquals('/1234567890/likes?access_token=foo_token&after=bar_after_cursor&appsecret_proof=857d5f035a894f16b4180f19966e055cdeab92d4d53017b13dccd6d43b6497af&foo=bar&keep=me', $nextPage); + $this->assertEquals('/1234567890/likes?access_token=foo_token&appsecret_proof=857d5f035a894f16b4180f19966e055cdeab92d4d53017b13dccd6d43b6497af&before=bar_before_cursor&foo=bar&keep=me', $prevPage); + } + + public function testCanInstantiateNewPaginationRequest() + { + $graphEdge = new GraphEdge( + $this->request, + [], + ['paging' => $this->cursorPagination], + '/1234567890/likes' + ); + $nextPage = $graphEdge->getNextPageRequest(); + $prevPage = $graphEdge->getPreviousPageRequest(); + + $this->assertInstanceOf('Facebook\FacebookRequest', $nextPage); + $this->assertInstanceOf('Facebook\FacebookRequest', $prevPage); + $this->assertNotSame($this->request, $nextPage); + $this->assertNotSame($this->request, $prevPage); + $this->assertEquals('/v1337/1234567890/likes?access_token=foo_token&after=bar_after_cursor&appsecret_proof=857d5f035a894f16b4180f19966e055cdeab92d4d53017b13dccd6d43b6497af&foo=bar&keep=me', $nextPage->getUrl()); + $this->assertEquals('/v1337/1234567890/likes?access_token=foo_token&appsecret_proof=857d5f035a894f16b4180f19966e055cdeab92d4d53017b13dccd6d43b6497af&before=bar_before_cursor&foo=bar&keep=me', $prevPage->getUrl()); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphEventTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphEventTest.php new file mode 100644 index 0000000..98ccd85 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphEventTest.php @@ -0,0 +1,109 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Facebook\FacebookResponse; +use Mockery as m; +use Facebook\GraphNodes\GraphNodeFactory; + +class GraphEventTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FacebookResponse + */ + protected $responseMock; + + public function setUp() + { + $this->responseMock = m::mock('\Facebook\FacebookResponse'); + } + + public function testCoverGetsCastAsGraphCoverPhoto() + { + $dataFromGraph = [ + 'cover' => ['id' => '1337'] + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphObject = $factory->makeGraphEvent(); + + $cover = $graphObject->getCover(); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphCoverPhoto', $cover); + } + + public function testPlaceGetsCastAsGraphPage() + { + $dataFromGraph = [ + 'place' => ['id' => '1337'] + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphObject = $factory->makeGraphEvent(); + + $place = $graphObject->getPlace(); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphPage', $place); + } + + public function testPictureGetsCastAsGraphPicture() + { + $dataFromGraph = [ + 'picture' => ['id' => '1337'] + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphObject = $factory->makeGraphEvent(); + + $picture = $graphObject->getPicture(); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphPicture', $picture); + } + + public function testParentGroupGetsCastAsGraphGroup() + { + $dataFromGraph = [ + 'parent_group' => ['id' => '1337'] + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphObject = $factory->makeGraphEvent(); + + $parentGroup = $graphObject->getParentGroup(); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphGroup', $parentGroup); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphGroupTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphGroupTest.php new file mode 100644 index 0000000..21893b5 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphGroupTest.php @@ -0,0 +1,75 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Facebook\FacebookResponse; +use Mockery as m; +use Facebook\GraphNodes\GraphNodeFactory; + +class GraphGroupTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FacebookResponse + */ + protected $responseMock; + + public function setUp() + { + $this->responseMock = m::mock('\Facebook\FacebookResponse'); + } + + public function testCoverGetsCastAsGraphCoverPhoto() + { + $dataFromGraph = [ + 'cover' => ['id' => '1337'] + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphGroup(); + + $cover = $graphNode->getCover(); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphCoverPhoto', $cover); + } + + public function testVenueGetsCastAsGraphLocation() + { + $dataFromGraph = [ + 'venue' => ['id' => '1337'] + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphGroup(); + + $venue = $graphNode->getVenue(); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphLocation', $venue); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphNodeFactoryTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphNodeFactoryTest.php new file mode 100644 index 0000000..7d2f023 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphNodeFactoryTest.php @@ -0,0 +1,437 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Facebook\FacebookApp; +use Facebook\FacebookRequest; +use Facebook\FacebookResponse; +use Facebook\GraphNodes\GraphNodeFactory; +use Facebook\GraphNodes\GraphNode; + +class MyFooSubClassGraphNode extends GraphNode +{ +} + +class MyFooGraphNode extends GraphNode +{ + protected static $graphObjectMap = [ + 'foo_object' => '\Facebook\Tests\GraphNodes\MyFooSubClassGraphNode', + ]; +} + +class GraphNodeFactoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Facebook\FacebookRequest + */ + protected $request; + + public function setUp() + { + $app = new FacebookApp('123', 'foo_app_secret'); + $this->request = new FacebookRequest( + $app, + 'foo_token', + 'GET', + '/me/photos?keep=me', + ['foo' => 'bar'], + 'foo_eTag', + 'v1337' + ); + } + + public function testAValidGraphNodeResponseWillNotThrow() + { + $data = '{"id":"123","name":"foo"}'; + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $factory->validateResponseCastableAsGraphNode(); + } + + /** + * @expectedException \Facebook\Exceptions\FacebookSDKException + */ + public function testANonGraphNodeResponseWillThrow() + { + $data = '{"data":[{"id":"123","name":"foo"},{"id":"1337","name":"bar"}]}'; + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $factory->validateResponseCastableAsGraphNode(); + } + + public function testAValidGraphEdgeResponseWillNotThrow() + { + $data = '{"data":[{"id":"123","name":"foo"},{"id":"1337","name":"bar"}]}'; + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $factory->validateResponseCastableAsGraphEdge(); + } + + /** + * @expectedException \Facebook\Exceptions\FacebookSDKException + */ + public function testANonGraphEdgeResponseWillThrow() + { + $data = '{"id":"123","name":"foo"}'; + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $factory->validateResponseCastableAsGraphEdge(); + } + + public function testOnlyNumericArraysAreCastableAsAGraphEdge() + { + $shouldPassOne = GraphNodeFactory::isCastableAsGraphEdge([]); + $shouldPassTwo = GraphNodeFactory::isCastableAsGraphEdge(['foo', 'bar']); + $shouldFail = GraphNodeFactory::isCastableAsGraphEdge(['faz' => 'baz']); + + $this->assertTrue($shouldPassOne, 'Expected the given array to be castable as a GraphEdge.'); + $this->assertTrue($shouldPassTwo, 'Expected the given array to be castable as a GraphEdge.'); + $this->assertFalse($shouldFail, 'Expected the given array to not be castable as a GraphEdge.'); + } + + /** + * @expectedException \Facebook\Exceptions\FacebookSDKException + */ + public function testInvalidSubClassesWillThrow() + { + GraphNodeFactory::validateSubclass('FooSubClass'); + } + + public function testValidSubClassesWillNotThrow() + { + GraphNodeFactory::validateSubclass('\Facebook\GraphNodes\GraphNode'); + GraphNodeFactory::validateSubclass('\Facebook\GraphNodes\GraphAlbum'); + GraphNodeFactory::validateSubclass('\Facebook\Tests\GraphNodes\MyFooGraphNode'); + } + + public function testCastingAsASubClassObjectWillInstantiateTheSubClass() + { + $data = '{"id":"123","name":"foo"}'; + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $mySubClassObject = $factory->makeGraphNode('\Facebook\Tests\GraphNodes\MyFooGraphNode'); + + $this->assertInstanceOf('\Facebook\Tests\GraphNodes\MyFooGraphNode', $mySubClassObject); + } + + public function testASubClassMappingWillAutomaticallyInstantiateSubClass() + { + $data = '{"id":"123","name":"Foo Name","foo_object":{"id":"1337","name":"Should be sub classed!"}}'; + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $mySubClassObject = $factory->makeGraphNode('\Facebook\Tests\GraphNodes\MyFooGraphNode'); + $fooObject = $mySubClassObject->getField('foo_object'); + + $this->assertInstanceOf('\Facebook\Tests\GraphNodes\MyFooGraphNode', $mySubClassObject); + $this->assertInstanceOf('\Facebook\Tests\GraphNodes\MyFooSubClassGraphNode', $fooObject); + } + + public function testAnUnknownGraphNodeWillBeCastAsAGenericGraphNode() + { + $data = json_encode([ + 'id' => '123', + 'name' => 'Foo Name', + 'unknown_object' => [ + 'id' => '1337', + 'name' => 'Should be generic!', + ], + ]); + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + + $mySubClassObject = $factory->makeGraphNode('\Facebook\Tests\GraphNodes\MyFooGraphNode'); + $unknownObject = $mySubClassObject->getField('unknown_object'); + + $this->assertInstanceOf('\Facebook\Tests\GraphNodes\MyFooGraphNode', $mySubClassObject); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphNode', $unknownObject); + $this->assertNotInstanceOf('\Facebook\Tests\GraphNodes\MyFooGraphNode', $unknownObject); + } + + public function testAListFromGraphWillBeCastAsAGraphEdge() + { + $data = json_encode([ + 'data' => [ + [ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ], + [ + 'id' => '1337', + 'name' => 'Bar McBaz', + 'link' => 'http://facebook/bar', + ], + ], + 'paging' => [ + 'next' => 'http://facebook/next', + 'previous' => 'http://facebook/prev', + ], + ]); + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $graphEdge = $factory->makeGraphEdge(); + $graphData = $graphEdge->asArray(); + + $this->assertInstanceOf('\Facebook\GraphNodes\GraphEdge', $graphEdge); + $this->assertEquals([ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ], $graphData[0]); + $this->assertEquals([ + 'id' => '1337', + 'name' => 'Bar McBaz', + 'link' => 'http://facebook/bar', + ], $graphData[1]); + } + + public function testAGraphNodeWillBeCastAsAGraphNode() + { + $data = json_encode([ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ]); + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $graphNode = $factory->makeGraphNode(); + $graphData = $graphNode->asArray(); + + $this->assertInstanceOf('\Facebook\GraphNodes\GraphNode', $graphNode); + $this->assertEquals([ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ], $graphData); + } + + public function testAGraphNodeWithARootDataKeyWillBeCastAsAGraphNode() + { + $data = json_encode([ + 'data' => [ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ], + ]); + + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $graphNode = $factory->makeGraphNode(); + $graphData = $graphNode->asArray(); + + $this->assertInstanceOf('\Facebook\GraphNodes\GraphNode', $graphNode); + $this->assertEquals([ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ], $graphData); + } + + public function testAGraphEdgeWillBeCastRecursively() + { + $someUser = [ + 'id' => '123', + 'name' => 'Foo McBar', + ]; + $likesCollection = [ + 'data' => [ + [ + 'id' => '1', + 'name' => 'Sammy Kaye Powers', + 'is_sexy' => true, + ], + [ + 'id' => '2', + 'name' => 'Yassine Guedidi', + 'is_sexy' => true, + ], + [ + 'id' => '3', + 'name' => 'Fosco Marotto', + 'is_sexy' => true, + ], + [ + 'id' => '4', + 'name' => 'Foo McUgly', + 'is_sexy' => false, + ], + ], + 'paging' => [ + 'next' => 'http://facebook/next_likes', + 'previous' => 'http://facebook/prev_likes', + ], + ]; + $commentsCollection = [ + 'data' => [ + [ + 'id' => '42_1', + 'from' => $someUser, + 'message' => 'Foo comment.', + 'created_time' => '2014-07-15T03:54:34+0000', + 'likes' => $likesCollection, + ], + [ + 'id' => '42_2', + 'from' => $someUser, + 'message' => 'Bar comment.', + 'created_time' => '2014-07-15T04:11:24+0000', + 'likes' => $likesCollection, + ], + ], + 'paging' => [ + 'next' => 'http://facebook/next_comments', + 'previous' => 'http://facebook/prev_comments', + ], + ]; + $dataFromGraph = [ + 'data' => [ + [ + 'id' => '1337_1', + 'from' => $someUser, + 'story' => 'Some great foo story.', + 'likes' => $likesCollection, + 'comments' => $commentsCollection, + ], + [ + 'id' => '1337_2', + 'from' => $someUser, + 'to' => [ + 'data' => [$someUser], + ], + 'message' => 'Some great bar message.', + 'likes' => $likesCollection, + 'comments' => $commentsCollection, + ], + ], + 'paging' => [ + 'next' => 'http://facebook/next', + 'previous' => 'http://facebook/prev', + ], + ]; + $data = json_encode($dataFromGraph); + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphNodeFactory($res); + $graphNode = $factory->makeGraphEdge(); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphEdge', $graphNode); + + // Story + $storyObject = $graphNode[0]; + $this->assertInstanceOf('\Facebook\GraphNodes\GraphNode', $storyObject['from']); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphEdge', $storyObject['likes']); + $this->assertInstanceOf('\Facebook\GraphNodes\GraphEdge', $storyObject['comments']); + + // Story Comments + $storyComments = $storyObject['comments']; + $firstStoryComment = $storyComments[0]; + $this->assertInstanceOf('\Facebook\GraphNodes\GraphNode', $firstStoryComment['from']); + + // Message + $messageObject = $graphNode[1]; + $this->assertInstanceOf('\Facebook\GraphNodes\GraphEdge', $messageObject['to']); + $toUsers = $messageObject['to']; + $this->assertInstanceOf('\Facebook\GraphNodes\GraphNode', $toUsers[0]); + } + + public function testAGraphEdgeWillGenerateTheProperParentGraphEdges() + { + $likesList = [ + 'data' => [ + [ + 'id' => '1', + 'name' => 'Sammy Kaye Powers', + ], + ], + 'paging' => [ + 'cursors' => [ + 'after' => 'like_after_cursor', + 'before' => 'like_before_cursor', + ], + ], + ]; + + $photosList = [ + 'data' => [ + [ + 'id' => '777', + 'name' => 'Foo Photo', + 'likes' => $likesList, + ], + ], + 'paging' => [ + 'cursors' => [ + 'after' => 'photo_after_cursor', + 'before' => 'photo_before_cursor', + ], + ], + ]; + + $data = json_encode([ + 'data' => [ + [ + 'id' => '111', + 'name' => 'Foo McBar', + 'likes' => $likesList, + 'photos' => $photosList, + ], + [ + 'id' => '222', + 'name' => 'Bar McBaz', + 'likes' => $likesList, + 'photos' => $photosList, + ], + ], + 'paging' => [ + 'next' => 'http://facebook/next', + 'previous' => 'http://facebook/prev', + ], + ]); + $res = new FacebookResponse($this->request, $data); + + $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(); + + $this->assertNull($topGraphEdge); + $this->assertEquals('/111/likes', $childGraphEdgeOne); + $this->assertEquals('/222/likes', $childGraphEdgeTwo); + $this->assertEquals('/222/photos', $childGraphEdgeThree); + $this->assertEquals('/777/likes', $childGraphEdgeFour); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphNodeTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphNodeTest.php new file mode 100644 index 0000000..50f58ae --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphNodeTest.php @@ -0,0 +1,138 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Facebook\GraphNodes\GraphNode; + +class GraphNodeTest extends \PHPUnit_Framework_TestCase +{ + public function testAnEmptyBaseGraphNodeCanInstantiate() + { + $graphNode = new GraphNode(); + $backingData = $graphNode->asArray(); + + $this->assertEquals([], $backingData); + } + + public function testAGraphNodeCanInstantiateWithData() + { + $graphNode = new GraphNode(['foo' => 'bar']); + $backingData = $graphNode->asArray(); + + $this->assertEquals(['foo' => 'bar'], $backingData); + } + + public function testDatesThatShouldBeCastAsDateTimeObjectsAreDetected() + { + $graphNode = new GraphNode(); + + // Should pass + $shouldPass = $graphNode->isIso8601DateString('1985-10-26T01:21:00+0000'); + $this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date from Back To The Future to pass.'); + + $shouldPass = $graphNode->isIso8601DateString('1999-12-31'); + $this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to party like it\'s 1999.'); + + $shouldPass = $graphNode->isIso8601DateString('2009-05-19T14:39Z'); + $this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to pass.'); + + $shouldPass = $graphNode->isIso8601DateString('2014-W36'); + $this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to pass.'); + + // Should fail + $shouldFail = $graphNode->isIso8601DateString('2009-05-19T14a39r'); + $this->assertFalse($shouldFail, 'Expected the invalid ISO 8601 format to fail.'); + + $shouldFail = $graphNode->isIso8601DateString('foo_time'); + $this->assertFalse($shouldFail, 'Expected the invalid ISO 8601 format to fail.'); + } + + public function testATimeStampCanBeConvertedToADateTimeObject() + { + $someTimeStampFromGraph = 1405547020; + $graphNode = new GraphNode(); + $dateTime = $graphNode->castToDateTime($someTimeStampFromGraph); + $prettyDate = $dateTime->format(\DateTime::RFC1036); + $timeStamp = $dateTime->getTimestamp(); + + $this->assertInstanceOf('DateTime', $dateTime); + $this->assertEquals('Wed, 16 Jul 14 23:43:40 +0200', $prettyDate); + $this->assertEquals(1405547020, $timeStamp); + } + + public function testAGraphDateStringCanBeConvertedToADateTimeObject() + { + $someDateStringFromGraph = '2014-07-15T03:44:53+0000'; + $graphNode = new GraphNode(); + $dateTime = $graphNode->castToDateTime($someDateStringFromGraph); + $prettyDate = $dateTime->format(\DateTime::RFC1036); + $timeStamp = $dateTime->getTimestamp(); + + $this->assertInstanceOf('DateTime', $dateTime); + $this->assertEquals('Tue, 15 Jul 14 03:44:53 +0000', $prettyDate); + $this->assertEquals(1405395893, $timeStamp); + } + + public function testUncastingAGraphNodeWillUncastTheDateTimeObject() + { + $collectionOne = new GraphNode(['foo', 'bar']); + $collectionTwo = new GraphNode([ + 'id' => '123', + 'date' => new \DateTime('2014-07-15T03:44:53+0000'), + 'some_collection' => $collectionOne, + ]); + + $uncastArray = $collectionTwo->uncastItems(); + + $this->assertEquals([ + 'id' => '123', + 'date' => '2014-07-15T03:44:53+0000', + 'some_collection' => ['foo', 'bar'], + ], $uncastArray); + } + + public function testGettingGraphNodeAsAnArrayWillNotUncastTheDateTimeObject() + { + $collection = new GraphNode([ + 'id' => '123', + 'date' => new \DateTime('2014-07-15T03:44:53+0000'), + ]); + + $collectionAsArray = $collection->asArray(); + + $this->assertInstanceOf('DateTime', $collectionAsArray['date']); + } + + public function testReturningACollectionAsJasonWillSafelyRepresentDateTimes() + { + $collection = new GraphNode([ + 'id' => '123', + 'date' => new \DateTime('2014-07-15T03:44:53+0000'), + ]); + + $collectionAsString = $collection->asJson(); + + $this->assertEquals('{"id":"123","date":"2014-07-15T03:44:53+0000"}', $collectionAsString); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphObjectFactoryTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphObjectFactoryTest.php new file mode 100644 index 0000000..764503a --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphObjectFactoryTest.php @@ -0,0 +1,114 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Facebook\GraphNodes\GraphObjectFactory; +use Facebook\FacebookApp; +use Facebook\FacebookRequest; +use Facebook\FacebookResponse; + +/** + * @todo v6: Remove this test + */ +class GraphObjectFactoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Facebook\FacebookRequest + */ + protected $request; + + public function setUp() + { + $app = new FacebookApp('123', 'foo_app_secret'); + $this->request = new FacebookRequest( + $app, + 'foo_token', + 'GET', + '/me/photos?keep=me', + ['foo' => 'bar'], + 'foo_eTag', + 'v1337' + ); + } + + public function testAGraphNodeWillBeCastAsAGraphNode() + { + $data = json_encode([ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ]); + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphObjectFactory($res); + $graphObject = $factory->makeGraphObject(); + $graphData = $graphObject->asArray(); + + $this->assertInstanceOf('\Facebook\GraphNodes\GraphObject', $graphObject); + $this->assertEquals([ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ], $graphData); + } + + public function testAListFromGraphWillBeCastAsAGraphEdge() + { + $data = json_encode([ + 'data' => [ + [ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ], + [ + 'id' => '1337', + 'name' => 'Bar McBaz', + 'link' => 'http://facebook/bar', + ], + ], + 'paging' => [ + 'next' => 'http://facebook/next', + 'previous' => 'http://facebook/prev', + ], + ]); + $res = new FacebookResponse($this->request, $data); + + $factory = new GraphObjectFactory($res); + $graphList = $factory->makeGraphList(); + $graphData = $graphList->asArray(); + + $this->assertInstanceOf('\Facebook\GraphNodes\GraphList', $graphList); + $this->assertEquals([ + 'id' => '123', + 'name' => 'Foo McBar', + 'link' => 'http://facebook/foo', + ], $graphData[0]); + $this->assertEquals([ + 'id' => '1337', + 'name' => 'Bar McBaz', + 'link' => 'http://facebook/bar', + ], $graphData[1]); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphPageTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphPageTest.php new file mode 100644 index 0000000..a3a88e9 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphPageTest.php @@ -0,0 +1,95 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Mockery as m; +use Facebook\GraphNodes\GraphNodeFactory; + +class GraphPageTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Facebook\FacebookResponse + */ + protected $responseMock; + + public function setUp() + { + $this->responseMock = m::mock('\\Facebook\\FacebookResponse'); + } + + public function testPagePropertiesReturnGraphPageObjects() + { + $dataFromGraph = [ + 'id' => '123', + 'name' => 'Foo Page', + 'best_page' => [ + 'id' => '1', + 'name' => 'Bar Page', + ], + 'global_brand_parent_page' => [ + 'id' => '2', + 'name' => 'Faz Page', + ], + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphPage(); + + $bestPage = $graphNode->getBestPage(); + $globalBrandParentPage = $graphNode->getGlobalBrandParentPage(); + + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphPage', $bestPage); + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphPage', $globalBrandParentPage); + } + + public function testLocationPropertyWillGetCastAsGraphLocationObject() + { + $dataFromGraph = [ + 'id' => '123', + 'name' => 'Foo Page', + 'location' => [ + 'city' => 'Washington', + 'country' => 'United States', + 'latitude' => 38.881634205431, + 'longitude' => -77.029121075722, + 'state' => 'DC', + ], + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphPage(); + + $location = $graphNode->getLocation(); + + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphLocation', $location); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphSessionInfoTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphSessionInfoTest.php new file mode 100644 index 0000000..2960c28 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphSessionInfoTest.php @@ -0,0 +1,62 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Mockery as m; +use Facebook\GraphNodes\GraphNodeFactory; + +class GraphSessionInfoTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Facebook\FacebookResponse + */ + protected $responseMock; + + public function setUp() + { + $this->responseMock = m::mock('\\Facebook\\FacebookResponse'); + } + + public function testDatesGetCastToDateTime() + { + $dataFromGraph = [ + 'expires_at' => 123, + 'issued_at' => 1337, + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + + $graphNode = $factory->makeGraphSessionInfo(); + + $expires = $graphNode->getExpiresAt(); + $issuedAt = $graphNode->getIssuedAt(); + + $this->assertInstanceOf('DateTime', $expires); + $this->assertInstanceOf('DateTime', $issuedAt); + } +} diff --git a/lib/facebook-graph-sdk/tests/GraphNodes/GraphUserTest.php b/lib/facebook-graph-sdk/tests/GraphNodes/GraphUserTest.php new file mode 100644 index 0000000..ca75573 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/GraphNodes/GraphUserTest.php @@ -0,0 +1,140 @@ +<?php +/** + * Copyright 2014 Facebook, Inc. + * + * You are hereby granted a non-exclusive, worldwide, royalty-free license to + * use, copy, modify, and distribute this software in source code or binary + * form for use in connection with the web services and APIs provided by + * Facebook. + * + * As with any software that integrates with the Facebook platform, your use + * of this software is subject to the Facebook Developer Principles and + * Policies [http://developers.facebook.com/policy/]. This copyright notice + * shall be included in all copies or substantial portions of the software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ +namespace Facebook\Tests\GraphNodes; + +use Facebook\FacebookResponse; +use Mockery as m; +use Facebook\GraphNodes\GraphNodeFactory; + +class GraphUserTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FacebookResponse + */ + protected $responseMock; + + public function setUp() + { + $this->responseMock = m::mock('\\Facebook\\FacebookResponse'); + } + + public function testDatesGetCastToDateTime() + { + $dataFromGraph = [ + 'birthday' => '1984-01-01', + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphUser(); + + $birthday = $graphNode->getBirthday(); + + $this->assertInstanceOf('DateTime', $birthday); + } + + public function testPagePropertiesWillGetCastAsGraphPageObjects() + { + $dataFromGraph = [ + 'id' => '123', + 'name' => 'Foo User', + 'hometown' => [ + 'id' => '1', + 'name' => 'Foo Place', + ], + 'location' => [ + 'id' => '2', + 'name' => 'Bar Place', + ], + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphUser(); + + $hometown = $graphNode->getHometown(); + $location = $graphNode->getLocation(); + + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphPage', $hometown); + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphPage', $location); + } + + public function testUserPropertiesWillGetCastAsGraphUserObjects() + { + $dataFromGraph = [ + 'id' => '123', + 'name' => 'Foo User', + 'significant_other' => [ + 'id' => '1337', + 'name' => 'Bar User', + ], + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphUser(); + + $significantOther = $graphNode->getSignificantOther(); + + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphUser', $significantOther); + } + + public function testPicturePropertiesWillGetCastAsGraphPictureObjects() + { + $dataFromGraph = [ + 'id' => '123', + 'name' => 'Foo User', + 'picture' => [ + 'is_silhouette' => true, + 'url' => 'http://foo.bar', + 'width' => 200, + 'height' => 200, + ], + ]; + + $this->responseMock + ->shouldReceive('getDecodedBody') + ->once() + ->andReturn($dataFromGraph); + $factory = new GraphNodeFactory($this->responseMock); + $graphNode = $factory->makeGraphUser(); + + $Picture = $graphNode->getPicture(); + + $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphPicture', $Picture); + $this->assertTrue($Picture->isSilhouette()); + $this->assertEquals(200, $Picture->getWidth()); + $this->assertEquals(200, $Picture->getHeight()); + $this->assertEquals('http://foo.bar', $Picture->getUrl()); + } +} |