Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
namespace Ratchet\Session;
3
use Ratchet\AbstractMessageComponentTestCase;
4
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
5
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
6
 
7
/**
8
 * @covers Ratchet\Session\SessionProvider
9
 * @covers Ratchet\Session\Storage\VirtualSessionStorage
10
 * @covers Ratchet\Session\Storage\Proxy\VirtualProxy
11
 */
12
class SessionProviderTest extends AbstractMessageComponentTestCase {
13
    public function setUp() {
14
        if (!class_exists('Symfony\Component\HttpFoundation\Session\Session')) {
15
            return $this->markTestSkipped('Dependency of Symfony HttpFoundation failed');
16
        }
17
 
18
        parent::setUp();
19
        $this->_serv = new SessionProvider($this->_app, new NullSessionHandler);
20
    }
21
 
22
    public function tearDown() {
23
        ini_set('session.serialize_handler', 'php');
24
    }
25
 
26
    public function getConnectionClassString() {
27
        return '\Ratchet\ConnectionInterface';
28
    }
29
 
30
    public function getDecoratorClassString() {
31
        return '\Ratchet\NullComponent';
32
    }
33
 
34
    public function getComponentClassString() {
35
        return '\Ratchet\Http\HttpServerInterface';
36
    }
37
 
38
    public function classCaseProvider() {
39
        return array(
40
            array('php', 'Php')
41
          , array('php_binary', 'PhpBinary')
42
        );
43
    }
44
 
45
    /**
46
     * @dataProvider classCaseProvider
47
     */
48
    public function testToClassCase($in, $out) {
49
        $ref = new \ReflectionClass('\\Ratchet\\Session\\SessionProvider');
50
        $method = $ref->getMethod('toClassCase');
51
        $method->setAccessible(true);
52
 
53
        $component = new SessionProvider($this->getMock($this->getComponentClassString()), $this->getMock('\SessionHandlerInterface'));
54
        $this->assertEquals($out, $method->invokeArgs($component, array($in)));
55
    }
56
 
57
    /**
58
     * I think I have severely butchered this test...it's not so much of a unit test as it is a full-fledged component test
59
     */
60
    public function testConnectionValueFromPdo() {
61
        if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
62
            return $this->markTestSkipped('Session test requires PDO and pdo_sqlite');
63
        }
64
 
65
        $sessionId = md5('testSession');
66
 
67
        $dbOptions = array(
68
            'db_table'    => 'sessions'
69
          , 'db_id_col'   => 'sess_id'
70
          , 'db_data_col' => 'sess_data'
71
          , 'db_time_col' => 'sess_time'
72
          , 'db_lifetime_col' => 'sess_lifetime'
73
        );
74
 
75
        $pdo = new \PDO("sqlite::memory:");
76
        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
77
        $pdo->exec(vsprintf("CREATE TABLE %s (%s TEXT NOT NULL PRIMARY KEY, %s BLOB NOT NULL, %s INTEGER NOT NULL, %s INTEGER)", $dbOptions));
78
 
79
        $pdoHandler = new PdoSessionHandler($pdo, $dbOptions);
80
        $pdoHandler->write($sessionId, '_sf2_attributes|a:2:{s:5:"hello";s:5:"world";s:4:"last";i:1332872102;}_sf2_flashes|a:0:{}');
81
 
82
        $component  = new SessionProvider($this->getMock($this->getComponentClassString()), $pdoHandler, array('auto_start' => 1));
83
        $connection = $this->getMock('Ratchet\\ConnectionInterface');
84
 
85
        $headers = $this->getMock('Psr\Http\Message\RequestInterface');
86
        $headers->expects($this->once())->method('getHeader')->will($this->returnValue([ini_get('session.name') . "={$sessionId};"]));
87
 
88
        $component->onOpen($connection, $headers);
89
 
90
        $this->assertEquals('world', $connection->Session->get('hello'));
91
    }
92
 
93
    protected function newConn() {
94
        $conn = $this->getMock('Ratchet\ConnectionInterface');
95
 
96
        $headers = $this->getMock('Psr\Http\Message\Request', array('getCookie'), array('POST', '/', array()));
97
        $headers->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue(null));
98
 
99
        return $conn;
100
    }
101
 
102
    public function testOnMessageDecorator() {
103
        $message = "Database calls are usually blocking  :(";
104
        $this->_app->expects($this->once())->method('onMessage')->with($this->isExpectedConnection(), $message);
105
        $this->_serv->onMessage($this->_conn, $message);
106
    }
107
 
108
    public function testRejectInvalidSeralizers() {
109
        if (!function_exists('wddx_serialize_value')) {
110
            $this->markTestSkipped();
111
        }
112
 
113
        ini_set('session.serialize_handler', 'wddx');
114
        $this->setExpectedException('\RuntimeException');
115
        new SessionProvider($this->getMock($this->getComponentClassString()), $this->getMock('\SessionHandlerInterface'));
116
    }
117
 
118
    protected function doOpen($conn) {
119
        $request = $this->getMock('Psr\Http\Message\RequestInterface');
120
        $request->expects($this->any())->method('getHeader')->will($this->returnValue([]));
121
 
122
        $this->_serv->onOpen($conn, $request);
123
    }
124
}