Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed May 26, 2021
1 parent 37ad5d1 commit 9f17bba
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 99 deletions.
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore

/tests export-ignore
/web export-ignore

/app.yaml export-ignore
/phpunit.xml.dist export-ignore

64 changes: 36 additions & 28 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,64 @@ on:

jobs:
unit-tests:
name: PHP ${{matrix.php}}, ${{matrix.os}}, ${{matrix.stability}} deps
runs-on: ${{matrix.os}}
name: PHP ${{matrix.php}}, ${{matrix.stability}} deps
runs-on: ubuntu-latest

strategy:
matrix:
php: [7.0, 7.1, 7.2, 7.3, 7.4]
os: [ubuntu-latest, windows-latest]
php: [7.2, 7.3, 7.4, 8.0]
stability: [lowest, stable]
exclude:
- stability: lowest
php: 7.1
- stability: lowest
php: 7.2
- stability: lowest
php: 7.3
- stability: lowest
php: 7.4
- os: windows-latest
php: 7.1
- os: windows-latest
php: 7.2
- os: windows-latest
php: 7.3
- os: windows-latest
php: 7.4

env:
extensions: dom, mbstring, xml
key: cache-v1

steps:
- name: Checkout code
uses: actions/checkout@v1
uses: actions/checkout@v2

- name: Setup cache environment
id: cache-env
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ matrix.php }}
extensions: ${{ env.extensions }}
key: ${{ env.key }}

- name: Cache extensions
uses: actions/cache@v2
with:
path: ${{ steps.cache-env.outputs.dir }}
key: ${{ steps.cache-env.outputs.key }}
restore-keys: ${{ steps.cache-env.outputs.key }}

- name: Setup PHP
uses: shivammathur/setup-php@v1
uses: shivammathur/setup-php@v2
with:
php-version: ${{matrix.php}}
extensions: dom, mbstring, xml
php-version: ${{ matrix.php }}
extensions: ${{ env.extensions }}
tools: composer
coverage: none

- name: Setup problem matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{steps.composer-cache.outputs.dir}}
key: ${{matrix.php}}-${{runner.os}}-composer-${{matrix.stability}}-${{hashFiles('**/composer.json')}}
restore-keys: ${{matrix.php}}-${{runner.os}}-composer-${{matrix.stability}}-
key: ${{matrix.php}}-composer-${{matrix.stability}}-${{hashFiles('**/composer.json')}}
restore-keys: ${{matrix.php}}-${{matrix.stability}}-composer-

- name: Install dependencies
run: composer update --prefer-${{matrix.stability}} --prefer-dist --no-interaction --no-suggest --no-progress

- name: Setup Problem Matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Execute tests
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.phpunit.result.cache
composer.lock
vendor
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## Unreleased

* The default branch of the GitHub repository has been renamed from `master` to `main` -
if you're using `dev-master` as a version constraint in your `composer.json`, please
update it to `dev-main`.
* Added support for Guzzle 7
* Raised minimum required Guzzle 6 version from 6.0 to 6.3.3
* Added support for PHP 8
* Dropped support for PHP <7.2

