3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
13 require_once(CONST_LibDir.'/init-website.php');
14 require_once(CONST_LibDir.'/AddressDetails.php');
17 class AddressDetailsTest extends \PHPUnit\Framework\TestCase
20 protected function setUp(): void
22 // How the fixture got created
24 // 1) search for '10 downing street'
25 // https://nominatim.openstreetmap.org/details.php?osmtype=R&osmid=1879842
27 // 2) find place_id in the local database
28 // SELECT place_id, name FROM placex WHERE osm_type='R' AND osm_id=1879842;
30 // 3) set postgresql to non-align output, e.g. psql -A or \a in the CLI
33 // SELECT row_to_json(row,true) FROM (
34 // SELECT *, get_name_by_language(name, ARRAY['name:en']) as localname
35 // FROM get_addressdata(194663412,10)
36 // ORDER BY rank_address DESC, isaddress DESC
39 // 5) copy&paste into file. Add commas between records
41 $json = file_get_contents(CONST_DataDir.'/test/php/fixtures/address_details_10_downing_street.json');
42 $data = json_decode($json, true);
44 $this->oDbStub = $this->getMockBuilder(\DB::class)
45 ->setMethods(array('getAll'))
47 $this->oDbStub->method('getAll')
51 public function testGetLocaleAddress()
53 $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
54 $expected = join(', ', array(
67 $this->assertEquals($expected, $oAD->getLocaleAddress());
70 public function testGetAddressDetails()
72 $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
73 $this->assertEquals(18, count($oAD->getAddressDetails(true)));
74 $this->assertEquals(12, count($oAD->getAddressDetails(false)));
77 public function testGetAddressNames()
79 $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
81 'tourism' => '10 Downing Street',
82 'house_number' => '10',
83 'road' => 'Downing Street',
84 'neighbourhood' => 'St. James\'s',
85 'suburb' => 'Covent Garden',
87 'state_district' => 'Greater London',
89 'ISO3166-2-lvl4' => 'GB-ENG',
90 'ISO3166-2-lvl6' => 'GB-LND',
91 'postcode' => 'SW1A 2AA',
92 'country' => 'United Kingdom',
93 'country_code' => 'gb'
96 $this->assertEquals($expected, $oAD->getAddressNames());
99 public function testGetAdminLevels()
101 $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
103 'level8' => 'Westminster',
104 'level6' => 'London',
105 'level5' => 'Greater London',
106 'level4' => 'England',
107 'level2' => 'United Kingdom'
109 $this->assertEquals($expected, $oAD->getAdminLevels());
112 public function testDebugInfo()
114 $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
115 $this->assertTrue(is_array($oAD->debugInfo()));
116 $this->assertEquals(18, count($oAD->debugInfo()));