Skip to content

Commit 75850a1

Browse files
committed
PHP 8.0 compatiblity
1 parent 4082dee commit 75850a1

File tree

15 files changed

+90
-95
lines changed

15 files changed

+90
-95
lines changed

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,27 @@ API-Key
2424
-----------
2525
Get a free API key here: <https://gender-api.com/en/account>
2626

27-
PHPUnit
27+
Development
2828
------------
2929

30+
Start the dockerized development machine with
3031
```
31-
gender-api-client$ ./bin/phpunit
32+
docker-compose up
33+
```
34+
35+
Install all required packages
36+
```
37+
bin/composer install
38+
```
39+
40+
Run all unit tests with mock data
41+
```
42+
bin/phpunit
43+
```
44+
45+
Run all unit tests against the API
46+
```
47+
API_KEY=<yourkey> bin/phpunit
3248
```
3349

3450
Simple Usage

bin/composer

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
cd"$(dirname "$0")"
4+
5+
docker exec --workdir /www -it gender-api-client /usr/bin/composer "$@"

bin/phpunit

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
cd"$(dirname "$0")"
44

5-
../vendor/phpunit/phpunit/phpunit ../tests "$@"
5+
docker exec -e API_KEY -e API_URL -e PROXY_HOST -e PROXY_PORT -it gender-api-client /www/vendor/phpunit/phpunit/phpunit /www/tests "$@"

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
}
1212
},
1313
"require-dev": {
14-
"phpunit/phpunit": "^5.7"
14+
"phpunit/phpunit": "9.*"
1515
},
1616
"require": {
17-
"php": ">=5.6.0"
17+
"php": ">=8.0.0",
18+
"ext-mbstring": "*",
19+
"ext-fileinfo": "*"
1820
}
1921
}

docker-compose.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ./docker-compose.yml
2+
3+
version: '3.7'
4+
5+
services:
6+
php:
7+
image: phpdockerio/php:8.1-fpm
8+
container_name: gender-api-client
9+
volumes:
10+
- ./.:/www
11+

src/GenderApi/Client/Downloader/Curl.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function download($url)
4848

4949
if ($response === false || $responseCode != 200) {
5050
$lastError = curl_error($this->curl);
51-
thrownewNetworkErrorException($lastError);
51+
thrownewNetworkErrorException($lastError . ' - ' . $url);
5252
}
5353

5454
return$response;

tests-integration/CsvReader.php

+24-42
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,9 @@ private function _removeBom()
105105
}
106106

107107

