Skip to content

Commit

Permalink
PHP 8.0 compatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-perl committed Jan 21, 2022
1 parent 4082dee commit 75850a1
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 95 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,27 @@ API-Key
-----------
Get a free API key here: <https://gender-api.com/en/account>

PHPUnit
Development
------------

Start the dockerized development machine with
```
gender-api-client$ ./bin/phpunit
docker-compose up
```

Install all required packages
```
bin/composer install
```

Run all unit tests with mock data
```
bin/phpunit
```

Run all unit tests against the API
```
API_KEY=<yourkey> bin/phpunit
```

Simple Usage
Expand Down
5 changes: 5 additions & 0 deletions bin/composer
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

cd "$(dirname "$0")"

docker exec --workdir /www -it gender-api-client /usr/bin/composer "$@"
2 changes: 1 addition & 1 deletion bin/phpunit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

cd "$(dirname "$0")"

../vendor/phpunit/phpunit/phpunit ../tests "$@"
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 "$@"
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
}
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "9.*"
},
"require": {
"php": ">=5.6.0"
"php": ">=8.0.0",
"ext-mbstring": "*",
"ext-fileinfo": "*"
}
}
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ./docker-compose.yml

version: '3.7'

services:
php:
image: phpdockerio/php:8.1-fpm
container_name: gender-api-client
volumes:
- ./.:/www

2 changes: 1 addition & 1 deletion src/GenderApi/Client/Downloader/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function download($url)

if ($response === false || $responseCode != 200) {
$lastError = curl_error($this->curl);
throw new NetworkErrorException($lastError);
throw new NetworkErrorException($lastError . ' - ' . $url);
}

return $response;
Expand Down
66 changes: 24 additions & 42 deletions tests-integration/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,9 @@ private function _removeBom()
}


/**
* Constructor
*
* @param string $file CSV Datei
* @param string $delimiter
*/

