Var_Dump
[ class tree: Var_Dump ] [ index: Var_Dump ] [ all elements ]

Source for file test.php

Documentation is available at test.php

  1. <?php
  2.  
  3. require_once 'PHPUnit.php';
  4. require_once 'Var_Dump.php';
  5.  
  6. /*=============*/
  7. /* Test Classe */
  8. /*=============*/
  9.  
  10. class Var_DumpTest extends PHPUnit_TestCase {
  11.  
  12.     var $vd;
  13.  
  14.     function Var_DumpTest($name{
  15.         $this->PHPUnit_TestCase($name);
  16.     }
  17.  
  18.     function setUp({
  19.         $this->vd = new Var_Dump(array('display_mode'=>'Text'));
  20.     }
  21.  
  22.     function tearDown({
  23.         unset($this->vd);
  24.     }
  25.  
  26.     /*====================*/
  27.     /* Test simple values */
  28.     /*====================*/
  29.  
  30.     function test_simple_int({
  31.         $this->assertEquals('int -2147483647',$this->vd->toString(-2147483647));
  32.         $this->assertEquals('int 0',$this->vd->toString(0));
  33.         $this->assertEquals('int 2147483647',$this->vd->toString(2147483647));
  34.     }
  35.  
  36.     function test_simple_bool({
  37.         $this->assertEquals('bool false',$this->vd->toString(FALSE));
  38.         $this->assertEquals('bool true',$this->vd->toString(TRUE));
  39.     }
  40.  
  41.     function test_simple_float({
  42.         $this->assertEquals('float 12.345678',$this->vd->toString(12.345678));
  43.         $this->assertEquals('float -12.345678',$this->vd->toString(-12.345678));
  44.         $this->assertEquals('float 2147483648',$this->vd->toString(2147483648));
  45.         $this->assertEquals('float -2147483648',$this->vd->toString(-2147483648));
  46.     }
  47.  
  48.     function test_simple_resource({
  49.         $dir='/tmp/';
  50.         if (is_dir($dir)) {
  51.             if ($dh=opendir($dir)) {
  52.                 $this->assertRegExp('/^resource\(stream\)\s[0-9]+$/',$this->vd->toString($dh));
  53.         closedir($dh);
  54.             else {
  55.                 $this->assertTrue(FALSE,'Unable to open directory /tmp, ');
  56.             }
  57.     else {
  58.             $this->assertTrue(FALSE,'Unable to open directory /tmp, ');
  59.         }
  60.     }
  61.  
  62.     function test_simple_null({
  63.         $this->assertEquals('NULL',$this->vd->toString(NULL));
  64.     }
  65.  
  66.     /*============*/
  67.     /* Test Array */
  68.     /*============*/
  69.  
  70.     function test_array({
  71.         $this->assertEquals(
  72.             'array(3) {'."\n".
  73.             '  key1 => string(44) The quick brown'."\n".
  74.             '                     fox jumped over'."\n".
  75.             '                     the lazy dog'."\n".
  76.             '  key2 => &array(3) {'."\n".
  77.             '    0 => bool true'."\n".
  78.             '    1 => int 123'."\n".
  79.             '    2 => float 123.45'."\n".
  80.             '  }'."\n".
  81.             '  key3 => NULL'."\n".
  82.             '}',
  83.             $this->vd->toString($GLOBALS['array'])
  84.         );
  85.     }
  86.  
  87.     /*=============*/
  88.     /* Test Object */
  89.     /*=============*/
  90.  
  91.     function test_object({
  92.         $this->assertEquals(
  93.             'object(object)(3) {'."\n".
  94.             '  key1 => string(44) The quick brown'."\n".
  95.             '                     fox jumped over'."\n".
  96.             '                     the lazy dog'."\n".
  97.             '  key2 => array(3) {'."\n".
  98.             '    0 => bool true'."\n".
  99.             '    1 => int 123'."\n".
  100.             '    2 => float 123.45'."\n".
  101.             '  }'."\n".
  102.             '  key3 => NULL'."\n".
  103.             '}',
  104.             $this->vd->toString(new object()));
  105.     }
  106.  
  107.     /*==================================*/
  108.     /* Bug #490 Recursions not managed. */
  109.     /*==================================*/
  110.  
  111.     function test_bug490({
  112.         $this->assertEquals(
  113.             'object(parent)(2) {'."\n".
  114.             '  myChild => object(child)(1) {'."\n".
  115.             '    myParent => &object(parent)(2) {'."\n".
  116.             '      myChild => object(child)(1) {'."\n".
  117.             '        myParent => &object(parent)(2) {'."\n".
  118.             '          myChild => *RECURSION*'."\n".
  119.             '          myName => string(6) parent'."\n".
  120.             '        }'."\n".
  121.             '      }'."\n".
  122.             '      myName => string(6) parent'."\n".
  123.             '    }'."\n".
  124.             '  }'."\n".
  125.             '  myName => string(6) parent'."\n".
  126.             '}',
  127.             $this->vd->toString(new parent())
  128.         );
  129.     }
  130.  
  131.     /*============================================================================*/
  132.     /* Bug #1321 Numeric zero values in array or object attributes are not shown. */
  133.     /*============================================================================*/
  134.  
  135.     function test_bug1321({
  136.         $this->assertEquals(
  137.             'object(zero)(2) {'."\n".
  138.             '  i => int 0'."\n".
  139.             '  f => float 0'."\n".
  140.             '}',
  141.             $this->vd->toString(new zero())
  142.         );
  143.         $this->assertEquals(
  144.             'array(2) {'."\n".
  145.             '  0 => int 0'."\n".
  146.             '  1 => float 0'."\n".
  147.             '}',
  148.             $this->vd->toString(array(0,0.0))
  149.         );
  150.     }
  151.  
  152.     /*======================*/
  153.     /* Test "Text" Renderer */
  154.     /*======================*/
  155.  
  156.     function test_renderer_text({
  157.         Var_Dump::displayInit(
  158.             array('display_mode'=>'Text'),
  159.             array(
  160.                 'show_eol'       => ' *',
  161.                 'offset'         => 3,
  162.                 'opening'        => '(',
  163.                 'closing'        => ')',
  164.                 'operator'       => ' -> ',
  165.                 'before_num_key' => '\'',
  166.                 'after_num_key'  => '\'',
  167.                 'before_str_key' => '"',
  168.                 'after_str_key'  => '"',
  169.                 'before_type'    => '[',
  170.                 'after_type'     => ']'
  171.             )
  172.         );
  173.         $this->assertEquals(
  174.             'array(3) ('."\n".
  175.             '   "key1" -> [string(44)] The quick brown *'."\n".
  176.             '                          fox jumped over *'."\n".
  177.             '                          the lazy dog'."\n".
  178.             '   "key2" -> &array(3) ('."\n".
  179.             '      \'0\' -> [bool] true'."\n".
  180.             '      \'1\' -> [int] 123'."\n".
  181.             '      \'2\' -> [float] 123.45'."\n".
  182.             '   )'."\n".
  183.             '   "key3" -> [NULL]'."\n".
  184.             ')',
  185.             Var_Dump::display($GLOBALS['array'],TRUE)
  186.         );
  187.     }
  188.  
  189.     /*============================*/
  190.     /* Test "HTML4_Text" Renderer */
  191.     /*============================*/
  192.  
  193.     function test_renderer_html4_text({
  194.         Var_Dump::displayInit(array('display_mode'=>'HTML4_Text'));
  195.         $this->assertEquals(
  196.             '<pre>array(3) {'."\n".
  197.             '  key1 =&gt; <font color="#006600">string(44)</font> <font color="#339900">The quick brown'."\n".
  198.             '</font>'.
  199.             '                                                  <font color="#339900">fox jumped over'."\n".
  200.             '</font>'.
  201.             '                                                  <font color="#339900">the lazy dog</font>'."\n".
  202.             '  key2 =&gt; &array(3) {'."\n".
  203.             '    0 =&gt; <font color="#006600">bool</font> <font color="#339900">true</font>'."\n".
  204.             '    1 =&gt; <font color="#006600">int</font> <font color="#339900">123</font>'."\n".
  205.             '    2 =&gt; <font color="#006600">float</font> <font color="#339900">123.45</font>'."\n".
  206.             '  }'."\n".
  207.             '  key3 =&gt; <font color="#006600">NULL</font>'."\n".
  208.             '}'."\n".
  209.             '</pre>',
  210.             Var_Dump::display($GLOBALS['array'],TRUE)
  211.         );
  212.     }
  213.  
  214.     /*============================*/
  215.     /* Test "XHTML_Text" Renderer */
  216.     /*============================*/
  217.  
  218.     function test_renderer_xhtml_text({
  219.         Var_Dump::displayInit(array('display_mode'=>'XHTML_Text'));
  220.         $this->assertEquals(
  221.             '<pre class="var_dump">array(3) {'."\n".
  222.             '  key1 =&gt; <span class="type">string(44)</span> <span class="value">The quick brown'."\n".
  223.             '</span>                                               <span class="value">fox jumped over'."\n".
  224.             '</span>                                               <span class="value">the lazy dog</span>'."\n".
  225.             '  key2 =&gt; &array(3) {'."\n".
  226.             '    0 =&gt; <span class="type">bool</span> <span class="value">true</span>'."\n".
  227.             '    1 =&gt; <span class="type">int</span> <span class="value">123</span>'."\n".
  228.             '    2 =&gt; <span class="type">float</span> <span class="value">123.45</span>'."\n".
  229.             '  }'."\n".
  230.             '  key3 =&gt; <span class="type">NULL</span>'."\n".
  231.             '}'."\n".
  232.             '</pre>',
  233.             Var_Dump::display($GLOBALS['array'],TRUE)
  234.         );
  235.     }
  236.  
  237.     /*=======================*/
  238.     /* Test "Table" Renderer */
  239.     /*=======================*/
  240.  
  241.     function test_renderer_table({
  242.         Var_Dump::displayInit(
  243.             array('display_mode'=>'Table'),
  244.             array(
  245.                 'before_str_key' => '"',
  246.                 'after_str_key'  => '"',
  247.                 'before_type'    => '[',
  248.                 'after_type'     => ']'
  249.             )
  250.         );
  251.         $this->assertEquals(
  252.             '<table>'.
  253.             '<caption>array(3)</caption>'.
  254.             '<tr>'.
  255.                 '<td>"key1"</td>'.
  256.                 '<td>[string(44)]</td>'.
  257.                 '<td>The quick brown<br />'."\n".'fox jumped over<br />'."\n".'the lazy dog</td>'.
  258.             '</tr><tr>'.
  259.                 '<td>"key2"</td>'.
  260.                 '<td colspan="2">'.
  261.                     '<table>'.
  262.                     '<caption>&amp;array(3)</caption>'.
  263.                     '<tr><td>0</td><td>[bool]</td><td>true</td></tr>'.
  264.                     '<tr><td>1</td><td>[int]</td><td>123</td></tr>'.
  265.                     '<tr><td>2</td><td>[float]</td><td>123.45</td></tr>'.
  266.                     '</table>'.
  267.                 '</td>'.
  268.             '</tr><tr>'.
  269.                 '<td>"key3"</td>'.
  270.                 '<td colspan="2">[NULL]</td>'.
  271.             '</tr>'.
  272.             '</table>',
  273.             Var_Dump::display($GLOBALS['array'],TRUE)
  274.         );
  275.     }
  276.  
  277.     /*=============================*/
  278.     /* Test "HTML4_Table" Renderer */
  279.     /*=============================*/
  280.  
  281.     function test_renderer_html4_table({
  282.         Var_Dump::displayInit(array('display_mode'=>'HTML4_Table'));
  283.         $this->assertEquals(
  284.             '<table border="0" cellpadding="1" cellspacing="0" bgcolor="black"><tr><td>'.
  285.             '<table border="0" cellpadding="4" cellspacing="0" width="100%">'.
  286.             '<caption style="color:white;background:#339900;">array(3)</caption>'.
  287.             '<tr valign="top" bgcolor="#F8F8F8">'.
  288.                 '<td bgcolor="#CCCCCC"><b>key1</b></td>'.
  289.                 '<td><font color="#000000">string(44)</font></td>'.
  290.                 '<td><font color="#006600">The quick brown<br />'."\n".'fox jumped over<br />'."\n".'the lazy dog</font></td>'.
  291.             '</tr><tr valign="top" bgcolor="#E8E8E8">'.
  292.                 '<td bgcolor="#CCCCCC"><b>key2</b></td>'.
  293.                 '<td colspan="2">'.
  294.                     '<table border="0" cellpadding="1" cellspacing="0" bgcolor="black"><tr><td>'.
  295.                     '<table border="0" cellpadding="4" cellspacing="0" width="100%">'.
  296.                     '<caption style="color:white;background:#339900;">&amp;array(3)</caption>'.
  297.                     '<tr valign="top" bgcolor="#F8F8F8">'.
  298.                         '<td bgcolor="#CCCCCC"><b>0</b></td>'.
  299.                         '<td><font color="#000000">bool</font></td>'.
  300.                         '<td><font color="#006600">true</font></td>'.
  301.                     '</tr><tr valign="top" bgcolor="#E8E8E8">'.
  302.                         '<td bgcolor="#CCCCCC"><b>1</b></td>'.
  303.                         '<td><font color="#000000">int</font></td>'.
  304.                         '<td><font color="#006600">123</font></td>'.
  305.                     '</tr><tr valign="top" bgcolor="#F8F8F8">'.
  306.                         '<td bgcolor="#CCCCCC"><b>2</b></td>'.
  307.                         '<td><font color="#000000">float</font></td>'.
  308.                         '<td><font color="#006600">123.45</font></td>'.
  309.                     '</tr>'.
  310.                     '</table></td></tr></table>'.
  311.                 '</td>'.
  312.             '</tr><tr valign="top" bgcolor="#F8F8F8">'.
  313.                 '<td bgcolor="#CCCCCC"><b>key3</b></td>'.
  314.                 '<td colspan="2"><font color="#000000">NULL</font></td>'.
  315.             '</tr>'.
  316.             '</table></td></tr></table>',
  317.             Var_Dump::display($GLOBALS['array'],TRUE)
  318.         );
  319.     }
  320.  
  321.     /*=============================*/
  322.     /* Test "XHTML_Table" Renderer */
  323.     /*=============================*/
  324.  
  325.     function test_renderer_xhtml_table({
  326.         Var_Dump::displayInit(array('display_mode'=>'XHTML_Table'));
  327.         $this->assertEquals(
  328.             '<table class="var_dump">'.
  329.             '<caption>array(3)</caption>'.
  330.             '<tr>'.
  331.                 '<th>key1</th>'.
  332.                 '<td><i>string(44)</i></td>'.
  333.                 '<td>The quick brown<br />'."\n".'fox jumped over<br />'."\n".'the lazy dog</td>'.
  334.             '</tr><tr class="alt">'.
  335.                 '<th>key2</th>'.
  336.                 '<td colspan="2">'.
  337.                     '<table class="var_dump">'.
  338.                     '<caption>&amp;array(3)</caption>'.
  339.                     '<tr><th>0</th><td><i>bool</i></td><td>true</td></tr>'.
  340.                     '<tr class="alt"><th>1</th><td><i>int</i></td><td>123</td></tr>'.
  341.                     '<tr><th>2</th><td><i>float</i></td><td>123.45</td></tr>'.
  342.                     '</table>'.
  343.                 '</td>'.
  344.             '</tr><tr>'.
  345.                 '<th>key3</th>'.
  346.                 '<td colspan="2"><i>NULL</i></td>'.
  347.             '</tr>'.
  348.             '</table>',
  349.             Var_Dump::display($GLOBALS['array'],TRUE)
  350.         );
  351.     }
  352.  
  353.     /*=====================*/
  354.     /* Test "XML" Renderer */
  355.     /*=====================*/
  356.  
  357.     function test_renderer_xml({
  358.         Var_Dump::displayInit(array('display_mode'=>'XML'));
  359.         $this->assertEquals(
  360.             '<group caption="array(3)">'."\n".
  361.             '  <element>'."\n".
  362.             '    <key>key1</key>'."\n".
  363.             '    <type>string(44)</type>'."\n".
  364.             '    <value>The quick brown'."\n".
  365.             'fox jumped over'."\n".
  366.             'the lazy dog</value>'."\n".
  367.             '  </element>'."\n".
  368.             '  <element>'."\n".
  369.             '    <key>key2</key>'."\n".
  370.             '    <type>group</type>'."\n".
  371.             '    <value>'."\n".
  372.             '      <group caption="&amp;array(3)">'."\n".
  373.             '        <element>'."\n".
  374.             '          <key>0</key>'."\n".
  375.             '          <type>bool</type>'."\n".
  376.             '          <value>true</value>'."\n".
  377.             '        </element>'."\n".
  378.             '        <element>'."\n".
  379.             '          <key>1</key>'."\n".
  380.             '          <type>int</type>'."\n".
  381.             '          <value>123</value>'."\n".
  382.             '        </element>'."\n".
  383.             '        <element>'."\n".
  384.             '          <key>2</key>'."\n".
  385.             '          <type>float</type>'."\n".
  386.             '          <value>123.45</value>'."\n".
  387.             '        </element>'."\n".
  388.             '      </group>'."\n".
  389.             '    </value>'."\n".
  390.             '  </element>'."\n".
  391.             '  <element>'."\n".
  392.             '    <key>key3</key>'."\n".
  393.             '    <type>NULL</type>'."\n".
  394.             '    <value></value>'."\n".
  395.             '  </element>'."\n".
  396.             '</group>'."\n",
  397.             Var_Dump::display($GLOBALS['array'],TRUE)
  398.         );
  399.     }
  400.  
  401. }
  402.  
  403. /*============================*/
  404. /* Classes used by test Cases */
  405. /*============================*/
  406.  
  407. class zero {
  408.     var $i = 0;
  409.     var $f = 0.0;
  410. }
  411.  
  412. class object {
  413.     var $key1 "The quick brown\nfox jumped over\nthe lazy dog";
  414.     var $key2 = array(TRUE123123.45);
  415.     var $key3 = NULL;
  416. }
  417.  
  418. class parent {
  419.     function parent({
  420.         $this->myChild = new child($this);
  421.         $this->myName 'parent';
  422.     }
  423. }
  424. class child {
  425.     function child(&$parent{
  426.         $this->myParent =$parent;
  427.     }
  428. }
  429.  
  430. class recursion {
  431.     function recursion({
  432.         $this->recursion = new recursion();
  433.     }
  434. }
  435.  
  436.         $linkedArray=array(TRUE123123.45);
  437.         $array=array(
  438.             'key1' => 'The quick brown'."\n".'fox jumped over'."\n".'the lazy dog',
  439.             'key2' => $linkedArray,
  440.             'key3' => NULL
  441.         );
  442.  
  443. /*========*/
  444. /* Main() */
  445. /*========*/
  446.  
  447. $suite  = new PHPUnit_TestSuite("Var_DumpTest");
  448. $result = PHPUnit::run($suite);
  449.  
  450. echo $result->toHTML();
  451.  
  452. ?>

Documentation generated on Mon, 11 Mar 2019 10:16:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.