Skip to content

Commit 0be956f

Browse files
committed
Added PHPUnit test cases
1 parent 2d44f33 commit 0be956f

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@
1515
"cakephp/cakephp": ">=3.0",
1616
"ip2location/ip2location-php": "8.*"
1717
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^9"
20+
},
1821
"autoload": {
1922
"psr-4": {
2023
"IP2LocationCakePHP\\": "src"
2124
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"IP2LocationCakePHP\\Test\\": "tests"
29+
}
2230
}
2331
}

phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="tests/bootstrap.php" colors="true">
2+
<testsuites>
3+
<testsuite name="IP2Location CakePHP Testcase">
4+
<directory suffix="Test.php">./tests/</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/Controller/IP2LocationCoresController.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33

44
// Web Service Settings
55
if(!defined('IP2LOCATION_API_KEY')) {
6-
define('IP2LOCATION_API_KEY', 'demo');
6+
define('IP2LOCATION_API_KEY', 'demo');
77
}
88

99
if(!defined('IP2LOCATION_PACKAGE')) {
10-
define('IP2LOCATION_PACKAGE', 'WS1');
10+
define('IP2LOCATION_PACKAGE', 'WS1');
1111
}
1212

1313
if(!defined('IP2LOCATION_USESSL')) {
14-
define('IP2LOCATION_USESSL', false);
14+
define('IP2LOCATION_USESSL', false);
1515
}
1616

1717
if(!defined('IP2LOCATION_ADDONS')) {
18-
define('IP2LOCATION_ADDONS', []);
18+
define('IP2LOCATION_ADDONS', []);
1919
}
2020

2121
if(!defined('IP2LOCATION_LANGUAGE')) {
22-
define('IP2LOCATION_LANGUAGE', 'en');
22+
define('IP2LOCATION_LANGUAGE', 'en');
2323
}
2424

2525
/**
@@ -38,9 +38,13 @@ public function index()
3838
//
3939
}
4040

41-
public function get($ip, $query = array())
41+
public function get($ip, $db = '')
4242
{
43-
$obj = new \IP2Location\Database(ROOT . DS . 'vendor' . DS . 'ip2location' . DS . 'ip2location-cakephp' . DS . 'src' . DS . 'Data' . DS . 'IP2LOCATION.BIN', \IP2Location\Database::FILE_IO);
43+
if($db == '') {
44+
$obj = new \IP2Location\Database(ROOT . DS . 'vendor' . DS . 'ip2location' . DS . 'ip2location-cakephp' . DS . 'src' . DS . 'Data' . DS . 'IP2LOCATION.BIN', \IP2Location\Database::FILE_IO);
45+
} else {
46+
$obj = $db;
47+
}
4448

4549
try {
4650
$records = $obj->lookup($ip, \IP2Location\Database::ALL);

tests/ControllerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\TestCase;
6+
use IP2LocationCakePHP\Controller\IP2LocationCoresController;
7+
8+
class ControllerTest extends TestCase
9+
{
10+
public function testGetDb() {
11+
$IP2Location = new IP2LocationCoresController();
12+
$db = new \IP2Location\Database('./src/Data/IP2LOCATION.BIN', \IP2Location\Database::FILE_IO);
13+
$record = $IP2Location->get('8.8.8.8', $db);
14+
15+
$this->assertEquals(
16+
'US',
17+
$record['countryCode'],
18+
);
19+
}
20+
21+
public function testGetWebService() {
22+
$IP2Location = new IP2LocationCoresController();
23+
$record = $IP2Location->getWebService('8.8.8.8');
24+
25+
$this->assertEquals(
26+
'US',
27+
$record['country_code'],
28+
);
29+
}
30+
}

tests/bootstrap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
if (!$loader = @include './vendor/autoload.php') {
6+
die('Project dependencies missing');
7+
}
8+
9+
$loader->add('IP2LocationCakePHP\Test', __DIR__);

0 commit comments

Comments
 (0)