mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-16 15:47:58 +00:00
replace database abstraction DB with PDO
This commit is contained in:
68
test/php/Nominatim/DBTest.php
Normal file
68
test/php/Nominatim/DBTest.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_BasePath.'/lib/lib.php');
|
||||
require_once(CONST_BasePath.'/lib/db.php');
|
||||
|
||||
class DBTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testErrorHandling()
|
||||
{
|
||||
$this->expectException(DatabaseError::class);
|
||||
$this->expectExceptionMessage('Failed to establish database connection');
|
||||
|
||||
$oDB = new \Nominatim\DB('pgsql:dbname=abc');
|
||||
$oDB->connect();
|
||||
}
|
||||
|
||||
public function testErrorHandling2()
|
||||
{
|
||||
$this->expectException(DatabaseError::class);
|
||||
$this->expectExceptionMessage('Database query failed');
|
||||
|
||||
$oPDOStub = $this->getMockBuilder(PDO::class)
|
||||
->setMethods(array('query', 'quote'))
|
||||
->getMock();
|
||||
|
||||
$oPDOStub->method('query')
|
||||
->will($this->returnCallback(function ($sVal) {
|
||||
return "'$sVal'";
|
||||
}));
|
||||
|
||||
$oPDOStub->method('query')
|
||||
->will($this->returnCallback(function () {
|
||||
throw new \PDOException('ERROR: syntax error at or near "FROM"');
|
||||
}));
|
||||
|
||||
$oDB = new \Nominatim\DB('');
|
||||
$oDB->connection = $oPDOStub;
|
||||
$oDB->tableExists('abc');
|
||||
}
|
||||
|
||||
public function testParseDSN()
|
||||
{
|
||||
$this->assertEquals(
|
||||
array(),
|
||||
\Nominatim\DB::parseDSN('')
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'database' => 'db1',
|
||||
'hostspec' => 'machine1'
|
||||
),
|
||||
\Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1')
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'database' => 'db1',
|
||||
'hostspec' => 'machine1',
|
||||
'port' => '1234',
|
||||
'username' => 'john',
|
||||
'password' => 'secret'
|
||||
),
|
||||
\Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1;port=1234;user=john;password=secret')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ class DatabaseErrorTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
public function testSqlMessage()
|
||||
{
|
||||
$oSqlStub = $this->getMockBuilder(\DB_Error::class)
|
||||
$oSqlStub = $this->getMockBuilder(PDOException::class)
|
||||
->setMethods(array('getMessage'))
|
||||
->getMock();
|
||||
|
||||
@@ -21,9 +21,6 @@ class DatabaseErrorTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals('Sql error', $oErr->getMessage());
|
||||
$this->assertEquals(123, $oErr->getCode());
|
||||
$this->assertEquals('Unknown table.', $oErr->getSqlError());
|
||||
|
||||
// causes a circular reference warning during dump
|
||||
// $this->assertRegExp('/Mock_DB_Error/', $oErr->getSqlDebugDump());
|
||||
}
|
||||
|
||||
public function testSqlObjectDump()
|
||||
@@ -31,14 +28,4 @@ class DatabaseErrorTest extends \PHPUnit\Framework\TestCase
|
||||
$oErr = new DatabaseError('Sql error', 123, null, array('one' => 'two'));
|
||||
$this->assertRegExp('/two/', $oErr->getSqlDebugDump());
|
||||
}
|
||||
|
||||
public function testChksqlThrows()
|
||||
{
|
||||
$this->expectException(DatabaseError::class);
|
||||
$this->expectExceptionMessage('My custom error message');
|
||||
$this->expectExceptionCode(500);
|
||||
|
||||
$oDB = new \DB_Error;
|
||||
$this->assertEquals(false, chksql($oDB, 'My custom error message'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,19 +23,20 @@ class StatusTest extends \PHPUnit\Framework\TestCase
|
||||
public function testNoDatabaseConnectionFail()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('No database');
|
||||
$this->expectExceptionMessage('Database connection failed');
|
||||
$this->expectExceptionCode(700);
|
||||
|
||||
// causes 'Non-static method should not be called statically, assuming $this from incompatible context'
|
||||
// failure on travis
|
||||
// $oDB = \DB::connect('', false); // returns a DB_Error instance
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('connect'))
|
||||
->getMock();
|
||||
|
||||
$oDB = new \DB_Error;
|
||||
$oStatus = new Status($oDB);
|
||||
$this->assertEquals('No database', $oStatus->status());
|
||||
$oDbStub->method('connect')
|
||||
->will($this->returnCallback(function () {
|
||||
throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database');
|
||||
}));
|
||||
|
||||
$oDB = null;
|
||||
$oStatus = new Status($oDB);
|
||||
|
||||
$oStatus = new Status($oDbStub);
|
||||
$this->assertEquals('No database', $oStatus->status());
|
||||
}
|
||||
|
||||
@@ -47,8 +48,8 @@ class StatusTest extends \PHPUnit\Framework\TestCase
|
||||
$this->expectExceptionCode(702);
|
||||
|
||||
// stub has getOne method but doesn't return anything
|
||||
$oDbStub = $this->getMockBuilder(\DB::class)
|
||||
->setMethods(array('getOne'))
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('connect', 'getOne'))
|
||||
->getMock();
|
||||
|
||||
$oStatus = new Status($oDbStub);
|
||||
@@ -62,8 +63,8 @@ class StatusTest extends \PHPUnit\Framework\TestCase
|
||||
$this->expectExceptionMessage('No value');
|
||||
$this->expectExceptionCode(704);
|
||||
|
||||
$oDbStub = $this->getMockBuilder(\DB::class)
|
||||
->setMethods(array('getOne'))
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('connect', 'getOne'))
|
||||
->getMock();
|
||||
|
||||
// return no word_id
|
||||
@@ -80,8 +81,8 @@ class StatusTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
public function testOK()
|
||||
{
|
||||
$oDbStub = $this->getMockBuilder(\DB::class)
|
||||
->setMethods(array('getOne'))
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('connect', 'getOne'))
|
||||
->getMock();
|
||||
|
||||
$oDbStub->method('getOne')
|
||||
@@ -96,7 +97,7 @@ class StatusTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
public function testDataDate()
|
||||
{
|
||||
$oDbStub = $this->getMockBuilder(\DB::class)
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('getOne'))
|
||||
->getMock();
|
||||
|
||||
|
||||
@@ -56,9 +56,18 @@ class TokenTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
$this->expectOutputRegex('/<p><tt>/');
|
||||
|
||||
$oDbStub = $this->getMockBuilder(\DB::class)
|
||||
->setMethods(array('getAll'))
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('getAll', 'getDBQuotedList'))
|
||||
->getMock();
|
||||
|
||||
$oDbStub->method('getDBQuotedList')
|
||||
->will($this->returnCallback(function ($aVals) {
|
||||
return array_map(function ($sVal) {
|
||||
return "'".$sVal."'";
|
||||
}, $aVals);
|
||||
}));
|
||||
|
||||
|
||||
$oDbStub->method('getAll')
|
||||
->will($this->returnCallback(function ($sql) {
|
||||
$aResults = array();
|
||||
|
||||
Reference in New Issue
Block a user