]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/php/Nominatim/StatusTest.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / php / Nominatim / StatusTest.php
index 06823720c48e582a3c70f219822bce5303594a8d..9e03a970fd2aa1211c234460918dd119cf6a7cc0 100644 (file)
@@ -2,18 +2,20 @@
 
 namespace Nominatim;
 
 
 namespace Nominatim;
 
-require_once('../../lib/Status.php');
-require_once('DB.php');
+@define('CONST_TokenizerDir', dirname(__FILE__));
 
 
-use Exception;
+require_once(CONST_LibDir.'/DB.php');
+require_once(CONST_LibDir.'/Status.php');
 
 
-class StatusTest extends \PHPUnit_Framework_TestCase
-{
 
 
+class StatusTest extends \PHPUnit\Framework\TestCase
+{
 
     public function testNoDatabaseGiven()
     {
 
     public function testNoDatabaseGiven()
     {
-        $this->setExpectedException(Exception::class, 'No database', 700);
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('No database');
+        $this->expectExceptionCode(700);
 
         $oDB = null;
         $oStatus = new Status($oDB);
 
         $oDB = null;
         $oStatus = new Status($oDB);
@@ -22,55 +24,29 @@ class StatusTest extends \PHPUnit_Framework_TestCase
 
     public function testNoDatabaseConnectionFail()
     {
 
     public function testNoDatabaseConnectionFail()
     {
-        $this->setExpectedException(Exception::class, 'No database', 700);
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Database connection failed');
+        $this->expectExceptionCode(700);
 
 
-        // causes 'Non-static method should not be called statically, assuming $this from incompatible context'
-        // failure on travis
-        // $oDB = \DB::connect('', false); // returns a DB_Error instance
-
-        $oDB = new \DB_Error;
-        $oStatus = new Status($oDB);
-        $this->assertEquals('No database', $oStatus->status());
+        $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
+                        ->setMethods(array('connect'))
+                        ->getMock();
 
 
-        $oDB = null;
-        $oStatus = new Status($oDB);
-        $this->assertEquals('No database', $oStatus->status());
-    }
-
-
-    public function testModuleFail()
-    {
-        $this->setExpectedException(Exception::class, 'Module call failed', 702);
-
-        // stub has getOne method but doesn't return anything
-        $oDbStub = $this->getMock(\DB::class, array('getOne'));
-
-        $oStatus = new Status($oDbStub);
-        $this->assertNull($oStatus->status());
-    }
-
-
-    public function testWordIdQueryFail()
-    {
-        $this->setExpectedException(Exception::class, 'No value', 704);
-
-        $oDbStub = $this->getMock(\DB::class, array('getOne'));
-
-        // return no word_id
-        $oDbStub->method('getOne')
-                ->will($this->returnCallback(function ($sql) {
-                    if (preg_match("/make_standard_name\('a'\)/", $sql)) return 'a';
-                    if (preg_match('/SELECT word_id, word_token/', $sql)) return null;
+        $oDbStub->method('connect')
+                ->will($this->returnCallback(function () {
+                    throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database');
                 }));
 
                 }));
 
+
         $oStatus = new Status($oDbStub);
         $oStatus = new Status($oDbStub);
-        $this->assertNull($oStatus->status());
+        $this->assertEquals('No database', $oStatus->status());
     }
 
     }
 
-
     public function testOK()
     {
     public function testOK()
     {
-        $oDbStub = $this->getMock(\DB::class, array('getOne'));
+        $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
+                        ->setMethods(array('connect', 'getOne'))
+                        ->getMock();
 
         $oDbStub->method('getOne')
                 ->will($this->returnCallback(function ($sql) {
 
         $oDbStub->method('getOne')
                 ->will($this->returnCallback(function ($sql) {
@@ -84,8 +60,10 @@ class StatusTest extends \PHPUnit_Framework_TestCase
 
     public function testDataDate()
     {
 
     public function testDataDate()
     {
-        $oDbStub = $this->getMock(\DB::class, array('getOne'));
-     
+        $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
+                        ->setMethods(array('getOne'))
+                        ->getMock();
+
         $oDbStub->method('getOne')
                 ->willReturn(1519430221);
 
         $oDbStub->method('getOne')
                 ->willReturn(1519430221);