Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
namespace Ratchet\Http;
3
use Ratchet\WebSocket\WsServerInterface;
4
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
5
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
6
use Symfony\Component\Routing\RequestContext;
7
use Symfony\Component\Routing\RouteCollection;
8
use Symfony\Component\Routing\Matcher\UrlMatcher;
9
 
10
 
11
/**
12
 * @covers Ratchet\Http\Router
13
 */
14
class RouterTest extends \PHPUnit_Framework_TestCase {
15
    protected $_router;
16
    protected $_matcher;
17
    protected $_conn;
18
    protected $_uri;
19
    protected $_req;
20
 
21
    public function setUp() {
22
        $this->_conn = $this->getMock('\Ratchet\ConnectionInterface');
23
        $this->_uri  = $this->getMock('Psr\Http\Message\UriInterface');
24
        $this->_req  = $this->getMock('\Psr\Http\Message\RequestInterface');
25
        $this->_req
26
            ->expects($this->any())
27
            ->method('getUri')
28
            ->will($this->returnValue($this->_uri));
29
        $this->_matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
30
        $this->_matcher
31
            ->expects($this->any())
32
            ->method('getContext')
33
            ->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')));
34
        $this->_router  = new Router($this->_matcher);
35
 
36
        $this->_uri->expects($this->any())->method('getPath')->will($this->returnValue('ws://doesnt.matter/'));
37
        $this->_uri->expects($this->any())->method('withQuery')->with($this->callback(function($val) {
38
            $this->setResult($val);
39
 
40
            return true;
41
        }))->will($this->returnSelf());
42
        $this->_uri->expects($this->any())->method('getQuery')->will($this->returnCallback([$this, 'getResult']));
43
        $this->_req->expects($this->any())->method('withUri')->will($this->returnSelf());
44
    }
45
 
46
    public function testFourOhFour() {
47
        $this->_conn->expects($this->once())->method('close');
48
 
49
        $nope = new ResourceNotFoundException;
50
        $this->_matcher->expects($this->any())->method('match')->will($this->throwException($nope));
51
 
52
        $this->_router->onOpen($this->_conn, $this->_req);
53
    }
54
 
55
    public function testNullRequest() {
56
        $this->setExpectedException('\UnexpectedValueException');
57
        $this->_router->onOpen($this->_conn);
58
    }
59
 
60
    public function testControllerIsMessageComponentInterface() {
61
        $this->setExpectedException('\UnexpectedValueException');
62
        $this->_matcher->expects($this->any())->method('match')->will($this->returnValue(array('_controller' => new \StdClass)));
63
        $this->_router->onOpen($this->_conn, $this->_req);
64
    }
65
 
66
    public function testControllerOnOpen() {
67
        $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock();
68
        $this->_matcher->expects($this->any())->method('match')->will($this->returnValue(array('_controller' => $controller)));
69
        $this->_router->onOpen($this->_conn, $this->_req);
70
 
71
        $expectedConn = new \PHPUnit_Framework_Constraint_IsInstanceOf('\Ratchet\ConnectionInterface');
72
        $controller->expects($this->once())->method('onOpen')->with($expectedConn, $this->_req);
73
 
74
        $this->_matcher->expects($this->any())->method('match')->will($this->returnValue(array('_controller' => $controller)));
75
        $this->_router->onOpen($this->_conn, $this->_req);
76
    }
77
 
78
    public function testControllerOnMessageBubbles() {
79
        $message = "The greatest trick the Devil ever pulled was convincing the world he didn't exist";
80
        $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock();
81
        $controller->expects($this->once())->method('onMessage')->with($this->_conn, $message);
82
 
83
        $this->_conn->controller = $controller;
84
 
85
        $this->_router->onMessage($this->_conn, $message);
86
    }
87
 
88
    public function testControllerOnCloseBubbles() {
89
        $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock();
90
        $controller->expects($this->once())->method('onClose')->with($this->_conn);
91
 
92
        $this->_conn->controller = $controller;
93
 
94
        $this->_router->onClose($this->_conn);
95
    }
96
 
97
    public function testControllerOnErrorBubbles() {
98
        $e= new \Exception('One cannot be betrayed if one has no exceptions');
99
        $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock();
100
        $controller->expects($this->once())->method('onError')->with($this->_conn, $e);
101
 
102
        $this->_conn->controller = $controller;
103
 
104
        $this->_router->onError($this->_conn, $e);
105
    }
106
 
107
    public function testRouterGeneratesRouteParameters() {
108
        /** @var $controller WsServerInterface */
109
        $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock();
110
        /** @var $matcher UrlMatcherInterface */
111
        $this->_matcher->expects($this->any())->method('match')->will(
112
            $this->returnValue(['_controller' => $controller, 'foo' => 'bar', 'baz' => 'qux'])
113
        );
114
        $conn = $this->getMock('Ratchet\Mock\Connection');
115
 
116
        $router = new Router($this->_matcher);
117
 
118
        $router->onOpen($conn, $this->_req);
119
 
120
        $this->assertEquals('foo=bar&baz=qux', $this->_req->getUri()->getQuery());
121
    }
122
 
123
    public function testQueryParams() {
124
        $controller = $this->getMockBuilder('\Ratchet\WebSocket\WsServer')->disableOriginalConstructor()->getMock();
125
        $this->_matcher->expects($this->any())->method('match')->will(
126
            $this->returnValue(['_controller' => $controller, 'foo' => 'bar', 'baz' => 'qux'])
127
        );
128
 
129
        $conn    = $this->getMock('Ratchet\Mock\Connection');
130
        $request = $this->getMock('Psr\Http\Message\RequestInterface');
131
        $uri = new \GuzzleHttp\Psr7\Uri('ws://doesnt.matter/endpoint?hello=world&foo=nope');
132
 
133
        $request->expects($this->any())->method('getUri')->will($this->returnCallback(function() use (&$uri) {
134
            return $uri;
135
        }));
136
        $request->expects($this->any())->method('withUri')->with($this->callback(function($url) use (&$uri) {
137
            $uri = $url;
138
 
139
            return true;
140
        }))->will($this->returnSelf());
141
 
142
        $router = new Router($this->_matcher);
143
        $router->onOpen($conn, $request);
144
 
145
        $this->assertEquals('foo=nope&baz=qux&hello=world', $request->getUri()->getQuery());
146
        $this->assertEquals('ws', $request->getUri()->getScheme());
147
        $this->assertEquals('doesnt.matter', $request->getUri()->getHost());
148
    }
149
 
150
    public function testImpatientClientOverflow() {
151
        $this->_conn->expects($this->once())->method('close');
152
 
153
        $header = "GET /nope HTTP/1.1
154
Upgrade: websocket                                   
155
Connection: upgrade                                  
156
Host: localhost                                 
157
Origin: http://localhost                        
158
Sec-WebSocket-Version: 13\r\n\r\n";
159
 
160
        $app = new HttpServer(new Router(new UrlMatcher(new RouteCollection, new RequestContext)));
161
        $app->onOpen($this->_conn);
162
        $app->onMessage($this->_conn, $header);
163
        $app->onMessage($this->_conn, 'Silly body');
164
    }
165
}