Skip to content

Commit cf0c666

Browse files
committed
Added PHPUnit test cases
1 parent 572874e commit cf0c666

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
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/ip2proxy-php": ">=3.0"
1717
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^9"
20+
},
1821
"autoload": {
1922
"psr-4": {
2023
"IP2ProxyCakePHP\\": "src"
2124
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"IP2ProxyCakePHP\\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="IP2Proxy CakePHP Testcase">
4+
<directory suffix="Test.php">./tests/</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/Controller/IP2ProxyCoresController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ public function index()
3030
//
3131
}
3232

33-
public function get($ip, $query = array())
33+
public function get($ip, $db = '')
3434
{
3535
$obj = new \IP2Proxy\Database();
36-
$obj->open(ROOT . DS . 'vendor' . DS . 'ip2location' . DS . 'ip2proxy-cakephp' . DS . 'src' . DS . 'Data' . DS . 'IP2PROXY.BIN', \IP2Proxy\Database::FILE_IO);
36+
37+
if($db == '') {
38+
$obj->open(ROOT . DS . 'vendor' . DS . 'ip2location' . DS . 'ip2proxy-cakephp' . DS . 'src' . DS . 'Data' . DS . 'IP2PROXY.BIN', \IP2Proxy\Database::FILE_IO);
39+
} else {
40+
$obj->open($db, \IP2Proxy\Database::FILE_IO);
41+
}
3742

3843
try {
3944
$records = $obj->getAll($ip);

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 IP2ProxyCakePHP\Controller\IP2ProxyCoresController;
7+
8+
class ControllerTest extends TestCase
9+
{
10+
public function testGetDb() {
11+
$IP2Proxy = new IP2ProxyCoresController();
12+
$db = './src/Data/IP2PROXY.BIN';
13+
$record = $IP2Proxy->get('1.0.241.135', $db);
14+
15+
$this->assertEquals(
16+
'TH',
17+
$record['countryCode'],
18+
);
19+
}
20+
21+
public function testGetWebService() {
22+
$IP2Proxy = new IP2ProxyCoresController();
23+
$record = $IP2Proxy->getWebService('1.0.241.135');
24+
25+
$this->assertEquals(
26+
'TH',
27+
$record['countryCode'],
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('IP2ProxyCakePHP\Test', __DIR__);

0 commit comments

Comments
 (0)