/var/www/hkosl.com/imusiccircle/webadmin/libraies/php-console/php-console/tests/Test/EvalProvider.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace PhpConsole\Test;

class 
EvalProvider extends Test {

    
/** @var  \PhpConsole\EvalProvider */
    
protected $evalProvider;

    protected function 
setUp() {
        
parent::setUp();
        
$this->evalProvider = new \PhpConsole\EvalProvider();
    }

    public static function 
provideEvalCodeAndExpectedReturns() {
        return array(
            
// forceEndingSemicolon
            
array('return 123'123),
            array(
'return 123;'123),
            array(
'if(true) { return 123; }'123),
            
// trimPhpTags
            
array('<?php return 123; ?>'123),
            array(
'<?PHP return 123; ?>'123),
            array(
'<? return 123; ?>'123),
            array(
'<? return 123;'123),
            array(
'<?= 123 ?>'null123),
            array(
'echo 3; ?>2<?= 1 ?><?php return 123; '123321),
            
// output
            
array('echo 123'null123),
            array(
'print_r(array(1,2))'nullprint_r(array(12), true)),
        );
    }

    
/**
     * @dataProvider provideEvalCodeAndExpectedReturns
     */
    
public function testEvaluate($code$expectedReturn$expectedOutput '') {
        
$result $this->evalProvider->evaluate($code);
        
$this->assertEquals($expectedReturn$result->return);
        
$this->assertEquals($expectedOutput$result->output);
        
$this->assertNull($result->exception);
    }

    public function 
testEvalExecuteMethodIsStatic() {
        
$this->assertFalse($this->evalProvider->evaluate('return isset($this)')->return);
    }

    public function 
testTimeInResult() {
        
$time $this->evalProvider->evaluate('usleep(10000)')->time;
        
$this->assertTrue($time && $time 1);
    }

    public function 
testSharedVarOverwritesGlobal() {
        
$_POST = array(123);
        
$this->evalProvider->addSharedVar('_POST', array(321));
        
$this->assertEquals(321$this->evalProvider->evaluate('return $_POST[0]')->return);
    }

    public function 
testGlobalVarsBackup() {
        
$_POST = array(123);
        
$this->evalProvider->evaluate('$_POST = array(321)');
        
$this->assertEquals(array(123), $_POST);
    }

    public function 
testAddSharedVar() {
        
$this->evalProvider->addSharedVar('asd'123);
        
$this->assertEquals(123$this->evalProvider->evaluate('return $asd')->return);
    }

    
/**
     * @expectedException \Exception
     */
    
public function testAddSameSharedVarThrowsException() {
        
$this->evalProvider->addSharedVar('asd'123);
        
$this->evalProvider->addSharedVar('asd'123);
    }

    public function 
testAddSharedVarReference() {
        
$var 123;
        
$this->evalProvider->addSharedVarReference('asd'$var);
        
$var 321;
        
$this->assertEquals(321$this->evalProvider->evaluate('return $asd')->return);
    }

    
/**
     * @expectedException \Exception
     */
    
public function testAddSameSharedVarReferenceThrowsException() {
        
$var 123;
        
$this->evalProvider->addSharedVar('asd'$var);
        
$this->evalProvider->addSharedVarReference('asd'$var);
    }

    public function 
testAddCodeHandler() {
        
$this->evalProvider->addCodeHandler(function (&$code) {
            
$code 'return 321';
        });
        
$this->assertEquals(321$this->evalProvider->evaluate('return 123')->return);
    }

    
/**
     * @expectedException \Exception
     */
    
public function testAddNotCallableCodeHandlerThrowsException() {
        
$this->evalProvider->addCodeHandler(123);
    }

    public function 
testThrowedInCodeExceptionIsCaught() {
        
$result $this->evalProvider->evaluate('throw new \Exception(123)');
        
$this->assertEquals(123$result->exception->getMessage());
    }
}