]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/StatusTest.php
Merge pull request #1548 from mtmail/centos7-postgresql-11
[nominatim.git] / test / php / Nominatim / StatusTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/DB.php');
6 require_once(CONST_BasePath.'/lib/Status.php');
7
8
9 class StatusTest extends \PHPUnit\Framework\TestCase
10 {
11
12     public function testNoDatabaseGiven()
13     {
14         $this->expectException(\Exception::class);
15         $this->expectExceptionMessage('No database');
16         $this->expectExceptionCode(700);
17
18         $oDB = null;
19         $oStatus = new Status($oDB);
20         $this->assertEquals('No database', $oStatus->status());
21     }
22
23     public function testNoDatabaseConnectionFail()
24     {
25         $this->expectException(\Exception::class);
26         $this->expectExceptionMessage('Database connection failed');
27         $this->expectExceptionCode(700);
28
29         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
30                         ->setMethods(array('connect'))
31                         ->getMock();
32
33         $oDbStub->method('connect')
34                 ->will($this->returnCallback(function () {
35                     throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database');
36                 }));
37
38
39         $oStatus = new Status($oDbStub);
40         $this->assertEquals('No database', $oStatus->status());
41     }
42
43
44     public function testModuleFail()
45     {
46         $this->expectException(\Exception::class);
47         $this->expectExceptionMessage('Module call failed');
48         $this->expectExceptionCode(702);
49
50         // stub has getOne method but doesn't return anything
51         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
52                         ->setMethods(array('connect', 'getOne'))
53                         ->getMock();
54
55         $oStatus = new Status($oDbStub);
56         $this->assertNull($oStatus->status());
57     }
58
59
60     public function testWordIdQueryFail()
61     {
62         $this->expectException(\Exception::class);
63         $this->expectExceptionMessage('No value');
64         $this->expectExceptionCode(704);
65
66         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
67                         ->setMethods(array('connect', 'getOne'))
68                         ->getMock();
69
70         // return no word_id
71         $oDbStub->method('getOne')
72                 ->will($this->returnCallback(function ($sql) {
73                     if (preg_match("/make_standard_name\('a'\)/", $sql)) return 'a';
74                     if (preg_match('/SELECT word_id, word_token/', $sql)) return null;
75                 }));
76
77         $oStatus = new Status($oDbStub);
78         $this->assertNull($oStatus->status());
79     }
80
81
82     public function testOK()
83     {
84         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
85                         ->setMethods(array('connect', 'getOne'))
86                         ->getMock();
87
88         $oDbStub->method('getOne')
89                 ->will($this->returnCallback(function ($sql) {
90                     if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
91                     if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
92                 }));
93
94         $oStatus = new Status($oDbStub);
95         $this->assertNull($oStatus->status());
96     }
97
98     public function testDataDate()
99     {
100         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
101                         ->setMethods(array('getOne'))
102                         ->getMock();
103      
104         $oDbStub->method('getOne')
105                 ->willReturn(1519430221);
106
107         $oStatus = new Status($oDbStub);
108         $this->assertEquals(1519430221, $oStatus->dataDate());
109     }
110 }