]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/DebugTest.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / php / Nominatim / DebugTest.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.'/DebugHtml.php');
14
15 class DebugTest extends \PHPUnit\Framework\TestCase
16 {
17
18     protected function setUp(): void
19     {
20         $this->oWithDebuginfo = $this->getMockBuilder(\GeococdeMock::class)
21                                     ->setMethods(array('debugInfo'))
22                                     ->getMock();
23         $this->oWithDebuginfo->method('debugInfo')
24                   ->willReturn(array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3'));
25
26
27         $this->oWithToString = $this->getMockBuilder(\SomeMock::class)
28                                     ->setMethods(array('__toString'))
29                                     ->getMock();
30         $this->oWithToString->method('__toString')->willReturn('me as string');
31     }
32
33     public function testPrintVar()
34     {
35         $this->expectOutputString(<<<EOT
36 <pre><b>Var0:</b>  </pre>
37 <pre><b>Var1:</b>  <i>True</i></pre>
38 <pre><b>Var2:</b>  <i>False</i></pre>
39 <pre><b>Var3:</b>  0</pre>
40 <pre><b>Var4:</b>  &#039;String&#039;</pre>
41 <pre><b>Var5:</b>  0 => &#039;one&#039;
42        1 => &#039;two&#039;
43        2 => &#039;three&#039;</pre>
44 <pre><b>Var6:</b>  &#039;key&#039; => &#039;value&#039;
45        &#039;key2&#039; => &#039;value2&#039;</pre>
46 <pre><b>Var7:</b>  me as string</pre>
47 <pre><b>Var8:</b>  &#039;value&#039;, &#039;value2&#039;</pre>
48
49 EOT
50         );
51     
52         Debug::printVar('Var0', null);
53         Debug::printVar('Var1', true);
54         Debug::printVar('Var2', false);
55         Debug::printVar('Var3', 0);
56         Debug::printVar('Var4', 'String');
57         Debug::printVar('Var5', array('one', 'two', 'three'));
58         Debug::printVar('Var6', array('key' => 'value', 'key2' => 'value2'));
59         Debug::printVar('Var7', $this->oWithToString);
60         Debug::printVar('Var8', Debug::fmtArrayVals(array('key' => 'value', 'key2' => 'value2')));
61     }
62
63
64     public function testDebugArray()
65     {
66         $this->expectOutputString(<<<EOT
67 <pre><b>Arr0:</b>  &#039;null&#039;</pre>
68 <pre><b>Arr1:</b>  &#039;key1&#039; => &#039;val1&#039;
69        &#039;key2&#039; => &#039;val2&#039;
70        &#039;key3&#039; => &#039;val3&#039;</pre>
71
72 EOT
73         );
74     
75         Debug::printDebugArray('Arr0', null);
76         Debug::printDebugArray('Arr1', $this->oWithDebuginfo);
77     }
78
79
80     public function testPrintDebugTable()
81     {
82         $this->expectOutputString(<<<EOT
83 <b>Table1:</b>
84 <table border='1'>
85 </table>
86 <b>Table2:</b>
87 <table border='1'>
88 </table>
89 <b>Table3:</b>
90 <table border='1'>
91   <tr>
92     <th><small>0</small></th>
93     <th><small>1</small></th>
94   </tr>
95   <tr>
96     <td><pre>&#039;one&#039;</pre></td>
97     <td><pre>&#039;two&#039;</pre></td>
98   </tr>
99   <tr>
100     <td><pre>&#039;three&#039;</pre></td>
101     <td><pre>&#039;four&#039;</pre></td>
102   </tr>
103 </table>
104 <b>Table4:</b>
105 <table border='1'>
106   <tr>
107     <th><small>key1</small></th>
108     <th><small>key2</small></th>
109     <th><small>key3</small></th>
110   </tr>
111   <tr>
112     <td><pre>&#039;val1&#039;</pre></td>
113     <td><pre>&#039;val2&#039;</pre></td>
114     <td><pre>&#039;val3&#039;</pre></td>
115   </tr>
116 </table>
117
118 EOT
119         );
120     
121         Debug::printDebugTable('Table1', null);
122
123         Debug::printDebugTable('Table2', array());
124
125         // Numeric headers
126         Debug::printDebugTable('Table3', array(array('one', 'two'), array('three', 'four')));
127
128         // Associate array
129         Debug::printDebugTable('Table4', array($this->oWithDebuginfo));
130     }
131
132     public function testPrintGroupTable()
133     {
134         $this->expectOutputString(<<<EOT
135 <b>Table1:</b>
136 <table border='1'>
137 </table>
138 <b>Table2:</b>
139 <table border='1'>
140 </table>
141 <b>Table3:</b>
142 <table border='1'>
143   <tr>
144     <th><small>Group</small></th>
145     <th><small>key1</small></th>
146     <th><small>key2</small></th>
147   </tr>
148   <tr>
149     <td><pre>group1</pre></td>
150     <td><pre>&#039;val1&#039;</pre></td>
151     <td><pre>&#039;val2&#039;</pre></td>
152   </tr>
153   <tr>
154     <td><pre>group1</pre></td>
155     <td><pre>&#039;one&#039;</pre></td>
156     <td><pre>&#039;two&#039;</pre></td>
157   </tr>
158   <tr>
159     <td><pre>group2</pre></td>
160     <td><pre>&#039;val1&#039;</pre></td>
161     <td><pre>&#039;val2&#039;</pre></td>
162   </tr>
163 </table>
164 <b>Table4:</b>
165 <table border='1'>
166   <tr>
167     <th><small>Group</small></th>
168     <th><small>key1</small></th>
169     <th><small>key2</small></th>
170     <th><small>key3</small></th>
171   </tr>
172   <tr>
173     <td><pre>group1</pre></td>
174     <td><pre>&#039;val1&#039;</pre></td>
175     <td><pre>&#039;val2&#039;</pre></td>
176     <td><pre>&#039;val3&#039;</pre></td>
177   </tr>
178   <tr>
179     <td><pre>group1</pre></td>
180     <td><pre>&#039;val1&#039;</pre></td>
181     <td><pre>&#039;val2&#039;</pre></td>
182     <td><pre>&#039;val3&#039;</pre></td>
183   </tr>
184 </table>
185
186 EOT
187         );
188     
189         Debug::printGroupTable('Table1', null);
190         Debug::printGroupTable('Table2', array());
191
192         // header are taken from first group item, thus no key3 gets printed
193         $aGroups = array(
194                     'group1' => array(
195                                  array('key1' => 'val1', 'key2' => 'val2'),
196                                  array('key1' => 'one', 'key2' => 'two', 'unknown' => 1),
197                                 ),
198                     'group2' => array(
199                                  array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3'),
200                                 )
201                    );
202         Debug::printGroupTable('Table3', $aGroups);
203
204         $aGroups = array(
205                     'group1' => array($this->oWithDebuginfo, $this->oWithDebuginfo),
206                    );
207         Debug::printGroupTable('Table4', $aGroups);
208     }
209 }