From 3d2b0bc01fa79d5416f5bde074e4336a6c3b48c8 Mon Sep 17 00:00:00 2001 From: AymericaRST Date: Tue, 25 Jul 2017 10:50:18 +0200 Subject: [PATCH 1/8] Location endpoint --- src/Endpoints/Location.php | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/src/Endpoints/Location.php b/src/Endpoints/Location.php index a529802..c3594bc 100644 --- a/src/Endpoints/Location.php +++ b/src/Endpoints/Location.php @@ -45,10 +45,7 @@ public function get($id = '') if ($id){ $this->id = $id; } - $res = $this->instagram->get(Location::API_SEGMENT.$this->id); - $this->data = $res->data; - $this->meta = $res->meta; - return $this; + return $this->instagram->get(Location::API_SEGMENT.$this->id); } /** @@ -61,17 +58,11 @@ public function recentMedia($minTagId='',$maxTagId='') if(!$this->id){ return "No Location id set"; } - $res = $this->instagram->get(Location::API_SEGMENT.$this->id.'/media/recent',[ + + return $this->instagram->get(Location::API_SEGMENT.$this->id.'/media/recent',[ 'min_tag_id' => $minTagId, 'max_tag_id' => $maxTagId ]); - $arr = []; - foreach ($res->data as $item){ - $tag = new Media($this->instagram,$item->id); - $tag->setData($item); - array_push($arr,$tag); - } - return $arr; } /** @@ -82,18 +73,11 @@ public function recentMedia($minTagId='',$maxTagId='') */ public function searchByCoordinates($lat, $lng, $distance=500) { - $res = $this->instagram->get(Location::API_SEGMENT.'search',[ + return $this->instagram->get(Location::API_SEGMENT.'search',[ 'lat' => $lat, 'lng' => $lng, 'distance' => $distance ]); - $arr = []; - foreach ($res->data as $item){ - $tag = new Location($this->instagram,$item->id); - $tag->setData($item); - array_push($arr,$tag); - } - return $arr; } /** @@ -103,18 +87,12 @@ public function searchByCoordinates($lat, $lng, $distance=500) */ public function searchByFbPlacesId($fb_places_id='',$distance=500) { - $res = $this->instagram->get(Location::API_SEGMENT.'search',[ + return $this->instagram->get(Location::API_SEGMENT.'search',[ 'facebook_places_id' => $fb_places_id, 'distance' => $distance ]); - $arr = []; - foreach ($res->data as $item){ - $tag = new Location($this->instagram,$item->id); - $tag->setData($item); - array_push($arr,$tag); - } - return $arr; } + /** * @param $name * @return null From 6e2bf6c89cc3e349ff686e0722956d5f0ede22fe Mon Sep 17 00:00:00 2001 From: AymericaRST Date: Tue, 25 Jul 2017 10:53:16 +0200 Subject: [PATCH 2/8] Media endpoint --- src/Endpoints/Media.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Endpoints/Media.php b/src/Endpoints/Media.php index de829e8..eac80ab 100644 --- a/src/Endpoints/Media.php +++ b/src/Endpoints/Media.php @@ -45,9 +45,7 @@ public function get($id = false) if ($id){ $this->id = $id; } - $res = $this->instagram->get(Media::API_SEGMENT.$this->id); - $this->data = $res->data; - return $this; + return $this->instagram->get(Media::API_SEGMENT.$this->id); } /** @@ -56,10 +54,7 @@ public function get($id = false) */ public function getByShortcode($shortcode) { - $res = $this->instagram->get(Media::API_SEGMENT.'shortcode/'.$shortcode); - $this->id = $res->data->id; - $this->data = $res->data; - return $this; + return $this->instagram->get(Media::API_SEGMENT.'shortcode/'.$shortcode); } /** @@ -76,6 +71,7 @@ public function search($lat,$lng,$distance='') 'distance' => $distance ]); } + /** * @return mixed|\Psr\Http\Message\ResponseInterface */ From 34414c44d5317f30861e8b5ca6373303053b9d58 Mon Sep 17 00:00:00 2001 From: AymericaRST Date: Tue, 25 Jul 2017 10:55:27 +0200 Subject: [PATCH 3/8] Tag endpoint --- src/Endpoints/Tag.php | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/Endpoints/Tag.php b/src/Endpoints/Tag.php index ca1c633..47ec42d 100644 --- a/src/Endpoints/Tag.php +++ b/src/Endpoints/Tag.php @@ -38,12 +38,7 @@ public function __construct(Instagram $instagram,$tagName = '') public function get($tagName = '') { - if($tagName){ - $this->tagName = $tagName; - } - $res = $this->instagram->get(Tag::API_SEGMENT.$this->tagName); - $this->data = $res->data; - return $this; + return $this->instagram->get(Tag::API_SEGMENT.$tagName); } /** @@ -66,16 +61,9 @@ public function recentMedia($count = '',$minTagId='',$maxTagId='') public function search($query) { - $res = $this->instagram->get(Tag::API_SEGMENT.'search',['q' => $query]); - - $arr = []; - foreach ($res->data as $item){ - $tag = new Tag($this->instagram,$item->name); - $tag->setData($item); - array_push($arr,$tag); - } - return $arr; + return $this->instagram->get(Tag::API_SEGMENT.'search',['q' => $query]); } + /** * @param $name * @return null From 9de041b18d0f6d17b95e255b18c3aa4d0cba04eb Mon Sep 17 00:00:00 2001 From: AymericaRST Date: Tue, 25 Jul 2017 10:58:51 +0200 Subject: [PATCH 4/8] User endpoint --- src/Endpoints/User.php | 51 ++++++------------------------------------ 1 file changed, 7 insertions(+), 44 deletions(-) diff --git a/src/Endpoints/User.php b/src/Endpoints/User.php index 8a528c6..07fbdd9 100644 --- a/src/Endpoints/User.php +++ b/src/Endpoints/User.php @@ -44,15 +44,9 @@ public function __construct(Instagram $instagram,$id = false) * @param $id * @return mixed|\Psr\Http\Message\ResponseInterface */ - public function get($id = false) + public function get($id) { - if($id){ - $this->id = $id; - } - $res = $this->instagram->get(User::API_SEGMENT.$this->id); - $this->data = $res->data; - $this->meta = $res->meta; - return $this; + return $this->instagram->get(User::API_SEGMENT.$id); } /** @@ -60,10 +54,7 @@ public function get($id = false) */ public function self() { - $res = $this->instagram->get(User::API_SEGMENT.'self'); - $this->data = $res->data; - $this->meta = $res->meta; - return $this; + return $this->instagram->get(User::API_SEGMENT.'self'); } /** @@ -74,18 +65,11 @@ public function self() */ public function getMediaRecent($count='',$minId='',$maxId='') { - $res = $this->instagram->get(User::API_SEGMENT.$this->id.'/media/recent',[ + return $this->instagram->get(User::API_SEGMENT.$this->id.'/media/recent',[ 'count' => $count, 'min_id' => $minId, 'max_id' => $maxId ]); - $arr = []; - foreach ($res->data as $item){ - $media = new Media($this->instagram,$item->id); - $media->setData($item); - array_push($arr,$media); - } - return $arr; } /** @@ -96,18 +80,11 @@ public function getMediaRecent($count='',$minId='',$maxId='') */ public function selfMediaRecent($count='',$minId='',$maxId='') { - $res = $this->instagram->get(User::API_SEGMENT.'self/media/recent',[ + return $this->instagram->get(User::API_SEGMENT.'self/media/recent',[ 'count' => $count, 'min_id' => $minId, 'max_id' => $maxId ]); - $arr = []; - foreach ($res->data as $item){ - $media = new Media($this->instagram,$item->id); - $media->setData($item); - array_push($arr,$media); - } - return $arr; } /** @@ -117,17 +94,10 @@ public function selfMediaRecent($count='',$minId='',$maxId='') */ public function selfMediaLiked($count = '',$maxLikeId='') { - $res = $this->instagram->get(User::API_SEGMENT.'self/media/liked',[ + return $this->instagram->get(User::API_SEGMENT.'self/media/liked',[ 'count' => $count, 'max_like_id' => $maxLikeId ]); - $arr = []; - foreach ($res->data as $item){ - $media = new Media($this->instagram,$item->id); - $media->setData($item); - array_push($arr,$media); - } - return $arr; } /** @@ -137,17 +107,10 @@ public function selfMediaLiked($count = '',$maxLikeId='') */ public function search($q,$count = false) { - $res = $this->instagram->get(User::API_SEGMENT.'search',[ + return $this->instagram->get(User::API_SEGMENT.'search',[ 'q' => $q, 'count' => $count ?: '' ]); - $arr = []; - foreach ($res->data as $item){ - $user = new User($this->instagram,$item->id); - $user->setData($item); - array_push($arr,$user); - } - return $arr; } /** From 8f3a1f32d08cec983cde1af781b0fb1db2dc8eb7 Mon Sep 17 00:00:00 2001 From: AymericaRST Date: Tue, 25 Jul 2017 11:29:40 +0200 Subject: [PATCH 5/8] some corrections --- src/Endpoints/Location.php | 2 -- src/Endpoints/Tag.php | 5 ++++- src/Endpoints/User.php | 7 +++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Endpoints/Location.php b/src/Endpoints/Location.php index c3594bc..006fab5 100644 --- a/src/Endpoints/Location.php +++ b/src/Endpoints/Location.php @@ -58,7 +58,6 @@ public function recentMedia($minTagId='',$maxTagId='') if(!$this->id){ return "No Location id set"; } - return $this->instagram->get(Location::API_SEGMENT.$this->id.'/media/recent',[ 'min_tag_id' => $minTagId, 'max_tag_id' => $maxTagId @@ -92,7 +91,6 @@ public function searchByFbPlacesId($fb_places_id='',$distance=500) 'distance' => $distance ]); } - /** * @param $name * @return null diff --git a/src/Endpoints/Tag.php b/src/Endpoints/Tag.php index 47ec42d..04821b5 100644 --- a/src/Endpoints/Tag.php +++ b/src/Endpoints/Tag.php @@ -38,7 +38,10 @@ public function __construct(Instagram $instagram,$tagName = '') public function get($tagName = '') { - return $this->instagram->get(Tag::API_SEGMENT.$tagName); + if($tagName){ + $this->tagName = $tagName; + } + return $this->instagram->get(Tag::API_SEGMENT.$this->tagName); } /** diff --git a/src/Endpoints/User.php b/src/Endpoints/User.php index 07fbdd9..bf2ea75 100644 --- a/src/Endpoints/User.php +++ b/src/Endpoints/User.php @@ -44,9 +44,12 @@ public function __construct(Instagram $instagram,$id = false) * @param $id * @return mixed|\Psr\Http\Message\ResponseInterface */ - public function get($id) + public function get($id = false) { - return $this->instagram->get(User::API_SEGMENT.$id); + if($id){ + $this->id = $id; + } + return $this->instagram->get(User::API_SEGMENT.$this->id); } /** From 6ed569a6eb0e1680a62445add288341abcb92c50 Mon Sep 17 00:00:00 2001 From: evgueni belozerov Date: Thu, 5 Oct 2017 17:53:51 +0300 Subject: [PATCH 6/8] rolled back tests for simplified endpoints also added two environment variables for testing with differents access tokens in UserTest --- tests/LocationTest.php | 8 ++++---- tests/MediaTest.php | 4 ++-- tests/TagTest.php | 4 ++-- tests/UserTest.php | 19 +++++++++---------- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/tests/LocationTest.php b/tests/LocationTest.php index 1e46c3c..f2db263 100644 --- a/tests/LocationTest.php +++ b/tests/LocationTest.php @@ -17,25 +17,25 @@ public static function setUpBeforeClass() public function testGet() { $res = $this::$instagram->location(213385402)->get(); - $this->assertEquals(213385402,$res->id); + $this->assertEquals(213385402,$res->data->id); } public function testRecentMedia() { $res = $this::$instagram->location(213385402)->recentMedia(); - $this->assertInternalType('array',$res); + $this->assertEquals(200,$res->meta->code); } public function testSearchByFbPlacesId() { $res = $this::$instagram->location()->searchByFbPlacesId('106078429431815'); - $this->assertEquals(213385402,$res[0]->id); + $this->assertEquals(213385402,$res->data[0]->id); } public function testSearchByCoordinates() { $res = $this::$instagram->location()->searchByCoordinates('51.518732','-0.129756'); - $this->assertEquals(213385402,$res[2]->id); + $this->assertEquals(213385402,$res->data[2]->id); } } \ No newline at end of file diff --git a/tests/MediaTest.php b/tests/MediaTest.php index cd615e6..8888588 100644 --- a/tests/MediaTest.php +++ b/tests/MediaTest.php @@ -17,12 +17,12 @@ public static function setUpBeforeClass() public function testGet() { $res = $this::$instagram->media('503364100596583201_45913985')->get(); - $this->assertEquals('503364100596583201_45913985',$res->id); + $this->assertEquals('503364100596583201_45913985',$res->data->id); } public function testGetByShortcode() { $res = $this::$instagram->media()->getByShortcode('b8TvuIvksh'); - $this->assertEquals('503364100596583201_45913985',$res->id); + $this->assertEquals('503364100596583201_45913985',$res->data->id); } public function testGetLikes() diff --git a/tests/TagTest.php b/tests/TagTest.php index 9233d71..c1e75bc 100644 --- a/tests/TagTest.php +++ b/tests/TagTest.php @@ -17,7 +17,7 @@ public static function setUpBeforeClass() public function testGet() { $res = $this::$instagram->tag('cats')->get(); - $this->assertEquals('cats',$res->name); + $this->assertEquals('cats',$res->data->name); } @@ -30,6 +30,6 @@ public function testRecentMedia() public function testSearch() { $res = $this::$instagram->tag()->search('cats'); - $this->assertEquals('cats',$res[0]->name); + $this->assertEquals('cats',$res->data[0]->name); } } \ No newline at end of file diff --git a/tests/UserTest.php b/tests/UserTest.php index 0560da2..a82af5a 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -16,33 +16,32 @@ public static function setUpBeforeClass() public function testSelf() { $res = $this::$instagram->user()->self(); - $this->assertEquals('marv_____',$res->username); + $this->assertEquals(getenv('INSTAGRAM_USERNAME'),$res->data->username); } public function testSelfMediaRecent() { $res = $this::$instagram->user()->selfMediaRecent(); - $this->assertEquals('marv_____',$res[0]->user->username); + $this->assertEquals(getenv('INSTAGRAM_USERNAME'),$res->data[0]->user->username); } public function testSelfMediaLiked() { $res = $this::$instagram->user()->selfMediaLiked(); - $this->assertInternalType('array',$res); + $this->assertInstanceOf('stdClass',$res); } public function testGet() { - $res = $this::$instagram->user()->get('45913985')->username; - $this->assertEquals('marv_____',$res); + $res = $this::$instagram->user()->get('299054539')->data->username; + $this->assertEquals(getenv('INSTAGRAM_USERNAME'),$res); } public function testGetMediaRecent() { - $res = $this::$instagram->user('45913985')->getMediaRecent()[0]->user->username; - $this->assertEquals('marv_____',$res); + $res = $this::$instagram->user('299054539')->getMediaRecent()->data[0]->user->username; + $this->assertEquals(getenv('INSTAGRAM_USERNAME'),$res); } - public function testSearch() { - $res = $this::$instagram->user()->search('marv_____'); - $this->assertEquals('marv_____', $res[0]->username); + $res = $this::$instagram->user()->search(getenv('INSTAGRAM_SEARCH'))->data[0]->username; + $this->assertEquals(getenv('INSTAGRAM_SEARCH'),$res); } } \ No newline at end of file From 57ff8eac318fef62a2ed3527fc47a85db640bcf5 Mon Sep 17 00:00:00 2001 From: evgueni belozerov Date: Thu, 5 Oct 2017 18:30:52 +0300 Subject: [PATCH 7/8] fixes removed useless INSTAGRAM_SEARCH and INSTAGRAM_USERNAME usages --- tests/UserTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/UserTest.php b/tests/UserTest.php index ded4423..ab1e441 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -33,8 +33,8 @@ public function testSelfMediaLiked() } public function testGet() { - $res = $this::$instagram->user()->get('299054539')->data->username; - $this->assertEquals(getenv('INSTAGRAM_USERNAME'),$res); + $res = $this::$instagram->user()->get('45913985')->data->username; + $this->assertEquals('marv_____',$res); } public function testGetClientException() @@ -56,12 +56,12 @@ public function testGetAPIError400() public function testGetMediaRecent() { - $res = $this::$instagram->user('299054539')->getMediaRecent()->data[0]->user->username; - $this->assertEquals(getenv('INSTAGRAM_USERNAME'),$res); + $res = $this::$instagram->user('45913985')->getMediaRecent()->data[0]->user->username; + $this->assertEquals('marv_____',$res); } public function testSearch() { - $res = $this::$instagram->user()->search(getenv('INSTAGRAM_SEARCH'))->data[0]->username; - $this->assertEquals(getenv('INSTAGRAM_SEARCH'),$res); + $res = $this::$instagram->user()->search("marv_____")->data[0]->username; + $this->assertEquals("marv_____",$res); } } \ No newline at end of file From 1f415a8c8a23c69c5bf08b5200f1a3b0de429866 Mon Sep 17 00:00:00 2001 From: evgueni belozerov Date: Fri, 6 Oct 2017 20:39:33 +0300 Subject: [PATCH 8/8] added failure tests for every endpoint --- tests/LocationTest.php | 17 ++++++++++++++++- tests/MediaTest.php | 17 ++++++++++++++++- tests/RelationshipsTest.php | 18 +++++++++++++++++- tests/TagTest.php | 17 ++++++++++++++++- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/tests/LocationTest.php b/tests/LocationTest.php index f2db263..a471a56 100644 --- a/tests/LocationTest.php +++ b/tests/LocationTest.php @@ -1,6 +1,8 @@ load(); } - self::$instagram = new \marvinosswald\Instagram\Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]); + self::$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]); } public function testGet() @@ -38,4 +40,17 @@ public function testSearchByCoordinates() $res = $this::$instagram->location()->searchByCoordinates('51.518732','-0.129756'); $this->assertEquals(213385402,$res->data[2]->id); } + public function testGetClientException() + { + $this->expectException(ClientException::class); + $this::$instagram->location(12345)->get(); + } + public function testGetAPIError400() + { + $instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')], ['http_errors' => false]); + $res = $instagram->location(12345)->get(); + $this->assertEquals($res->meta->code,'400'); + $this->assertInternalType('string', $res->meta->error_type); + $this->assertInternalType('string', $res->meta->error_message); + } } \ No newline at end of file diff --git a/tests/MediaTest.php b/tests/MediaTest.php index 8888588..52eb35d 100644 --- a/tests/MediaTest.php +++ b/tests/MediaTest.php @@ -1,6 +1,8 @@ load(); } - self::$instagram = new \marvinosswald\Instagram\Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]); + self::$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]); } public function testGet() @@ -30,4 +32,17 @@ public function testGetLikes() $res = $this::$instagram->media('503364100596583201_45913985')->likes(); $this->assertEquals(200,$res->meta->code); } + public function testGetClientException() + { + $this->expectException(ClientException::class); + $this::$instagram->media('dummy_media_id')->get(); + } + public function testGetAPIError400() + { + $instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')], ['http_errors' => false]); + $res = $instagram->media('dummy_media_id')->get(); + $this->assertEquals($res->meta->code,'400'); + $this->assertInternalType('string', $res->meta->error_type); + $this->assertInternalType('string', $res->meta->error_message); + } } \ No newline at end of file diff --git a/tests/RelationshipsTest.php b/tests/RelationshipsTest.php index 48dcc25..3542538 100644 --- a/tests/RelationshipsTest.php +++ b/tests/RelationshipsTest.php @@ -1,6 +1,8 @@ load(); } - self::$instagram = new \marvinosswald\Instagram\Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]); + self::$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]); } public function testFollows() { @@ -28,4 +30,18 @@ public function testFollowingRequests() $res = $this::$instagram->relationships->followingRequests(); $this->assertEquals(200,$res->meta->code); } + public function testGetClientException() + { + $this->expectException(ClientException::class); + $instagram = new Instagram(['accessToken' => "dummy_token"], ['http_errors' => true]); + $instagram->relationships->follows(); + } + public function testGetAPIError400() + { + $instagram = new Instagram(['accessToken' => "dummy_token"], ['http_errors' => false]); + $res = $instagram->relationships->follows(); + $this->assertEquals($res->meta->code,'400'); + $this->assertInternalType('string', $res->meta->error_type); + $this->assertInternalType('string', $res->meta->error_message); + } } \ No newline at end of file diff --git a/tests/TagTest.php b/tests/TagTest.php index c1e75bc..cf24b6b 100644 --- a/tests/TagTest.php +++ b/tests/TagTest.php @@ -1,6 +1,8 @@ load(); } - self::$instagram = new \marvinosswald\Instagram\Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]); + self::$instagram = new Instagram(['accessToken' => getenv('INSTAGRAM_ACCESS_TOKEN')]); } public function testGet() @@ -32,4 +34,17 @@ public function testSearch() $res = $this::$instagram->tag()->search('cats'); $this->assertEquals('cats',$res->data[0]->name); } + public function testGetClientException() + { + $this->expectException(ClientException::class); + $res = $this::$instagram->tag('@$*')->get(); + } + public function testGetAPIError400() + { + $instagram = new Instagram(['accessToken' => "dummy_token"], ['http_errors' => false]); + $res = $instagram->tag('cats')->get(); + $this->assertEquals($res->meta->code,'400'); + $this->assertInternalType('string', $res->meta->error_type); + $this->assertInternalType('string', $res->meta->error_message); + } } \ No newline at end of file