Skip to content

Commit

Permalink
Handle profile not found as error
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrimaud committed Sep 13, 2020
1 parent 4e95e66 commit a8e19f2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Instagram/Transport/HtmlProfileDataFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Instagram\Transport;

use GuzzleHttp\Exception\ClientException;
use Instagram\Exception\InstagramFetchException;
use Instagram\Utils\{InstagramHelper, UserAgentHelper};

class HtmlProfileDataFeed extends AbstractDataFeed
{
/**
* @param string $userName
*
* @return \StdClass
*
* @throws InstagramFetchException
Expand All @@ -27,7 +27,15 @@ public function fetchData(string $userName): \StdClass
'cookies' => $this->session->getCookies()
];

$res = $this->client->request('GET', $endpoint, $headers);
try {
$res = $this->client->request('GET', $endpoint, $headers);
} catch (ClientException $exception) {
if ($exception->getCode() === 404) {
throw new InstagramFetchException('User' . $userName . ' not found');
} else {
throw new InstagramFetchException('Internal error');
}
}

$html = (string)$res->getBody();

Expand Down
52 changes: 52 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,56 @@ public function testErrorWithGetFollowingsFeed()

$api->getFollowings(1234567);
}

public function testNotFoundOnProfile()
{
$this->expectException(InstagramFetchException::class);

$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/cache');

$mock = new MockHandler([
new Response(200, ['Set-Cookie' => 'cookie'], file_get_contents(__DIR__ . '/fixtures/home.html')),
new Response(200, [], file_get_contents(__DIR__ . '/fixtures/login-success.json')),
new Response(404, [], file_get_contents(__DIR__ . '/fixtures/profile.html')),
]);

$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);

$api = new Api($cachePool, $client);

// clear cache
$api->logout('username');

$api->login('username', 'password');
$api->getProfile('odazdozajdoazjdazodjzaodazjdazod');

$api->logout('username');
}

public function testInternalErrorOnProfile()
{
$this->expectException(InstagramFetchException::class);

$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/cache');

$mock = new MockHandler([
new Response(200, ['Set-Cookie' => 'cookie'], file_get_contents(__DIR__ . '/fixtures/home.html')),
new Response(200, [], file_get_contents(__DIR__ . '/fixtures/login-success.json')),
new Response(429, [], file_get_contents(__DIR__ . '/fixtures/profile.html')),
]);

$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);

$api = new Api($cachePool, $client);

// clear cache
$api->logout('username');

$api->login('username', 'password');
$api->getProfile('odazdozajdoazjdazodjzaodazjdazod');

$api->logout('username');
}
}

0 comments on commit a8e19f2

Please sign in to comment.