]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/StatusTest.php
Merge pull request #3525 from lonvia/project-dir-less-library
[nominatim.git] / test / php / Nominatim / StatusTest.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim;
12
13 @define('CONST_TokenizerDir', dirname(__FILE__));
14
15 require_once(CONST_LibDir.'/DB.php');
16 require_once(CONST_LibDir.'/Status.php');
17
18
19 class StatusTest extends \PHPUnit\Framework\TestCase
20 {
21
22     public function testNoDatabaseGiven()
23     {
24         $this->expectException(\Exception::class);
25         $this->expectExceptionMessage('No database');
26         $this->expectExceptionCode(700);
27
28         $oDB = null;
29         $oStatus = new Status($oDB);
30         $this->assertEquals('No database', $oStatus->status());
31     }
32
33     public function testNoDatabaseConnectionFail()
34     {
35         $this->expectException(\Exception::class);
36         $this->expectExceptionMessage('Database connection failed');
37         $this->expectExceptionCode(700);
38
39         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
40                         ->setMethods(array('connect'))
41                         ->getMock();
42
43         $oDbStub->method('connect')
44                 ->will($this->returnCallback(function () {
45                     throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database');
46                 }));
47
48
49         $oStatus = new Status($oDbStub);
50         $this->assertEquals('No database', $oStatus->status());
51     }
52
53     public function testOK()
54     {
55         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
56                         ->setMethods(array('connect', 'getOne'))
57                         ->getMock();
58
59         $oDbStub->method('getOne')
60                 ->will($this->returnCallback(function ($sql) {
61                     if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
62                     if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
63                 }));
64
65         $oStatus = new Status($oDbStub);
66         $this->assertNull($oStatus->status());
67     }
68
69     public function testDataDate()
70     {
71         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
72                         ->setMethods(array('getOne'))
73                         ->getMock();
74
75         $oDbStub->method('getOne')
76                 ->willReturn(1519430221);
77
78         $oStatus = new Status($oDbStub);
79         $this->assertEquals(1519430221, $oStatus->dataDate());
80     }
81 }