]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/ParameterParserTest.php
PHP tests for ParameterParser
[nominatim.git] / test / php / Nominatim / ParameterParserTest.php
1 <?php
2
3 namespace Nominatim;
4
5 use Exception;
6
7 require_once('../../lib/ParameterParser.php');
8
9
10 function userError($sError)
11 {
12     throw new Exception($sError);
13 }
14
15 class ParameterParserTest extends \PHPUnit_Framework_TestCase
16 {
17
18
19     protected function setUp()
20     {
21     }
22
23
24     public function testGetBool()
25     {
26         $oParams = new ParameterParser([
27                                         'bool1' => '1',
28                                         'bool2' => '0',
29                                         'bool3' => 'true',
30                                         'bool4' => 'false',
31                                         'bool5' => ''
32                                        ]);
33
34         $this->assertEquals(false, $oParams->getBool('non-exists'));
35         $this->assertEquals(true, $oParams->getBool('non-exists', true));
36         $this->assertEquals(true, $oParams->getBool('bool1'));
37         $this->assertEquals(false, $oParams->getBool('bool2'));
38         $this->assertEquals(true, $oParams->getBool('bool3'));
39         $this->assertEquals(true, $oParams->getBool('bool4'));
40         $this->assertEquals(false, $oParams->getBool('bool5'));
41     }
42
43
44     public function testGetInt()
45     {
46         $oParams = new ParameterParser([
47                                         'int1' => '5',
48                                         'int2' => 'a',
49                                         'int3' => '-1',
50                                         'int4' => '',
51                                         'int5' => 0
52                                        ]);
53
54         $this->assertEquals(false, $oParams->getInt('non-exists'));
55         $this->assertEquals(999, $oParams->getInt('non-exists', 999));
56         $this->assertEquals(5, $oParams->getInt('int1'));
57
58         try {
59             $this->assertEquals(false, $oParams->getInt('int2'));
60         } catch (Exception $e) {
61             $this->assertEquals($e->getMessage(), "Integer number expected for parameter 'int2'");
62         }
63         $this->assertEquals(-1, $oParams->getInt('int3'));
64         $this->assertEquals(false, $oParams->getInt('int4'));
65         $this->assertEquals(false, $oParams->getInt('int5')); // FIXME: should be 0 instead?
66     }
67
68
69     public function testGetFloat()
70     {
71         $oParams = new ParameterParser([
72                                         'float1' => '1.0',
73                                         'float2' => '-5',
74                                         'float3' => '-55.',
75                                         'float4' => 'a',
76                                         'float5' => '',
77                                         'float6' => 0
78                                        ]);
79
80         $this->assertEquals(false, $oParams->getFloat('non-exists'));
81         $this->assertEquals(999, $oParams->getFloat('non-exists', 999));
82         $this->assertEquals(1, $oParams->getFloat('float1'));
83         $this->assertEquals(-5, $oParams->getFloat('float2'));
84
85         try {
86             $this->assertEquals(false, $oParams->getFloat('float3'));
87         } catch (Exception $e) {
88             $this->assertEquals($e->getMessage(), "Floating-point number expected for parameter 'float3'");
89         }
90
91         try {
92             $this->assertEquals(false, $oParams->getFloat('float4'));
93         } catch (Exception $e) {
94             $this->assertEquals($e->getMessage(), "Floating-point number expected for parameter 'float4'");
95         }
96         $this->assertEquals(false, $oParams->getFloat('float5'));
97         $this->assertEquals(false, $oParams->getFloat('float6')); // FIXME: should be 0 instead?
98     }
99
100
101
102     public function testGetString()
103     {
104         $oParams = new ParameterParser([
105                                         'str1' => 'abc',
106                                         'str2' => '',
107                                         'str3' => '0'
108                                        ]);
109
110         $this->assertEquals(false, $oParams->getString('non-exists'));
111         $this->assertEquals('default', $oParams->getString('non-exists', 'default'));
112         $this->assertEquals('abc', $oParams->getString('str1'));
113         $this->assertEquals(false, $oParams->getStringList('str2'));
114         $this->assertEquals(false, $oParams->getStringList('str3')); // FIXME: should be 0 instead?
115     }
116
117
118
119     public function testGetSet()
120     {
121         $oParams = new ParameterParser([
122                                         'val1' => 'foo',
123                                         'val2' => 'FOO',
124                                         'val3' => '',
125                                         'val4' => 0
126                                        ]);
127
128         $this->assertEquals(false, $oParams->getSet('non-exists', ['foo', 'bar']));
129         // FIXME: unclear if the default value has to be part of the set
130         $this->assertEquals('default', $oParams->getSet('non-exists', ['foo', 'bar'], 'default'));
131         $this->assertEquals('foo', $oParams->getSet('val1', ['foo', 'bar']));
132
133         try {
134             $this->assertEquals(false, $oParams->getSet('val2', ['foo', 'bar']));
135         } catch (Exception $e) {
136             $this->assertEquals($e->getMessage(), "Parameter 'val2' must be one of: foo, bar");
137         }
138         $this->assertEquals(false, $oParams->getSet('val3', ['foo', 'bar']));
139         $this->assertEquals(false, $oParams->getSet('val4', ['foo', 'bar']));
140     }
141
142
143     public function testGetStringList()
144     {
145         $oParams = new ParameterParser([
146                                         'list1' => ',a,b,c,,c,d',
147                                         'list2' => 'a',
148                                         'list3' => '',
149                                         'list4' => '0'
150                                        ]);
151
152         $this->assertEquals(false, $oParams->getStringList('non-exists'));
153         $this->assertEquals(['a', 'b'], $oParams->getStringList('non-exists', ['a', 'b']));
154         // FIXME: unclear if empty string items should be removed
155         $this->assertEquals(['', 'a', 'b', 'c', '', 'c', 'd'], $oParams->getStringList('list1'));
156         $this->assertEquals(['a'], $oParams->getStringList('list2'));
157         $this->assertEquals(false, $oParams->getStringList('list3'));
158         $this->assertEquals(false, $oParams->getStringList('list4'));
159     }
160
161     public function testGetPreferredLanguages()
162     {
163         $oParams = new ParameterParser(['accept-language' => '']);
164         $this->assertEquals([
165                              'brand' => 'brand',
166                              'ref' => 'ref',
167                              'type' => 'type',
168                              'name' => 'name',
169                              'name:default' => 'name:default',
170                              'short_name' => 'short_name',
171                              'short_name:default' => 'short_name:default',
172                              'official_name' => 'official_name',
173                              'official_name:default' => 'official_name:default',
174                             ], $oParams->getPreferredLanguages('default'));
175
176         $oParams = new ParameterParser(['accept-language' => 'de,en']);
177         $this->assertEquals([
178                              'brand' => 'brand',
179                              'ref' => 'ref',
180                              'type' => 'type',
181                              'name' => 'name',
182                              'name:de' => 'name:de',
183                              'name:en' => 'name:en',
184                              'short_name' => 'short_name',
185                              'short_name:de' => 'short_name:de',
186                              'short_name:en' => 'short_name:en',
187                              'official_name' => 'official_name',
188                              'official_name:de' => 'official_name:de',
189                              'official_name:en' => 'official_name:en',
190                             ], $oParams->getPreferredLanguages('default'));
191
192         $oParams = new ParameterParser(['accept-language' => 'fr-ca,fr;q=0.8,en-ca;q=0.5,en;q=0.3']);
193         $this->assertEquals([
194                              'short_name:fr-ca' => 'short_name:fr-ca',
195                              'name:fr-ca' => 'name:fr-ca',
196                              'short_name:fr' => 'short_name:fr',
197                              'name:fr' => 'name:fr',
198                              'short_name:en-ca' => 'short_name:en-ca',
199                              'name:en-ca' => 'name:en-ca',
200                              'short_name:en' => 'short_name:en',
201                              'name:en' => 'name:en',
202                              'short_name' => 'short_name',
203                              'name' => 'name',
204                              'brand' => 'brand',
205                              'official_name:fr-ca' => 'official_name:fr-ca',
206                              'official_name:fr' => 'official_name:fr',
207                              'official_name:en-ca' => 'official_name:en-ca',
208                              'official_name:en' => 'official_name:en',
209                              'official_name' => 'official_name',
210                              'ref' => 'ref',
211                              'type' => 'type',
212                             ], $oParams->getPreferredLanguages('default'));
213     }
214 }