From: marc tobias Date: Sat, 15 Sep 2018 13:23:10 +0000 (+0200) Subject: make PHP testsuite work with PHPUnit6 X-Git-Tag: v3.3.0~94^2 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/a9bdac836c043c034268a48136af668c469d27f3 make PHP testsuite work with PHPUnit6 --- diff --git a/test/php/Nominatim/DebugTest.php b/test/php/Nominatim/DebugTest.php index 9db7557e..7ef1dba3 100644 --- a/test/php/Nominatim/DebugTest.php +++ b/test/php/Nominatim/DebugTest.php @@ -2,19 +2,23 @@ namespace Nominatim; -use Exception; - -require_once('../../lib/DebugHtml.php'); +require_once(CONST_BasePath.'/lib/DebugHtml.php'); class DebugTest extends \PHPUnit\Framework\TestCase { + protected function setUp() { - $this->oWithDebuginfo = $this->getMock(Geocode::class, array('debugInfo')); + $this->oWithDebuginfo = $this->getMockBuilder(\GeococdeMock::class) + ->setMethods(array('debugInfo')) + ->getMock(); $this->oWithDebuginfo->method('debugInfo') ->willReturn(array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3')); - $this->oWithToString = $this->getMock(Geocode::class, array('__toString')); + + $this->oWithToString = $this->getMockBuilder(\SomeMock::class) + ->setMethods(array('__toString')) + ->getMock(); $this->oWithToString->method('__toString')->willReturn('me as string'); } diff --git a/test/php/Nominatim/LibTest.php b/test/php/Nominatim/LibTest.php index 322cc23e..d2237b60 100644 --- a/test/php/Nominatim/LibTest.php +++ b/test/php/Nominatim/LibTest.php @@ -2,8 +2,7 @@ namespace Nominatim; -require_once '../../lib/lib.php'; -require_once '../../lib/ClassTypes.php'; +require_once(CONST_BasePath.'/lib/ClassTypes.php'); class LibTest extends \PHPUnit\Framework\TestCase { diff --git a/test/php/Nominatim/ParameterParserTest.php b/test/php/Nominatim/ParameterParserTest.php index 0e88d318..9f51a629 100644 --- a/test/php/Nominatim/ParameterParserTest.php +++ b/test/php/Nominatim/ParameterParserTest.php @@ -2,14 +2,12 @@ namespace Nominatim; -use Exception; - -require_once('../../lib/ParameterParser.php'); +require_once(CONST_BasePath.'/lib/ParameterParser.php'); function userError($sError) { - throw new Exception($sError); + throw new \Exception($sError); } class ParameterParserTest extends \PHPUnit\Framework\TestCase @@ -55,14 +53,18 @@ class ParameterParserTest extends \PHPUnit\Framework\TestCase public function testGetIntWithNonNumber() { - $this->setExpectedException(Exception::class, "Integer number expected for parameter 'int4'"); + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Integer number expected for parameter 'int4'"); + (new ParameterParser(array('int4' => 'a')))->getInt('int4'); } public function testGetIntWithEmpytString() { - $this->setExpectedException(Exception::class, "Integer number expected for parameter 'int5'"); + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Integer number expected for parameter 'int5'"); + (new ParameterParser(array('int5' => '')))->getInt('int5'); } @@ -85,20 +87,26 @@ class ParameterParserTest extends \PHPUnit\Framework\TestCase public function testGetFloatWithEmptyString() { - $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float4'"); + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Floating-point number expected for parameter 'float4'"); + (new ParameterParser(array('float4' => '')))->getFloat('float4'); } public function testGetFloatWithTextString() { - $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float5'"); + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Floating-point number expected for parameter 'float5'"); + (new ParameterParser(array('float5' => 'a')))->getFloat('float5'); } public function testGetFloatWithInvalidNumber() { - $this->setExpectedException(Exception::class, "Floating-point number expected for parameter 'float6'"); + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Floating-point number expected for parameter 'float6'"); + (new ParameterParser(array('float6' => '-55.')))->getFloat('float6'); } @@ -138,7 +146,9 @@ class ParameterParserTest extends \PHPUnit\Framework\TestCase public function testGetSetWithValueNotInSet() { - $this->setExpectedException(Exception::class, "Parameter 'val4' must be one of: foo, bar"); + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Parameter 'val4' must be one of: foo, bar"); + (new ParameterParser(array('val4' => 'faz')))->getSet('val4', array('foo', 'bar')); } diff --git a/test/php/Nominatim/PhraseTest.php b/test/php/Nominatim/PhraseTest.php index 35a8a804..cd742715 100644 --- a/test/php/Nominatim/PhraseTest.php +++ b/test/php/Nominatim/PhraseTest.php @@ -2,7 +2,7 @@ namespace Nominatim; -require_once '../../lib/Phrase.php'; +require_once(CONST_BasePath.'/lib/Phrase.php'); class PhraseTest extends \PHPUnit\Framework\TestCase { diff --git a/test/php/Nominatim/SearchContextTest.php b/test/php/Nominatim/SearchContextTest.php index 0a2131b9..166e6433 100644 --- a/test/php/Nominatim/SearchContextTest.php +++ b/test/php/Nominatim/SearchContextTest.php @@ -2,9 +2,7 @@ namespace Nominatim; -@define('CONST_BasePath', '../../'); - -require_once '../../lib/SearchContext.php'; +require_once(CONST_BasePath.'/lib/SearchContext.php'); class SearchContextTest extends \PHPUnit\Framework\TestCase { diff --git a/test/php/Nominatim/StatusTest.php b/test/php/Nominatim/StatusTest.php index 448940db..4f21706e 100644 --- a/test/php/Nominatim/StatusTest.php +++ b/test/php/Nominatim/StatusTest.php @@ -2,18 +2,17 @@ namespace Nominatim; -require_once('../../lib/Status.php'); -require_once('DB.php'); +require_once(CONST_BasePath.'/lib/Status.php'); -use Exception; 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,7 +21,9 @@ class StatusTest extends \PHPUnit\Framework\TestCase public function testNoDatabaseConnectionFail() { - $this->setExpectedException(Exception::class, 'No database', 700); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('No database'); + $this->expectExceptionCode(700); // causes 'Non-static method should not be called statically, assuming $this from incompatible context' // failure on travis @@ -40,10 +41,14 @@ class StatusTest extends \PHPUnit\Framework\TestCase 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(\DB::class) + ->setMethods(array('getOne')) + ->getMock(); $oStatus = new Status($oDbStub); $this->assertNull($oStatus->status()); @@ -52,9 +57,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(\DB::class) + ->setMethods(array('getOne')) + ->getMock(); // return no word_id $oDbStub->method('getOne') @@ -70,7 +79,9 @@ class StatusTest extends \PHPUnit\Framework\TestCase public function testOK() { - $oDbStub = $this->getMock(\DB::class, array('getOne')); + $oDbStub = $this->getMockBuilder(\DB::class) + ->setMethods(array('getOne')) + ->getMock(); $oDbStub->method('getOne') ->will($this->returnCallback(function ($sql) { @@ -84,7 +95,9 @@ class StatusTest extends \PHPUnit\Framework\TestCase public function testDataDate() { - $oDbStub = $this->getMock(\DB::class, array('getOne')); + $oDbStub = $this->getMockBuilder(\DB::class) + ->setMethods(array('getOne')) + ->getMock(); $oDbStub->method('getOne') ->willReturn(1519430221); diff --git a/test/php/Nominatim/TokenListTest.php b/test/php/Nominatim/TokenListTest.php index ad91dff1..fa1331e8 100644 --- a/test/php/Nominatim/TokenListTest.php +++ b/test/php/Nominatim/TokenListTest.php @@ -2,17 +2,18 @@ namespace Nominatim; -@define('CONST_BasePath', '../../'); +require_once(CONST_BasePath.'/lib/db.php'); +require_once(CONST_BasePath.'/lib/cmd.php'); +require_once(CONST_BasePath.'/lib/TokenList.php'); -require_once '../../lib/db.php'; -require_once '../../lib/cmd.php'; -require_once '../../lib/TokenList.php'; class TokenTest extends \PHPUnit\Framework\TestCase { protected function setUp() { - $this->oNormalizer = $this->getMock(\MockNormalizer::class, array('transliterate')); + $this->oNormalizer = $this->getMockBuilder(\MockNormalizer::class) + ->setMethods(array('transliterate')) + ->getMock(); $this->oNormalizer->method('transliterate') ->will($this->returnCallback(function ($text) { return strtolower($text); @@ -55,7 +56,9 @@ class TokenTest extends \PHPUnit\Framework\TestCase { $this->expectOutputRegex('/

/'); - $oDbStub = $this->getMock(\DB::class, array('getAll')); + $oDbStub = $this->getMockBuilder(\DB::class) + ->setMethods(array('getAll')) + ->getMock(); $oDbStub->method('getAll') ->will($this->returnCallback(function ($sql) { $aResults = array(); diff --git a/test/php/bootstrap.php b/test/php/bootstrap.php index b3d9bbc7..0d475962 100644 --- a/test/php/bootstrap.php +++ b/test/php/bootstrap.php @@ -1 +1,2 @@ - ./test/php/Nominatim + ./Nominatim