From 082b8a200f2174fb4088d533c14e883dc888912e Mon Sep 17 00:00:00 2001 From: Bas van Dorst Date: Sat, 21 Oct 2017 17:39:24 +0200 Subject: [PATCH] Added getAthleteRoutes and getRoute #30 --- README.md | 3 ++ src/Strava/API/Client.php | 23 +++++++++++-- src/Strava/API/Service/REST.php | 20 ++++++++++++ src/Strava/API/Service/ServiceInterface.php | 14 ++++++++ src/Strava/API/Service/Stub.php | 14 ++++++-- tests/Strava/API/ClientTest.php | 36 +++++++++++++++++++++ tests/Strava/API/Service/RESTTest.php | 24 ++++++++++++++ tests/Strava/API/Service/StubTest.php | 15 +++++++++ 8 files changed, 144 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3ab8f2c..267b106 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ print_r($athlete); $client->getAthlete($id = null); $client->getAthleteStats($id); $client->getAthleteClubs(); +$client->getAthleteRoutes($id, $type = null, $after = null, $page = null, $per_page = null); $client->getAthleteActivities($before = null, $after = null, $page = null, $per_page = null); $client->getAthleteFriends($id = null, $page = null, $per_page = null); $client->getAthleteFollowers($id = null, $page = null, $per_page = null); @@ -185,6 +186,7 @@ $client->getGear($id); $client->getClub($id); $client->getClubMembers($id, $page = null, $per_page = null); $client->getClubActivities($id, $page = null, $per_page = null); +$client->getRoute($id); $client->getSegment($id); $client->getSegmentLeaderboard($id, $gender = null, $age_group = null, $weight_class = null, $following = null, $club_id = null, $date_range = null, $page = null, $per_page = null); $client->getSegmentExplorer($bounds, $activity_type = 'riding', $min_cat = null, $max_cat = null); @@ -192,6 +194,7 @@ $client->getSegmentEffort($id, $athlete_id = null, $start_date_local = null, $en $client->getStreamsActivity($id, $types, $resolution = null, $series_type = 'distance'); $client->getStreamsEffort($id, $types, $resolution = null, $series_type = 'distance'); $client->getStreamsSegment($id, $types, $resolution = null, $series_type = 'distance'); +$client->getStreamsRoute($id); ``` ## UML diagrams diff --git a/src/Strava/API/Client.php b/src/Strava/API/Client.php index 33e18ff..efeb2ca 100644 --- a/src/Strava/API/Client.php +++ b/src/Strava/API/Client.php @@ -71,15 +71,15 @@ public function getAthleteStats($id) /** * Retrieve athlete routes * - * @link https://strava.github.io/api/v3/athlete/#stats + * @link https://strava.github.io/api/v3/routes/#list * @param int $id * @return array * @throws ClientException */ - public function getAthleteRoutes($id) + public function getAthleteRoutes($id, $type = null, $after = null, $page = null, $per_page = null) { try { - return $this->service->getAthleteRoutes($id); + return $this->service->getAthleteRoutes($id, $type, $after, $page, $per_page); } catch (ServiceException $e) { throw new ClientException('[SERVICE] ' . $e->getMessage()); } @@ -634,6 +634,23 @@ public function leaveClub($id) } } + /** + * Get route details + * + * @link https://strava.github.io/api/v3/routes/#list + * @param int $id + * @return array + * @throws Exception + */ + public function getRoute($id) + { + try { + return $this->service->getRoute($id); + } catch (ServiceException $e) { + throw new ClientException('[SERVICE] ' . $e->getMessage()); + } + } + /** * Retrieve a segment * diff --git a/src/Strava/API/Service/REST.php b/src/Strava/API/Service/REST.php index de7ddbc..cff48a7 100644 --- a/src/Strava/API/Service/REST.php +++ b/src/Strava/API/Service/REST.php @@ -58,6 +58,19 @@ public function getAthleteStats($id) return $this->format($result); } + public function getAthleteRoutes($id, $type = null, $after = null, $page = null, $per_page = null) + { + $path = '/athletes/' . $id . '/routes'; + $parameters = array( + 'type' => $type, + 'after' => $after, + 'page' => $page, + 'per_page' => $per_page, + ); + $result = $this->adapter->get($path, $parameters, $this->getHeaders()); + return $this->format($result); + } + public function getAthleteClubs() { $path = '/athlete/clubs'; @@ -369,6 +382,13 @@ public function leaveClub($id) return $this->format($result); } + public function getRoute($id) + { + $path = '/routes/' . $id; + $result = $this->adapter->get($path, array(), $this->getHeaders()); + return $this->format($result); + } + public function getSegment($id) { $path = '/segments/' . $id; diff --git a/src/Strava/API/Service/ServiceInterface.php b/src/Strava/API/Service/ServiceInterface.php index ec1ed79..5c045b4 100644 --- a/src/Strava/API/Service/ServiceInterface.php +++ b/src/Strava/API/Service/ServiceInterface.php @@ -28,6 +28,15 @@ public function getAthlete($id = null); */ public function getAthleteStats($id); + /** + * @param integer $id + * @param string $type + * @param integer $after + * @param integer $page + * @param integer $per_page + */ + public function getAthleteRoutes($id, $type = null, $after = null, $page = null, $per_page = null); + public function getAthleteClubs(); /** @@ -214,6 +223,11 @@ public function joinClub($id); */ public function leaveClub($id); + /** + * @param integer $id + */ + public function getRoute($id); + /** * @param integer $id */ diff --git a/src/Strava/API/Service/Stub.php b/src/Strava/API/Service/Stub.php index 3d4ba3d..76614f9 100644 --- a/src/Strava/API/Service/Stub.php +++ b/src/Strava/API/Service/Stub.php @@ -9,8 +9,6 @@ */ class Stub implements ServiceInterface { - - public function getAthlete($id = null) { $json = '{ "id": 227615, "resource_state": 2, "firstname": "John", "lastname": "Applestrava", "profile_medium": "http://pics.com/227615/medium.jpg", "profile": "http://pics.com/227615/large.jpg", "city": "San Francisco", "state": "CA", "country": "United States", "sex": "M", "friend": null, "follower": "accepted", "premium": true, "created_at": "2011-03-19T21:59:57Z", "updated_at": "2013-09-05T16:46:54Z", "approve_followers": false }'; @@ -23,6 +21,12 @@ public function getAthleteStats($id) return $this->format($json); } + public function getAthleteRoutes($id, $type = null, $after = null, $page = null, $per_page = null) + { + $json = '[{"athlete":{"id":19,"resource_state":2},"id":743064,"resource_state":2,"description":"","distance":17781.6,"elevation_gain":207.8}]'; + return $this->format($json); + } + public function getAthleteClubs() { $json = '[ { "id": 1, "resource_state": 2, "name": "Team Strava Cycling", "profile_medium": "http://pics.com/clubs/1/medium.jpg", "profile": "http://pics.com/clubs/1/large.jpg" } ]'; @@ -197,6 +201,12 @@ public function leaveClub($id) return $this->format($json); } + public function getRoute($id) + { + $json = '{"response": 1}'; + return $this->format($json); + } + public function getSegment($id) { $json = '{"response": 1}'; diff --git a/tests/Strava/API/ClientTest.php b/tests/Strava/API/ClientTest.php index c80e617..8b28bd9 100644 --- a/tests/Strava/API/ClientTest.php +++ b/tests/Strava/API/ClientTest.php @@ -54,6 +54,18 @@ public function testGetAthleteStats() $this->assertEquals('output', $output); } + public function testGetAthleteRoutes() + { + $serviceMock = $this->getServiceMock(); + $serviceMock->expects($this->once())->method('getAthleteRoutes') + ->will($this->returnValue('output')); + + $client = new Strava\API\Client($serviceMock); + $output = $client->getAthleteRoutes(1234); + + $this->assertEquals('output', $output); + } + public function testGetAthleteClubs() { $serviceMock = $this->getServiceMock(); @@ -727,6 +739,30 @@ public function testLeaveClubException() $output = $client->leaveClub(1234); } + public function testGetRoute() + { + $serviceMock = $this->getServiceMock(); + $serviceMock->expects($this->once())->method('getRoute') + ->will($this->returnValue('output')); + + $client = new Strava\API\Client($serviceMock); + $output = $client->getRoute(1234); + + $this->assertEquals('output', $output); + } + + public function testGetRouteException() + { + $this->setExpectedException('Strava\API\Exception'); + + $serviceMock = $this->getServiceMock(); + $serviceMock->expects($this->once())->method('getRoute') + ->will($this->throwException(new ServiceException)); + + $client = new Strava\API\Client($serviceMock); + $output = $client->getRoute(1234); + } + public function testGetSegment() { $serviceMock = $this->getServiceMock(); diff --git a/tests/Strava/API/Service/RESTTest.php b/tests/Strava/API/Service/RESTTest.php index ad61193..957982c 100644 --- a/tests/Strava/API/Service/RESTTest.php +++ b/tests/Strava/API/Service/RESTTest.php @@ -53,6 +53,18 @@ public function testGetStats() $this->assertArrayHasKey('response', $output); } + public function testGetRoutes() + { + $pestMock = $this->getPestMock(); + $pestMock->expects($this->once())->method('get') + ->with($this->equalTo('/athletes/1234/routes')) + ->will($this->returnValue('{"response": 1}')); + + $service = new Strava\API\Service\REST('TOKEN', $pestMock); + $output = $service->getAthleteRoutes(1234); + $this->assertArrayHasKey('response', $output); + } + public function testGetAthleteClubs() { $pestMock = $this->getPestMock(); @@ -433,6 +445,18 @@ public function testLeaveClub() $this->assertArrayHasKey('response', $output); } + public function testGetRoute() + { + $pestMock = $this->getPestMock(); + $pestMock->expects($this->once())->method('get') + ->with($this->equalTo('/routes/1234')) + ->will($this->returnValue('{"response": 1}')); + + $service = new Strava\API\Service\REST('TOKEN', $pestMock); + $output = $service->getRoute(1234); + $this->assertArrayHasKey('response', $output); + } + public function testGetSegment() { $pestMock = $this->getPestMock(); diff --git a/tests/Strava/API/Service/StubTest.php b/tests/Strava/API/Service/StubTest.php index 5c850c8..e002662 100644 --- a/tests/Strava/API/Service/StubTest.php +++ b/tests/Strava/API/Service/StubTest.php @@ -23,6 +23,13 @@ public function testGetAthleteStats() $this->assertTrue(is_array($output)); } + public function testGetAthleteRoutes() + { + $service = new Strava\API\Service\Stub(); + $output = $service->getAthleteRoutes(1234); + $this->assertTrue(is_array($output)); + } + public function testGetAthleteClubs() { $service = new Strava\API\Service\Stub(); @@ -219,6 +226,14 @@ public function testLeaveClub() $this->assertTrue(is_array($output)); } + public function testGetRoute() + { + $service = new Strava\API\Service\Stub(); + $output = $service->getRoute(1234); + $this->assertTrue(is_array($output)); + } + + public function testGetSegment() { $service = new Strava\API\Service\Stub();