Skip to content

Commit 4ec44b0

Browse files
author
Evert Harmeling
authored
Merge pull request #20 from evertharmeling/develop
Bumped supported versions of httplug + upgraded phpunit version (and …
2 parents 355b0ea + 976ac49 commit 4ec44b0

File tree

4 files changed

+102
-94
lines changed

4 files changed

+102
-94
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.phpunit.result.cache
12
composer.lock
23
coverage
34
phpunit.xml

.travis.yml

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
language: php
2+
sudo: false
3+
cache:
4+
directories:
5+
- $HOME/.composer/cache/files
26

3-
php:
4-
- 5.5
5-
- 5.6
6-
- 7.0
7-
- 7.1
7+
env:
8+
global:
9+
- PHPUNIT_FLAGS="-v -c ."
10+
- SYMFONY_DEPRECATIONS_HELPER="max[self]=0"
811

9-
before_script:
10-
- composer install
12+
matrix:
13+
fast_finish: true
14+
include:
15+
# Minimum supported dependencies with the latest and oldest PHP version
16+
- php: 7.3
17+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
18+
- php: 7.1
19+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
1120

12-
script: phpunit --coverage-text
21+
# Test the latest stable release
22+
- php: 7.1
23+
- php: 7.2
24+
- php: 7.3
25+
env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-text"
26+
27+
# Latest commit to master
28+
- php: 7.3
29+
env: STABILITY="dev"
30+
31+
allow_failures:
32+
# Dev-master is allowed to fail.
33+
- env: STABILITY="dev"
34+
35+
before_install:
36+
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
37+
- if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi;
38+
39+
install:
40+
- composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction
41+
42+
script:
43+
- composer validate --strict --no-check-lock
44+
- ./vendor/bin/phpunit $PHPUNIT_FLAGS
1345

1446
notifications:
15-
email: "travis-ci@freshheads.com"
47+
email: [ 'travis-ci@freshheads.com' ]

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=5.4.0",
20-
"php-http/httplug": "^1.1",
19+
"php": "^7.1",
20+
"php-http/httplug": "^2.0",
2121
"guzzlehttp/psr7": "^1.3"
2222
},
2323
"require-dev": {
24-
"phpunit/phpunit": "^4.3.5|^5.0",
25-
"php-http/mock-client": "^0.3.0"
24+
"phpunit/phpunit": "^7.5 || ^8.2",
25+
"php-http/mock-client": "^1.3.0"
2626
},
2727
"suggest": {
2828
"php-http/guzzle6-adapter": "An HTTPlug adapter for the Guzzle 6 HTTP client"
@@ -36,6 +36,6 @@
3636
}
3737
},
3838
"scripts": {
39-
"test": "./vendor/phpunit/phpunit/phpunit --coverage-text"
39+
"test": "./vendor/bin/phpunit"
4040
}
4141
}

tests/FH/PostcodeAPI/ClientTest.php

Lines changed: 55 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,41 @@
33
namespace FH\PostcodeAPI\Test;
44

55
use FH\PostcodeAPI\Client;
6+
use FH\PostcodeAPI\Exception\InvalidApiKeyException;
7+
use FH\PostcodeAPI\Exception\ServerErrorException;
68
use GuzzleHttp\Psr7\Response;
9+
use Http\Mock\Client as MockClient;
10+
use PHPUnit\Framework\Assert;
11+
use PHPUnit\Framework\TestCase;
12+
use stdClass;
13+
14+
use function GuzzleHttp\Psr7\parse_response;
715

