make PHP testsuite work with PHPUnit6

This commit is contained in:
marc tobias
2018-09-15 15:23:10 +02:00
parent bc26244114
commit a9bdac836c
9 changed files with 70 additions and 41 deletions

View File

@@ -2,19 +2,23 @@
namespace Nominatim; namespace Nominatim;
use Exception; require_once(CONST_BasePath.'/lib/DebugHtml.php');
require_once('../../lib/DebugHtml.php');
class DebugTest extends \PHPUnit\Framework\TestCase class DebugTest extends \PHPUnit\Framework\TestCase
{ {
protected function setUp() 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') $this->oWithDebuginfo->method('debugInfo')
->willReturn(array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3')); ->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'); $this->oWithToString->method('__toString')->willReturn('me as string');
} }

View File

@@ -2,8 +2,7 @@
namespace Nominatim; namespace Nominatim;
require_once '../../lib/lib.php'; require_once(CONST_BasePath.'/lib/ClassTypes.php');
require_once '../../lib/ClassTypes.php';
class LibTest extends \PHPUnit\Framework\TestCase class LibTest extends \PHPUnit\Framework\TestCase
{ {

View File

@@ -2,14 +2,12 @@
namespace Nominatim; namespace Nominatim;
use Exception; require_once(CONST_BasePath.'/lib/ParameterParser.php');
require_once('../../lib/ParameterParser.php');
function userError($sError) function userError($sError)
{ {
throw new Exception($sError); throw new \Exception($sError);
} }
class ParameterParserTest extends \PHPUnit\Framework\TestCase class ParameterParserTest extends \PHPUnit\Framework\TestCase
@@ -55,14 +53,18 @@ class ParameterParserTest extends \PHPUnit\Framework\TestCase
public function testGetIntWithNonNumber() 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'); (new ParameterParser(array('int4' => 'a')))->getInt('int4');
} }
public function testGetIntWithEmpytString() 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'); (new ParameterParser(array('int5' => '')))->getInt('int5');
} }
@@ -85,20 +87,26 @@ class ParameterParserTest extends \PHPUnit\Framework\TestCase
public function testGetFloatWithEmptyString() 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'); (new ParameterParser(array('float4' => '')))->getFloat('float4');
} }
public function testGetFloatWithTextString() 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'); (new ParameterParser(array('float5' => 'a')))->getFloat('float5');
} }
public function testGetFloatWithInvalidNumber() 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'); (new ParameterParser(array('float6' => '-55.')))->getFloat('float6');
} }
@@ -138,7 +146,9 @@ class ParameterParserTest extends \PHPUnit\Framework\TestCase
public function testGetSetWithValueNotInSet() 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')); (new ParameterParser(array('val4' => 'faz')))->getSet('val4', array('foo', 'bar'));
} }

View File

@@ -2,7 +2,7 @@
namespace Nominatim; namespace Nominatim;
require_once '../../lib/Phrase.php'; require_once(CONST_BasePath.'/lib/Phrase.php');
class PhraseTest extends \PHPUnit\Framework\TestCase class PhraseTest extends \PHPUnit\Framework\TestCase
{ {

View File

@@ -2,9 +2,7 @@
namespace Nominatim; namespace Nominatim;
@define('CONST_BasePath', '../../'); require_once(CONST_BasePath.'/lib/SearchContext.php');
require_once '../../lib/SearchContext.php';
class SearchContextTest extends \PHPUnit\Framework\TestCase class SearchContextTest extends \PHPUnit\Framework\TestCase
{ {

View File

@@ -2,18 +2,17 @@
namespace Nominatim; namespace Nominatim;
require_once('../../lib/Status.php'); require_once(CONST_BasePath.'/lib/Status.php');
require_once('DB.php');
use Exception;
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; $oDB = null;
$oStatus = new Status($oDB); $oStatus = new Status($oDB);
@@ -22,7 +21,9 @@ 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('No database');
$this->expectExceptionCode(700);
// causes 'Non-static method should not be called statically, assuming $this from incompatible context' // causes 'Non-static method should not be called statically, assuming $this from incompatible context'
// failure on travis // failure on travis
@@ -40,10 +41,14 @@ class StatusTest extends \PHPUnit\Framework\TestCase
public function testModuleFail() 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 // 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); $oStatus = new Status($oDbStub);
$this->assertNull($oStatus->status()); $this->assertNull($oStatus->status());
@@ -52,9 +57,13 @@ class StatusTest extends \PHPUnit\Framework\TestCase
public function testWordIdQueryFail() 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 // return no word_id
$oDbStub->method('getOne') $oDbStub->method('getOne')
@@ -70,7 +79,9 @@ class StatusTest extends \PHPUnit\Framework\TestCase
public function testOK() public function testOK()
{ {
$oDbStub = $this->getMock(\DB::class, array('getOne')); $oDbStub = $this->getMockBuilder(\DB::class)
->setMethods(array('getOne'))
->getMock();
$oDbStub->method('getOne') $oDbStub->method('getOne')
->will($this->returnCallback(function ($sql) { ->will($this->returnCallback(function ($sql) {
@@ -84,7 +95,9 @@ class StatusTest extends \PHPUnit\Framework\TestCase
public function testDataDate() public function testDataDate()
{ {
$oDbStub = $this->getMock(\DB::class, array('getOne')); $oDbStub = $this->getMockBuilder(\DB::class)
->setMethods(array('getOne'))
->getMock();
$oDbStub->method('getOne') $oDbStub->method('getOne')
->willReturn(1519430221); ->willReturn(1519430221);

View File

@@ -2,17 +2,18 @@
namespace Nominatim; 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 class TokenTest extends \PHPUnit\Framework\TestCase
{ {
protected function setUp() 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') $this->oNormalizer->method('transliterate')
->will($this->returnCallback(function ($text) { ->will($this->returnCallback(function ($text) {
return strtolower($text); return strtolower($text);
@@ -55,7 +56,9 @@ class TokenTest extends \PHPUnit\Framework\TestCase
{ {
$this->expectOutputRegex('/<p><tt>/'); $this->expectOutputRegex('/<p><tt>/');
$oDbStub = $this->getMock(\DB::class, array('getAll')); $oDbStub = $this->getMockBuilder(\DB::class)
->setMethods(array('getAll'))
->getMock();
$oDbStub->method('getAll') $oDbStub->method('getAll')
->will($this->returnCallback(function ($sql) { ->will($this->returnCallback(function ($sql) {
$aResults = array(); $aResults = array();

View File

@@ -1 +1,2 @@
<?php <?php
@define('CONST_BasePath', '../..');

View File

@@ -8,13 +8,14 @@
processIsolation="false" processIsolation="false"
stopOnFailure="false" stopOnFailure="false"
syntaxCheck="true" syntaxCheck="true"
bootstrap="test/php/bootstrap.php" bootstrap="./bootstrap.php"
beStrictAboutTestsThatDoNotTestAnything="true"
> >
<php> <php>
</php> </php>
<testsuites> <testsuites>
<testsuite name="Nominatim PHP Test Suite"> <testsuite name="Nominatim PHP Test Suite">
<directory>./test/php/Nominatim</directory> <directory>./Nominatim</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter> <filter>