]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/AddressDetailsTest.php
2041dcb4bff2558ee447ef1fb3c15361090f7b39
[nominatim.git] / test / php / Nominatim / AddressDetailsTest.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim;
12
13 require_once(CONST_LibDir.'/init-website.php');
14 require_once(CONST_LibDir.'/AddressDetails.php');
15
16
17 class AddressDetailsTest extends \PHPUnit\Framework\TestCase
18 {
19
20     protected function setUp(): void
21     {
22         // How the fixture got created
23         //
24         // 1) search for '10 downing street'
25         // https://nominatim.openstreetmap.org/details.php?osmtype=R&osmid=1879842
26         //
27         // 2) find place_id in the local database
28         // SELECT place_id, name FROM placex WHERE osm_type='R' AND osm_id=1879842;
29         //
30         // 3) set postgresql to non-align output, e.g. psql -A or \a in the CLI
31         //
32         // 4) query
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
37         // ) AS row;
38         //
39         // 5) copy&paste into file. Add commas between records
40         //
41         $json = file_get_contents(CONST_DataDir.'/test/php/fixtures/address_details_10_downing_street.json');
42         $data = json_decode($json, true);
43
44         $this->oDbStub = $this->getMockBuilder(\DB::class)
45                               ->setMethods(array('getAll'))
46                               ->getMock();
47         $this->oDbStub->method('getAll')
48                       ->willReturn($data);
49     }
50
51     public function testGetLocaleAddress()
52     {
53         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
54         $expected = join(', ', array(
55             '10 Downing Street',
56             '10',
57             'Downing Street',
58             'St. James\'s',
59             'Covent Garden',
60             'Westminster',
61             'London',
62             'Greater London',
63             'England',
64             'SW1A 2AA',
65             'United Kingdom'
66         ));
67         $this->assertEquals($expected, $oAD->getLocaleAddress());
68     }
69
70     public function testGetAddressDetails()
71     {
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)));
75     }
76
77     public function testGetAddressNames()
78     {
79         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
80         $expected = array(
81                      'tourism' => '10 Downing Street',
82                      'house_number' => '10',
83                      'road' => 'Downing Street',
84                      'neighbourhood' => 'St. James\'s',
85                      'suburb' => 'Covent Garden',
86                      'city' => 'London',
87                      'state_district' => 'Greater London',
88                      'state' => 'England',
89                      'ISO3166-2-lvl4' => 'GB-ENG',
90                      'ISO3166-2-lvl6' => 'GB-LND',
91                      'postcode' => 'SW1A 2AA',
92                      'country' => 'United Kingdom',
93                      'country_code' => 'gb'
94         );
95
96         $this->assertEquals($expected, $oAD->getAddressNames());
97     }
98
99     public function testGetAdminLevels()
100     {
101         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
102         $expected = array(
103                      'level8' => 'Westminster',
104                      'level6' => 'London',
105                      'level5' => 'Greater London',
106                      'level4' => 'England',
107                      'level2' => 'United Kingdom'
108         );
109         $this->assertEquals($expected, $oAD->getAdminLevels());
110     }
111
112     public function testDebugInfo()
113     {
114         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
115         $this->assertTrue(is_array($oAD->debugInfo()));
116         $this->assertEquals(18, count($oAD->debugInfo()));
117     }
118 }