Skip to content

Commit 900b21f

Browse files
authored
feat!: Use pest and remove Laravel 9 (#61)
* feat!: Use pest and remove Laravel 9 * update composer * Fix styling * remove drift * remove drift * update workflow * remove cache
1 parent eaeddee commit 900b21f

File tree

12 files changed

+346
-389
lines changed

12 files changed

+346
-389
lines changed

.github/workflows/run-tests.yml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
name: "Run Tests"
22

3-
on: [push]
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: "0 0 * * *"
48

59
jobs:
610
test:
711

812
runs-on: ubuntu-latest
913
strategy:
10-
fail-fast: true
14+
fail-fast: false
1115
matrix:
12-
php: [8.0, 8.1, 8.2]
13-
laravel: [9.*, 10.*]
14-
dependency-version: [prefer-lowest, prefer-stable]
15-
exclude:
16-
- laravel: 10.*
17-
php: 8.0
16+
php: [8.3, 8.2, 8.1]
17+
laravel: ["^11.0", "^10.0"]
18+
dependency-version: [prefer-lowest, prefer-stable]
19+
exclude:
20+
- laravel: "^11.0"
21+
php: 8.1
1822

1923
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
2024

2125
steps:
2226
- name: Checkout code
2327
uses: actions/checkout@v4
2428

25-
- name: Cache dependencies
26-
uses: actions/cache@v4
27-
with:
28-
path: ~/.composer/cache/files
29-
key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
30-
3129
- name: Setup PHP
3230
uses: shivammathur/setup-php@v2
3331
with:
@@ -41,7 +39,7 @@ jobs:
4139
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
4240
4341
- name: Execute tests
44-
run: vendor/bin/phpunit tests
42+
run: vendor/bin/pest tests
4543

4644
- uses: codecov/codecov-action@v4
4745
with:

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
build
22
composer.lock
33
vendor
4-
.php_cs.cache
54
coverage
6-
.phpunit.result.cache
75
/.idea
86
/.vscode
7+
.phpunit.cache
8+
.phpunit.result.cache
99
.php-cs-fixer.cache
10+
.php_cs.cache
1011
.DS_Store

composer.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
}
1515
],
1616
"require": {
17-
"php": ">=8.0",
18-
"illuminate/support": "^9|^10.0|^11.0",
19-
"illuminate/console": "^9|^10.0|^11.0",
20-
"guzzlehttp/guzzle": "^7.2|^7.3",
21-
"geoip2/geoip2": "^2.13"
17+
"php": "^8.1",
18+
"illuminate/support": "^10.0|^11.0",
19+
"illuminate/console": "^10.0|^11.0",
20+
"guzzlehttp/guzzle": "^7.5|^7.8",
21+
"geoip2/geoip2": "^3.0"
2222
},
2323
"require-dev": {
24-
"phpunit/phpunit": "^9.5.24",
25-
"mockery/mockery": "^1.5"
24+
"mockery/mockery": "^1.6",
25+
"pestphp/pest": "^1.20|^2.0"
2626
},
2727
"autoload": {
2828
"psr-4": {
@@ -48,9 +48,12 @@
4848
}
4949
},
5050
"scripts": {
51-
"test": "vendor/bin/phpunit"
51+
"test": "vendor/bin/pest"
5252
},
5353
"config": {
54-
"sort-packages": true
54+
"sort-packages": true,
55+
"allow-plugins": {
56+
"pestphp/pest-plugin": true
57+
}
5558
}
5659
}

phpunit.xml.dist

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
33
<coverage>
4-
<include>
5-
<directory suffix=".php">src/</directory>
6-
</include>
7-
<exclude>
8-
<file>src/GeoIPServiceProvider.php</file>
9-
<directory suffix=".php">src/config/</directory>
10-
<directory suffix=".php">src/Facades/</directory>
11-
<directory suffix=".php">src/Console/</directory>
12-
</exclude>
134
<report>
145
<clover outputFile="build/logs/clover.xml"/>
156
<html outputDirectory="build/coverage"/>
@@ -25,4 +16,15 @@
2516
<logging>
2617
<junit outputFile="build/report.junit.xml"/>
2718
</logging>
19+
<source>
20+
<include>
21+
<directory suffix=".php">src/</directory>
22+
</include>
23+
<exclude>
24+
<file>src/GeoIPServiceProvider.php</file>
25+
<directory suffix=".php">src/config/</directory>
26+
<directory suffix=".php">src/Facades/</directory>
27+
<directory suffix=".php">src/Console/</directory>
28+
</exclude>
29+
</source>
2830
</phpunit>

