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

Source for file ShellTests.php

Documentation is available at ShellTests.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. require_once 'PHPUnit2/Framework/TestCase.php';
  5. require_once 'PHP/Shell.php';
  6.  
  7. class ShellTests extends PHPUnit2_Framework_TestCase {
  8.     private $vars;
  9.  
  10.     public function setUp({
  11.         /* create a fresh shell object */
  12.  
  13.         $this->shell = new PHP_Shell();
  14.  
  15.         $this->vars = array();
  16.     }
  17.  
  18.     public function tearDown({
  19.         foreach ($this->vars as $k => $v{
  20.             unset($GLOBALS[$k]);
  21.         }
  22.     }
  23.  
  24.     public function execute({
  25.         $__retval = null;
  26.  
  27.         ## get local vars
  28.         foreach($this->vars as $__k => $__v{
  29.             ${$__k$GLOBALS[$__k];
  30.         }
  31.         unset($__k);
  32.         unset($__v);
  33.         
  34.         if ($this->shell->parse(== 0{
  35.             $__retval = eval($this->shell->getCode())
  36.  
  37.             ## export vars to global scope
  38.             foreach (array_diff_key(get_defined_vars()$GLOBALSarray("__retval" => 1)) as $__k => $__v{
  39.                 $GLOBALS[$__k$__v;
  40.                 $this->vars[$__k$__v;
  41.             }
  42.         }
  43.  
  44.         return $__retval;
  45.     }
  46.  
  47.     public function testComments({
  48.         $tests = array(
  49.             '## comment',
  50.             '/* comment */',
  51.             '// comment'
  52.             );
  53.         foreach ($tests as $code{
  54.             $this->shell->resetCode();
  55.             $this->shell->appendCode($code);
  56.  
  57.             if ($this->shell->parse(== 0{
  58.                 eval($this->shell->getCode())
  59.             }
  60.         }
  61.     }
  62.  
  63.     public function testUndefVars({
  64.         $tests = array(
  65.             '$v',
  66.             );
  67.         foreach ($tests as $code{
  68.             $this->shell->resetCode();
  69.             $this->shell->appendCode($code);
  70.  
  71.             ## we should get a Exception
  72.             try $this->execute()$this->fail()}
  73.             catch Exception $e }
  74.         }
  75.     }
  76.  
  77.     public function testNewClass({
  78.         $tests = array(
  79.             '$v = new ArrayObject()',
  80.             );
  81.         foreach ($tests as $code{
  82.             $this->shell->resetCode();
  83.             $this->shell->appendCode($code);
  84.             $this->execute();
  85.         }
  86.     }
  87.  
  88.     public function testMethod({
  89.         $tests = array(
  90.             '$v = new ArrayObject()',
  91.             '$v->count()'
  92.             );
  93.         foreach ($tests as $code{
  94.             $this->shell->resetCode();
  95.             $this->shell->appendCode($code);
  96.             $this->execute();
  97.         }
  98.     }
  99.  
  100.     public function testNotExistingMethod({
  101.         $tests = array(
  102.             '$v = new ArrayObject()',
  103.             '$v->not_existing()'
  104.             );
  105.         foreach ($tests as $code{
  106.             $this->shell->resetCode();
  107.             $this->shell->appendCode($code);
  108.  
  109.             ## we should get a Exception
  110.             try $this->execute()$this->fail()}
  111.             catch Exception $e }
  112.         }
  113.     }
  114.  
  115.     public function testNotExistingClass({
  116.         $tests = array(
  117.             '$not_existing->not_existing()'
  118.             );
  119.         foreach ($tests as $code{
  120.             $this->shell->resetCode();
  121.             $this->shell->appendCode($code);
  122.  
  123.             ## we should get a Exception
  124.             try $this->execute()$this->fail()}
  125.             catch Exception $e }
  126.         }
  127.     }
  128.  
  129.     public function testClass({
  130.         $tests = array(
  131.             'class a { const a = "a"; }'
  132.             );
  133.         foreach ($tests as $code{
  134.             $this->shell->resetCode();
  135.             $this->shell->appendCode($code);
  136.  
  137.             ## we should get a Exception
  138.             $this->execute();
  139.         }
  140.     }
  141.  
  142.     public function testClassExtends({
  143.         $tests = array(
  144.             'class b extends a { }',
  145.             );
  146.         foreach ($tests as $code{
  147.             $this->shell->resetCode();
  148.             $this->shell->appendCode($code);
  149.  
  150.             ## we should get a Exception
  151.             $this->execute();
  152.         }
  153.     }
  154.  
  155.     public function testClassExtendsNotExisting({
  156.         $tests = array(
  157.             'class c extends not_existing { }',
  158.             );
  159.         foreach ($tests as $code{
  160.             $this->shell->resetCode();
  161.             $this->shell->appendCode($code);
  162.  
  163.             ## we should get a Exception
  164.             try $this->execute()$this->fail()}
  165.             catch Exception $e }
  166.         }
  167.     }
  168.  
  169.     public function testClassDuplicate({
  170.         $tests = array(
  171.             'class duplicate_class { }',
  172.             'class duplicate_class { }',
  173.             );
  174.         foreach ($tests as $code{
  175.             $this->shell->resetCode();
  176.             $this->shell->appendCode($code);
  177.  
  178.             ## we should get a Exception
  179.             try $this->execute()$this->fail()}
  180.             catch Exception $e }
  181.         }
  182.     }
  183.  
  184.     public function testClassDuplicateMethod({
  185.         $tests = array(
  186.             'class d { function duplicate_method () { } function duplicate_method() {} }',
  187.             );
  188.         foreach ($tests as $code{
  189.             $this->shell->resetCode();
  190.             $this->shell->appendCode($code);
  191.  
  192.             ## we should get a Exception
  193.             try $this->execute()$this->fail()}
  194.             catch Exception $e }
  195.         }
  196.     }
  197.  
  198.     public function testClassConstant({
  199.         $tests = array(
  200.             'a::a',
  201.             );
  202.         foreach ($tests as $code{
  203.             $this->shell->resetCode();
  204.             $this->shell->appendCode($code);
  205.  
  206.             $this->execute();
  207.         }
  208.     }
  209.  
  210.     public function testClassConstantNotExisting({
  211.         $tests = array(
  212.             'a::not_existing',
  213.             );
  214.         foreach ($tests as $code{
  215.             $this->shell->resetCode();
  216.             $this->shell->appendCode($code);
  217.  
  218.             ## we should get a Exception
  219.             try $this->execute()$this->fail()}
  220.             catch Exception $e }
  221.         }
  222.     }
  223.  
  224.     public function testFunctionDuplicate({
  225.         $tests = array(
  226.             'function duplicate_function() {}',
  227.             'function duplicate_function() {}',
  228.             );
  229.         foreach ($tests as $code{
  230.             $this->shell->resetCode();
  231.             $this->shell->appendCode($code);
  232.  
  233.             ## we should get a Exception
  234.             try $this->execute()$this->fail()}
  235.             catch Exception $e }
  236.         }
  237.     }
  238.  
  239.     public function testFunctionDynamicNotExisting({
  240.         $tests = array(
  241.             '$v = "vfunc"',
  242.             '$v()',
  243.             );
  244.         foreach ($tests as $code{
  245.             $this->shell->resetCode();
  246.             $this->shell->appendCode($code);
  247.  
  248.             ## we should get a Exception
  249.             try $this->execute()$this->fail()}
  250.             catch Exception $e }
  251.         }
  252.     }
  253.  
  254.     public function testLoops({
  255.         $tests = array(
  256.             '$a = array(0 => "2", 1 => "3")',
  257.             'foreach ($a as $k => $v) { }',
  258.             'do {} while(0)',
  259.             'while(0) {}',
  260.             'for ($i = 0; $i < count($a); $i++) {} '
  261.             );
  262.         foreach ($tests as $code{
  263.             $this->shell->resetCode();
  264.             $this->shell->appendCode($code);
  265.  
  266.             $this->execute();
  267.         }
  268.     }
  269.  
  270.     public function testIf({
  271.         $tests = array(
  272.             'if (0) { } else if (1) { } else { } ',
  273.             );
  274.         foreach ($tests as $code{
  275.             $this->shell->resetCode();
  276.             $this->shell->appendCode($code);
  277.  
  278.             $this->execute();
  279.         }
  280.     }
  281.  
  282.     public function testImplements({
  283.         $tests = array(
  284.             'class e implements Serializable { function serialize() { } function unserialize($a) {} }',
  285.             );
  286.         foreach ($tests as $code{
  287.             $this->shell->resetCode();
  288.             $this->shell->appendCode($code);
  289.  
  290.             $this->execute();
  291.         }
  292.     }
  293.  
  294.     public function testInterfaceNotExists({
  295.         $tests = array(
  296.             'class f implements not_existing_interface { }',
  297.             );
  298.         foreach ($tests as $code{
  299.             $this->shell->resetCode();
  300.             $this->shell->appendCode($code);
  301.  
  302.             ## we should get a Exception
  303.             try $this->execute()$this->fail()}
  304.             catch Exception $e }
  305.         }
  306.     }
  307.  
  308.     public function testAbstractClass({
  309.         $tests = array(
  310.             'abstract class g { abstract function f(); }',
  311.             '$f = new g()',
  312.             );
  313.         foreach ($tests as $code{
  314.             $this->shell->resetCode();
  315.             $this->shell->appendCode($code);
  316.  
  317.             ## we should get a Exception
  318.             try $this->execute()$this->fail()}
  319.             catch Exception $e }
  320.         }
  321.     }
  322.  
  323.     public function testStaticCall({
  324.         $tests = array(
  325.             'class h { static function foo() { } }',
  326.             'h::foo()'
  327.             );
  328.         foreach ($tests as $code{
  329.             $this->shell->resetCode();
  330.             $this->shell->appendCode($code);
  331.  
  332.             $this->execute();
  333.         }
  334.     }
  335.  
  336.     public function testStaticCallNotExists({
  337.         $tests = array(
  338.             'h::not_exists()'
  339.             );
  340.         foreach ($tests as $code{
  341.             $this->shell->resetCode();
  342.             $this->shell->appendCode($code);
  343.  
  344.             ## we should get a Exception
  345.             try $this->execute()$this->fail()}
  346.             catch Exception $e }
  347.         }
  348.     }
  349.  
  350.     public function testObjectArray({
  351.         $tests = array(
  352.             'class obj_array { function params($p1, $p2) { } }',
  353.             '$c = new ReflectionClass("obj_array")',
  354.             '$m = $c->getMethods()',
  355.             '$m[0]->getParameters()'
  356.             );
  357.         foreach ($tests as $code{
  358.             $this->shell->resetCode();
  359.             $this->shell->appendCode($code);
  360.  
  361.             $this->execute();
  362.         }
  363.     }
  364.  
  365.     public function testObjectArrayMethodNotExists({
  366.         $tests = array(
  367.             '$c = new ReflectionClass("obj_array")',
  368.             '$m = $c->getMethods()',
  369.             '$m[0]->getPrototyp()'
  370.             );
  371.         foreach ($tests as $code{
  372.             $this->shell->resetCode();
  373.             $this->shell->appendCode($code);
  374.  
  375.             ## we should get a Exception
  376.             try $this->execute()$this->fail()}
  377.             catch Exception $e }
  378.         }
  379.     }
  380.  
  381.     public function testVariableMethod({
  382.         $tests = array(
  383.             '$c = new obj_array()',
  384.             '$m = "params"',
  385.             '$c->$m(1, 2)'
  386.             );
  387.         foreach ($tests as $code{
  388.             $this->shell->resetCode();
  389.             $this->shell->appendCode($code);
  390.  
  391.             $this->execute();
  392.         }
  393.     }
  394.  
  395.     public function testVariableStaticMethod({
  396.         $tests = array(
  397.             '$m = "params"',
  398.             'obj_array::$m(1, 2)'
  399.             );
  400.         foreach ($tests as $code{
  401.             $this->shell->resetCode();
  402.             $this->shell->appendCode($code);
  403.  
  404.             $this->execute();
  405.         }
  406.     }
  407.  
  408.     public function testVariableMethodNotExisting({
  409.         $tests = array(
  410.             '$c = new obj_array()',
  411.             '$m = "foo"',
  412.             '$c->$m(1, 2)'
  413.             );
  414.         foreach ($tests as $code{
  415.             $this->shell->resetCode();
  416.             $this->shell->appendCode($code);
  417.  
  418.             ## we should get a Exception
  419.             try $this->execute()$this->fail()}
  420.             catch Exception $e }
  421.         }
  422.     }
  423.  
  424.     public function testInternalMethodCall({
  425.         $tests = array(
  426.             'class thisclass { function a() { } function b () { $this->a(); }}',
  427.             );
  428.         foreach ($tests as $code{
  429.             $this->shell->resetCode();
  430.             $this->shell->appendCode($code);
  431.  
  432.             $this->execute();
  433.         }
  434.     }
  435.  
  436.     public function testSingletonClass({
  437.         $tests = array(
  438.             'class singleton { '.
  439.             '  static private $inst = null; '.
  440.             '  static function getInstance() { '.
  441.             '    if (is_null(self::$inst)) { '.
  442.             '       self::$inst = new singleton(); '.
  443.             '    } '.
  444.             '    return self::$inst; '.
  445.             '  } '.
  446.             '  public function get() { return 1; }'.
  447.             '}',
  448.             );
  449.         foreach ($tests as $code{
  450.             $this->shell->resetCode();
  451.             $this->shell->appendCode($code);
  452.  
  453.             $this->execute();
  454.         }
  455.     }
  456.  
  457.     public function testGetInstance({
  458.         $tests = array(
  459.             'singleton::getInstance()->get()'
  460.             );
  461.         foreach ($tests as $code{
  462.             $this->shell->resetCode();
  463.             $this->shell->appendCode($code);
  464.  
  465.             $this->execute();
  466.         }
  467.     }
  468.  
  469.     public function testArrayAccessOnObject({
  470.         $tests = array(
  471.             '$a = new stdClass()',
  472.             '$a[0]'
  473.             );
  474.         foreach ($tests as $code{
  475.             $this->shell->resetCode();
  476.             $this->shell->appendCode($code);
  477.  
  478.             ## we should get a Exception
  479.             try $this->execute()$this->fail()}
  480.             catch Exception $e }
  481.         }
  482.     }
  483.  
  484.     public function testFunctionVars({
  485.         $tests = array(
  486.             'function '.__FUNCTION__.'($a) { print $a; }'
  487.             );
  488.         foreach ($tests as $code{
  489.             $this->shell->resetCode();
  490.             $this->shell->appendCode($code);
  491.  
  492.             $this->execute();
  493.         }
  494.     }
  495.  
  496.     public function testMethodVars({
  497.         $tests = array(
  498.             'class '.__FUNCTION__.' { function f2($a) { print $a; } }'
  499.             );
  500.         foreach ($tests as $code{
  501.             $this->shell->resetCode();
  502.             $this->shell->appendCode($code);
  503.  
  504.             $this->execute();
  505.         }
  506.     }
  507.  
  508.     public function testMethodDynamicConst({
  509.         $tests = array(
  510.             'class '.__FUNCTION__.' { function f2($a) { print $a->foo; } }'
  511.             );
  512.         foreach ($tests as $code{
  513.             $this->shell->resetCode();
  514.             $this->shell->appendCode($code);
  515.  
  516.             $this->execute();
  517.         }
  518.     }
  519.  
  520.     public function testMethodDynamicFunction({
  521.         $tests = array(
  522.             'class '.__FUNCTION__.' { function f2($a) { print $a->foo(); } }'
  523.             );
  524.         foreach ($tests as $code{
  525.             $this->shell->resetCode();
  526.             $this->shell->appendCode($code);
  527.  
  528.             $this->execute();
  529.         }
  530.     }
  531.     public function testMethodDynamicFunctionDynamic({
  532.         $tests = array(
  533.             'class '.__FUNCTION__.' { function f2($a) { print $a->$foo(); } }'
  534.             );
  535.         foreach ($tests as $code{
  536.             $this->shell->resetCode();
  537.             $this->shell->appendCode($code);
  538.  
  539.             $this->execute();
  540.         }
  541.     }
  542.  
  543.  
  544. }

Documentation generated on Mon, 11 Mar 2019 14:40:36 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.