## 1.1.0 - 2020-03-19

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
}
],
"require": {
"php": "^7.0",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"php": "^7.2|^8.0",
"guzzlehttp/guzzle": "^6.3.3|^7.0",
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5.14|^7.5.19"
"phpunit/phpunit": "^8.5.15"
},
"autoload": {
"psr-4": {
Expand Down
34 changes: 17 additions & 17 deletions src/GcpMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Kreait\GcpMetadata\Error;
use Psr\Http\Message\ResponseInterface;

class GcpMetadata
{
const baseUrl = 'http://169.254.169.254/computeMetadata/v1/';
const flavorHeaderName = 'Metadata-Flavor';
const flavorHeaderValue = 'Google';
public const baseUrl = 'http://169.254.169.254/computeMetadata/v1/';
public const flavorHeaderName = 'Metadata-Flavor';
public const flavorHeaderValue = 'Google';

/**
* @var ClientInterface|null
Expand Down Expand Up @@ -72,40 +73,39 @@ private function request(string $type, string $property = '', array $params = []
$options = [
'headers' => [self::flavorHeaderName => self::flavorHeaderValue],
'query' => $params,
'http_errors' => false,
];

try {
$response = $this->client->request('GET', $url, $options);
} catch (ConnectException $e) {
throw new Error('Unable to connect: '.$e->getMessage());
}

$this->verifyHttpStatus($response);
$this->verifyHeaders($response);

return $this->parseResponse($response);
} catch (RequestException $e) {
$message = $e->getMessage();

if ($response = $e->getResponse()) {
$message .= ': '.$response->getBody();
}
$this->verifyHttpStatus($response);
$this->verifyHeaders($response);

throw new Error($message);
}
return $this->parseResponse($response);
}

private function verifyHttpStatus(ResponseInterface $response)
private function verifyHttpStatus(ResponseInterface $response): void
{
if (($statusCode = $response->getStatusCode()) !== 200) {
throw new Error('Unsuccessful response status code: '.$statusCode);
}
}

private function verifyHeaders(ResponseInterface $response)
private function verifyHeaders(ResponseInterface $response): void
{
if ($response->getHeaderLine(self::flavorHeaderName) !== self::flavorHeaderValue) {
throw new Error('"'.self::flavorHeaderName.'" header is missing or incorrect.');
}
}

/**
* @param ResponseInterface $response
* @return string|string[]
*/
private function parseResponse(ResponseInterface $response)
{
$body = trim((string) $response->getBody());
Expand Down
72 changes: 26 additions & 46 deletions tests/GcpMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,29 @@

namespace Kreait;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Kreait\GcpMetadata\Error;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class GcpMetadataTest extends TestCase
{
/**
* @var Client
*/
private $client;

/**
* @var GcpMetadata
*/
private $metadata;

protected function setUp()
protected function setUp(): void
{
$this->client = $this->prophesize(ClientInterface::class);
$this->client = $this->createMock(ClientInterface::class);

$this->metadata = new GcpMetadata($this->client->reveal());
$this->metadata = new GcpMetadata($this->client);
}

/**
* @test
*/
public function it_uses_a_default_client()
public function it_uses_a_default_client(): void
{
$metadata = new GcpMetadata();

Expand All @@ -51,79 +38,72 @@ public function it_uses_a_default_client()
/**
* @test
*/
public function it_is_available()
public function it_is_available(): void
{
$this->client->request(Argument::cetera())->willReturn($this->createResponse());
$this->client->method('request')->willReturn($this->createResponse());

$this->assertTrue($this->metadata->isAvailable());
}

/**
* @test
*/
public function it_is_not_available()
public function it_is_not_available(): void
{
$this->client->request(Argument::cetera())->willThrow(ConnectException::create(new Request('GET', GcpMetadata::baseUrl)));
$this->client->method('request')
->willThrowException(new ConnectException('Connection refused', new Request('GET', GcpMetadata::baseUrl)));

$this->assertFalse($this->metadata->isAvailable());
}

/**
* @test
*/
public function it_is_requires_certain_http_response_headers()
public function it_is_requires_certain_http_response_headers(): void
{
$this->client->request(Argument::cetera())->willReturn(new Response(200));
$this->client->method('request')->willReturn(new Response(200));

$this->assertFalse($this->metadata->isAvailable());
}

/**
* @test
*/
public function it_requires_a_successful_http_response()
public function it_requires_a_successful_http_response(): void
{
$this->client->request(Argument::cetera())->willReturn($this->createResponse(500, 'details'));
$this->client->method('request')->willReturn($this->createResponse(500, 'details'));

$this->expectException(Error::class);
$this->metadata->instance();
}

/**
* @test
* @dataProvider responseStrings
*/
public function it_catches_unexpected_http_errors()
public function it_parses_http_responses_containing($expectedResult, $responseString): void
{
$request = $this->createMock(RequestInterface::class);

$responseBody = $this->prophesize(StreamInterface::class);
$responseBody->__toString()->willReturn('foo');

$response = $this->prophesize(ResponseInterface::class);
$response->getStatusCode()->willReturn(500);
$response->getBody()->willReturn($responseBody->reveal());

$e = new RequestException('Foo', $request, $response->reveal());
$this->client->method('request')->willReturn($this->createResponse(200, $responseString));

$this->client->request(Argument::cetera())->willThrow($e);

$this->expectException(Error::class);
$this->metadata->instance();
$this->assertSame($expectedResult, $this->metadata->instance('foo'));
$this->assertSame($expectedResult, $this->metadata->project('foo'));
}

/**
* @test
* @dataProvider responseStrings
*/
public function it_parses_http_responses_containing($expectedResult, $responseString)
public function it_caches_its_results(): void
{
$this->client->request(Argument::cetera())->willReturn($this->createResponse(200, $responseString));
$this->client
->expects($this->once())
->method('request')
->willReturn($this->createResponse());

$this->assertSame($expectedResult, $this->metadata->instance('foo'));
$this->assertSame($expectedResult, $this->metadata->project('foo'));
$this->metadata->isAvailable();
$this->metadata->isAvailable();
}

public function responseStrings()
public function responseStrings(): array
{
return [
'an empty body' => ['', null],
Expand Down

0 comments on commit 9f17bba

Please sign in to comment.