]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/DBTest.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / php / Nominatim / DBTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/lib.php');
6 require_once(CONST_BasePath.'/lib/db.php');
7
8 class DBTest extends \PHPUnit\Framework\TestCase
9 {
10
11     public function testErrorHandling()
12     {
13         $this->expectException(DatabaseError::class);
14         $this->expectExceptionMessage('Failed to establish database connection');
15
16         $oDB = new \Nominatim\DB('pgsql:dbname=abc');
17         $oDB->connect();
18     }
19
20     public function testErrorHandling2()
21     {
22         $this->expectException(DatabaseError::class);
23         $this->expectExceptionMessage('Database query failed');
24
25         $oPDOStub = $this->getMockBuilder(PDO::class)
26                          ->setMethods(array('query', 'quote'))
27                          ->getMock();
28
29         $oPDOStub->method('query')
30                  ->will($this->returnCallback(function ($sVal) {
31                     return "'$sVal'";
32                  }));
33
34         $oPDOStub->method('query')
35                  ->will($this->returnCallback(function () {
36                      throw new \PDOException('ERROR:  syntax error at or near "FROM"');
37                  }));
38
39         $oDB = new \Nominatim\DB('');
40         $oDB->connection = $oPDOStub;
41         $oDB->tableExists('abc');
42     }
43
44     public function testParseDSN()
45     {
46         $this->assertEquals(
47             array(),
48             \Nominatim\DB::parseDSN('')
49         );
50         $this->assertEquals(
51             array(
52              'database' => 'db1',
53              'hostspec' => 'machine1'
54             ),
55             \Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1')
56         );
57         $this->assertEquals(
58             array(
59              'database' => 'db1',
60              'hostspec' => 'machine1',
61              'port' => '1234',
62              'username' => 'john',
63              'password' => 'secret'
64             ),
65             \Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1;port=1234;user=john;password=secret')
66         );
67     }
68 }