public function __construct($file, $delimiter = 'auto')
{
ini_set("auto_detect_line_endings", true);

$this->_filePath = $file;

$finfo = new \finfo(FILEINFO_MIME_TYPE);
Expand Down Expand Up @@ -203,10 +196,8 @@ public function detectDelimiter()
/**
* @return int
*/
public function count()
public function count(): int
{
// return max(0, count(file($this->_filePath)) - 1);

$handle = $this->fopen();
$count = 0;

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

return max(0, $count - 1);

}

/**
* Iterator Funktion zum zurücksetzen des Arrays
*
* @return void
*/
public function rewind()
public function rewind(): void
{
$this->_index = 1;

Expand All @@ -239,30 +228,32 @@ public function rewind()
}

/**
* Iterator Funktion, gibt das aktuelle Element zurück
*
* @return array
* @return array|null
* @throws Exception
*/
public function current()
public function current(): ?array
{
if (is_resource($this->_file)) {
$values = $this->mapLabelsToValues(fgetcsv($this->_file, self::MAX_LINE_LENGTH, $this->_delimiter));
$values = $this->mapLabelsToValues(fgetcsv($this->_file, self::MAX_LINE_LENGTH, $this->_delimiter, '"', ''));
return $values;
}

return null;
}

/**
* Mapt die Labels auf die Datensätze
* Map labels to values
*
* @param mixed $data
* @return mixed
* @throws Exception
*/
private function mapLabelsToValues($data)
{
$result = array();

if (!is_array($data)) {
return $data;
return $result;
}

$mapped = array();
Expand Down Expand Up @@ -295,7 +286,6 @@ private function mapLabelsToValues($data)
throw new \Exception($msg);
}

$result = array();
foreach ($this->_labels as $id => $label) {
if (isset($mapped[$id])) {
$result[$label] = $mapped[$id];
Expand All @@ -306,45 +296,37 @@ private function mapLabelsToValues($data)
}

/**
* Iterator Funktion, gibt den aktuellen Index zurück
*
* @return int
*/
public function key()
public function key(): string|int|null
{
return $this->_index;
}

/**
* Zum nächsten Element springen
*
* @return boolean true wenn noch ein element vorhanden ist
* Jump to the next element
*/
public function next($index = true)
public function next(): void
{
if (is_resource($this->_file)) {
if ($index) {
++$this->_index;
}
return !feof($this->_file);
++$this->_index;
}
return false;
}

/**
* Iterator Funktion, prüft ob Element gültig
*
* @return boolean
*/
public function valid()
public function valid(): bool
{
if (!$this->next(false)) {
if (is_resource($this->_file)) {
if (is_resource($this->_file)) {
if (!feof($this->_file)) {
return true;
} else {
$this->fclose($this->_file);
}
return false;
}
return true;

return false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests-integration/run-test-first-last-name.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$fails = 0;
$failures = [];
foreach ($csvFile as $line) {
if ($line['name']) {
if ($line && isset($line['name']) && $line['name']) {
echo $line['name'] . ' - ';
$result = $client->getByFirstNameAndLastName($line['name']);
if ($result->getFirstName() == $line['correct_first_name'] && $result->getLastName() == $line['correct_last_name']) {
Expand Down
21 changes: 9 additions & 12 deletions tests/GenderApi/Client/CountryOfOriginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
class CountryOfOriginTest extends TestCase
{

/**
*
*/
public function testGetCountryOfOrigin()
{
$genderApiClient = $this->getClient();
Expand All @@ -26,16 +23,16 @@ public function testGetCountryOfOrigin()
->willReturn('{
"name": "markus",
"country_of_origin": [
{
"country_name": "Austria",
"country": "AT",
{
"country_name": "Germany",
"country": "DE",
"probability": 0.29,
"continental_region": "Europe",
"statistical_region": "Western Europe"
},
{
"country_name": "Germany",
"country": "DE",
"country_name": "Austria",
"country": "AT",
"probability": 0.15,
"continental_region": "Europe",
"statistical_region": "Western Europe"
Expand All @@ -62,18 +59,18 @@ public function testGetCountryOfOrigin()
$this->assertEquals('markus', $result->getName());
$this->assertEquals('male', $result->getGender());
$this->assertEquals(99, $result->getAccuracy());
$this->assertContains('https://beta.gender-api.com/en/map/', $result->getCountryOfOriginMapUurl());
$this->assertStringContainsString('/en/map/', $result->getCountryOfOriginMapUrl());

$countryOfOrigin = $result->getCountryOfOrigin();
$this->assertEquals('AT', $countryOfOrigin[0]->getCountry());
$this->assertEquals('Austria', $countryOfOrigin[0]->getCountryName());
$this->assertEquals('DE', $countryOfOrigin[0]->getCountry());
$this->assertEquals('Germany', $countryOfOrigin[0]->getCountryName());
$this->assertEquals('Western Europe', $countryOfOrigin[0]->getStatisticalRegion());
$this->assertEquals('Europe', $countryOfOrigin[0]->getContinentalRegion());

if ($this->doMock) {
$this->assertEquals(26494, $result->getSamples());
$this->assertEquals(82, $result->getDurationInMs());
$this->assertEquals('https://beta.gender-api.com/en/map/19/35a978bd6265e1a8', $result->getCountryOfOriginMapUurl());
$this->assertStringContainsString('en/map/19/35a978bd6265e1a8', $result->getCountryOfOriginMapUrl());
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/GenderApi/Client/Downloader/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public function testDownload()

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

/**
* @expectedException \GenderApi\Client\Downloader\NetworkErrorException
*/
public function testDownloadNetworkError()
{
$this->expectException(\GenderApi\Client\Downloader\NetworkErrorException::class);
$curl = new Curl();

if ($this->doMock) {
Expand Down
6 changes: 2 additions & 4 deletions tests/GenderApi/Client/Downloader/FileGetContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ public function testDownload()
$response = $fgt->download('https://gender-api.com/get?name=markus&key=' . $this->apiKey);
}

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

/**
* @expectedException \GenderApi\Client\Downloader\NetworkErrorException
*/
public function testDownloadNetworkError()
{
$this->expectException(\GenderApi\Client\Downloader\NetworkErrorException::class);
$fgt = new FileGetContents();

if ($this->doMock) {
Expand Down
5 changes: 2 additions & 3 deletions tests/GenderApi/Client/EmailAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ public function testGetByEmailAddressWithoutCountry()
$genderApiClient = $this->getClient();

if ($this->doMock) {
/* @var FileGetContents|\PHPUnit_Framework_MockObject_MockObject $downloader */
$downloader = $this->createMock(FileGetContents::class);
$downloader->method('download')
->willReturn('{"email":"[email protected]","lastname":null,"mailprovider":"gmail","name":"elisabeth","gender":"female","samples":17296,"accuracy":98,"duration":"20ms"}');
->willReturn('{"email":"[email protected]","lastname":null,"mailprovider":"gmail","name":"elisabeth","gender":"female","samples":17296,"accuracy":99,"duration":"20ms"}');
$genderApiClient->setDownloader($downloader);
}

$result = $genderApiClient->getByEmailAddress('[email protected]');

$this->assertEquals('elisabeth', $result->getName());
$this->assertEquals('female', $result->getGender());
$this->assertEquals(98, $result->getAccuracy());
$this->assertEquals(99, $result->getAccuracy());
$this->assertEquals('[email protected]', $result->getEmailAddress());
$this->assertEquals(null, $result->getLastName());
$this->assertEquals('gmail', $result->getMailProvider());
Expand Down
Loading

0 comments on commit 75850a1

Please sign in to comment.