5 require_once(CONST_LibDir.'/lib.php');
 
   6 require_once(CONST_LibDir.'/DB.php');
 
   8 // subclassing so we can set the protected connection variable
 
   9 class NominatimSubClassedDB extends \Nominatim\DB
 
  11     public function setConnection($oConnection)
 
  13         $this->connection = $oConnection;
 
  17 // phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
 
  18 class DBTest extends \PHPUnit\Framework\TestCase
 
  20     public function testReusingConnection()
 
  22         $oDB = new NominatimSubClassedDB('');
 
  23         $oDB->setConnection('anything');
 
  24         $this->assertTrue($oDB->connect());
 
  27     public function testCheckConnection()
 
  29         $oDB = new \Nominatim\DB('');
 
  30         $this->assertFalse($oDB->checkConnection());
 
  33     public function testErrorHandling()
 
  35         $this->expectException(DatabaseError::class);
 
  36         $this->expectExceptionMessage('Failed to establish database connection');
 
  38         $oDB = new \Nominatim\DB('pgsql:dbname=abc');
 
  42     public function testErrorHandling2()
 
  44         $this->expectException(DatabaseError::class);
 
  45         $this->expectExceptionMessage('Database query failed');
 
  47         $oPDOStub = $this->getMockBuilder(PDO::class)
 
  48                          ->setMethods(array('query', 'quote'))
 
  51         $oPDOStub->method('query')
 
  52                  ->will($this->returnCallback(function ($sVal) {
 
  56         $oPDOStub->method('query')
 
  57                  ->will($this->returnCallback(function () {
 
  58                      throw new \PDOException('ERROR:  syntax error at or near "FROM"');
 
  61         $oDB = new NominatimSubClassedDB('');
 
  62         $oDB->setConnection($oPDOStub);
 
  63         $oDB->getOne('SELECT name FROM');
 
  66     public function testGetPostgresVersion()
 
  68         $oDBStub = $this->getMockBuilder(\Nominatim\DB::class)
 
  69                         ->disableOriginalConstructor()
 
  70                         ->setMethods(array('getOne'))
 
  73         $oDBStub->method('getOne')
 
  74                 ->willReturn('100006');
 
  76         $this->assertEquals(10, $oDBStub->getPostgresVersion());
 
  79     public function testGetPostgisVersion()
 
  81         $oDBStub = $this->getMockBuilder(\Nominatim\DB::class)
 
  82                         ->disableOriginalConstructor()
 
  83                         ->setMethods(array('getOne'))
 
  86         $oDBStub->method('getOne')
 
  87                 ->willReturn('2.4.4');
 
  89         $this->assertEquals(2.4, $oDBStub->getPostgisVersion());
 
  92     public function testParseDSN()
 
  96             \Nominatim\DB::parseDSN('')
 
 101              'hostspec' => 'machine1'
 
 103             \Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1')
 
 108              'hostspec' => 'machine1',
 
 110              'username' => 'john',
 
 111              'password' => 'secret'
 
 113             \Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1;port=1234;user=john;password=secret')
 
 117     public function testGenerateDSN()
 
 121             \Nominatim\DB::generateDSN(array())
 
 124             'pgsql:host=machine1;dbname=db1',
 
 125             \Nominatim\DB::generateDSN(\Nominatim\DB::parseDSN('pgsql:host=machine1;dbname=db1'))
 
 129     public function testAgainstDatabase()
 
 131         $unit_test_dsn = getenv('UNIT_TEST_DSN') != false ?
 
 132                             getenv('UNIT_TEST_DSN') :
 
 133                             'pgsql:dbname=nominatim_unit_tests';
 
 138             'Test database will get destroyed, thus should have a name like unit_test to be safe'
 
 141         ## Create the database.
 
 143             $aDSNParsed = \Nominatim\DB::parseDSN($unit_test_dsn);
 
 144             $sDbname = $aDSNParsed['database'];
 
 145             $aDSNParsed['database'] = 'postgres';
 
 147             $oDB = new \Nominatim\DB(\Nominatim\DB::generateDSN($aDSNParsed));
 
 149             $oDB->exec('DROP DATABASE IF EXISTS ' . $sDbname);
 
 150             $oDB->exec('CREATE DATABASE ' . $sDbname);
 
 153         $oDB = new \Nominatim\DB($unit_test_dsn);
 
 157             $oDB->checkConnection($sDbname)
 
 162             $oDB->exec('CREATE TABLE table1 (id integer, city varchar, country varchar)');
 
 164             $this->assertTrue($oDB->tableExists('table1'));
 
 165             $this->assertFalse($oDB->tableExists('table99'));
 
 166             $this->assertFalse($oDB->tableExists(null));
 
 172                 "INSERT INTO table1 VALUES (1, 'Berlin', 'Germany'), (2, 'Paris', 'France')"
 
 177                     array('city' => 'Berlin'),
 
 178                     array('city' => 'Paris')
 
 180                 $oDB->getAll('SELECT city FROM table1')
 
 184                 $oDB->getAll('SELECT city FROM table1 WHERE id=999')
 
 189                 array('id' => 1, 'city' => 'Berlin', 'country' => 'Germany'),
 
 190                 $oDB->getRow('SELECT * FROM table1 WHERE id=1')
 
 194                 $oDB->getRow('SELECT * FROM table1 WHERE id=999')
 
 199                 array('Berlin', 'Paris'),
 
 200                 $oDB->getCol('SELECT city FROM table1')
 
 204                 $oDB->getCol('SELECT city FROM table1 WHERE id=999')
 
 209                 $oDB->getOne('SELECT city FROM table1 WHERE id=1')
 
 213                 $oDB->getOne('SELECT city FROM table1 WHERE id=999')
 
 217                 array('Berlin' => 'Germany', 'Paris' => 'France'),
 
 218                 $oDB->getAssoc('SELECT city, country FROM table1')
 
 222                 $oDB->getAssoc('SELECT city, country FROM table1 WHERE id=999')