src/GeoIPUpdater.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,28 @@ protected function updateMaxmindDatabase()
8282

8383
$dir = head(glob("$tempDir/GeoLite2-City_*"));
8484

85-
@unlink($database);
86-
@unlink($tempFile.'.tar');
85+
$this->removeIfExists($database);
86+
$this->removeIfExists($tempFile.'.tar');
8787

8888
// Save database to final location
8989
rename($dir.'/GeoLite2-City.mmdb', $database);
9090

9191
// Delete temp file
92-
@unlink($tempFile);
92+
$this->removeIfExists($tempFile);
9393

94-
array_map('unlink', glob("$dir/*.*"));
94+
array_map(fn ($file) => $this->removeIfExists($file), glob("$dir/*.*"));
9595
@rmdir($dir);
9696
} catch (Exception $e) {
9797
return false;
9898
}
9999

100100
return $database;
101101
}
102+
103+
protected function removeIfExists(string $file): void
104+
{
105+
if (file_exists($file)) {
106+
unlink($file);
107+
}
108+
}
102109
}

tests/Drivers/IPApiDriverTest.php

Lines changed: 99 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,105 @@
11
<?php
22

3-
namespace PulkitJalan\GeoIP\Tests\Drivers;
4-
53
use Illuminate\Support\Arr;
64
use PulkitJalan\GeoIP\GeoIP;
7-
use PulkitJalan\GeoIP\Tests\AbstractTestCase;
85
use PulkitJalan\GeoIP\Exceptions\GeoIPException;
96

