3  * SPDX-License-Identifier: GPL-2.0-only
 
   5  * This file is part of Nominatim. (https://nominatim.org)
 
   7  * Copyright (C) 2022 by the Nominatim developer community.
 
   8  * For a full list of authors see the git log.
 
  13 @define('CONST_TokenizerDir', dirname(__FILE__));
 
  15 require_once(CONST_LibDir.'/DB.php');
 
  16 require_once(CONST_LibDir.'/Status.php');
 
  19 class StatusTest extends \PHPUnit\Framework\TestCase
 
  22     public function testNoDatabaseGiven()
 
  24         $this->expectException(\Exception::class);
 
  25         $this->expectExceptionMessage('No database');
 
  26         $this->expectExceptionCode(700);
 
  29         $oStatus = new Status($oDB);
 
  30         $this->assertEquals('No database', $oStatus->status());
 
  33     public function testNoDatabaseConnectionFail()
 
  35         $this->expectException(\Exception::class);
 
  36         $this->expectExceptionMessage('Database connection failed');
 
  37         $this->expectExceptionCode(700);
 
  39         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
 
  40                         ->setMethods(array('connect'))
 
  43         $oDbStub->method('connect')
 
  44                 ->will($this->returnCallback(function () {
 
  45                     throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database');
 
  49         $oStatus = new Status($oDbStub);
 
  50         $this->assertEquals('No database', $oStatus->status());
 
  53     public function testOK()
 
  55         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
 
  56                         ->setMethods(array('connect', 'getOne'))
 
  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;
 
  65         $oStatus = new Status($oDbStub);
 
  66         $this->assertNull($oStatus->status());
 
  69     public function testDataDate()
 
  71         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
 
  72                         ->setMethods(array('getOne'))
 
  75         $oDbStub->method('getOne')
 
  76                 ->willReturn(1519430221);
 
  78         $oStatus = new Status($oDbStub);
 
  79         $this->assertEquals(1519430221, $oStatus->dataDate());