]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/StatusTest.php
specify text type in test SQL
[nominatim.git] / test / php / Nominatim / StatusTest.php
1 <?php
2
3 namespace Nominatim;
4
5 @define('CONST_TokenizerDir', dirname(__FILE__));
6
7 require_once(CONST_LibDir.'/DB.php');
8 require_once(CONST_LibDir.'/Status.php');
9
10
11 class StatusTest extends \PHPUnit\Framework\TestCase
12 {
13
14     public function testNoDatabaseGiven()
15     {
16         $this->expectException(\Exception::class);
17         $this->expectExceptionMessage('No database');
18         $this->expectExceptionCode(700);
19
20         $oDB = null;
21         $oStatus = new Status($oDB);
22         $this->assertEquals('No database', $oStatus->status());
23     }
24
25     public function testNoDatabaseConnectionFail()
26     {
27         $this->expectException(\Exception::class);
28         $this->expectExceptionMessage('Database connection failed');
29         $this->expectExceptionCode(700);
30
31         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
32                         ->setMethods(array('connect'))
33                         ->getMock();
34
35         $oDbStub->method('connect')
36                 ->will($this->returnCallback(function () {
37                     throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database');
38                 }));
39
40
41         $oStatus = new Status($oDbStub);
42         $this->assertEquals('No database', $oStatus->status());
43     }
44
45     public function testOK()
46     {
47         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
48                         ->setMethods(array('connect', 'getOne'))
49                         ->getMock();
50
51         $oDbStub->method('getOne')
52                 ->will($this->returnCallback(function ($sql) {
53                     if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
54                     if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
55                 }));
56
57         $oStatus = new Status($oDbStub);
58         $this->assertNull($oStatus->status());
59     }
60
61     public function testDataDate()
62     {
63         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
64                         ->setMethods(array('getOne'))
65                         ->getMock();
66
67         $oDbStub->method('getOne')
68                 ->willReturn(1519430221);
69
70         $oStatus = new Status($oDbStub);
71         $this->assertEquals(1519430221, $oStatus->dataDate());
72     }
73 }