]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/AddressDetailsTest.php
make HTML error message less technical
[nominatim.git] / test / php / Nominatim / AddressDetailsTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/init-website.php');
6 require_once(CONST_BasePath.'/lib/AddressDetails.php');
7
8
9 class AddressDetailsTest extends \PHPUnit\Framework\TestCase
10 {
11
12     protected function setUp()
13     {
14         // How the fixture got created
15         //
16         // 1) search for '10 downing street'
17         // https://nominatim.openstreetmap.org/details.php?osmtype=R&osmid=1879842
18         //
19         // 2) find place_id in the local database
20         // SELECT place_id, name FROM placex WHERE osm_type='R' AND osm_id=1879842;
21         //
22         // 3) set postgresql to non-align output, e.g. psql -A or \a in the CLI
23         //
24         // 4) query
25         // SELECT row_to_json(row,true) FROM (
26         //   SELECT *, get_name_by_language(name, ARRAY['name:en']) as localname
27         //   FROM get_addressdata(194663412,10)
28         //   ORDER BY rank_address DESC, isaddress DESC
29         // ) AS row;
30         //
31         // 5) copy&paste into file. Add commas between records
32         //
33         $json = file_get_contents(CONST_BasePath.'/test/php/fixtures/address_details_10_downing_street.json');
34         $data = json_decode($json, true);
35
36         $this->oDbStub = $this->getMockBuilder(\DB::class)
37                               ->setMethods(array('getAll'))
38                               ->getMock();
39         $this->oDbStub->method('getAll')
40                       ->willReturn($data);
41     }
42
43     public function testGetLocaleAddress()
44     {
45         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
46         $expected = join(', ', array(
47             '10 Downing Street',
48             '10',
49             'Downing Street',
50             'St. James\'s',
51             'Covent Garden',
52             'Westminster',
53             'London',
54             'Greater London',
55             'England',
56             'SW1A 2AA',
57             'United Kingdom'
58         ));
59         $this->assertEquals($expected, $oAD->getLocaleAddress());
60     }
61
62     public function testGetAddressDetails()
63     {
64         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
65         $this->assertEquals(18, count($oAD->getAddressDetails(true)));
66         $this->assertEquals(12, count($oAD->getAddressDetails(false)));
67     }
68
69     public function testGetAddressNames()
70     {
71         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
72         $expected = array(
73                      'attraction' => '10 Downing Street',
74                      'house_number' => '10',
75                      'road' => 'Downing Street',
76                      'neighbourhood' => 'St. James\'s',
77                      'suburb' => 'Covent Garden',
78                      'city' => 'London',
79                      'state_district' => 'Greater London',
80                      'state' => 'England',
81                      'postcode' => 'SW1A 2AA',
82                      'country' => 'United Kingdom',
83                      'country_code' => 'gb'
84         );
85
86         $this->assertEquals($expected, $oAD->getAddressNames());
87     }
88
89     public function testGetAdminLevels()
90     {
91         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
92         $expected = array(
93                      'level8' => 'Westminster',
94                      'level6' => 'London',
95                      'level5' => 'Greater London',
96                      'level4' => 'England',
97                      'level2' => 'United Kingdom'
98         );
99         $this->assertEquals($expected, $oAD->getAdminLevels());
100     }
101
102     public function testDebugInfo()
103     {
104         $oAD = new AddressDetails($this->oDbStub, 194663412, 10, 'en');
105         $this->assertTrue(is_array($oAD->debugInfo()));
106         $this->assertEquals(18, count($oAD->debugInfo()));
107     }
108 }