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