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_type_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_type_bool({
  37.         $this->assertEquals('bool false'$this->vd->toString(FALSE));
  38.         $this->assertEquals('bool true'$this->vd->toString(TRUE));
  39.     }
  40.  
  41.     function test_type_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_type_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_type_null({
  63.         $this->assertEquals('NULL'$this->vd->toString(NULL));
  64.     }
  65.  
  66.     /*============*/
  67.     /* Test Array */
  68.     /*============*/
  69.  
  70.     function test_type_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_type_object({
  92.         $this->assertRegExp(
  93.             '/' .
  94.             'object\(object\)(#[0-9]+ )?\(3\) {' "\n" .
  95.             '  key1 => string\(44\) The quick brown' "\n" .
  96.             '                     fox jumped over' "\n" .
  97.             '                     the lazy dog' "\n" .
  98.             '  key2 => array\(3\) {' "\n" .
  99.             '    0 => bool true' "\n" .
  100.             '    1 => int 123' "\n" .
  101.             '    2 => float 123.45' "\n" .
  102.             '  }' "\n" .
  103.             '  key3 => NULL' "\n" .
  104.             '}' .
  105.             '/',
  106.             $this->vd->toString(new object()));
  107.     }
  108.  
  109.     /*======================*/
  110.     /* Test "Text" Renderer */
  111.     /*======================*/
  112.  
  113.     function test_renderer_text({
  114.         Var_Dump::displayInit(
  115.             array('display_mode' => 'Text'),
  116.             array(
  117.                 'show_eol'       => ' *',
  118.                 'offset'         => 3,
  119.                 'opening'        => '(',
  120.                 'closing'        => ')',
  121.                 'operator'       => ' -> ',
  122.                 'before_num_key' => '\'',
  123.                 'after_num_key'  => '\'',
  124.                 'before_str_key' => '"',
  125.                 'after_str_key'  => '"',
  126.                 'before_type'    => '[',
  127.                 'after_type'     => ']'
  128.             )
  129.         );
  130.         $this->assertEquals(
  131.             'array(3) (' "\n" .
  132.             '   "key1" -> [string(44)] The quick brown *' "\n" .
  133.             '                          fox jumped over *' "\n" .
  134.             '                          the lazy dog' "\n" .
  135.             '   "key2" -> &array(3) (' "\n" .
  136.             '      \'0\' -> [bool] true' "\n" .
  137.             '      \'1\' -> [int] 123' "\n" .
  138.             '      \'2\' -> [float] 123.45' "\n" .
  139.             '   )' "\n" .
  140.             '   "key3" -> [NULL]' "\n" .
  141.             ')',
  142.             Var_Dump::display($GLOBALS['array']TRUE)
  143.         );
  144.     }
  145.  
  146.     /*============================*/
  147.     /* Test "HTML4_Text" Renderer */
  148.     /*============================*/
  149.  
  150.     function test_renderer_html4_text({
  151.         Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'));
  152.         $this->assertEquals(
  153.             '<pre>array(3) {' "\n" .
  154.             '  key1 =&gt; <font color="#006600">string(44)</font> <font color="#339900">The quick brown' "\n" .
  155.             '</font>                     <font color="#339900">fox jumped over' "\n" .
  156.             '</font>                     <font color="#339900">the lazy dog</font>' "\n" .
  157.             '  key2 =&gt; &array(3) {' "\n" .
  158.             '    0 =&gt; <font color="#006600">bool</font> <font color="#339900">true</font>' "\n" .
  159.             '    1 =&gt; <font color="#006600">int</font> <font color="#339900">123</font>' "\n" .
  160.             '    2 =&gt; <font color="#006600">float</font> <font color="#339900">123.45</font>' "\n" .
  161.             '  }' "\n" .
  162.             '  key3 =&gt; <font color="#006600">NULL</font>' "\n" .
  163.             '}' "\n" .
  164.             '</pre>',
  165.             Var_Dump::display($GLOBALS['array']TRUE)
  166.         );
  167.     }
  168.  
  169.     /*============================*/
  170.     /* Test "XHTML_Text" Renderer */
  171.     /*============================*/
  172.  
  173.     function test_renderer_xhtml_text({
  174.         Var_Dump::displayInit(array('display_mode' => 'XHTML_Text'));
  175.         $this->assertEquals(
  176.             '<pre class="var_dump">array(3) {' "\n" .
  177.             '  key1 =&gt; <span class="type">string(44)</span> <span class="value">The quick brown' "\n" .
  178.             '</span>                     <span class="value">fox jumped over' "\n" .
  179.             '</span>                     <span class="value">the lazy dog</span>' "\n" .
  180.             '  key2 =&gt; &array(3) {' "\n" .
  181.             '    0 =&gt; <span class="type">bool</span> <span class="value">true</span>' "\n" .
  182.             '    1 =&gt; <span class="type">int</span> <span class="value">123</span>' "\n" .
  183.             '    2 =&gt; <span class="type">float</span> <span class="value">123.45</span>' "\n" .
  184.             '  }' "\n" .
  185.             '  key3 =&gt; <span class="type">NULL</span>' "\n" .
  186.             '}' "\n" .
  187.             '</pre>',
  188.             Var_Dump::display($GLOBALS['array']TRUE)
  189.         );
  190.     }
  191.  
  192.     /*=======================*/
  193.     /* Test "Table" Renderer */
  194.     /*=======================*/
  195.  
  196.     function test_renderer_table({
  197.         Var_Dump::displayInit(
  198.             array('display_mode' => 'Table'),
  199.             array(
  200.                 'before_str_key' => '"',
  201.                 'after_str_key'  => '"',
  202.                 'before_type'    => '[',
  203.                 'after_type'     => ']'
  204.             )
  205.         );
  206.         $this->assertEquals(
  207.             '<table>' .
  208.             '<caption>array(3)</caption>' .
  209.             '<tr>' .
  210.                 '<td>"key1"</td>' .
  211.                 '<td>[string(44)]</td>' .
  212.                 '<td>The quick brown<br />' "\n" .
  213.                     'fox jumped over<br />' "\n" 
  214.                     'the lazy dog</td>' .
  215.             '</tr><tr>' .
  216.                 '<td>"key2"</td>' .
  217.                 '<td colspan="2">' .
  218.                     '<table>' .
  219.                     '<caption>&amp;array(3)</caption>' .
  220.                     '<tr><td>0</td><td>[bool]</td><td>true</td></tr>' .
  221.                     '<tr><td>1</td><td>[int]</td><td>123</td></tr>' .
  222.                     '<tr><td>2</td><td>[float]</td><td>123.45</td></tr>' .
  223.                     '</table>' .
  224.                 '</td>' .
  225.             '</tr><tr>' .
  226.                 '<td>"key3"</td>' .
  227.                 '<td colspan="2">[NULL]</td>' .
  228.             '</tr>' .
  229.             '</table>',
  230.             Var_Dump::display($GLOBALS['array']TRUE)
  231.         );
  232.     }
  233.  
  234.     /*=============================*/
  235.     /* Test "HTML4_Table" Renderer */
  236.     /*=============================*/
  237.  
  238.     function test_renderer_html4_table({
  239.         Var_Dump::displayInit(array('display_mode' => 'HTML4_Table'));
  240.         $this->assertEquals(
  241.             '<table border="0" cellpadding="1" cellspacing="0" bgcolor="black"><tr><td>' .
  242.             '<table border="0" cellpadding="4" cellspacing="0" width="100%">' .
  243.             '<caption style="color:white;background:#339900;">array(3)</caption>' .
  244.             '<tr valign="top" bgcolor="#F8F8F8">' .
  245.                 '<td bgcolor="#CCCCCC"><b>key1</b></td>' .
  246.                 '<td><font color="#000000">string(44)</font></td>' .
  247.                 '<td><font color="#006600">The quick brown<br />' "\n" .
  248.                     'fox jumped over<br />' "\n" .
  249.                     'the lazy dog</font></td>' .
  250.             '</tr><tr valign="top" bgcolor="#E8E8E8">' .
  251.                 '<td bgcolor="#CCCCCC"><b>key2</b></td>' .
  252.                 '<td colspan="2">' .
  253.                     '<table border="0" cellpadding="1" cellspacing="0" bgcolor="black"><tr><td>' .
  254.                     '<table border="0" cellpadding="4" cellspacing="0" width="100%">' .
  255.                     '<caption style="color:white;background:#339900;">&amp;array(3)</caption>' .
  256.                     '<tr valign="top" bgcolor="#F8F8F8">' .
  257.                         '<td bgcolor="#CCCCCC"><b>0</b></td>' .
  258.                         '<td><font color="#000000">bool</font></td>' .
  259.                         '<td><font color="#006600">true</font></td>' .
  260.                     '</tr><tr valign="top" bgcolor="#E8E8E8">' .
  261.                         '<td bgcolor="#CCCCCC"><b>1</b></td>' .
  262.                         '<td><font color="#000000">int</font></td>' .
  263.                         '<td><font color="#006600">123</font></td>' .
  264.                     '</tr><tr valign="top" bgcolor="#F8F8F8">' .
  265.                         '<td bgcolor="#CCCCCC"><b>2</b></td>' .
  266.                         '<td><font color="#000000">float</font></td>' .
  267.                         '<td><font color="#006600">123.45</font></td>' .
  268.                     '</tr>' .
  269.                     '</table></td></tr></table>' .
  270.                 '</td>' .
  271.             '</tr><tr valign="top" bgcolor="#F8F8F8">' .
  272.                 '<td bgcolor="#CCCCCC"><b>key3</b></td>' .
  273.                 '<td colspan="2"><font color="#000000">NULL</font></td>' .
  274.             '</tr>' .
  275.             '</table></td></tr></table>',
  276.             Var_Dump::display($GLOBALS['array']TRUE)
  277.         );
  278.     }
  279.  
  280.     /*=============================*/
  281.     /* Test "XHTML_Table" Renderer */
  282.     /*=============================*/
  283.  
  284.     function test_renderer_xhtml_table({
  285.         Var_Dump::displayInit(array('display_mode' => 'XHTML_Table'));
  286.         $this->assertEquals(
  287.             '<table class="var_dump">' .
  288.             '<caption>array(3)</caption>' .
  289.             '<tr>' .
  290.                 '<th>key1</th>' .
  291.                 '<td><i>string(44)</i></td>' .
  292.                 '<td>The quick brown<br />' "\n" .
  293.                     'fox jumped over<br />' "\n" .
  294.                     'the lazy dog</td>' .
  295.             '</tr><tr class="alt">' .
  296.                 '<th>key2</th>' .
  297.                 '<td colspan="2">' .
  298.                     '<table class="var_dump">' .
  299.                     '<caption>&amp;array(3)</caption>' .
  300.                     '<tr><th>0</th><td><i>bool</i></td><td>true</td></tr>' .
  301.                     '<tr class="alt"><th>1</th><td><i>int</i></td><td>123</td></tr>' .
  302.                     '<tr><th>2</th><td><i>float</i></td><td>123.45</td></tr>' .
  303.                     '</table>' .
  304.                 '</td>' .
  305.             '</tr><tr>' .
  306.                 '<th>key3</th>' .
  307.                 '<td colspan="2"><i>NULL</i></td>' .
  308.             '</tr>' .
  309.             '</table>',
  310.             Var_Dump::display($GLOBALS['array']TRUE)
  311.         );
  312.     }
  313.  
  314.     /*=====================*/
  315.     /* Test "XML" Renderer */
  316.     /*=====================*/
  317.  
  318.     function test_renderer_xml({
  319.         Var_Dump::displayInit(array('display_mode' => 'XML'));
  320.         $this->assertEquals(
  321.             '<group caption="array(3)">' "\n" .
  322.             '  <element>' "\n" .
  323.             '    <key>key1</key>' "\n" .
  324.             '    <type>string(44)</type>' "\n" .
  325.             '    <value>The quick brown' "\n" .
  326.             'fox jumped over' "\n" .
  327.             'the lazy dog</value>' "\n" .
  328.             '  </element>' "\n" .
  329.             '  <element>' "\n" .
  330.             '    <key>key2</key>' "\n" .
  331.             '    <type>group</type>' "\n" .
  332.             '    <value>' "\n" .
  333.             '      <group caption="&amp;array(3)">' "\n" .
  334.             '        <element>' "\n" .
  335.             '          <key>0</key>' "\n" .
  336.             '          <type>bool</type>' "\n" .
  337.             '          <value>true</value>' "\n" .
  338.             '        </element>' "\n" .
  339.             '        <element>' "\n" .
  340.             '          <key>1</key>' "\n" .
  341.             '          <type>int</type>' "\n" .
  342.             '          <value>123</value>' "\n" .
  343.             '        </element>' "\n" .
  344.             '        <element>' "\n" .
  345.             '          <key>2</key>' "\n" .
  346.             '          <type>float</type>' "\n" .
  347.             '          <value>123.45</value>' "\n" .
  348.             '        </element>' ."\n" .
  349.             '      </group>' "\n" .
  350.             '    </value>' "\n" .
  351.             '  </element>' "\n" .
  352.             '  <element>' "\n" .
  353.             '    <key>key3</key>' "\n" .
  354.             '    <type>NULL</type>' "\n" .
  355.             '    <value></value>' "\n" .
  356.             '  </element>' "\n" .
  357.             '</group>' "\n",
  358.             Var_Dump::display($GLOBALS['array']TRUE)
  359.         );
  360.     }
  361.  
  362.     /*===================================*/
  363.     /* Test Text Renderer : Compact mode */
  364.     /*===================================*/
  365.  
  366.     function test_renderer_text_compact({
  367.         Var_Dump::displayInit(
  368.             array('display_mode' => 'Text'),
  369.             array('mode' => 'compact')
  370.         );
  371.         $this->assertEquals(
  372.             'array(3) {' "\n" .
  373.             '  key-1 => string(44) The quick brown fox jumped over the lazy dog' "\n" .
  374.             '  key-2 => array(2) {' "\n" .
  375.             '    long-key => &array(3) {' "\n" .
  376.             '      0 => string(4) John' "\n" .
  377.             '      11 => string(4) Jack' "\n" .
  378.             '      127 => string(4) Bill' "\n" .
  379.             '    }' "\n" .
  380.             '    file => NULL' "\n" .
  381.             '  }' "\n" .
  382.             '  long-key-3 => int 234' "\n" .
  383.             '}',
  384.             Var_Dump::display($GLOBALS['array2']TRUE)
  385.         );
  386.     }
  387.  
  388.     /*==================================*/
  389.     /* Test Text Renderer : Normal mode */
  390.     /*==================================*/
  391.  
  392.     function test_renderer_text_normal({
  393.         Var_Dump::displayInit(
  394.             array('display_mode' => 'Text'),
  395.             array('mode' => 'normal')
  396.         );
  397.         $this->assertEquals(
  398.             'array(3) {' "\n" .
  399.             '  key-1      => string(44) The quick brown fox jumped over the lazy dog' "\n" .
  400.             '  key-2      => array(2) {' "\n" .
  401.             '    long-key => &array(3) {' "\n" .
  402.             '      0   => string(4) John' "\n" .
  403.             '      11  => string(4) Jack' "\n" .
  404.             '      127 => string(4) Bill' "\n" .
  405.             '    }' "\n" .
  406.             '    file     => NULL' "\n" .
  407.             '  }' "\n" .
  408.             '  long-key-3 => int 234' "\n" .
  409.             '}',
  410.             Var_Dump::display($GLOBALS['array2']TRUE)
  411.         );
  412.     }
  413.  
  414.     /*================================*/
  415.     /* Test Text Renderer : Wide mode */
  416.     /*================================*/
  417.  
  418.     function test_renderer_text_wide({
  419.         Var_Dump::displayInit(
  420.             array('display_mode' => 'Text'),
  421.             array('mode' => 'wide')
  422.         );
  423.         $this->assertEquals(
  424.             'array(3) {' "\n" .
  425.             '  key-1      => string(44) The quick brown fox jumped over the lazy dog' "\n" .
  426.             '  key-2      => array(2) {' "\n" .
  427.             '                  long-key => &array(3) {' "\n" .
  428.             '                                0   => string(4) John' "\n" .
  429.             '                                11  => string(4) Jack' "\n" .
  430.             '                                127 => string(4) Bill' "\n" .
  431.             '                              }' "\n" .
  432.             '                  file     => NULL' "\n" .
  433.             '                }' "\n" .
  434.             '  long-key-3 => int 234' "\n" .
  435.             '}',
  436.             Var_Dump::display($GLOBALS['array2']TRUE)
  437.         );
  438.     }
  439.  
  440.     /*==================================*/
  441.     /* Bug #490 Recursions not managed. */
  442.     /*==================================*/
  443.  
  444.     function test_bug_490({
  445.         if (version_compare(PHP_VERSION'4.9.9''>')) {
  446.             $pattern =
  447.                 '/' .
  448.                 'object\(c_parent\)(#[0-9]+ )?\(2\) {' "\n" .
  449.                 '  myChild => object\(child\)(#[0-9]+ )?\(1\) {' "\n" .
  450.                 '    myParent => object\(c_parent\)(#[0-9]+ )?\(2\) {' "\n" .
  451.                 '      myChild => object\(child\)(#[0-9]+ )?\(1\) {' "\n" .
  452.                 '        myParent => \*RECURSION\*' "\n" .
  453.                 '      }' "\n" .
  454.                 '      myName => string\(8\) c_parent' "\n" .
  455.                 '    }' "\n" .
  456.                 '  }' "\n" .
  457.                 '  myName => string\(8\) c_parent' "\n" .
  458.                 '}' .
  459.                 '/';
  460.         else {
  461.             $pattern =
  462.                 '/' .
  463.                 'object\(c_parent\)(#[0-9]+ )?\(2\) {' "\n" .
  464.                 '  myChild => object\(child\)(#[0-9]+ )?\(1\) {' "\n" .
  465.                 '    myParent => &object\(c_parent\)(#[0-9]+ )?\(2\) {' "\n" .
  466.                 '      myChild => object\(child\)(#[0-9]+ )?\(1\) {' "\n" .
  467.                 '        myParent => &object\(c_parent\)\(2\) {' "\n" .
  468.                 '          myChild => \*RECURSION\*' "\n" .
  469.                 '          myName => string\(8\) c_parent' "\n" .
  470.                 '        }' "\n" .
  471.                 '      }' "\n" .
  472.                 '      myName => string\(8\) c_parent' "\n" .
  473.                 '    }' "\n" .
  474.                 '  }' "\n" .
  475.                 '  myName => string\(8\) c_parent' "\n" .
  476.                 '}' .
  477.                 '/';
  478.         }
  479.         $this->assertRegExp(
  480.             $pattern,
  481.             $this->vd->toString(new c_parent())
  482.         );
  483.     }
  484.  
  485.     /*============================================================================*/
  486.     /* Bug #1321 Numeric zero values in array or object attributes are not shown. */
  487.     /*============================================================================*/
  488.  
  489.     function test_bug_1321({
  490.         $this->assertRegExp(
  491.             '/' .
  492.             'object\(zero\)(#[0-9]+ )?\(2\) {' "\n" .
  493.             '  i => int 0' "\n" .
  494.             '  f => float 0' "\n" .
  495.             '}' .
  496.             '/',
  497.             $this->vd->toString(new zero())
  498.         );
  499.         $this->assertEquals(
  500.             'array(2) {' "\n" .
  501.             '  0 => int 0' "\n" .
  502.             '  1 => float 0' "\n" .
  503.             '}',
  504.             $this->vd->toString(array(00.0))
  505.         );
  506.     }
  507.  
  508. }
  509.  
  510. /*============================*/
  511. /* Classes used by test Cases */
  512. /*============================*/
  513.  
  514. class zero {
  515.     var $i = 0;
  516.     var $f = 0.0;
  517. }
  518.  
  519. class object {
  520.     var $key1 "The quick brown\nfox jumped over\nthe lazy dog";
  521.     var $key2 = array(TRUE123123.45);
  522.     var $key3 = NULL;
  523. }
  524.  
  525. class c_parent {
  526.     function c_parent({
  527.         $this->myChild = new child($this);
  528.         $this->myName 'c_parent';
  529.     }
  530. }
  531. class child {
  532.     function child($c_parent{
  533.         $this->myParent $c_parent;
  534.     }
  535. }
  536.  
  537. $linkedArray = array(TRUE123123.45);
  538. $array = array(
  539.     'key1' => 'The quick brown' "\n" 'fox jumped over' "\n" 'the lazy dog',
  540.     'key2' => $linkedArray,
  541.     'key3' => NULL
  542. );
  543.  
  544. $linkedArray2 = array(0 => 'John'11 => 'Jack'127 => 'Bill');
  545. $array2 = array(
  546.     'key-1' => 'The quick brown fox jumped over the lazy dog',
  547.     'key-2' => array(
  548.         'long-key' => $linkedArray2,
  549.         'file' => NULL
  550.     ),
  551.     'long-key-3' => 234,
  552. );
  553.  
  554. /*========*/
  555. /* Main() */
  556. /*========*/
  557.  
  558. $suite = new PHPUnit_TestSuite('Var_DumpTest');
  559. $result = PHPUnit::run($suite);
  560.  
  561. echo $result->toHTML();
  562.  
  563. ?>

Documentation generated on Mon, 11 Mar 2019 13:58:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.