Skip to content

Commit

Permalink
Fix crash when package is not listed on Packagist
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDesmond-ca committed Nov 24, 2021
1 parent 45dd897 commit 9ac609c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Arguments:
- `path to project`: required, directory containing `composer.json` and `composer.lock` files
- `-q`: optional, quiet mode will only output libraries which are not up-to-date (that is, where "Libyears Behind" > 0)

### Limitations

- Currently only packages listed on Packagist are supported (feel free to submit a PR for issue #1)

## Contributing

Please be sure to read and follow ecoAPM's [Contribution Guidelines](CONTRIBUTING.md) when submitting issues or pull requests.
Expand Down
11 changes: 4 additions & 7 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
namespace LibYear;

use GuzzleHttp\Client;
use LibYear\Calculator;
use LibYear\ComposerFile;
use LibYear\FileSystem;
use LibYear\PackagistAPI;
use function cli\err;

class Factory
{
public static function App()
{
public static function App(): App
{
$fs = new FileSystem();
$file = new ComposerFile($fs);

$http = new Client();
$api = new PackagistAPI($http);
$api = new PackagistAPI($http, STDERR);

$calculator = new Calculator($file, $api);
return new App($calculator, STDOUT);
Expand Down
35 changes: 25 additions & 10 deletions src/PackagistAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@
namespace LibYear;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;

class PackagistAPI
{
private ClientInterface $http_client;
private ClientInterface $http_client;

public function __construct(ClientInterface $http_client)
{
$this->http_client = $http_client;
}
/** @var resource */
private $stderr;

public function getPackageInfo(string $package): array
{
$response = $this->http_client->request('GET', "https://repo.packagist.org/packages/{$package}.json");
return json_decode($response->getBody()->getContents(), true) ?? [];
}
/**
* @param ClientInterface $http_client
* @param resource $stderr
*/
public function __construct(ClientInterface $http_client, $stderr)
{
$this->http_client = $http_client;
$this->stderr = $stderr;
}

public function getPackageInfo(string $package): array
{
try {
$response = $this->http_client->request('GET', "https://repo.packagist.org/packages/{$package}.json");
return json_decode($response->getBody()->getContents(), true) ?? [];

} catch (GuzzleException) {
fwrite($this->stderr, "Could not find info for {$package} on Packagist\n");
return [];
}
}
}
41 changes: 22 additions & 19 deletions tests/PackagistAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ public function testCallsCorrectURL()
//arrange
$http_client = Mockery::mock(ClientInterface::class, [
'request' => Mockery::mock(ResponseInterface::class, [
'getStatusCode' => 200,
'getBody' => Mockery::mock(StreamInterface::class, [
'getContents' => json_encode(['test_field' => 'test value'])
])
])
]);
$api = new PackagistAPI($http_client);
$api = new PackagistAPI($http_client, STDERR);

//act
$package_info = $api->getPackageInfo('vendor_name/package_name');
Expand All @@ -38,12 +39,13 @@ public function testCanGetPackageInfo()
//arrange
$http_client = Mockery::mock(ClientInterface::class, [
'request' => Mockery::mock(ResponseInterface::class, [
'getBody' => Mockery::mock(StreamInterface::class, [
'getStatusCode' => 200,
'getBody' => Mockery::mock(StreamInterface::class, [
'getContents' => json_encode(['test_field' => 'test value'])
])
])
]);
$api = new PackagistAPI($http_client);
$api = new PackagistAPI($http_client, STDERR);

//act
$package_info = $api->getPackageInfo('vendor_name/package_name');
Expand All @@ -52,22 +54,23 @@ public function testCanGetPackageInfo()
$this->assertEquals('test value', $package_info['test_field']);
}

public function testCanHandleBadResponse()
{
//arrange
$http_client = Mockery::mock(ClientInterface::class, [
'request' => Mockery::mock(ResponseInterface::class, [
'getBody' => Mockery::mock(StreamInterface::class, [
'getContents' => '<html>This is not valid JSON</html>'
])
])
]);
$api = new PackagistAPI($http_client);
public function testCanHandleBadResponse()
{
//arrange
$http_client = Mockery::mock(ClientInterface::class, [
'request' => Mockery::mock(ResponseInterface::class, [
'getStatusCode' => 200,
'getBody' => Mockery::mock(StreamInterface::class, [
'getContents' => '<html>This is not valid JSON</html>'
])
])
]);
$api = new PackagistAPI($http_client, STDERR);

//act
$package_info = $api->getPackageInfo('vendor_name/package_name');
//act
$package_info = $api->getPackageInfo('vendor_name/package_name');

//assert
$this->assertEquals([], $package_info);
}
//assert
$this->assertEquals([], $package_info);
}
}

0 comments on commit 9ac609c

Please sign in to comment.