816
/**
917
* @author Gijs Nieuwenhuis <gijs.nieuwenhuis@freshheads.com>
1018
*/
11-
final class ClientTest extends \PHPUnit_Framework_TestCase
19+
final class ClientTest extends TestCase
1220
{
13-
/** @var string */
14-
const POSTCODE_PATTERN = '/^[\d]{4}[\w]{2}$/i';
15-
16-
/** @var string */
17-
const FRESHHEADS_POSTCODE = '5041EB';
18-
19-
/** @var int */
20-
const FRESHHEADS_NUMBER = 21;
21-
22-
/** @var string */
23-
const FRESHHEADS_CITY = 'Tilburg';
24-
25-
/** @var float */
26-
const FRESHHEADS_LONGITUDE = 5.07717893166;
27-
28-
/** @var float */
29-
const FRESHHEADS_LATITUDE = 51.566414786;
30-
31-
/** @var string */
32-
const FRESHHEADS_ADDRESS_ID = '0855200000061001';
33-
34-
/**
35-
* @expectedException FH\PostcodeAPI\Exception\InvalidApiKeyException
36-
*/
37-
public function testRequestExceptionIsThrownWhenUsingAnInvalidApiKey()
21+
private const POSTCODE_PATTERN = '/^[\d]{4}[\w]{2}$/i';
22+
private const FRESHHEADS_POSTCODE = '5041EB';
23+
private const FRESHHEADS_NUMBER = 21;
24+
private const FRESHHEADS_CITY = 'Tilburg';
25+
private const FRESHHEADS_LONGITUDE = 5.07717893166;
26+
private const FRESHHEADS_LATITUDE = 51.566414786;
27+
private const FRESHHEADS_ADDRESS_ID = '0855200000061001';
28+
29+
public function testRequestExceptionIsThrownWhenUsingAnInvalidApiKey(): void
3830
{
31+
$this->expectException(InvalidApiKeyException::class);
32+
3933
$client = $this->createClient(
4034
$this->loadMockResponse('failed_list_with_invalid_api_key')
4135
);
4236

4337
$client->getAddresses();
4438
}
4539

46-
public function testListResourceReturnsAllAddressesWhenNoParamsAreSupplied()
40+
public function testListResourceReturnsAllAddressesWhenNoParamsAreSupplied(): void
4741
{
4842
$client = $this->createClient(
4943
$this->loadMockResponse('successful_list_without_filtering')
@@ -55,12 +49,12 @@ public function testListResourceReturnsAllAddressesWhenNoParamsAreSupplied()
5549

5650
$addresses = $response->_embedded->addresses;
5751

58-
static::assertGreaterThan(0, count($addresses), 'Expecting that there are always addresses available');
52+
Assert::assertGreaterThan(0, count($addresses), 'Expecting that there are always addresses available');
5953

6054
$this->applyAddressFieldAreSetAndOfTheCorrectTypeAssertions($addresses[0]);
6155
}
6256

63-
public function testListResourceReturnsExpectedAddressWhenPostcodeAndNumberAreSupplied()
57+
public function testListResourceReturnsExpectedAddressWhenPostcodeAndNumberAreSupplied(): void
6458
{
6559
$client = $this->createClient(
6660
$this->loadMockResponse('successful_list_freshheads_postcode_and_number')
@@ -72,15 +66,15 @@ public function testListResourceReturnsExpectedAddressWhenPostcodeAndNumberAreSu
7266

7367
$addresses = $response->_embedded->addresses;
7468

75-
static::assertGreaterThan(0, count($addresses), 'Expecting that there are always addresses available when no filters are applied');
69+
Assert::assertGreaterThan(0, count($addresses), 'Expecting that there are always addresses available when no filters are applied');
7670

7771
$firstAddress = $addresses[0];
7872

7973
$this->applyAddressFieldAreSetAndOfTheCorrectTypeAssertions($firstAddress);
8074
$this->applyIsFreshheadsAddressAssertions($firstAddress);
8175
}
8276

83-
public function testExpectedAddressInformationIsReturnedFromDetailResource()
77+
public function testExpectedAddressInformationIsReturnedFromDetailResource(): void
8478
{
8579
$client = $this->createClient(
8680
$this->loadMockResponse('successful_detail_freshheads')
@@ -92,97 +86,78 @@ public function testExpectedAddressInformationIsReturnedFromDetailResource()
9286
$this->applyIsFreshheadsAddressAssertions($address);
9387
}
9488

95-
/**
96-
* @expectedException FH\PostcodeAPI\Exception\ServerErrorException
97-
*/
98-
public function testClientThrowsExceptionWhenInvalidInputIsSupplied()
89+
public function testClientThrowsExceptionWhenInvalidInputIsSupplied(): void
9990
{
91+
$this->expectException(ServerErrorException::class);
92+
10093
$client = $this->createClient(
10194
$this->loadMockResponse('failed_list_with_invalid_postalcode_and_number')
10295
);
10396

10497
$client->getAddresses('invalid_postcode', 'invalid_number');
10598
}
10699

107-
/**
108-
* @param string $name
109-
*
110-
* @return string
111-
*/
112-
private function loadMockResponse($name)
100+
private function loadMockResponse(string $name): Response
113101
{
114-
return \GuzzleHttp\Psr7\parse_response(file_get_contents(__DIR__ . "/../../Mock/{$name}"));
102+
return parse_response(file_get_contents(__DIR__ . "/../../Mock/{$name}"));
115103
}
116104

117-
/**
118-
* @param \stdClass $address
119-
*/
120-
private function applyIsFreshheadsAddressAssertions(\stdClass $address)
105+
private function applyIsFreshheadsAddressAssertions(stdClass $address): void
121106
{
122-
static::assertSame(strtoupper($address->postcode), self::FRESHHEADS_POSTCODE, 'Incoming postcode did not match the expected postcode');
123-
static::assertSame((string)$address->number, (string)self::FRESHHEADS_NUMBER, 'Incoming number did not match the expected number');
124-
static::assertSame($address->city->label, self::FRESHHEADS_CITY, 'Incoming city did not match the expected city');
107+
Assert::assertSame(strtoupper($address->postcode), self::FRESHHEADS_POSTCODE, 'Incoming postcode did not match the expected postcode');
108+
Assert::assertSame((string)$address->number, (string)self::FRESHHEADS_NUMBER, 'Incoming number did not match the expected number');
109+
Assert::assertSame($address->city->label, self::FRESHHEADS_CITY, 'Incoming city did not match the expected city');
125110

126111
// use number_format number rounding to allow for minor changes between expected and actual value
127112
$wgs84 = $address->geo->center->wgs84;
128113

129-
static::assertSame(
114+
Assert::assertSame(
130115
number_format($wgs84->coordinates[0], 5),
131116
number_format(self::FRESHHEADS_LONGITUDE, 5),
132117
'Incoming longitude did not match the expected value'
133118
);
134-
static::assertSame(
119+
120+
Assert::assertSame(
135121
number_format($wgs84->coordinates[1], 5),
136122
number_format(self::FRESHHEADS_LATITUDE, 5),
137123
'Incoming latitude did not match the expected value'
138124
);
139125
}
140126

141-
/**
142-
* @param \stdClass $response
143-
*/
144-
private function applyAssertsToMakeSureAddressesArrayIsAvailableInResponse(\stdClass $response)
127+
private function applyAssertsToMakeSureAddressesArrayIsAvailableInResponse(stdClass $response): void
145128
{
146-
static::assertTrue(isset($response->_embedded->addresses));
147-
static::assertTrue(is_array($response->_embedded->addresses));
129+
Assert::assertTrue(isset($response->_embedded->addresses));
130+
Assert::assertIsArray($response->_embedded->addresses);
148131
}
149132

150-
/**
151-
* @param \stdClass $address
152-
*/
153-
private function applyAddressFieldAreSetAndOfTheCorrectTypeAssertions(\stdClass $address)
133+
private function applyAddressFieldAreSetAndOfTheCorrectTypeAssertions(stdClass $address): void
154134
{
155135
// only test the availability of the most import fields and their values
156136

157-
static::assertTrue(isset($address->street), 'Incoming address did not have a street field');
158-
static::assertTrue(is_string($address->street), 'Incoming address did not have a street value of type string');
137+
Assert::assertTrue(isset($address->street), 'Incoming address did not have a street field');
138+
Assert::assertIsString($address->street, 'Incoming address did not have a street value of type string');
159139

160-
static::assertTrue(isset($address->city->label), 'Incoming address did not have a city.label field');
161-
static::assertTrue(is_string($address->city->label), 'Incoming address did not have a city.label value of type string');
140+
Assert::assertTrue(isset($address->city->label), 'Incoming address did not have a city.label field');
141+
Assert::assertIsString($address->city->label, 'Incoming address did not have a city.label value of type string');
162142

163-
static::assertTrue(isset($address->postcode), 'Incoming address did not have a postcode field');
164-
static::assertTrue(preg_match(self::POSTCODE_PATTERN, $address->postcode) === 1, 'Incoming address did not have a postcode value that matches the pattern: ' . self::POSTCODE_PATTERN);
143+
Assert::assertTrue(isset($address->postcode), 'Incoming address did not have a postcode field');
144+
Assert::assertTrue(preg_match(self::POSTCODE_PATTERN, $address->postcode) === 1, 'Incoming address did not have a postcode value that matches the pattern: ' . self::POSTCODE_PATTERN);
165145

166-
static::assertTrue(isset($address->number), 'Incoming address did not have a number field');
167-
static::assertTrue(is_string($address->number) || is_numeric($address->number), 'Incoming address did not have a number field with type string');
146+
Assert::assertTrue(isset($address->number), 'Incoming address did not have a number field');
147+
Assert::assertTrue(is_string($address->number) || is_numeric($address->number), 'Incoming address did not have a number field with type string');
168148

169149
$wgs84 = $address->geo->center->wgs84;
170150

171-
static::assertTrue(isset($wgs84->coordinates[0]), 'Incoming address did not have a longitude field');
172-
static::assertTrue(is_float($address->geo->center->wgs84->coordinates[0]), 'Incoming address did not have a longitude value of type float');
151+
Assert::assertTrue(isset($wgs84->coordinates[0]), 'Incoming address did not have a longitude field');
152+
Assert::assertIsFloat($wgs84->coordinates[0], 'Incoming address did not have a longitude value of type float');
173153

174-
static::assertTrue(isset($wgs84->coordinates[1]), 'Incoming address did not have a latitude field');
175-
static::assertTrue(is_float($address->geo->center->wgs84->coordinates[1]), 'Incoming address did not have a latitude value of type float');
154+
Assert::assertTrue(isset($wgs84->coordinates[1]), 'Incoming address did not have a latitude field');
155+
Assert::assertIsFloat($wgs84->coordinates[1], 'Incoming address did not have a latitude value of type float');
176156
}
177157

178-
/**
179-
* @param string $mockedResponses
180-
*
181-
* @return Client
182-
*/
183-
private function createClient(Response $mockedResponse)
158+
private function createClient(Response $mockedResponse): Client
184159
{
185-
$httpClient = new \Http\Mock\Client();
160+
$httpClient = new MockClient();
186161
$httpClient->addResponse($mockedResponse);
187162

188163
return new Client($httpClient);

0 commit comments

Comments
 (0)