]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/DatabaseErrorTest.php
25b4aa0bfa1aeba60e45caa76887d951ea5d6ed3
[nominatim.git] / test / php / Nominatim / DatabaseErrorTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/init-website.php');
6 require_once(CONST_BasePath.'/lib/DatabaseError.php');
7
8 class DatabaseErrorTest extends \PHPUnit\Framework\TestCase
9 {
10
11     public function testSqlMessage()
12     {
13         $oSqlStub = $this->getMockBuilder(\DB_Error::class)
14                     ->setMethods(array('getMessage'))
15                     ->getMock();
16
17         $oSqlStub->method('getMessage')
18                 ->willReturn('Unknown table.');
19
20         $oErr = new DatabaseError('Sql error', 123, null, $oSqlStub);
21         $this->assertEquals('Sql error', $oErr->getMessage());
22         $this->assertEquals(123, $oErr->getCode());
23         $this->assertEquals('Unknown table.', $oErr->getSqlError());
24
25         // causes a circular reference warning during dump
26         // $this->assertRegExp('/Mock_DB_Error/', $oErr->getSqlDebugDump());
27     }
28
29     public function testSqlObjectDump()
30     {
31         $oErr = new DatabaseError('Sql error', 123, null, array('one' => 'two'));
32         $this->assertRegExp('/two/', $oErr->getSqlDebugDump());
33     }
34
35     public function testChksqlThrows()
36     {
37         $this->expectException(DatabaseError::class);
38         $this->expectExceptionMessage('My custom error message');
39         $this->expectExceptionCode(500);
40
41         $oDB = new \DB_Error;
42         $this->assertEquals(false, chksql($oDB, 'My custom error message'));
43     }
44 }