Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit ca26c83

Browse files
committed
Merge branch 'hotfix/47-geocode-validation'
Close #52 Fixes #47
2 parents 3f29b72 + 40eea07 commit ca26c83

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ All notable changes to this project will be documented in this file, in reverse
2626
submission of direct messages to the Twitter API. Payloads for DMs have been
2727
broken since the 3.0.0 release.
2828

29+
- [#52](https://github.com/zendframework/ZendService_Twitter/pull/52) fixes
30+
the `search/tweets` logic concerning geocode parameter validation, ensuring it
31+
no longer raises an exception for a valid geocode parameter.
32+
2933
## 3.0.1 - 2017-08-17
3034

3135
### Added

src/Twitter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,12 +977,12 @@ public function searchTweets(string $query, array $options = []) : Response
977977
foreach ($options as $key => $value) {
978978
switch (strtolower($key)) {
979979
case 'geocode':
980-
if (! substr_count($value, ',') !== 2) {
980+
if (substr_count($value, ',') !== 2) {
981981
throw new Exception\InvalidArgumentException(
982982
'"geocode" must be of the format "latitude,longitude,radius"'
983983
);
984984
}
985-
list($latitude, $longitude, $radius) = explode(',', $value);
985+
list($latitude, $longitude, $radius) = explode(',', $value, 3);
986986
$radius = trim($radius);
987987
if (! preg_match('/^\d+(mi|km)$/', $radius)) {
988988
throw new Exception\InvalidArgumentException(

test/TwitterTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,4 +1233,64 @@ public function testFriendshipsLookupRaisesExceptionIfInvalidIdentifiersProvided
12331233

12341234
$twitter->friendships->lookup($ids);
12351235
}
1236+
1237+
public function validGeocodeOptions()
1238+
{
1239+
return [
1240+
'mile-radius' => ['53.3242381,-6.3857848,1mi'],
1241+
'km-radius' => ['53.3242381,-6.3857848,1km'],
1242+
];
1243+
}
1244+
1245+
/**
1246+
* @dataProvider validGeocodeOptions
1247+
* @param string $geocode
1248+
*/
1249+
public function testSearchTweetsShouldNotInvalidateCorrectlyFormattedGeocodeOption($geocode)
1250+
{
1251+
$twitter = new Twitter\Twitter();
1252+
1253+
$options = [
1254+
'geocode' => $geocode,
1255+
];
1256+
1257+
$twitter->setHttpClient($this->stubOAuthClient(
1258+
'search/tweets.json',
1259+
Http\Request::METHOD_GET,
1260+
'search.tweets.json',
1261+
array_merge($options, ['q' => 'foo'])
1262+
));
1263+
1264+
$finalResponse = $twitter->search->tweets('foo', $options);
1265+
$this->assertInstanceOf(TwitterResponse::class, $finalResponse);
1266+
}
1267+
1268+
public function invalidGeocodeOptions()
1269+
{
1270+
return [
1271+
'too-few-commas' => ['53.3242381,-6.3857848', 'latitude,longitude,radius'],
1272+
'too-many-commas' => ['53.3242381,-6.3857848,1mi,why', 'latitude,longitude,radius'],
1273+
'invalid-radius' => ['53.3242381,-6.3857848,1', 'Radius segment'],
1274+
];
1275+
}
1276+
1277+
/**
1278+
* @dataProvider invalidGeocodeOptions
1279+
* @param string $geocode
1280+
* @param string $expectedMessage
1281+
*/
1282+
public function testSearchTweetsShouldInvalidateIncorrectlyFormattedGeocodeOption(
1283+
$geocode,
1284+
$expectedMessage
1285+
) {
1286+
$twitter = new Twitter\Twitter();
1287+
1288+
$options = [
1289+
'geocode' => $geocode,
1290+
];
1291+
1292+
$this->expectException(Twitter\Exception\InvalidArgumentException::class);
1293+
$this->expectExceptionMessage($expectedMessage);
1294+
$twitter->search->tweets('foo', $options);
1295+
}
12361296
}

0 commit comments

Comments
 (0)