X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/7075a5828e658cda653955096932a610bbbe0f99..764dcd7f6459c08aafd5cf2fcb7902cc2868cb30:/test/php/Nominatim/StatusTest.php diff --git a/test/php/Nominatim/StatusTest.php b/test/php/Nominatim/StatusTest.php index 06823720..f45e6633 100644 --- a/test/php/Nominatim/StatusTest.php +++ b/test/php/Nominatim/StatusTest.php @@ -2,18 +2,18 @@ namespace Nominatim; -require_once('../../lib/Status.php'); -require_once('DB.php'); +require_once(CONST_BasePath.'/lib/DB.php'); +require_once(CONST_BasePath.'/lib/Status.php'); -use Exception; -class StatusTest extends \PHPUnit_Framework_TestCase +class StatusTest extends \PHPUnit\Framework\TestCase { - 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); @@ -22,28 +22,35 @@ class StatusTest extends \PHPUnit_Framework_TestCase 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 + $oDbStub = $this->getMockBuilder(Nominatim\DB::class) + ->setMethods(array('connect')) + ->getMock(); - $oDB = new \DB_Error; - $oStatus = new Status($oDB); - $this->assertEquals('No database', $oStatus->status()); + $oDbStub->method('connect') + ->will($this->returnCallback(function () { + throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database'); + })); - $oDB = null; - $oStatus = new Status($oDB); + + $oStatus = new Status($oDbStub); $this->assertEquals('No database', $oStatus->status()); } public function testModuleFail() { - $this->setExpectedException(Exception::class, 'Module call failed', 702); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Module call failed'); + $this->expectExceptionCode(702); // stub has getOne method but doesn't return anything - $oDbStub = $this->getMock(\DB::class, array('getOne')); + $oDbStub = $this->getMockBuilder(Nominatim\DB::class) + ->setMethods(array('connect', 'getOne')) + ->getMock(); $oStatus = new Status($oDbStub); $this->assertNull($oStatus->status()); @@ -52,9 +59,13 @@ class StatusTest extends \PHPUnit_Framework_TestCase public function testWordIdQueryFail() { - $this->setExpectedException(Exception::class, 'No value', 704); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('No value'); + $this->expectExceptionCode(704); - $oDbStub = $this->getMock(\DB::class, array('getOne')); + $oDbStub = $this->getMockBuilder(Nominatim\DB::class) + ->setMethods(array('connect', 'getOne')) + ->getMock(); // return no word_id $oDbStub->method('getOne') @@ -70,7 +81,9 @@ class StatusTest extends \PHPUnit_Framework_TestCase 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) { @@ -84,7 +97,9 @@ class StatusTest extends \PHPUnit_Framework_TestCase 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);