SessionComponentTest.php 4.8 KB

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