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