From a885e7309ab1feba1e2ec7b183eb383e775c162e Mon Sep 17 00:00:00 2001 From: Marc Tobias Metten Date: Thu, 3 May 2018 22:19:19 +0200 Subject: [PATCH] PHP unit tests for DebugHtml --- lib/DebugHtml.php | 44 +++---- test/php/Nominatim/DebugTest.php | 197 +++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+), 21 deletions(-) create mode 100644 test/php/Nominatim/DebugTest.php diff --git a/lib/DebugHtml.php b/lib/DebugHtml.php index ce2b8361..0f5af241 100644 --- a/lib/DebugHtml.php +++ b/lib/DebugHtml.php @@ -39,73 +39,75 @@ class Debug public static function printDebugTable($sHeading, $aVar) { echo ''.$sHeading.":\n"; - echo ''; + echo "
\n"; if (!empty($aVar)) { - echo ''; + echo " \n"; $aKeys = array(); $aInfo = reset($aVar); if (!is_array($aInfo)) { $aInfo = $aInfo->debugInfo(); } foreach ($aInfo as $sKey => $mVal) { - echo ''; + echo ' '."\n"; $aKeys[] = $sKey; } - echo ''; + echo " \n"; foreach ($aVar as $oRow) { $aInfo = $oRow; if (!is_array($oRow)) { $aInfo = $oRow->debugInfo(); } - echo ''; + echo " \n"; foreach ($aKeys as $sKey) { - echo ''; + echo ''."\n"; } - echo ''; + echo " \n"; } } - echo '
'.$sKey.''.$sKey.'
';
+                    echo '    
';
                     if (isset($aInfo[$sKey])) {
                         Debug::outputVar($aInfo[$sKey], '');
                     }
-                    echo '
'; + echo "\n"; } public static function printGroupTable($sHeading, $aVar) { echo ''.$sHeading.":\n"; - echo ''; + echo "
\n"; if (!empty($aVar)) { - echo ''; + echo " \n"; + echo ' '."\n"; $aKeys = array(); - $aInfo = reset(reset($aVar)); + $aInfo = reset($aVar)[0]; if (!is_array($aInfo)) { $aInfo = $aInfo->debugInfo(); } foreach ($aInfo as $sKey => $mVal) { - echo ''; + echo ' '."\n"; $aKeys[] = $sKey; } - echo ''; + echo " \n"; foreach ($aVar as $sGrpKey => $aGroup) { foreach ($aGroup as $oRow) { $aInfo = $oRow; if (!is_array($oRow)) { $aInfo = $oRow->debugInfo(); } - echo ''; + echo " \n"; + echo ' '."\n"; foreach ($aKeys as $sKey) { - echo ''; + echo ''."\n"; } - echo ''; + echo " \n"; } } } - echo '
Group
Group'.$sKey.''.$sKey.'
'.$sGrpKey.'
'.$sGrpKey.'
';
+                        echo '    
';
                         if (!empty($aInfo[$sKey])) {
                             Debug::outputVar($aInfo[$sKey], '');
                         }
-                        echo '
'; + echo "\n"; } public static function printSQL($sSQL) @@ -128,9 +130,9 @@ class Debug $sPre = "\n".$sPreNL; } } elseif (is_array($mVar) && isset($mVar['__debug_format'])) { - if (!empty($mVar[data])) { + if (!empty($mVar['data'])) { $sPre = ''; - foreach ($mVar[data] as $mValue) { + foreach ($mVar['data'] as $mValue) { echo $sPre; Debug::outputSimpleVar($mValue); $sPre = ', '; diff --git a/test/php/Nominatim/DebugTest.php b/test/php/Nominatim/DebugTest.php new file mode 100644 index 00000000..7ed08122 --- /dev/null +++ b/test/php/Nominatim/DebugTest.php @@ -0,0 +1,197 @@ +oWithDebuginfo = $this->getMock(Geocode::class, array('debugInfo')); + $this->oWithDebuginfo->method('debugInfo') + ->willReturn(array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3')); + + $this->oWithToString = $this->getMock(Geocode::class, array('__toString')); + $this->oWithToString->method('__toString')->willReturn('me as string'); + } + + public function testPrintVar() + { + $this->expectOutputString(<<Var0: +
Var1:  True
+
Var2:  False
+
Var3:  0
+
Var4:  'String'
+
Var5:  0 => 'one'
+       1 => 'two'
+       2 => 'three'
+
Var6:  'key' => 'value'
+       'key2' => 'value2'
+
Var7:  me as string
+
Var8:  'value', 'value2'
+ +EOT + ); + + Debug::printVar('Var0', null); + Debug::printVar('Var1', true); + Debug::printVar('Var2', false); + Debug::printVar('Var3', 0); + Debug::printVar('Var4', 'String'); + Debug::printVar('Var5', array('one', 'two', 'three')); + Debug::printVar('Var6', array('key' => 'value', 'key2' => 'value2')); + Debug::printVar('Var7', $this->oWithToString); + Debug::printVar('Var8', Debug::fmtArrayVals(array('key' => 'value', 'key2' => 'value2'))); + } + + + public function testDebugArray() + { + $this->expectOutputString(<<Arr0: 'null' +
Arr1:  'key1' => 'val1'
+       'key2' => 'val2'
+       'key3' => 'val3'
+ +EOT + ); + + Debug::printDebugArray('Arr0', null); + Debug::printDebugArray('Arr1', $this->oWithDebuginfo); + } + + + public function testPrintDebugTable() + { + $this->expectOutputString(<<Table1: + +
+Table2: + +
+Table3: + + + + + + + + + + + + + +
01
'one'
'two'
'three'
'four'
+Table4: + + + + + + + + + + + +
key1key2key3
'val1'
'val2'
'val3'
+ +EOT + ); + + Debug::printDebugTable('Table1', null); + + Debug::printDebugTable('Table2', array()); + + // Numeric headers + Debug::printDebugTable('Table3', array(array('one', 'two'), array('three', 'four'))); + + // Associate array + Debug::printDebugTable('Table4', array($this->oWithDebuginfo)); + } + + public function testPrintGroupTable() + { + $this->expectOutputString(<<Table1: + +
+Table2: + +
+Table3: + + + + + + + + + + + + + + + + + + + + + +
Groupkey1key2
group1
'val1'
'val2'
group1
'one'
'two'
group2
'val1'
'val2'
+Table4: + + + + + + + + + + + + + + + + + + + +
Groupkey1key2key3
group1
'val1'
'val2'
'val3'
group1
'val1'
'val2'
'val3'
+ +EOT + ); + + Debug::printGroupTable('Table1', null); + Debug::printGroupTable('Table2', array()); + + // header are taken from first group item, thus no key3 gets printed + $aGroups = array( + 'group1' => array( + array('key1' => 'val1', 'key2' => 'val2'), + array('key1' => 'one', 'key2' => 'two', 'unknown' => 1), + ), + 'group2' => array( + array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3'), + ) + ); + Debug::printGroupTable('Table3', $aGroups); + + $aGroups = array( + 'group1' => array($this->oWithDebuginfo, $this->oWithDebuginfo), + ); + Debug::printGroupTable('Table4', $aGroups); + } +}