]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/StatusTest.php
Merge pull request #1233 from mtmail/better-gbpostcode-setup-warning
[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('No database');
27         $this->expectExceptionCode(700);
28
29         // causes 'Non-static method should not be called statically, assuming $this from incompatible context'
30         // failure on travis
31         // $oDB = \DB::connect('', false); // returns a DB_Error instance
32
33         $oDB = new \DB_Error;
34         $oStatus = new Status($oDB);
35         $this->assertEquals('No database', $oStatus->status());
36
37         $oDB = null;
38         $oStatus = new Status($oDB);
39         $this->assertEquals('No database', $oStatus->status());
40     }
41
42
43     public function testModuleFail()
44     {
45         $this->expectException(\Exception::class);
46         $this->expectExceptionMessage('Module call failed');
47         $this->expectExceptionCode(702);
48
49         // stub has getOne method but doesn't return anything
50         $oDbStub = $this->getMockBuilder(\DB::class)
51                         ->setMethods(array('getOne'))
52                         ->getMock();
53
54         $oStatus = new Status($oDbStub);
55         $this->assertNull($oStatus->status());
56     }
57
58
59     public function testWordIdQueryFail()
60     {
61         $this->expectException(\Exception::class);
62         $this->expectExceptionMessage('No value');
63         $this->expectExceptionCode(704);
64
65         $oDbStub = $this->getMockBuilder(\DB::class)
66                         ->setMethods(array('getOne'))
67                         ->getMock();
68
69         // return no word_id
70         $oDbStub->method('getOne')
71                 ->will($this->returnCallback(function ($sql) {
72                     if (preg_match("/make_standard_name\('a'\)/", $sql)) return 'a';
73                     if (preg_match('/SELECT word_id, word_token/', $sql)) return null;
74                 }));
75
76         $oStatus = new Status($oDbStub);
77         $this->assertNull($oStatus->status());
78     }
79
80
81     public function testOK()
82     {
83         $oDbStub = $this->getMockBuilder(\DB::class)
84                         ->setMethods(array('getOne'))
85                         ->getMock();
86
87         $oDbStub->method('getOne')
88                 ->will($this->returnCallback(function ($sql) {
89                     if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
90                     if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
91                 }));
92
93         $oStatus = new Status($oDbStub);
94         $this->assertNull($oStatus->status());
95     }
96
97     public function testDataDate()
98     {
99         $oDbStub = $this->getMockBuilder(\DB::class)
100                         ->setMethods(array('getOne'))
101                         ->getMock();
102      
103         $oDbStub->method('getOne')
104                 ->willReturn(1519430221);
105
106         $oStatus = new Status($oDbStub);
107         $this->assertEquals(1519430221, $oStatus->dataDate());
108     }
109 }