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