108-
/**
109-
* Constructor
110-
*
111-
* @param string $file CSV Datei
112-
* @param string $delimiter
113-
*/
108+
114109
publicfunction__construct($file, $delimiter = 'auto')
115110
{
116-
ini_set("auto_detect_line_endings", true);
117-
118111
$this->_filePath = $file;
119112

120113
$finfo = new \finfo(FILEINFO_MIME_TYPE);
@@ -203,10 +196,8 @@ public function detectDelimiter()
203196
/**
204197
* @return int
205198
*/
206-
publicfunctioncount()
199+
publicfunctioncount(): int
207200
{
208-
// return max(0, count(file($this->_filePath)) - 1);
209-
210201
$handle = $this->fopen();
211202
$count = 0;
212203

@@ -219,14 +210,12 @@ public function count()
219210
$this->fclose($handle);
220211

221212
returnmax(0, $count - 1);
222-
223213
}
224214

225215
/**
226-
* Iterator Funktion zum zurücksetzen des Arrays
227-
*
216+
* @return void
228217
*/
229-
publicfunctionrewind()
218+
publicfunctionrewind(): void
230219
{
231220
$this->_index = 1;
232221

@@ -239,30 +228,32 @@ public function rewind()
239228
}
240229

241230
/**
242-
* Iterator Funktion, gibt das aktuelle Element zurück
243-
*
244-
* @return array
231+
* @return array|null
232+
* @throws Exception
245233
*/
246-
publicfunctioncurrent()
234+
publicfunctioncurrent(): ?array
247235
{
248236
if (is_resource($this->_file)) {
249-
$values = $this->mapLabelsToValues(fgetcsv($this->_file, self::MAX_LINE_LENGTH, $this->_delimiter));
237+
$values = $this->mapLabelsToValues(fgetcsv($this->_file, self::MAX_LINE_LENGTH, $this->_delimiter, '"', ''));
250238
return$values;
251239
}
252240

253241
returnnull;
254242
}
255243

256244
/**
257-
* Mapt die Labels auf die Datensätze
245+
* Map labels to values
258246
*
259247
* @param mixed $data
260248
* @return mixed
249+
* @throws Exception
261250
*/
262251
privatefunctionmapLabelsToValues($data)
263252
{
253+
$result = array();
254+
264255
if (!is_array($data)) {
265-
return$data;
256+
return$result;
266257
}
267258

268259
$mapped = array();
@@ -295,7 +286,6 @@ private function mapLabelsToValues($data)
295286
thrownew \Exception($msg);
296287
}
297288

298-
$result = array();
299289
foreach ($this->_labelsas$id => $label) {
300290
if (isset($mapped[$id])) {
301291
$result[$label] = $mapped[$id];
@@ -306,45 +296,37 @@ private function mapLabelsToValues($data)
306296
}
307297

308298
/**
309-
* Iterator Funktion, gibt den aktuellen Index zurück
310-
*
311299
* @return int
312300
*/
313-
publicfunctionkey()
301+
publicfunctionkey(): string|int|null
314302
{
315303
return$this->_index;
316304
}
317305

318306
/**
319-
* Zum nächsten Element springen
320-
*
321-
* @return boolean true wenn noch ein element vorhanden ist
307+
* Jump to the next element
322308
*/
323-
publicfunctionnext($index = true)
309+
publicfunctionnext(): void
324310
{
325311
if (is_resource($this->_file)) {
326-
if ($index) {
327-
++$this->_index;
328-
}
329-
return !feof($this->_file);
312+
++$this->_index;
330313
}
331-
returnfalse;
332314
}
333315

334316
/**
335-
* Iterator Funktion, prüft ob Element gültig
336-
*
337317
* @return boolean
338318
*/
339-
publicfunctionvalid()
319+
publicfunctionvalid(): bool
340320
{
341-
if (!$this->next(false)) {
342-
if (is_resource($this->_file)) {
321+
if (is_resource($this->_file)) {
322+
if (!feof($this->_file)) {
323+
returntrue;
324+
} else {
343325
$this->fclose($this->_file);
344326
}
345-
returnfalse;
346327
}
347-
returntrue;
328+
329+
returnfalse;
348330
}
349331

350332
/**

tests-integration/run-test-first-last-name.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
$fails = 0;
2525
$failures = [];
2626
foreach ($csvFileas$line) {
27-
if ($line['name']) {
27+
if ($line && isset($line['name']) && $line['name']) {
2828
echo$line['name'] . ' - ';
2929
$result = $client->getByFirstNameAndLastName($line['name']);
3030
if ($result->getFirstName() == $line['correct_first_name'] && $result->getLastName() == $line['correct_last_name']) {

tests/GenderApi/Client/CountryOfOriginTest.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
class CountryOfOriginTest extends TestCase
1414
{
1515

16-
/**
17-
*
18-
*/
1916
publicfunctiontestGetCountryOfOrigin()
2017
{
2118
$genderApiClient = $this->getClient();
@@ -26,16 +23,16 @@ public function testGetCountryOfOrigin()
2623
->willReturn('{
2724
"name": "markus",
2825
"country_of_origin": [
29-
{
30-
"country_name": "Austria",
31-
"country": "AT",
26+
{
27+
"country_name": "Germany",
28+
"country": "DE",
3229
"probability": 0.29,
3330
"continental_region": "Europe",
3431
"statistical_region": "Western Europe"
3532
},
3633
{
37-
"country_name": "Germany",
38-
"country": "DE",
34+
"country_name": "Austria",
35+
"country": "AT",
3936
"probability": 0.15,
4037
"continental_region": "Europe",
4138
"statistical_region": "Western Europe"
@@ -62,18 +59,18 @@ public function testGetCountryOfOrigin()
6259
$this->assertEquals('markus', $result->getName());
6360
$this->assertEquals('male', $result->getGender());
6461
$this->assertEquals(99, $result->getAccuracy());
65-
$this->assertContains('https://beta.gender-api.com/en/map/', $result->getCountryOfOriginMapUurl());
62+
$this->assertStringContainsString('/en/map/', $result->getCountryOfOriginMapUrl());
6663

6764
$countryOfOrigin = $result->getCountryOfOrigin();
68-
$this->assertEquals('AT', $countryOfOrigin[0]->getCountry());
69-
$this->assertEquals('Austria', $countryOfOrigin[0]->getCountryName());
65+
$this->assertEquals('DE', $countryOfOrigin[0]->getCountry());
66+
$this->assertEquals('Germany', $countryOfOrigin[0]->getCountryName());
7067
$this->assertEquals('Western Europe', $countryOfOrigin[0]->getStatisticalRegion());
7168
$this->assertEquals('Europe', $countryOfOrigin[0]->getContinentalRegion());
7269

7370
if ($this->doMock) {
7471
$this->assertEquals(26494, $result->getSamples());
7572
$this->assertEquals(82, $result->getDurationInMs());
76-
$this->assertEquals('https://beta.gender-api.com/en/map/19/35a978bd6265e1a8', $result->getCountryOfOriginMapUurl());
73+
$this->assertStringContainsString('en/map/19/35a978bd6265e1a8', $result->getCountryOfOriginMapUrl());
7774
}
7875
}
7976

tests/GenderApi/Client/Downloader/CurlTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public function testDownload()
2020

2121
if (!$this->doMock) {
2222
$response = $curl->download('https://gender-api.com/get?name=markus&key=' . $this->apiKey);
23-
$this->assertContains('gender":"male"', $response);
23+
$this->assertStringContainsString('gender":"male"', $response);
24+
} else {
25+
$this->markTestSkipped('This test is only executed with an API key');
2426
}
2527
}
2628

27-
/**
28-
* @expectedException \GenderApi\Client\Downloader\NetworkErrorException
29-
*/
3029
publicfunctiontestDownloadNetworkError()
3130
{
31+
$this->expectException(\GenderApi\Client\Downloader\NetworkErrorException::class);
3232
$curl = newCurl();
3333

3434
if ($this->doMock) {

tests/GenderApi/Client/Downloader/FileGetContentsTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ public function testDownload()
2424
$response = $fgt->download('https://gender-api.com/get?name=markus&key=' . $this->apiKey);
2525
}
2626

27-
$this->assertContains('gender":"male"', $response);
27+
$this->assertStringContainsString('gender":"male"', $response);
2828
}
2929

30-
/**
31-
* @expectedException \GenderApi\Client\Downloader\NetworkErrorException
32-
*/
3330
publicfunctiontestDownloadNetworkError()
3431
{
32+
$this->expectException(\GenderApi\Client\Downloader\NetworkErrorException::class);
3533
$fgt = newFileGetContents();
3634

3735
if ($this->doMock) {

tests/GenderApi/Client/EmailAddressTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@ public function testGetByEmailAddressWithoutCountry()
2121
$genderApiClient = $this->getClient();
2222

2323
if ($this->doMock) {
24-
/* @var FileGetContents|\PHPUnit_Framework_MockObject_MockObject $downloader */
2524
$downloader = $this->createMock(FileGetContents::class);
2625
$downloader->method('download')
27-
->willReturn('{"email":"elisabeth1499@gmail.com","lastname":null,"mailprovider":"gmail","name":"elisabeth","gender":"female","samples":17296,"accuracy":98,"duration":"20ms"}');
26+
->willReturn('{"email":"elisabeth1499@gmail.com","lastname":null,"mailprovider":"gmail","name":"elisabeth","gender":"female","samples":17296,"accuracy":99,"duration":"20ms"}');
2827
$genderApiClient->setDownloader($downloader);
2928
}
3029

3130
$result = $genderApiClient->getByEmailAddress('elisabeth1499@gmail.com');
3231

3332
$this->assertEquals('elisabeth', $result->getName());
3433
$this->assertEquals('female', $result->getGender());
35-
$this->assertEquals(98, $result->getAccuracy());
34+
$this->assertEquals(99, $result->getAccuracy());
3635
$this->assertEquals('elisabeth1499@gmail.com', $result->getEmailAddress());
3736
$this->assertEquals(null, $result->getLastName());
3837
$this->assertEquals('gmail', $result->getMailProvider());

0 commit comments

Comments
 (0)
close