/var/www/hkosl.com/imusiccircle/webadmin/libraies/php-console/php-console/tests/Test/Dispatcher/Eval.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace PhpConsole\Test;

class 
Dispatcher_Evaluate extends Dispatcher {

    
/** @var  \PhpConsole\Dispatcher\Evaluate */
    
protected $dispatcher;

    protected function 
initDispatcher(\PhpConsole\Connector $connector) {
        return new \
PhpConsole\Dispatcher\Evaluate($connector, new \PhpConsole\EvalProvider(), new \PhpConsole\Dumper());
    }

    public function 
testDispatchMessageIsSent() {
        
$this->connector->expects($this->once())->method('sendMessage');
        
$this->dispatcher->dispatchCode('return 123');
    }

    public function 
testDispatchIgnoredOnNotActiveClient() {
        
$this->isDispatcherActive false;
        
$this->connector->expects($this->never())->method('sendMessage');
        
$this->dispatcher->dispatchCode('return 123');
    }

    public function 
testDispatchMessageData() {
        
$test $this;
        
$this->connector->expects($this->once())
            ->
method('sendMessage')
            ->
with($this->callback(function (\PhpConsole\EvalResultMessage $message) use ($test) {
                
$test->assertContainsRecursive(array(
                    
'type' => 'eval_result',
                    
'return' => 321,
                    
'output' => 123,
                ), 
$message);
                
$test->assertTrue($message->time && $message->time 1);
                return 
true;
            }));

        
$this->dispatcher->dispatchCode('echo 123; usleep(1000); return 321;');
    }

    public function 
testDispatchDataIsDumped() {
        
$dumper = new \PhpConsole\Dumper(1110);
        
$this->dispatcher->setDumper($dumper);

        
$actualString str_repeat('x'$dumper->itemSizeLimit 10);
        
$dumpedString $dumper->dump($actualString);
        
$this->assertTrue(strlen($dumpedString) < strlen($actualString));

        
$test $this;
        
$this->connector->expects($this->once())
            ->
method('sendMessage')
            ->
with($this->callback(function (\PhpConsole\EvalResultMessage $message) use ($test$dumpedString) {
                
$test->assertEquals($dumpedString$message->return);
                
$test->assertEquals($dumpedString$message->output);
                return 
true;
            }));

        
$this->dispatcher->dispatchCode('echo "' $actualString '"; return "' $actualString '";');
    }

    public function 
testGetEvalProvider() {
        
$this->assertInstanceOf('\PhpConsole\EvalProvider'$this->dispatcher->getEvalProvider());
    }

    public function 
testSetEvalProvider() {
        
$evalProvider = new \PhpConsole\EvalProvider();
        
$this->dispatcher->setEvalProvider($evalProvider);
        
$this->assertEquals(spl_object_hash($evalProvider), spl_object_hash($this->dispatcher->getEvalProvider()));
    }

    public function 
testEvalErrorIsDispatched() {
        
$test $this;

        
$this->connector->expects($this->at(2))
            ->
method('sendMessage')
            ->
with($this->callback(function ($message) use ($test) {
                
$test->assertContainsRecursive(array(
                    
'type' => 'error',
                    
'code' => E_PARSE,
                    
'line' => 1,
                ), 
$message);
                return 
true;
            }));

        
$this->connector->expects($this->at(3))
            ->
method('sendMessage')
            ->
with($this->callback(function (\PhpConsole\Message $message) use ($test) {
                
$test->assertContainsRecursive(array(
                    
'type' => 'eval_result',
                    
'return' => null,
                    
'output' => null,
                ), 
$message);
                return 
true;
            }));

        
$this->dispatcher->dispatchCode('if(');
    }

    public function 
testEvalExceptionIsDispatched() {
        
$test $this;

        
$this->connector->expects($this->at(2))
            ->
method('sendMessage')
            ->
with($this->callback(function (\PhpConsole\Message $message) use ($test) {
                
$test->assertContainsRecursive(array(
                    
'type' => 'error',
                    
'class' => 'Exception',
                    
'data' => 321,
                    
'line' => 1,
                ), 
$message);
                return 
true;
            }));

        
$this->connector->expects($this->at(3))
            ->
method('sendMessage')
            ->
with($this->callback(function (\PhpConsole\Message $message) use ($test) {
                
$test->assertContainsRecursive(array(
                    
'type' => 'eval_result',
                    
'return' => null,
                    
'output' => 123,
                ), 
$message);
                return 
true;
            }));

        
$this->dispatcher->dispatchCode('echo 123; throw new Exception(321)');
    }
}