5 require_once(CONST_BasePath.'/lib/DB.php');
 
   6 require_once(CONST_BasePath.'/lib/Status.php');
 
   9 class StatusTest extends \PHPUnit\Framework\TestCase
 
  12     public function testNoDatabaseGiven()
 
  14         $this->expectException(\Exception::class);
 
  15         $this->expectExceptionMessage('No database');
 
  16         $this->expectExceptionCode(700);
 
  19         $oStatus = new Status($oDB);
 
  20         $this->assertEquals('No database', $oStatus->status());
 
  23     public function testNoDatabaseConnectionFail()
 
  25         $this->expectException(\Exception::class);
 
  26         $this->expectExceptionMessage('Database connection failed');
 
  27         $this->expectExceptionCode(700);
 
  29         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
 
  30                         ->setMethods(array('connect'))
 
  33         $oDbStub->method('connect')
 
  34                 ->will($this->returnCallback(function () {
 
  35                     throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database');
 
  39         $oStatus = new Status($oDbStub);
 
  40         $this->assertEquals('No database', $oStatus->status());
 
  44     public function testModuleFail()
 
  46         $this->expectException(\Exception::class);
 
  47         $this->expectExceptionMessage('Module call failed');
 
  48         $this->expectExceptionCode(702);
 
  50         // stub has getOne method but doesn't return anything
 
  51         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
 
  52                         ->setMethods(array('connect', 'getOne'))
 
  55         $oStatus = new Status($oDbStub);
 
  56         $this->assertNull($oStatus->status());
 
  60     public function testWordIdQueryFail()
 
  62         $this->expectException(\Exception::class);
 
  63         $this->expectExceptionMessage('No value');
 
  64         $this->expectExceptionCode(704);
 
  66         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
 
  67                         ->setMethods(array('connect', 'getOne'))
 
  71         $oDbStub->method('getOne')
 
  72                 ->will($this->returnCallback(function ($sql) {
 
  73                     if (preg_match("/make_standard_name\('a'\)/", $sql)) return 'a';
 
  74                     if (preg_match('/SELECT word_id, word_token/', $sql)) return null;
 
  77         $oStatus = new Status($oDbStub);
 
  78         $this->assertNull($oStatus->status());
 
  82     public function testOK()
 
  84         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
 
  85                         ->setMethods(array('connect', 'getOne'))
 
  88         $oDbStub->method('getOne')
 
  89                 ->will($this->returnCallback(function ($sql) {
 
  90                     if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
 
  91                     if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
 
  94         $oStatus = new Status($oDbStub);
 
  95         $this->assertNull($oStatus->status());
 
  98     public function testDataDate()
 
 100         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
 
 101                         ->setMethods(array('getOne'))
 
 104         $oDbStub->method('getOne')
 
 105                 ->willReturn(1519430221);
 
 107         $oStatus = new Status($oDbStub);
 
 108         $this->assertEquals(1519430221, $oStatus->dataDate());