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

Source for file test1.php

Documentation is available at test1.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Testing_DocTest package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * <code>
  15.  * <?php
  16.  * echo "Foo!";
  17.  * // expects:
  18.  * // Foo!
  19.  * ?>
  20.  * </code>
  21.  *
  22.  * @category  Testing
  23.  * @package   Testing_DocTest
  24.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  25.  * @copyright 2008 David JEAN LOUIS
  26.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  27.  * @version   CVS: $Id$
  28.  * @link      http://pear.php.net/package/Testing_DocTest
  29.  * @since     Class available since release 0.1.0
  30.  * @filesource
  31.  */
  32.  
  33. /**
  34.  * A class that does nothing.
  35.  *
  36.  * @category  Testing
  37.  * @package   Testing_DocTest
  38.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  39.  * @copyright 2008 David JEAN LOUIS
  40.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  41.  * @version   Release: 0.6.0
  42.  * @link      http://pear.php.net/package/Testing_DocTest
  43.  * @since     Class available since release 0.1.0
  44.  */
  45. class Bar
  46. {
  47. }
  48.  
  49. /**
  50.  * This is a file level test.
  51.  *
  52.  * <code>
  53.  * // doctest: file-level doctest 1
  54.  * // setup:
  55.  * // $_ENV['OSTYPE'] = 'linux';
  56.  * echo OS_TYPE;
  57.  * // expects:
  58.  * // linux
  59.  * </code>
  60.  */ 
  61. define('OS_TYPE'$_ENV['OSTYPE']);
  62.  
  63. /**
  64.  * Foo class.
  65.  *
  66.  * Below, an example of class level doc test.
  67.  *
  68.  * <code>
  69.  * // we can name our doctest explicitely, if a name is not provided it
  70.  * // defaults to "class ClassName" (here "class Foo")
  71.  *
  72.  * // doctest: my test for class Foo
  73.  * $foo1 = new Foo();
  74.  * $foo1->attr1 = 'value1';
  75.  * $foo1->attr2 = 'value2';
  76.  * echo $foo1 . "\n";
  77.  * $foo1->attr1 = null;
  78.  * echo $foo1;
  79.  *
  80.  * // expects:
  81.  * // value1_value2
  82.  * // value2
  83.  * </code>
  84.  *
  85.  * @category  Testing
  86.  * @package   Testing_DocTest
  87.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  88.  * @copyright 2008 David JEAN LOUIS
  89.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  90.  * @version   Release: 0.6.0
  91.  * @link      http://pear.php.net/package/Testing_DocTest
  92.  * @since     Class available since release 0.1.0
  93.  */
  94. class Foo extends Bar
  95. {
  96.     // Foo properties {{{
  97.  
  98.     /**
  99.      * Properties doc blocs do not accept doc tests.
  100.      *
  101.      * @var string $attr1 
  102.      * @access public
  103.      */
  104.     var $attr1 = null;
  105.  
  106.     /**
  107.      * Properties doc blocs do not accept doc tests.
  108.      *
  109.      * @var string $attr2 
  110.      * @access public
  111.      */
  112.     var $attr2 = null;
  113.  
  114.     // }}}
  115.     // Foo::__construct() {{{
  116.  
  117.     /**
  118.      * Constructor.
  119.      *
  120.      * @param array $params an optional array of parameters
  121.      *
  122.      * @access public
  123.      */
  124.     public function __construct(array $params=array()) 
  125.     {
  126.         if (isset($params['attr1'])) {
  127.             $this->attr1 = $params['attr1'];
  128.         }
  129.         if (isset($params['attr2'])) {
  130.             $this->attr2 = $params['attr2'];
  131.         }
  132.     }
  133.  
  134.     // }}}
  135.     // Foo::__toString() {{{
  136.  
  137.     /**
  138.      * toString method.
  139.      *
  140.      * @access public
  141.      * @return string 
  142.      */
  143.     public function __toString(
  144.     {
  145.         $ret = array();
  146.         if (null !== $this->attr1{
  147.             $ret[$this->attr1;
  148.         }
  149.         if (null !== $this->attr2{
  150.             $ret[$this->attr2;
  151.         }
  152.         return implode('_'$ret);
  153.     }
  154.  
  155.     // }}}
  156.     // Foo::testString() {{{
  157.  
  158.     /**
  159.      * <code>
  160.      * // below we specify the name of our test, if not specified it default
  161.      * // to "method ClassName::methodName()" (here "method Foo::testString")
  162.      *
  163.      * // doctest: my test for method Foo::testString
  164.      * $foo = new Foo();
  165.      * echo $foo->testString();
  166.      * // expects:
  167.      * // bar
  168.      * </code>
  169.      *
  170.      * @access public
  171.      * @return string 
  172.      */
  173.     public function testString(
  174.     {
  175.         return 'bar';
  176.     }
  177.  
  178.     // }}}
  179.     // Foo::testBool() {{{
  180.  
  181.     /**
  182.      * <code>
  183.      * $foo = new Foo();
  184.      * var_dump($foo->testBool());
  185.      * var_dump($foo->testBool(false));
  186.      * // expects:
  187.      * // bool(true)
  188.      * // bool(false)
  189.      * </code>
  190.      *
  191.      * @param bool $ret return value
  192.      *
  193.      * @access public
  194.      * @return boolean 
  195.      */
  196.     public function testBool($ret=true
  197.     {
  198.         return $ret;
  199.     }
  200.  
  201.     // }}}
  202.     // Foo::testInt() {{{
  203.  
  204.     /**
  205.      * <code>
  206.      * $foo = new Foo();
  207.      * var_dump($foo->testInt());
  208.      * // expects:
  209.      * // int(7)
  210.      * </code>
  211.      *
  212.      * @access public
  213.      * @return int 
  214.      */
  215.     public function testInt(
  216.     {
  217.         return 7;
  218.     }
  219.  
  220.     // }}}
  221.     // Foo::testFloat() {{{
  222.  
  223.     /**
  224.      * <code>
  225.      * $foo = new Foo();
  226.      * var_dump($foo->testFloat());
  227.      * // expects:
  228.      * // float(12.34)
  229.      * </code>
  230.      *
  231.      * @access public
  232.      * @return float 
  233.      */
  234.     public function testFloat(
  235.     {
  236.         return 12.34;
  237.     }
  238.  
  239.     // }}}
  240.     // Foo::testArray() {{{
  241.  
  242.     /**
  243.      * <code>
  244.      * $foo = new Foo();
  245.      * print_r($foo->testArray());
  246.      * // expects:
  247.      * // Array
  248.      * // (
  249.      * //     [foo] => foo value
  250.      * //     [bar] => bar value
  251.      * // )
  252.      * </code>
  253.      *
  254.      * @access public
  255.      * @return array 
  256.      */
  257.     public function testArray(
  258.     {
  259.         return array(
  260.             'foo' => 'foo value',
  261.             'bar' => 'bar value',
  262.         );
  263.     }
  264.  
  265.     // }}}
  266.     // Foo::testObject() {{{
  267.  
  268.     /**
  269.      * <code>
  270.      * $foo = new Foo(array('attr1'=>'foo', 'attr2'=>'bar'));
  271.      * echo $foo . "\n";
  272.      * echo get_class($foo);
  273.      * // expects:
  274.      * // foo_bar
  275.      * // Foo
  276.      * </code>
  277.      *
  278.      * @access public
  279.      * @return object Foo 
  280.      */
  281.     public function testObject(
  282.     {
  283.         return new Foo();
  284.     }
  285.  
  286.     // }}}
  287.     // Foo::testResource() {{{
  288.  
  289.     /**
  290.      * <code>
  291.      * $foo = new Foo();
  292.      * var_dump(is_resource($foo->testResource()));
  293.      * echo get_resource_type($foo->testResource());
  294.      * // expects:
  295.      * // bool(true)
  296.      * // stream
  297.      * </code>
  298.      *
  299.      * @access public
  300.      * @return ressource file
  301.      */
  302.     public function testResource(
  303.     {
  304.         return fopen(__FILE__'r');
  305.     }
  306.  
  307.     // }}}
  308.     // Foo::testException() {{{
  309.  
  310.     /**
  311.      * <code>
  312.      * $foo = new Foo();
  313.      * try {
  314.      *     $foo->testException();
  315.      * } catch (Exception $exc) {
  316.      *     echo $exc->getMessage();
  317.      * }
  318.      * // expects:
  319.      * // Some descriptive message
  320.      * </code>
  321.      *
  322.      * @access public
  323.      * @return Exception 
  324.      * @throws Exception
  325.      */
  326.     public function testException(
  327.     {
  328.         throw new Exception('Some descriptive message');
  329.     }
  330.  
  331.     // }}}
  332.     // Foo::testNull() {{{
  333.  
  334.     /**
  335.      * When an instruction does not return a value or return the value NULL,
  336.      * accepted syntaxes are:
  337.      * // expects: null
  338.      *
  339.      * <code>
  340.      * $foo = new Foo();
  341.      * echo $foo->testNull();
  342.      * // expects:
  343.      * </code>
  344.      *
  345.      * @access public
  346.      * @return null 
  347.      */
  348.     public function testNull(
  349.     {
  350.         return null;
  351.     }
  352.  
  353.     // }}}
  354.     // Foo::testError() {{{
  355.  
  356.     /**
  357.      * <code>
  358.      * // flags: ELLIPSIS
  359.      * $foo = new Foo();
  360.      * echo $foo->testError(E_USER_ERROR);
  361.      * // expects:
  362.      * // Fatal error: Foo ! in [...] on line [...]
  363.      * </code>
  364.      *
  365.      * <code>
  366.      * // flags: ELLIPSIS
  367.      * // ini-set: display_errors=Off
  368.      * $foo = new Foo(E_USER_ERROR);
  369.      * echo $foo->testError();
  370.      * // expects:
  371.      * </code>
  372.      *
  373.      * <code>
  374.      * // flags: ELLIPSIS
  375.      * $foo = new Foo();
  376.      * echo $foo->testError(E_USER_WARNING);
  377.      * // expects:
  378.      * // Warning: Foo ! in [...] on line [...]
  379.      * </code>
  380.      *
  381.      * <code>
  382.      * // flags: ELLIPSIS
  383.      * $foo = new Foo();
  384.      * echo $foo->testError(E_USER_NOTICE);
  385.      * // expects:
  386.      * // Notice: Foo ! in [...] on line [...]
  387.      * </code>
  388.      *
  389.      * @param int $level error level
  390.      *
  391.      * @access public
  392.      * @return null 
  393.      */
  394.     public function testError($level
  395.     {
  396.         trigger_error('Foo !'$level);
  397.     }
  398.  
  399.     // }}}
  400.     // Foo::testBug16372pre() {{{
  401.  
  402.     /**
  403.      * <code>
  404.      * // flags: ELLIPSIS
  405.      * $foo = new Foo();
  406.      * echo $foo->testBug16372pre();
  407.      * // expects:
  408.      * // 2
  409.      * </code>
  410.      *
  411.      * @access public
  412.      * @return int 
  413.      */
  414.     public function testBug16372pre(
  415.     {
  416.         $x = 1;
  417.         $y = "{$x}";
  418.         return $x $y;
  419.     }
  420.  
  421.     // }}}
  422.     // Foo::testBug16372() {{{
  423.  
  424.     /**
  425.      * <code>
  426.      * // flags: ELLIPSIS
  427.      * $foo = new Foo();
  428.      * echo $foo->testBug16372();
  429.      * // expects:
  430.      * // 2
  431.      * </code>
  432.      *
  433.      * @access public
  434.      * @return int 
  435.      */
  436.     public function testBug16372(
  437.     {
  438.         return 2;
  439.     }
  440.  
  441.     // }}}
  442. }
  443.  
  444. /**
  445.  * This little function will explain the usage of flags in doc tests.
  446.  * At the moment, doc tests can have the following flags:
  447.  *
  448.  *   - NORMALIZE_WHITESPACE: tells the runner to compare strings ignoring
  449.  *     all whitespace differences;
  450.  *   - CASE_INSENSITIVE:  tells the runner to compare strings ignoring case;
  451.  *   - SKIP: tells the parser to just ignore the test;
  452.  *   - ELLIPSIS: allow to pass a wildcard pattern: [...] that will match
  453.  *     any string in the actual result.
  454.  *
  455.  * flags syntax:
  456.  * // flags: FLAG_1, FLAG_2 , ... , FLAG_N
  457.  *
  458.  * or:
  459.  *
  460.  * // flags: FLAG_1
  461.  * // FLAG_2 , FLAG_3
  462.  * // FLAG_N
  463.  *
  464.  * Here are some examples:
  465.  *
  466.  * <code>
  467.  * // flags: NORMALIZE_WHITESPACE
  468.  * echo testFlags('   fo  o        ');
  469.  * // expects:
  470.  * // function says: foo
  471.  * </code>
  472.  *
  473.  * <code>
  474.  * // flags: CASE_INSENSITIVE
  475.  * echo testFlags('foo');
  476.  * // expects:
  477.  * // FUNCtion says: Foo
  478.  * </code>
  479.  *
  480.  * <code>
  481.  * // flags: SKIP
  482.  * echo testFlags('bar');
  483.  * // expects:
  484.  * // don't care too much...
  485.  * </code>
  486.  *
  487.  * <code>
  488.  * // flags: ELLIPSIS
  489.  * echo testFlags('bar');
  490.  * // expects:
  491.  * // function [...]: [...]
  492.  * </code>
  493.  *
  494.  * @param string $foo some string
  495.  *
  496.  * @return string 
  497.  */
  498. function testFlags($foo='')
  499. {
  500.     return 'function says: ' strtolower($foo);
  501. }
  502.  
  503. /**
  504.  * A simple function that multiply two int or float and return a float number.
  505.  * It throws an exception if arguments given have a wrong type.
  506.  *
  507.  * Note that the "^M" chars have been intentionally added for tests purpose ;)
  508.  *
  509.  * <code>
  510.  *
  511.  * printf("%01.2f\n", multiply(3, 4));
  512.  * printf("%01.2f\n", multiply(3.2, 4));
  513.  * printf("%01.2f\n", multiply(3.2, 4.2));
  514.  * try {
  515.  *     multiply('foo', 4.2);
  516.  * } catch (Exception $exc) {
  517.  *     echo $exc->getMessage() . "\n";
  518.  * }
  519.  * try {
  520.  *     multiply(3.2, 'foo');
  521.  * } catch (Exception $exc) {
  522.  *     echo $exc->getMessage() . "\n";
  523.  * }
  524.  * // expects:
  525.  * // 12.00
  526.  * // 12.80
  527.  * // 13.44
  528.  * // Wrong type for first argument.
  529.  * // Wrong type for second argument.
  530.  *
  531.  * </code>
  532.  *
  533.  * @param mixed $a an int or a float
  534.  * @param mixed $b an int or a float
  535.  *
  536.  * @return float the result of the multiplication
  537.  * @throws Exception if arguments given have a wrong type
  538.  */
  539. function multiply($a$b)
  540. {
  541.     // check first arg type
  542.     if (!is_int($a&& !is_float($a)) {
  543.         throw new Exception("Wrong type for first argument.");
  544.     }
  545.     // check second arg type
  546.     if (!is_int($b&& !is_float($b)) {
  547.         throw new Exception("Wrong type for second argument.");
  548.     }
  549.     return (float)($a $b);
  550. }
  551.  
  552. /**
  553.  * A simple function that multiply two int or float and return a float number.
  554.  * It throws an exception if arguments given have a wrong type.
  555.  * 
  556.  * This example shows the use of an external doctest file.
  557.  *
  558.  * <code>
  559.  * // test-file: docs/external_file.doctest
  560.  * </code>
  561.  *
  562.  * @param mixed $a an int or a float
  563.  * @param mixed $b an int or a float
  564.  *
  565.  * @return float the result of the multiplication
  566.  * @throws Exception if arguments given have a wrong type
  567.  */
  568. function multiply2($a$b)
  569. {
  570.     // check first arg type
  571.     if (!is_int($a&& !is_float($a)) {
  572.         throw new Exception("Wrong type for first argument.");
  573.     }
  574.     // check second arg type
  575.     if (!is_int($b&& !is_float($b)) {
  576.         throw new Exception("Wrong type for second argument.");
  577.     }
  578.     return (float)($a $b);
  579. }
  580.  
  581. /**
  582.  * A simple function that return a simple or multidimensional array.
  583.  *
  584.  * <code>
  585.  * // flags:
  586.  * print_r(testArray(true));
  587.  * // expects:
  588.  * // Array
  589.  * // (
  590.  * //     [foo] => 1
  591.  * //     [bar] => 2
  592.  * // )
  593.  * </code>
  594.  *
  595.  * <code>
  596.  * // note that here we must add a blank line at the end because we are using
  597.  * // STRICT_WHITESPACE flag.
  598.  *
  599.  * // flags: NORMALIZE_WHITESPACE
  600.  * print_r(testArray());
  601.  * // expects:
  602.  * // Array([0]=>foo [1]=>bar)
  603.  * </code>
  604.  *
  605.  * @param bool $multi return a multidimensional array if set to true.
  606.  *
  607.  * @return array multidimensionnal array
  608.  */
  609. function testArray($multi=false)
  610. {
  611.     return $multi ? array('foo'=>'1''bar'=>'2': array('foo''bar');
  612. }
  613.  
  614. /**
  615.  * A simple function that return a string.
  616.  *
  617.  * <code>
  618.  * // note that here we must set the ELLIPSIS flag cause we cannot predict
  619.  * // exactly the result of the function
  620.  *
  621.  * // flags: ELLIPSIS
  622.  * echo testString();
  623.  * // expects:
  624.  * // A string that cannot be predicted [...].
  625.  *
  626.  * </code>
  627.  *
  628.  * <code>
  629.  * // an example on how we can wrap long text
  630.  *
  631.  * // flags: ELLIPSIS
  632.  * echo testString();
  633.  * // expects:
  634.  * // A string \
  635.  * // that cannot \
  636.  * // be predicted [...].
  637.  *
  638.  * </code>
  639.  *
  640.  * @return array multidimensionnal array
  641.  */
  642. function testString()
  643. {
  644.     return sprintf('A string that cannot be predicted %s.'microtime());
  645. }
  646.  
  647. /**
  648.  * This is another file level test.
  649.  *
  650.  * <code>
  651.  * // doctest: file-level doctest 2
  652.  * // setup:
  653.  * // $_REQUEST['foo'] = 'bar';
  654.  * var_dump(defined('FOO'));
  655.  * // expects:
  656.  * // bool(true)
  657.  * </code>
  658.  */ 
  659. if (isset($_REQUEST['foo']&& $_REQUEST['foo'== 'bar'{
  660.     define('FOO''bar');
  661. }
  662.  
  663.  
  664. /**
  665.  * simple test of tmplCode flag
  666.  *
  667.  * <code>
  668.  * // doctest: tmpl-code doctest
  669.  * // tmpl-code: docs/tmpl_code.doctest.php
  670.  *
  671.  * print $this->bar();
  672.  *
  673.  * // expects:
  674.  * // I'm really private
  675.  * </code>
  676.  */

Documentation generated on Mon, 11 Mar 2019 15:52:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.