10-
class IPApiDriverTest extends AbstractTestCase
11-
{
12-
public function test_ip_api()
13-
{
14-
$config = [
15-
'driver' => 'ip-api',
16-
];
17-
18-
$geoip = new GeoIP($config);
19-
$geoip = $geoip->setIp($this->validIp);
20-
21-
$this->assertEquals($geoip->getCountry(), 'United Kingdom');
22-
23-
$geoip = $geoip->setIp($this->invalidIp);
24-
25-
$this->assertEquals('fail', Arr::get($geoip->getRaw(), 'status'));
26-
27-
$this->assertEquals([
28-
'city' => null,
29-
'country' => null,
30-
'countryCode' => null,
31-
'latitude' => null,
32-
'longitude' => null,
33-
'region' => null,
34-
'regionCode' => null,
35-
'timezone' => null,
36-
'postalCode' => null,
37-
], $geoip->get());
38-
39-
$this->assertEquals('', $geoip->getCountry());
40-
}
41-
42-
public function test_get_multiple_ipaddress()
43-
{
44-
$config = [
45-
'driver' => 'ip-api',
46-
];
47-
48-
$geoip = new GeoIP($config);
49-
$geoip->setIp($this->multipleIps);
50-
$ip = $geoip->getIp();
51-
52-
$this->assertEquals($this->validIp, $ip);
53-
$this->assertTrue(! filter_var($ip, FILTER_VALIDATE_IP) === false);
54-
}
55-
56-
public function test_get_random_ipaddress()
57-
{
58-
$config = [
59-
'driver' => 'ip-api',
60-
'random' => true,
61-
];
62-
63-
$geoip = new GeoIP($config);
64-
$ip = $geoip->getIp();
65-
66-
$this->assertNotEquals($this->invalidIp, $ip);
67-
$this->assertTrue(! filter_var($ip, FILTER_VALIDATE_IP) === false);
68-
}
69-
70-
public function test_get_non_random_ipaddress()
71-
{
72-
$config = [
73-
'driver' => 'ip-api',
74-
'random' => false,
75-
];
76-
77-
$geoip = new GeoIP($config);
78-
$ip = $geoip->getIp();
79-
80-
$this->assertEquals($this->invalidIp, $ip);
81-
$this->assertTrue(! filter_var($ip, FILTER_VALIDATE_IP) === false);
82-
}
83-
84-
public function test_ip_api_pro_exception()
85-
{
86-
$config = [
87-
'driver' => 'ip-api',
88-
'ip-api' => [
89-
'key' => 'test',
90-
],
91-
];
92-
93-
$this->expectException(GeoIPException::class);
94-
95-
$geoip = new GeoIP($config);
96-
$geoip = $geoip->setIp($this->validIp);
97-
98-
$geoip->get();
99-
}
100-
101-
public function test_ip_api_pro_exception_getRaw()
102-
{
103-
$config = [
104-
'driver' => 'ip-api',
105-
'ip-api' => [
106-
'key' => 'test',
107-
],
108-
];
109-
110-
$this->expectException(GeoIPException::class);
111-
112-
$geoip = new GeoIP($config);
113-
$geoip = $geoip->setIp($this->validIp);
114-
115-
$geoip->getRaw();
116-
}
117-
}
7+
test('ip api', function () {
8+
$config = [
9+
'driver' => 'ip-api',
10+
];
11+
12+
$geoip = new GeoIP($config);
13+
$geoip = $geoip->setIp($this->validIp);
14+
15+
expect('United Kingdom')->toEqual($geoip->getCountry());
16+
17+
$geoip = $geoip->setIp($this->invalidIp);
18+
19+
expect(Arr::get($geoip->getRaw(), 'status'))->toEqual('fail');
20+
21+
expect($geoip->get())->toEqual([
22+
'city' => null,
23+
'country' => null,
24+
'countryCode' => null,
25+
'latitude' => null,
26+
'longitude' => null,
27+
'region' => null,
28+
'regionCode' => null,
29+
'timezone' => null,
30+
'postalCode' => null,
31+
]);
32+
33+
expect($geoip->getCountry())->toEqual('');
34+
});
35+
36+
test('get multiple ipaddress', function () {
37+
$config = [
38+
'driver' => 'ip-api',
39+
];
40+
41+
$geoip = new GeoIP($config);
42+
$geoip->setIp($this->multipleIps);
43+
$ip = $geoip->getIp();
44+
45+
expect($ip)->toEqual($this->validIp);
46+
expect(! filter_var($ip, FILTER_VALIDATE_IP) === false)->toBeTrue();
47+
});
48+
49+
test('get random ipaddress', function () {
50+
$config = [
51+
'driver' => 'ip-api',
52+
'random' => true,
53+
];
54+
55+
$geoip = new GeoIP($config);
56+
$ip = $geoip->getIp();
57+
58+
$this->assertNotEquals($this->invalidIp, $ip);
59+
expect(! filter_var($ip, FILTER_VALIDATE_IP) === false)->toBeTrue();
60+
});
61+
62+
test('get non random ipaddress', function () {
63+
$config = [
64+
'driver' => 'ip-api',
65+
'random' => false,
66+
];
67+
68+
$geoip = new GeoIP($config);
69+
$ip = $geoip->getIp();
70+
71+
expect($ip)->toEqual($this->invalidIp);
72+
expect(! filter_var($ip, FILTER_VALIDATE_IP) === false)->toBeTrue();
73+
});
74+
75+
test('ip api pro exception', function () {
76+
$config = [
77+
'driver' => 'ip-api',
78+
'ip-api' => [
79+
'key' => 'test',
80+
],
81+
];
82+
83+
$this->expectException(GeoIPException::class);
84+
85+
$geoip = new GeoIP($config);
86+
$geoip = $geoip->setIp($this->validIp);
87+
88+
$geoip->get();
89+
});
90+
91+
test('ip api pro exception get raw', function () {
92+
$config = [
93+
'driver' => 'ip-api',
94+
'ip-api' => [
95+
'key' => 'test',
96+
],
97+
];
98+
99+
$this->expectException(GeoIPException::class);
100+
101+
$geoip = new GeoIP($config);
102+
$geoip = $geoip->setIp($this->validIp);
103+
104+
$geoip->getRaw();
105+
});

0 commit comments

Comments
 (0)