mirror of
https://github.com/osm-search/Nominatim.git
synced 2024-11-22 12:06:27 +03:00
remove PHP tests
This commit is contained in:
parent
979aebbfcd
commit
06683edaae
93
.github/workflows/ci-tests.yml
vendored
93
.github/workflows/ci-tests.yml
vendored
@ -131,99 +131,6 @@ jobs:
|
||||
working-directory: Nominatim
|
||||
if: matrix.flavour != 'oldstuff'
|
||||
|
||||
legacy-test:
|
||||
needs: create-archive
|
||||
runs-on: ubuntu-20.04
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
postgresql: ["13", "16"]
|
||||
|
||||
steps:
|
||||
- uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: full-source
|
||||
|
||||
- name: Unpack Nominatim
|
||||
run: tar xf nominatim-src.tar.bz2
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: '7.4'
|
||||
|
||||
- uses: ./Nominatim/.github/actions/setup-postgresql
|
||||
with:
|
||||
postgresql-version: ${{ matrix.postgresql }}
|
||||
postgis-version: 3
|
||||
|
||||
- name: Install Postgresql server dev
|
||||
run: sudo apt-get install postgresql-server-dev-$PGVER
|
||||
env:
|
||||
PGVER: ${{ matrix.postgresql }}
|
||||
|
||||
- uses: ./Nominatim/.github/actions/build-nominatim
|
||||
with:
|
||||
cmake-args: -DBUILD_MODULE=on
|
||||
|
||||
- name: Install test prerequisites
|
||||
run: sudo apt-get install -y -qq python3-behave
|
||||
|
||||
- name: BDD tests (legacy tokenizer)
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/build/osm2pgsql:$PATH
|
||||
python3 -m behave -DREMOVE_TEMPLATE=1 -DSERVER_MODULE_PATH=$GITHUB_WORKSPACE/build/module -DAPI_ENGINE=php -DTOKENIZER=legacy --format=progress3
|
||||
working-directory: Nominatim/test/bdd
|
||||
|
||||
|
||||
php-test:
|
||||
needs: create-archive
|
||||
runs-on: ubuntu-22.04
|
||||
|
||||
steps:
|
||||
- uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: full-source
|
||||
|
||||
- name: Unpack Nominatim
|
||||
run: tar xf nominatim-src.tar.bz2
|
||||
|
||||
- uses: ./Nominatim/.github/actions/setup-postgresql
|
||||
with:
|
||||
postgresql-version: 15
|
||||
postgis-version: 3
|
||||
|
||||
- name: Setup PHP
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
php-version: 8.1
|
||||
tools: phpunit:9, phpcs, composer
|
||||
ini-values: opcache.jit=disable
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: PHP linting
|
||||
run: phpcs --report-width=120 .
|
||||
working-directory: Nominatim
|
||||
|
||||
- name: PHP unit tests
|
||||
run: phpunit ./
|
||||
working-directory: Nominatim/test/php
|
||||
|
||||
- uses: ./Nominatim/.github/actions/build-nominatim
|
||||
with:
|
||||
flavour: 'ubuntu-22'
|
||||
|
||||
- name: Install test prerequisites
|
||||
run: sudo apt-get install -y -qq python3-behave
|
||||
|
||||
- name: BDD tests (php)
|
||||
run: |
|
||||
export PATH=$GITHUB_WORKSPACE/build/osm2pgsql:$PATH
|
||||
python3 -m behave -DREMOVE_TEMPLATE=1 -DAPI_ENGINE=php --format=progress3
|
||||
working-directory: Nominatim/test/bdd
|
||||
|
||||
|
||||
install:
|
||||
runs-on: ubuntu-latest
|
||||
needs: create-archive
|
||||
|
@ -1,14 +1,10 @@
|
||||
all: bdd php python
|
||||
no-test-db: bdd-no-test-db php
|
||||
all: bdd python
|
||||
|
||||
bdd:
|
||||
cd bdd && behave -DREMOVE_TEMPLATE=1
|
||||
|
||||
php:
|
||||
cd php && phpunit ./
|
||||
|
||||
python:
|
||||
pytest python
|
||||
|
||||
|
||||
.PHONY: bdd php no-test-db python
|
||||
.PHONY: bdd python
|
||||
|
@ -1,118 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/init-website.php');
|
||||
require_once(CONST_LibDir.'/AddressDetails.php');
|
||||
|
||||
|
||||
class AddressDetailsTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
// How the fixture got created
|
||||
//
|
||||
// 1) search for '10 downing street'
|
||||
// https://nominatim.openstreetmap.org/details.php?osmtype=R&osmid=1879842
|
||||
//
|
||||
// 2) find place_id in the local database
|
||||
// SELECT place_id, name FROM placex WHERE osm_type='R' AND osm_id=1879842;
|
||||
//
|
||||
// 3) set postgresql to non-align output, e.g. psql -A or \a in the CLI
|
||||
//
|
||||
// 4) query
|
||||
// SELECT row_to_json(row,true) FROM (
|
||||
// SELECT *, get_name_by_language(name, ARRAY['name:en']) as localname
|
||||
// FROM get_addressdata(194663412,10)
|
||||
// ORDER BY rank_address DESC, isaddress DESC
|
||||
// ) AS row;
|
||||
//
|
||||
// 5) copy&paste into file. Add commas between records
|
||||
//
|
||||
$json = file_get_contents(CONST_DataDir.'/test/php/fixtures/address_details_10_downing_street.json');
|
||||
$data = json_decode($json, true);
|
||||
|
||||
$this->oDbStub = $this->getMockBuilder(\DB::class)
|
||||
->setMethods(array('getAll'))
|
||||
->getMock();
|
||||
$this->oDbStub->method('getAll')
|
||||
->willReturn($data);
|
||||
}
|
||||
|
||||
public function testGetLocaleAddress()
|
||||
{
|
||||
$oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
|
||||
$expected = join(', ', array(
|
||||
'10 Downing Street',
|
||||
'10',
|
||||
'Downing Street',
|
||||
'St. James\'s',
|
||||
'Covent Garden',
|
||||
'Westminster',
|
||||
'London',
|
||||
'Greater London',
|
||||
'England',
|
||||
'SW1A 2AA',
|
||||
'United Kingdom'
|
||||
));
|
||||
$this->assertEquals($expected, $oAD->getLocaleAddress());
|
||||
}
|
||||
|
||||
public function testGetAddressDetails()
|
||||
{
|
||||
$oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
|
||||
$this->assertEquals(18, count($oAD->getAddressDetails(true)));
|
||||
$this->assertEquals(12, count($oAD->getAddressDetails(false)));
|
||||
}
|
||||
|
||||
public function testGetAddressNames()
|
||||
{
|
||||
$oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
|
||||
$expected = array(
|
||||
'tourism' => '10 Downing Street',
|
||||
'house_number' => '10',
|
||||
'road' => 'Downing Street',
|
||||
'neighbourhood' => 'St. James\'s',
|
||||
'suburb' => 'Covent Garden',
|
||||
'city' => 'London',
|
||||
'state_district' => 'Greater London',
|
||||
'state' => 'England',
|
||||
'ISO3166-2-lvl4' => 'GB-ENG',
|
||||
'ISO3166-2-lvl6' => 'GB-LND',
|
||||
'postcode' => 'SW1A 2AA',
|
||||
'country' => 'United Kingdom',
|
||||
'country_code' => 'gb'
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $oAD->getAddressNames());
|
||||
}
|
||||
|
||||
public function testGetAdminLevels()
|
||||
{
|
||||
$oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
|
||||
$expected = array(
|
||||
'level8' => 'Westminster',
|
||||
'level6' => 'London',
|
||||
'level5' => 'Greater London',
|
||||
'level4' => 'England',
|
||||
'level2' => 'United Kingdom'
|
||||
);
|
||||
$this->assertEquals($expected, $oAD->getAdminLevels());
|
||||
}
|
||||
|
||||
public function testDebugInfo()
|
||||
{
|
||||
$oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
|
||||
$this->assertTrue(is_array($oAD->debugInfo()));
|
||||
$this->assertEquals(18, count($oAD->debugInfo()));
|
||||
}
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/ClassTypes.php');
|
||||
|
||||
class ClassTypesTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testGetLabelTag()
|
||||
{
|
||||
$aPlace = array('class' => 'boundary', 'type' => 'administrative',
|
||||
'rank_address' => '4', 'place_type' => 'city');
|
||||
$this->assertEquals('city', ClassTypes\getLabelTag($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'boundary', 'type' => 'administrative',
|
||||
'rank_address' => '10');
|
||||
$this->assertEquals('state_district', ClassTypes\getLabelTag($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'boundary', 'type' => 'administrative');
|
||||
$this->assertEquals('administrative', ClassTypes\getLabelTag($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'place', 'type' => 'hamlet', 'rank_address' => '20');
|
||||
$this->assertEquals('hamlet', ClassTypes\getLabelTag($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'highway', 'type' => 'residential',
|
||||
'rank_address' => '26');
|
||||
$this->assertEquals('road', ClassTypes\getLabelTag($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'place', 'type' => 'house_number',
|
||||
'rank_address' => '30');
|
||||
$this->assertEquals('house_number', ClassTypes\getLabelTag($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'amenity', 'type' => 'prison',
|
||||
'rank_address' => '30');
|
||||
$this->assertEquals('amenity', ClassTypes\getLabelTag($aPlace));
|
||||
}
|
||||
|
||||
public function testGetLabel()
|
||||
{
|
||||
$aPlace = array('class' => 'boundary', 'type' => 'administrative',
|
||||
'rank_address' => '4', 'place_type' => 'city');
|
||||
$this->assertEquals('City', ClassTypes\getLabel($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'boundary', 'type' => 'administrative',
|
||||
'rank_address' => '10');
|
||||
$this->assertEquals('State District', ClassTypes\getLabel($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'boundary', 'type' => 'administrative');
|
||||
$this->assertEquals('Administrative', ClassTypes\getLabel($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'amenity', 'type' => 'prison');
|
||||
$this->assertEquals('Prison', ClassTypes\getLabel($aPlace));
|
||||
|
||||
$aPlace = array('class' => 'amenity', 'type' => 'foobar');
|
||||
$this->assertNull(ClassTypes\getLabel($aPlace));
|
||||
}
|
||||
|
||||
public function testGetBoundaryLabel()
|
||||
{
|
||||
$this->assertEquals('City', ClassTypes\getBoundaryLabel(8, null));
|
||||
$this->assertEquals('Administrative', ClassTypes\getBoundaryLabel(18, null));
|
||||
$this->assertEquals('None', ClassTypes\getBoundaryLabel(18, null, 'None'));
|
||||
$this->assertEquals('State', ClassTypes\getBoundaryLabel(4, 'de', 'None'));
|
||||
$this->assertEquals('County', ClassTypes\getBoundaryLabel(4, 'se', 'None'));
|
||||
$this->assertEquals('Municipality', ClassTypes\getBoundaryLabel(7, 'se', 'None'));
|
||||
}
|
||||
|
||||
public function testGetDefRadius()
|
||||
{
|
||||
$aResult = array('class' => '', 'type' => '');
|
||||
$this->assertEquals(0.00005, ClassTypes\getDefRadius($aResult));
|
||||
|
||||
$aResult = array('class' => 'place', 'type' => 'country');
|
||||
$this->assertEquals(7, ClassTypes\getDefRadius($aResult));
|
||||
}
|
||||
|
||||
public function testGetIcon()
|
||||
{
|
||||
$aResult = array('class' => '', 'type' => '');
|
||||
$this->assertNull(ClassTypes\getIcon($aResult));
|
||||
|
||||
$aResult = array('class' => 'place', 'type' => 'airport');
|
||||
$this->assertEquals('transport_airport2', ClassTypes\getIcon($aResult));
|
||||
}
|
||||
|
||||
public function testGetImportance()
|
||||
{
|
||||
$aResult = array('class' => '', 'type' => '');
|
||||
$this->assertNull(ClassTypes\getImportance($aResult));
|
||||
|
||||
$aResult = array('class' => 'place', 'type' => 'airport');
|
||||
$this->assertGreaterThan(0, ClassTypes\getImportance($aResult));
|
||||
}
|
||||
}
|
@ -1,228 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/lib.php');
|
||||
require_once(CONST_LibDir.'/DB.php');
|
||||
|
||||
// subclassing so we can set the protected connection variable
|
||||
class NominatimSubClassedDB extends \Nominatim\DB
|
||||
{
|
||||
public function setConnection($oConnection)
|
||||
{
|
||||
$this->connection = $oConnection;
|
||||
}
|
||||
}
|
||||
|
||||
// phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
|
||||
class DBTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testReusingConnection()
|
||||
{
|
||||
$oDB = new NominatimSubClassedDB('');
|
||||
$oDB->setConnection('anything');
|
||||
$this->assertTrue($oDB->connect());
|
||||
}
|
||||
|
||||
public function testCheckConnection()
|
||||
{
|
||||
$oDB = new \Nominatim\DB('');
|
||||
$this->assertFalse($oDB->checkConnection());
|
||||
}
|
||||
|
||||
public function testErrorHandling()
|
||||
{
|
||||
$this->expectException(DatabaseError::class);
|
||||
$this->expectExceptionMessage('Failed to establish database connection');
|
||||
|
||||
$oDB = new \Nominatim\DB('pgsql:dbname=abc');
|
||||
$oDB->connect();
|
||||
}
|
||||
|
||||
public function testErrorHandling2()
|
||||
{
|
||||
$this->expectException(DatabaseError::class);
|
||||
$this->expectExceptionMessage('Database query failed');
|
||||
|
||||
$oPDOStub = $this->getMockBuilder(PDO::class)
|
||||
->setMethods(array('query', 'quote'))
|
||||
->getMock();
|
||||
|
||||
$oPDOStub->method('query')
|
||||
->will($this->returnCallback(function ($sVal) {
|
||||
return "'$sVal'";
|
||||
}));
|
||||
|
||||
$oPDOStub->method('query')
|
||||
->will($this->returnCallback(function () {
|
||||
throw new \PDOException('ERROR: syntax error at or near "FROM"');
|
||||
}));
|
||||
|
||||
$oDB = new NominatimSubClassedDB('');
|
||||
$oDB->setConnection($oPDOStub);
|
||||
$oDB->getOne('SELECT name FROM');
|
||||
}
|
||||
|
||||
public function testGetPostgresVersion()
|
||||
{
|
||||
$oDBStub = $this->getMockBuilder(\Nominatim\DB::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getOne'))
|
||||
->getMock();
|
||||
|
||||
$oDBStub->method('getOne')
|
||||
->willReturn('100006');
|
||||
|
||||
$this->assertEquals(10, $oDBStub->getPostgresVersion());
|
||||
}
|
||||
|
||||
public function testGetPostgisVersion()
|
||||
{
|
||||
$oDBStub = $this->getMockBuilder(\Nominatim\DB::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('getOne'))
|
||||
->getMock();
|
||||
|
||||
$oDBStub->method('getOne')
|
||||
->willReturn('2.4.4');
|
||||
|
||||
$this->assertEquals(2.4, $oDBStub->getPostgisVersion());
|
||||
}
|
||||
|
||||
public function testParseDSN()
|
||||
{
|
||||
$this->assertEquals(
|
||||
array(),
|
||||
\Nominatim\DB::parseDSN('')
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'database' => 'db1',
|
||||
'hostspec' => 'machine1'
|
||||
),
|
||||
\Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1')
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'database' => 'db1',
|
||||
'hostspec' => 'machine1',
|
||||
'port' => '1234',
|
||||
'username' => 'john',
|
||||
'password' => 'secret'
|
||||
),
|
||||
\Nominatim\DB::parseDSN('pgsql:dbname=db1;host=machine1;port=1234;user=john;password=secret')
|
||||
);
|
||||
}
|
||||
|
||||
public function testGenerateDSN()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'pgsql:',
|
||||
\Nominatim\DB::generateDSN(array())
|
||||
);
|
||||
$this->assertEquals(
|
||||
'pgsql:host=machine1;dbname=db1',
|
||||
\Nominatim\DB::generateDSN(\Nominatim\DB::parseDSN('pgsql:host=machine1;dbname=db1'))
|
||||
);
|
||||
}
|
||||
|
||||
public function testAgainstDatabase()
|
||||
{
|
||||
$unit_test_dsn = getenv('UNIT_TEST_DSN') != false ?
|
||||
getenv('UNIT_TEST_DSN') :
|
||||
'pgsql:dbname=nominatim_unit_tests';
|
||||
|
||||
## Create the database.
|
||||
{
|
||||
$aDSNParsed = \Nominatim\DB::parseDSN($unit_test_dsn);
|
||||
$sDbname = $aDSNParsed['database'];
|
||||
$aDSNParsed['database'] = 'postgres';
|
||||
|
||||
$oDB = new \Nominatim\DB(\Nominatim\DB::generateDSN($aDSNParsed));
|
||||
$oDB->connect();
|
||||
$oDB->exec('DROP DATABASE IF EXISTS ' . $sDbname);
|
||||
$oDB->exec('CREATE DATABASE ' . $sDbname);
|
||||
}
|
||||
|
||||
$oDB = new \Nominatim\DB($unit_test_dsn);
|
||||
$oDB->connect();
|
||||
|
||||
$this->assertTrue(
|
||||
$oDB->checkConnection($sDbname)
|
||||
);
|
||||
|
||||
# Tables, Indices
|
||||
{
|
||||
$oDB->exec('CREATE TABLE table1 (id integer, city varchar, country varchar)');
|
||||
|
||||
$this->assertTrue($oDB->tableExists('table1'));
|
||||
$this->assertFalse($oDB->tableExists('table99'));
|
||||
$this->assertFalse($oDB->tableExists(null));
|
||||
}
|
||||
|
||||
# select queries
|
||||
{
|
||||
$oDB->exec(
|
||||
"INSERT INTO table1 VALUES (1, 'Berlin', 'Germany'), (2, 'Paris', 'France')"
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
array('city' => 'Berlin'),
|
||||
array('city' => 'Paris')
|
||||
),
|
||||
$oDB->getAll('SELECT city FROM table1')
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(),
|
||||
$oDB->getAll('SELECT city FROM table1 WHERE id=999')
|
||||
);
|
||||
|
||||
|
||||
$this->assertEquals(
|
||||
array('id' => 1, 'city' => 'Berlin', 'country' => 'Germany'),
|
||||
$oDB->getRow('SELECT * FROM table1 WHERE id=1')
|
||||
);
|
||||
$this->assertEquals(
|
||||
false,
|
||||
$oDB->getRow('SELECT * FROM table1 WHERE id=999')
|
||||
);
|
||||
|
||||
|
||||
$this->assertEquals(
|
||||
array('Berlin', 'Paris'),
|
||||
$oDB->getCol('SELECT city FROM table1')
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(),
|
||||
$oDB->getCol('SELECT city FROM table1 WHERE id=999')
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'Berlin',
|
||||
$oDB->getOne('SELECT city FROM table1 WHERE id=1')
|
||||
);
|
||||
$this->assertEquals(
|
||||
null,
|
||||
$oDB->getOne('SELECT city FROM table1 WHERE id=999')
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
array('Berlin' => 'Germany', 'Paris' => 'France'),
|
||||
$oDB->getAssoc('SELECT city, country FROM table1')
|
||||
);
|
||||
$this->assertEquals(
|
||||
array(),
|
||||
$oDB->getAssoc('SELECT city, country FROM table1 WHERE id=999')
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/init-website.php');
|
||||
require_once(CONST_LibDir.'/DatabaseError.php');
|
||||
|
||||
class DatabaseErrorTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testSqlMessage()
|
||||
{
|
||||
$oSqlStub = $this->getMockBuilder(PDOException::class)
|
||||
->setMethods(array('getMessage'))
|
||||
->getMock();
|
||||
|
||||
$oSqlStub->method('getMessage')
|
||||
->willReturn('Unknown table.');
|
||||
|
||||
$oErr = new DatabaseError('Sql error', 123, null, $oSqlStub);
|
||||
$this->assertEquals('Sql error', $oErr->getMessage());
|
||||
$this->assertEquals(123, $oErr->getCode());
|
||||
$this->assertEquals('Unknown table.', $oErr->getSqlError());
|
||||
}
|
||||
|
||||
public function testSqlObjectDump()
|
||||
{
|
||||
$oErr = new DatabaseError('Sql error', 123, null, array('one' => 'two'));
|
||||
$this->assertStringContainsString('two', $oErr->getSqlDebugDump());
|
||||
}
|
||||
}
|
@ -1,209 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/DebugHtml.php');
|
||||
|
||||
class DebugTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$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->getMockBuilder(\SomeMock::class)
|
||||
->setMethods(array('__toString'))
|
||||
->getMock();
|
||||
$this->oWithToString->method('__toString')->willReturn('me as string');
|
||||
}
|
||||
|
||||
public function testPrintVar()
|
||||
{
|
||||
$this->expectOutputString(<<<EOT
|
||||
<pre><b>Var0:</b> </pre>
|
||||
<pre><b>Var1:</b> <i>True</i></pre>
|
||||
<pre><b>Var2:</b> <i>False</i></pre>
|
||||
<pre><b>Var3:</b> 0</pre>
|
||||
<pre><b>Var4:</b> 'String'</pre>
|
||||
<pre><b>Var5:</b> 0 => 'one'
|
||||
1 => 'two'
|
||||
2 => 'three'</pre>
|
||||
<pre><b>Var6:</b> 'key' => 'value'
|
||||
'key2' => 'value2'</pre>
|
||||
<pre><b>Var7:</b> me as string</pre>
|
||||
<pre><b>Var8:</b> 'value', 'value2'</pre>
|
||||
|
||||
EOT
|
||||
);
|
||||
|
||||
Debug::printVar('Var0', null);
|
||||
Debug::printVar('Var1', true);
|
||||
Debug::printVar('Var2', false);
|
||||
Debug::printVar('Var3', 0);
|
||||
Debug::printVar('Var4', 'String');
|
||||
Debug::printVar('Var5', array('one', 'two', 'three'));
|
||||
Debug::printVar('Var6', array('key' => 'value', 'key2' => 'value2'));
|
||||
Debug::printVar('Var7', $this->oWithToString);
|
||||
Debug::printVar('Var8', Debug::fmtArrayVals(array('key' => 'value', 'key2' => 'value2')));
|
||||
}
|
||||
|
||||
|
||||
public function testDebugArray()
|
||||
{
|
||||
$this->expectOutputString(<<<EOT
|
||||
<pre><b>Arr0:</b> 'null'</pre>
|
||||
<pre><b>Arr1:</b> 'key1' => 'val1'
|
||||
'key2' => 'val2'
|
||||
'key3' => 'val3'</pre>
|
||||
|
||||
EOT
|
||||
);
|
||||
|
||||
Debug::printDebugArray('Arr0', null);
|
||||
Debug::printDebugArray('Arr1', $this->oWithDebuginfo);
|
||||
}
|
||||
|
||||
|
||||
public function testPrintDebugTable()
|
||||
{
|
||||
$this->expectOutputString(<<<EOT
|
||||
<b>Table1:</b>
|
||||
<table border='1'>
|
||||
</table>
|
||||
<b>Table2:</b>
|
||||
<table border='1'>
|
||||
</table>
|
||||
<b>Table3:</b>
|
||||
<table border='1'>
|
||||
<tr>
|
||||
<th><small>0</small></th>
|
||||
<th><small>1</small></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><pre>'one'</pre></td>
|
||||
<td><pre>'two'</pre></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><pre>'three'</pre></td>
|
||||
<td><pre>'four'</pre></td>
|
||||
</tr>
|
||||
</table>
|
||||
<b>Table4:</b>
|
||||
<table border='1'>
|
||||
<tr>
|
||||
<th><small>key1</small></th>
|
||||
<th><small>key2</small></th>
|
||||
<th><small>key3</small></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><pre>'val1'</pre></td>
|
||||
<td><pre>'val2'</pre></td>
|
||||
<td><pre>'val3'</pre></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
EOT
|
||||
);
|
||||
|
||||
Debug::printDebugTable('Table1', null);
|
||||
|
||||
Debug::printDebugTable('Table2', array());
|
||||
|
||||
// Numeric headers
|
||||
Debug::printDebugTable('Table3', array(array('one', 'two'), array('three', 'four')));
|
||||
|
||||
// Associate array
|
||||
Debug::printDebugTable('Table4', array($this->oWithDebuginfo));
|
||||
}
|
||||
|
||||
public function testPrintGroupTable()
|
||||
{
|
||||
$this->expectOutputString(<<<EOT
|
||||
<b>Table1:</b>
|
||||
<table border='1'>
|
||||
</table>
|
||||
<b>Table2:</b>
|
||||
<table border='1'>
|
||||
</table>
|
||||
<b>Table3:</b>
|
||||
<table border='1'>
|
||||
<tr>
|
||||
<th><small>Group</small></th>
|
||||
<th><small>key1</small></th>
|
||||
<th><small>key2</small></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><pre>group1</pre></td>
|
||||
<td><pre>'val1'</pre></td>
|
||||
<td><pre>'val2'</pre></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><pre>group1</pre></td>
|
||||
<td><pre>'one'</pre></td>
|
||||
<td><pre>'two'</pre></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><pre>group2</pre></td>
|
||||
<td><pre>'val1'</pre></td>
|
||||
<td><pre>'val2'</pre></td>
|
||||
</tr>
|
||||
</table>
|
||||
<b>Table4:</b>
|
||||
<table border='1'>
|
||||
<tr>
|
||||
<th><small>Group</small></th>
|
||||
<th><small>key1</small></th>
|
||||
<th><small>key2</small></th>
|
||||
<th><small>key3</small></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><pre>group1</pre></td>
|
||||
<td><pre>'val1'</pre></td>
|
||||
<td><pre>'val2'</pre></td>
|
||||
<td><pre>'val3'</pre></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><pre>group1</pre></td>
|
||||
<td><pre>'val1'</pre></td>
|
||||
<td><pre>'val2'</pre></td>
|
||||
<td><pre>'val3'</pre></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
EOT
|
||||
);
|
||||
|
||||
Debug::printGroupTable('Table1', null);
|
||||
Debug::printGroupTable('Table2', array());
|
||||
|
||||
// header are taken from first group item, thus no key3 gets printed
|
||||
$aGroups = array(
|
||||
'group1' => array(
|
||||
array('key1' => 'val1', 'key2' => 'val2'),
|
||||
array('key1' => 'one', 'key2' => 'two', 'unknown' => 1),
|
||||
),
|
||||
'group2' => array(
|
||||
array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3'),
|
||||
)
|
||||
);
|
||||
Debug::printGroupTable('Table3', $aGroups);
|
||||
|
||||
$aGroups = array(
|
||||
'group1' => array($this->oWithDebuginfo, $this->oWithDebuginfo),
|
||||
);
|
||||
Debug::printGroupTable('Table4', $aGroups);
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/lib.php');
|
||||
require_once(CONST_LibDir.'/ClassTypes.php');
|
||||
|
||||
class LibTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testAddQuotes()
|
||||
{
|
||||
// FIXME: not quoting existing quote signs is probably a bug
|
||||
$this->assertSame("'St. John's'", addQuotes("St. John's"));
|
||||
$this->assertSame("''", addQuotes(''));
|
||||
}
|
||||
|
||||
public function testParseLatLon()
|
||||
{
|
||||
// no coordinates expected
|
||||
$this->assertFalse(parseLatLon(''));
|
||||
$this->assertFalse(parseLatLon('abc'));
|
||||
$this->assertFalse(parseLatLon('12 34'));
|
||||
|
||||
// coordinates expected
|
||||
$this->assertNotNull(parseLatLon('0.0 -0.0'));
|
||||
|
||||
$aRes = parseLatLon(' abc 12.456 -78.90 def ');
|
||||
$this->assertEquals($aRes[1], 12.456);
|
||||
$this->assertEquals($aRes[2], -78.90);
|
||||
$this->assertEquals($aRes[0], ' 12.456 -78.90 ');
|
||||
|
||||
$aRes = parseLatLon(' [12.456,-78.90] ');
|
||||
$this->assertEquals($aRes[1], 12.456);
|
||||
$this->assertEquals($aRes[2], -78.90);
|
||||
$this->assertEquals($aRes[0], ' [12.456,-78.90] ');
|
||||
|
||||
$aRes = parseLatLon(' -12.456,-78.90 ');
|
||||
$this->assertEquals($aRes[1], -12.456);
|
||||
$this->assertEquals($aRes[2], -78.90);
|
||||
$this->assertEquals($aRes[0], ' -12.456,-78.90 ');
|
||||
|
||||
// http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
|
||||
// these all represent the same location
|
||||
$aQueries = array(
|
||||
'40 26.767 N 79 58.933 W',
|
||||
'40° 26.767′ N 79° 58.933′ W',
|
||||
"40° 26.767' N 79° 58.933' W",
|
||||
"40° 26.767'
|
||||
N 79° 58.933' W",
|
||||
'N 40 26.767, W 79 58.933',
|
||||
'N 40°26.767′, W 79°58.933′',
|
||||
' N 40°26.767′, W 79°58.933′',
|
||||
"N 40°26.767', W 79°58.933'",
|
||||
|
||||
'40 26 46 N 79 58 56 W',
|
||||
'40° 26′ 46″ N 79° 58′ 56″ W',
|
||||
'40° 26′ 46.00″ N 79° 58′ 56.00″ W',
|
||||
'40°26′46″N 79°58′56″W',
|
||||
'N 40 26 46 W 79 58 56',
|
||||
'N 40° 26′ 46″, W 79° 58′ 56″',
|
||||
'N 40° 26\' 46", W 79° 58\' 56"',
|
||||
'N 40° 26\' 46", W 79° 58\' 56"',
|
||||
|
||||
'40.446 -79.982',
|
||||
'40.446,-79.982',
|
||||
'40.446° N 79.982° W',
|
||||
'N 40.446° W 79.982°',
|
||||
|
||||
'[40.446 -79.982]',
|
||||
'[40.446,-79.982]',
|
||||
' 40.446 , -79.982 ',
|
||||
' 40.446 , -79.982 ',
|
||||
' 40.446 , -79.982 ',
|
||||
' 40.446, -79.982 ',
|
||||
);
|
||||
|
||||
|
||||
foreach ($aQueries as $sQuery) {
|
||||
$aRes = parseLatLon($sQuery);
|
||||
$this->assertEqualsWithDelta(40.446, $aRes[1], 0.01, 'degrees decimal ' . $sQuery);
|
||||
$this->assertEqualsWithDelta(-79.982, $aRes[2], 0.01, 'degrees decimal ' . $sQuery);
|
||||
$this->assertEquals($sQuery, $aRes[0]);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,248 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/ParameterParser.php');
|
||||
|
||||
|
||||
function userError($sError)
|
||||
{
|
||||
throw new \Exception($sError);
|
||||
}
|
||||
|
||||
class ParameterParserTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
|
||||
public function testGetBool()
|
||||
{
|
||||
$oParams = new ParameterParser(array(
|
||||
'bool1' => '1',
|
||||
'bool2' => '0',
|
||||
'bool3' => 'true',
|
||||
'bool4' => 'false',
|
||||
'bool5' => ''
|
||||
));
|
||||
|
||||
$this->assertSame(false, $oParams->getBool('non-exists'));
|
||||
$this->assertSame(true, $oParams->getBool('non-exists', true));
|
||||
$this->assertSame(true, $oParams->getBool('bool1'));
|
||||
$this->assertSame(false, $oParams->getBool('bool2'));
|
||||
$this->assertSame(true, $oParams->getBool('bool3'));
|
||||
$this->assertSame(true, $oParams->getBool('bool4'));
|
||||
$this->assertSame(false, $oParams->getBool('bool5'));
|
||||
}
|
||||
|
||||
|
||||
public function testGetInt()
|
||||
{
|
||||
$oParams = new ParameterParser(array(
|
||||
'int1' => '5',
|
||||
'int2' => '-1',
|
||||
'int3' => 0
|
||||
));
|
||||
|
||||
$this->assertSame(false, $oParams->getInt('non-exists'));
|
||||
$this->assertSame(999, $oParams->getInt('non-exists', 999));
|
||||
$this->assertSame(5, $oParams->getInt('int1'));
|
||||
|
||||
$this->assertSame(-1, $oParams->getInt('int2'));
|
||||
$this->assertSame(0, $oParams->getInt('int3'));
|
||||
}
|
||||
|
||||
|
||||
public function testGetIntWithNonNumber()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage("Integer number expected for parameter 'int4'");
|
||||
|
||||
(new ParameterParser(array('int4' => 'a')))->getInt('int4');
|
||||
}
|
||||
|
||||
|
||||
public function testGetIntWithEmpytString()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage("Integer number expected for parameter 'int5'");
|
||||
|
||||
(new ParameterParser(array('int5' => '')))->getInt('int5');
|
||||
}
|
||||
|
||||
|
||||
public function testGetFloat()
|
||||
{
|
||||
|
||||
$oParams = new ParameterParser(array(
|
||||
'float1' => '1.0',
|
||||
'float2' => '-5',
|
||||
'float3' => 0
|
||||
));
|
||||
|
||||
$this->assertSame(false, $oParams->getFloat('non-exists'));
|
||||
$this->assertSame(999, $oParams->getFloat('non-exists', 999));
|
||||
$this->assertSame(1.0, $oParams->getFloat('float1'));
|
||||
$this->assertSame(-5.0, $oParams->getFloat('float2'));
|
||||
$this->assertSame(0.0, $oParams->getFloat('float3'));
|
||||
}
|
||||
|
||||
public function testGetFloatWithEmptyString()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage("Floating-point number expected for parameter 'float4'");
|
||||
|
||||
(new ParameterParser(array('float4' => '')))->getFloat('float4');
|
||||
}
|
||||
|
||||
public function testGetFloatWithTextString()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage("Floating-point number expected for parameter 'float5'");
|
||||
|
||||
(new ParameterParser(array('float5' => 'a')))->getFloat('float5');
|
||||
}
|
||||
|
||||
|
||||
public function testGetFloatWithInvalidNumber()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage("Floating-point number expected for parameter 'float6'");
|
||||
|
||||
(new ParameterParser(array('float6' => '-55.')))->getFloat('float6');
|
||||
}
|
||||
|
||||
|
||||
public function testGetString()
|
||||
{
|
||||
$oParams = new ParameterParser(array(
|
||||
'str1' => 'abc',
|
||||
'str2' => '',
|
||||
'str3' => '0'
|
||||
));
|
||||
|
||||
$this->assertSame(false, $oParams->getString('non-exists'));
|
||||
$this->assertSame('default', $oParams->getString('non-exists', 'default'));
|
||||
$this->assertSame('abc', $oParams->getString('str1'));
|
||||
$this->assertSame(false, $oParams->getStringList('str2'));
|
||||
$this->assertSame(false, $oParams->getStringList('str3')); // sadly PHP magic treats 0 as false when returned
|
||||
}
|
||||
|
||||
|
||||
public function testGetSet()
|
||||
{
|
||||
$oParams = new ParameterParser(array(
|
||||
'val1' => 'foo',
|
||||
'val2' => '',
|
||||
'val3' => 0
|
||||
));
|
||||
|
||||
$this->assertSame(false, $oParams->getSet('non-exists', array('foo', 'bar')));
|
||||
$this->assertSame('default', $oParams->getSet('non-exists', array('foo', 'bar'), 'default'));
|
||||
$this->assertSame('foo', $oParams->getSet('val1', array('foo', 'bar')));
|
||||
|
||||
$this->assertSame(false, $oParams->getSet('val2', array('foo', 'bar')));
|
||||
$this->assertSame(false, $oParams->getSet('val3', array('foo', 'bar')));
|
||||
}
|
||||
|
||||
|
||||
public function testGetSetWithValueNotInSet()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage("Parameter 'val4' must be one of: foo, bar");
|
||||
|
||||
(new ParameterParser(array('val4' => 'faz')))->getSet('val4', array('foo', 'bar'));
|
||||
}
|
||||
|
||||
|
||||
public function testGetStringList()
|
||||
{
|
||||
$oParams = new ParameterParser(array(
|
||||
'list1' => ',a,b,c,,c,d',
|
||||
'list2' => 'a',
|
||||
'list3' => '',
|
||||
'list4' => '0'
|
||||
));
|
||||
|
||||
$this->assertSame(false, $oParams->getStringList('non-exists'));
|
||||
$this->assertSame(array('a', 'b'), $oParams->getStringList('non-exists', array('a', 'b')));
|
||||
$this->assertSame(array('a', 'b', 'c', 'c', 'd'), $oParams->getStringList('list1'));
|
||||
$this->assertSame(array('a'), $oParams->getStringList('list2'));
|
||||
$this->assertSame(false, $oParams->getStringList('list3'));
|
||||
$this->assertSame(false, $oParams->getStringList('list4'));
|
||||
}
|
||||
|
||||
|
||||
public function testGetPreferredLanguages()
|
||||
{
|
||||
$oParams = new ParameterParser(array('accept-language' => ''));
|
||||
$this->assertSame(array(
|
||||
'name:default' => 'name:default',
|
||||
'_place_name:default' => '_place_name:default',
|
||||
'name' => 'name',
|
||||
'_place_name' => '_place_name'
|
||||
), array_slice($oParams->getPreferredLanguages('default'), 0, 4));
|
||||
|
||||
$oParams = new ParameterParser(array('accept-language' => 'de,en'));
|
||||
$this->assertSame(array(
|
||||
'name:de' => 'name:de',
|
||||
'_place_name:de' => '_place_name:de',
|
||||
'name:en' => 'name:en',
|
||||
'_place_name:en' => '_place_name:en',
|
||||
'name' => 'name',
|
||||
'_place_name' => '_place_name'
|
||||
), array_slice($oParams->getPreferredLanguages('default'), 0, 6));
|
||||
|
||||
$oParams = new ParameterParser(array('accept-language' => 'fr-ca,fr;q=0.8,en-ca;q=0.5,en;q=0.3'));
|
||||
$this->assertSame(array(
|
||||
'name:fr-ca' => 'name:fr-ca',
|
||||
'_place_name:fr-ca' => '_place_name:fr-ca',
|
||||
'name:fr' => 'name:fr',
|
||||
'_place_name:fr' => '_place_name:fr',
|
||||
'name:en-ca' => 'name:en-ca',
|
||||
'_place_name:en-ca' => '_place_name:en-ca',
|
||||
'name:en' => 'name:en',
|
||||
'_place_name:en' => '_place_name:en',
|
||||
'name' => 'name',
|
||||
'_place_name' => '_place_name'
|
||||
), array_slice($oParams->getPreferredLanguages('default'), 0, 10));
|
||||
|
||||
$oParams = new ParameterParser(array('accept-language' => 'ja_rm,zh_pinyin'));
|
||||
$this->assertSame(array(
|
||||
'name:ja_rm' => 'name:ja_rm',
|
||||
'_place_name:ja_rm' => '_place_name:ja_rm',
|
||||
'name:zh_pinyin' => 'name:zh_pinyin',
|
||||
'_place_name:zh_pinyin' => '_place_name:zh_pinyin',
|
||||
'name:ja' => 'name:ja',
|
||||
'_place_name:ja' => '_place_name:ja',
|
||||
'name:zh' => 'name:zh',
|
||||
'_place_name:zh' => '_place_name:zh',
|
||||
'name' => 'name',
|
||||
'_place_name' => '_place_name'
|
||||
), array_slice($oParams->getPreferredLanguages('default'), 0, 10));
|
||||
}
|
||||
|
||||
public function testHasSetAny()
|
||||
{
|
||||
$oParams = new ParameterParser(array(
|
||||
'one' => '',
|
||||
'two' => 0,
|
||||
'three' => '0',
|
||||
'four' => '1',
|
||||
'five' => 'anystring'
|
||||
));
|
||||
$this->assertFalse($oParams->hasSetAny(array()));
|
||||
$this->assertFalse($oParams->hasSetAny(array('')));
|
||||
$this->assertFalse($oParams->hasSetAny(array('unknown')));
|
||||
$this->assertFalse($oParams->hasSetAny(array('one', 'two', 'three')));
|
||||
$this->assertTrue($oParams->hasSetAny(array('one', 'four')));
|
||||
$this->assertTrue($oParams->hasSetAny(array('four')));
|
||||
$this->assertTrue($oParams->hasSetAny(array('five')));
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/Result.php');
|
||||
|
||||
function mkRankedResult($iId, $iResultRank)
|
||||
{
|
||||
$oResult = new Result($iId);
|
||||
$oResult->iResultRank = $iResultRank;
|
||||
|
||||
return $oResult;
|
||||
}
|
||||
|
||||
|
||||
class ResultTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testSplitResults()
|
||||
{
|
||||
$aSplitResults = Result::splitResults(array(
|
||||
mkRankedResult(1, 2),
|
||||
mkRankedResult(2, 0),
|
||||
mkRankedResult(3, 0),
|
||||
mkRankedResult(4, 2),
|
||||
mkRankedResult(5, 1)
|
||||
));
|
||||
|
||||
|
||||
$aHead = array_keys($aSplitResults['head']);
|
||||
$aTail = array_keys($aSplitResults['tail']);
|
||||
|
||||
$this->assertEquals($aHead, array(2, 3));
|
||||
$this->assertEquals($aTail, array(1, 4, 5));
|
||||
}
|
||||
}
|
@ -1,89 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/SearchContext.php');
|
||||
|
||||
class SearchContextTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
private $oCtx;
|
||||
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->oCtx = new SearchContext();
|
||||
}
|
||||
|
||||
public function testHasNearPoint()
|
||||
{
|
||||
$this->assertFalse($this->oCtx->hasNearPoint());
|
||||
$this->oCtx->setNearPoint(0, 0);
|
||||
$this->assertTrue($this->oCtx->hasNearPoint());
|
||||
}
|
||||
|
||||
public function testNearRadius()
|
||||
{
|
||||
$this->oCtx->setNearPoint(1, 1);
|
||||
$this->assertEquals(0.1, $this->oCtx->nearRadius());
|
||||
$this->oCtx->setNearPoint(1, 1, 0.338);
|
||||
$this->assertEquals(0.338, $this->oCtx->nearRadius());
|
||||
}
|
||||
|
||||
public function testWithinSQL()
|
||||
{
|
||||
$this->oCtx->setNearPoint(0.1, 23, 1);
|
||||
|
||||
$this->assertEquals(
|
||||
'ST_DWithin(foo, ST_SetSRID(ST_Point(23,0.1),4326), 1.000000)',
|
||||
$this->oCtx->withinSQL('foo')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDistanceSQL()
|
||||
{
|
||||
$this->oCtx->setNearPoint(0.1, 23, 1);
|
||||
|
||||
$this->assertEquals(
|
||||
'ST_Distance(ST_SetSRID(ST_Point(23,0.1),4326), foo)',
|
||||
$this->oCtx->distanceSQL('foo')
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetViewboxFromBox()
|
||||
{
|
||||
$viewbox = array(30, 20, 40, 50);
|
||||
$this->oCtx->setViewboxFromBox($viewbox, true);
|
||||
$this->assertEquals(
|
||||
'ST_SetSRID(ST_MakeBox2D(ST_Point(30.000000,20.000000),ST_Point(40.000000,50.000000)),4326)',
|
||||
$this->oCtx->sqlViewboxSmall
|
||||
);
|
||||
// height: 10
|
||||
// width: 30
|
||||
$this->assertEquals(
|
||||
'ST_SetSRID(ST_MakeBox2D(ST_Point(50.000000,80.000000),ST_Point(20.000000,-10.000000)),4326)',
|
||||
$this->oCtx->sqlViewboxLarge
|
||||
);
|
||||
|
||||
|
||||
$viewbox = array(-1.5, -2, 1.5, 2);
|
||||
$this->oCtx->setViewboxFromBox($viewbox, true);
|
||||
$this->assertEquals(
|
||||
'ST_SetSRID(ST_MakeBox2D(ST_Point(-1.500000,-2.000000),ST_Point(1.500000,2.000000)),4326)',
|
||||
$this->oCtx->sqlViewboxSmall
|
||||
);
|
||||
// height: 3
|
||||
// width: 4
|
||||
$this->assertEquals(
|
||||
'ST_SetSRID(ST_MakeBox2D(ST_Point(4.500000,6.000000),ST_Point(-4.500000,-6.000000)),4326)',
|
||||
$this->oCtx->sqlViewboxLarge
|
||||
);
|
||||
}
|
||||
}
|
@ -1,128 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/Shell.php');
|
||||
|
||||
class ShellTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testNew()
|
||||
{
|
||||
$this->expectException('ArgumentCountError');
|
||||
$this->expectExceptionMessage('Too few arguments to function');
|
||||
$oCmd = new \Nominatim\Shell();
|
||||
|
||||
|
||||
$oCmd = new \Nominatim\Shell('wc', '-l', 'file.txt');
|
||||
$this->assertSame(
|
||||
"wc -l 'file.txt'",
|
||||
$oCmd->escapedCmd()
|
||||
);
|
||||
}
|
||||
|
||||
public function testaddParams()
|
||||
{
|
||||
$oCmd = new \Nominatim\Shell('grep');
|
||||
$oCmd->addParams('-a', 'abc')
|
||||
->addParams(10);
|
||||
|
||||
$this->assertSame(
|
||||
'grep -a abc 10',
|
||||
$oCmd->escapedCmd(),
|
||||
'no escaping needed, chained'
|
||||
);
|
||||
|
||||
$oCmd = new \Nominatim\Shell('grep');
|
||||
$oCmd->addParams();
|
||||
$oCmd->addParams(null);
|
||||
$oCmd->addParams('');
|
||||
|
||||
$this->assertEmpty($oCmd->aParams);
|
||||
$this->assertSame('grep', $oCmd->escapedCmd(), 'empty params');
|
||||
|
||||
$oCmd = new \Nominatim\Shell('echo', '-n', 0);
|
||||
$this->assertSame(
|
||||
'echo -n 0',
|
||||
$oCmd->escapedCmd(),
|
||||
'zero param'
|
||||
);
|
||||
|
||||
$oCmd = new \Nominatim\Shell('/path with space/do.php');
|
||||
$oCmd->addParams('-a', ' b ');
|
||||
$oCmd->addParams('--flag');
|
||||
$oCmd->addParams('two words');
|
||||
$oCmd->addParams('v=1');
|
||||
|
||||
$this->assertSame(
|
||||
"'/path with space/do.php' -a ' b ' --flag 'two words' 'v=1'",
|
||||
$oCmd->escapedCmd(),
|
||||
'escape whitespace'
|
||||
);
|
||||
|
||||
$oCmd = new \Nominatim\Shell('grep');
|
||||
$oCmd->addParams(';', '|more&', '2>&1');
|
||||
|
||||
$this->assertSame(
|
||||
"grep ';' '|more&' '2>&1'",
|
||||
$oCmd->escapedCmd(),
|
||||
'escape shell characters'
|
||||
);
|
||||
}
|
||||
|
||||
public function testaddEnvPair()
|
||||
{
|
||||
$oCmd = new \Nominatim\Shell('date');
|
||||
|
||||
$oCmd->addEnvPair('one', 'two words')
|
||||
->addEnvPair('null', null)
|
||||
->addEnvPair(null, 'null')
|
||||
->addEnvPair('empty', '')
|
||||
->addEnvPair('', 'empty');
|
||||
|
||||
$this->assertEquals(
|
||||
array('one' => 'two words', 'empty' => ''),
|
||||
$oCmd->aEnv
|
||||
);
|
||||
|
||||
$oCmd->addEnvPair('one', 'overwrite');
|
||||
$this->assertEquals(
|
||||
array('one' => 'overwrite', 'empty' => ''),
|
||||
$oCmd->aEnv
|
||||
);
|
||||
}
|
||||
|
||||
public function testClone()
|
||||
{
|
||||
$oCmd = new \Nominatim\Shell('wc', '-l', 'file.txt');
|
||||
$oCmd2 = clone $oCmd;
|
||||
$oCmd->addParams('--flag');
|
||||
$oCmd2->addParams('--flag2');
|
||||
|
||||
$this->assertSame(
|
||||
"wc -l 'file.txt' --flag",
|
||||
$oCmd->escapedCmd()
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
"wc -l 'file.txt' --flag2",
|
||||
$oCmd2->escapedCmd()
|
||||
);
|
||||
}
|
||||
|
||||
public function testRun()
|
||||
{
|
||||
$oCmd = new \Nominatim\Shell('echo');
|
||||
|
||||
$this->assertSame(0, $oCmd->run());
|
||||
|
||||
// var_dump($sStdout);
|
||||
}
|
||||
}
|
@ -1,136 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/SimpleWordList.php');
|
||||
|
||||
class TokensFullSet
|
||||
{
|
||||
public function containsAny($sTerm)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
|
||||
class TokensPartialSet
|
||||
{
|
||||
public function __construct($aTokens)
|
||||
{
|
||||
$this->aTokens = array_flip($aTokens);
|
||||
}
|
||||
|
||||
public function containsAny($sTerm)
|
||||
{
|
||||
return isset($this->aTokens[$sTerm]);
|
||||
}
|
||||
}
|
||||
|
||||
// phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses
|
||||
class SimpleWordListTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
|
||||
private function serializeSets($aSets)
|
||||
{
|
||||
$aParts = array();
|
||||
foreach ($aSets as $aSet) {
|
||||
$aParts[] = '(' . join('|', $aSet) . ')';
|
||||
}
|
||||
return join(',', $aParts);
|
||||
}
|
||||
|
||||
|
||||
public function testEmptyPhrase()
|
||||
{
|
||||
$oList = new SimpleWordList('');
|
||||
$this->assertNull($oList->getWordSets(new TokensFullSet()));
|
||||
}
|
||||
|
||||
|
||||
public function testSingleWordPhrase()
|
||||
{
|
||||
$oList = new SimpleWordList('a');
|
||||
|
||||
$this->assertEquals(
|
||||
'(a)',
|
||||
$this->serializeSets($oList->getWordSets(new TokensFullSet()))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testMultiWordPhrase()
|
||||
{
|
||||
$oList = new SimpleWordList('a b');
|
||||
$this->assertEquals(
|
||||
'(a b),(a|b)',
|
||||
$this->serializeSets($oList->getWordSets(new TokensFullSet()))
|
||||
);
|
||||
|
||||
$oList = new SimpleWordList('a b c');
|
||||
$this->assertEquals(
|
||||
'(a b c),(a b|c),(a|b c),(a|b|c)',
|
||||
$this->serializeSets($oList->getWordSets(new TokensFullSet()))
|
||||
);
|
||||
|
||||
$oList = new SimpleWordList('a b c d');
|
||||
$this->assertEquals(
|
||||
'(a b c d),(a b c|d),(a b|c d),(a|b c d),(a b|c|d),(a|b c|d),(a|b|c d),(a|b|c|d)',
|
||||
$this->serializeSets($oList->getWordSets(new TokensFullSet()))
|
||||
);
|
||||
}
|
||||
|
||||
public function testCmpByArraylen()
|
||||
{
|
||||
// Array elements are phrases, we want to sort so longest phrases are first
|
||||
$aList1 = array('hackney', 'bridge', 'london', 'england');
|
||||
$aList2 = array('hackney', 'london', 'bridge');
|
||||
$aList3 = array('bridge', 'hackney', 'london', 'england');
|
||||
|
||||
$this->assertEquals(0, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList1));
|
||||
|
||||
// list2 "wins". Less array elements
|
||||
$this->assertEquals(1, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList2));
|
||||
$this->assertEquals(-1, \Nominatim\SimpleWordList::cmpByArraylen($aList2, $aList3));
|
||||
|
||||
// list1 "wins". Same number of array elements but longer first element
|
||||
$this->assertEquals(-1, \Nominatim\SimpleWordList::cmpByArraylen($aList1, $aList3));
|
||||
}
|
||||
|
||||
public function testMaxWordSets()
|
||||
{
|
||||
$aWords = array_fill(0, 4, 'a');
|
||||
$oList = new SimpleWordList(join(' ', $aWords));
|
||||
$this->assertEquals(8, count($oList->getWordSets(new TokensFullSet())));
|
||||
|
||||
$aWords = array_fill(0, 18, 'a');
|
||||
$oList = new SimpleWordList(join(' ', $aWords));
|
||||
$this->assertEquals(100, count($oList->getWordSets(new TokensFullSet())));
|
||||
}
|
||||
|
||||
|
||||
public function testPartialTokensShortTerm()
|
||||
{
|
||||
$oList = new SimpleWordList('a b c d');
|
||||
$this->assertEquals(
|
||||
'(a|b c d),(a|b c|d)',
|
||||
$this->serializeSets($oList->getWordSets(new TokensPartialSet(array('a', 'b', 'd', 'b c', 'b c d'))))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testPartialTokensLongTerm()
|
||||
{
|
||||
$aWords = array_fill(0, 18, 'a');
|
||||
$oList = new SimpleWordList(join(' ', $aWords));
|
||||
$this->assertEquals(80, count($oList->getWordSets(new TokensPartialSet(array('a', 'a a a a a')))));
|
||||
}
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
@define('CONST_TokenizerDir', dirname(__FILE__));
|
||||
|
||||
require_once(CONST_LibDir.'/DB.php');
|
||||
require_once(CONST_LibDir.'/Status.php');
|
||||
|
||||
|
||||
class StatusTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testNoDatabaseGiven()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('No database');
|
||||
$this->expectExceptionCode(700);
|
||||
|
||||
$oDB = null;
|
||||
$oStatus = new Status($oDB);
|
||||
$this->assertEquals('No database', $oStatus->status());
|
||||
}
|
||||
|
||||
public function testNoDatabaseConnectionFail()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Database connection failed');
|
||||
$this->expectExceptionCode(700);
|
||||
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('connect'))
|
||||
->getMock();
|
||||
|
||||
$oDbStub->method('connect')
|
||||
->will($this->returnCallback(function () {
|
||||
throw new \Nominatim\DatabaseError('psql connection problem', 500, null, 'unknown database');
|
||||
}));
|
||||
|
||||
|
||||
$oStatus = new Status($oDbStub);
|
||||
$this->assertEquals('No database', $oStatus->status());
|
||||
}
|
||||
|
||||
public function testOK()
|
||||
{
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('connect', 'getOne'))
|
||||
->getMock();
|
||||
|
||||
$oDbStub->method('getOne')
|
||||
->will($this->returnCallback(function ($sql) {
|
||||
if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
|
||||
if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
|
||||
}));
|
||||
|
||||
$oStatus = new Status($oDbStub);
|
||||
$this->assertNull($oStatus->status());
|
||||
}
|
||||
|
||||
public function testDataDate()
|
||||
{
|
||||
$oDbStub = $this->getMockBuilder(Nominatim\DB::class)
|
||||
->setMethods(array('getOne'))
|
||||
->getMock();
|
||||
|
||||
$oDbStub->method('getOne')
|
||||
->willReturn(1519430221);
|
||||
|
||||
$oStatus = new Status($oDbStub);
|
||||
$this->assertEquals(1519430221, $oStatus->dataDate());
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
require_once(CONST_LibDir.'/TokenList.php');
|
||||
|
||||
|
||||
class TokenListTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->oNormalizer = $this->getMockBuilder(\MockNormalizer::class)
|
||||
->setMethods(array('transliterate'))
|
||||
->getMock();
|
||||
$this->oNormalizer->method('transliterate')
|
||||
->will($this->returnCallback(function ($text) {
|
||||
return strtolower($text);
|
||||
}));
|
||||
}
|
||||
|
||||
private function wordResult($aFields)
|
||||
{
|
||||
$aRow = array(
|
||||
'word_id' => null,
|
||||
'word_token' => null,
|
||||
'word' => null,
|
||||
'class' => null,
|
||||
'type' => null,
|
||||
'country_code' => null,
|
||||
'count' => 0
|
||||
);
|
||||
return array_merge($aRow, $aFields);
|
||||
}
|
||||
|
||||
public function testList()
|
||||
{
|
||||
$TL = new TokenList;
|
||||
|
||||
$this->assertEquals(0, $TL->count());
|
||||
|
||||
$TL->addToken('word1', 'token1');
|
||||
$TL->addToken('word1', 'token2');
|
||||
|
||||
$this->assertEquals(1, $TL->count());
|
||||
|
||||
$this->assertTrue($TL->contains('word1'));
|
||||
$this->assertEquals(array('token1', 'token2'), $TL->get('word1'));
|
||||
|
||||
$this->assertFalse($TL->contains('unknownword'));
|
||||
$this->assertEquals(array(), $TL->get('unknownword'));
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
|
||||
namespace Nominatim;
|
||||
|
||||
class Tokenizer
|
||||
{
|
||||
private $oDB;
|
||||
|
||||
public function __construct(&$oDB)
|
||||
{
|
||||
$this->oDB =& $oDB;
|
||||
}
|
||||
|
||||
public function checkStatus()
|
||||
{
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* SPDX-License-Identifier: GPL-2.0-only
|
||||
*
|
||||
* This file is part of Nominatim. (https://nominatim.org)
|
||||
*
|
||||
* Copyright (C) 2022 by the Nominatim developer community.
|
||||
* For a full list of authors see the git log.
|
||||
*/
|
||||
@define('CONST_LibDir', '../../lib-php');
|
||||
@define('CONST_DataDir', '../..');
|
||||
|
||||
@define('CONST_Debug', true);
|
||||
@define('CONST_NoAccessControl', false);
|
File diff suppressed because one or more lines are too long
@ -1,26 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
bootstrap="./bootstrap.php"
|
||||
beStrictAboutTestsThatDoNotTestAnything="true"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
|
||||
<coverage>
|
||||
<include>
|
||||
<directory>../../lib-php/</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
<php>
|
||||
</php>
|
||||
<testsuites>
|
||||
<testsuite name="Nominatim PHP Test Suite">
|
||||
<directory>./Nominatim</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
Loading…
Reference in New Issue
Block a user