Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump PHP7 version and PHPUnit #773

Merged
merged 6 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

dist: trusty

before_script:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "session.serialize_handler = php" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;'
- php -m
- composer install --dev --prefer-source
- composer install --prefer-source
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CHANGELOG
* "BF": Bug fix

---
* 0.5.0
* PHP 7.2 is minimum supported version
* BC: `Ratchet\Session\Storage\Proxy\VirtualSessionStorage` requires `Ratchet\Session\OptionsHandler`

* 0.4.1 (2017-12-11)
* Only enableKeepAlive in App if no WsServer passed allowing user to set their own timeout duration
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
"Ratchet\\": "src/Ratchet"
}
}
, "autoload-dev": {
"psr-4": {
"Ratchet\\Tests\\": "tests/"
}
}
, "require": {
"php": ">=5.4.2"
"php": "^7.2"
, "ratchet/rfc6455": "^0.2"
, "react/socket": "^1.0 || ^0.8 || ^0.7 || ^0.6 || ^0.5"
, "guzzlehttp/psr7": "^1.0"
, "symfony/http-foundation": "^2.6|^3.0|^4.0|^5.0"
, "symfony/routing": "^2.6|^3.0|^4.0|^5.0"
}
, "require-dev": {
"phpunit/phpunit": "~4.8"
"phpunit/phpunit": "^8.5"
}
}
11 changes: 3 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
forceCoversAnnotation="true"
mapTestClassNameToCoveredClassName="true"
bootstrap="tests/bootstrap.php"
colors="true"
backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false"
stopOnError="false"
>

<testsuites>
<testsuite name="unit">
<directory>./tests/unit/</directory>
</testsuite>
</testsuites>

<testsuites>
<testsuite name="integration">
<directory>./tests/integration/</directory>
</testsuite>
Expand All @@ -27,4 +22,4 @@
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
19 changes: 19 additions & 0 deletions src/Ratchet/Session/IniOptionsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Ratchet\Session;

use function ini_get;
use function ini_set;

final class IniOptionsHandler implements OptionsHandlerInterface
{
public function get(string $name) : string
{
return ini_get($name);
}

public function set(string $name, $value) : void
{
ini_set($name, $value);
}
}
13 changes: 13 additions & 0 deletions src/Ratchet/Session/OptionsHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Ratchet\Session;

interface OptionsHandlerInterface
{
public function get(string $name) : string;

/**
* @param mixed $value
*/
public function set(string $name, $value) : void;
}
45 changes: 26 additions & 19 deletions src/Ratchet/Session/SessionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServerInterface;
use Psr\Http\Message\RequestInterface;
use Ratchet\Session\OptionsHandlerInterface;
use Ratchet\Session\IniOptionsHandler;
use Ratchet\Session\Storage\VirtualSessionStorage;
use Ratchet\Session\Serialize\HandlerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
Expand Down Expand Up @@ -37,26 +39,31 @@ class SessionProvider implements HttpServerInterface {
*/
protected $_serializer;

/** @var OptionsHandlerInterface */
private $optionsHandler;

/**
* @param \Ratchet\Http\HttpServerInterface $app
* @param \SessionHandlerInterface $handler
* @param array $options
* @param \Ratchet\Session\Serialize\HandlerInterface $serializer
* @param array<string, mixed> $options
* @throws \RuntimeException
*/
public function __construct(HttpServerInterface $app, \SessionHandlerInterface $handler, array $options = array(), HandlerInterface $serializer = null) {
$this->_app = $app;
$this->_handler = $handler;
$this->_null = new NullSessionHandler;
public function __construct(HttpServerInterface $app, \SessionHandlerInterface $handler, array $options = array(), HandlerInterface $serializer = null, ?OptionsHandlerInterface $optionsHandler = null) {
$this->_app = $app;
$this->_handler = $handler;
$this->_null = new NullSessionHandler;
$this->optionsHandler = $optionsHandler;

if($optionsHandler === null){
$optionsHandler = new IniOptionsHandler();
}

ini_set('session.auto_start', 0);
ini_set('session.cache_limiter', '');
ini_set('session.use_cookies', 0);
$optionsHandler->set('session.auto_start', 0);
$optionsHandler->set('session.cache_limiter', '');
$optionsHandler->set('session.use_cookies', 0);

$this->setOptions($options);

if (null === $serializer) {
$serialClass = __NAMESPACE__ . "\\Serialize\\{$this->toClassCase(ini_get('session.serialize_handler'))}Handler"; // awesome/terrible hack, eh?
$serialClass = __NAMESPACE__ . "\\Serialize\\{$this->toClassCase($optionsHandler->get('session.serialize_handler'))}Handler"; // awesome/terrible hack, eh?
if (!class_exists($serialClass)) {
throw new \RuntimeException('Unable to parse session serialize handler');
}
Expand All @@ -71,7 +78,7 @@ public function __construct(HttpServerInterface $app, \SessionHandlerInterface $
* {@inheritdoc}
*/
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) {
$sessionName = ini_get('session.name');
$sessionName = $this->optionsHandler->get('session.name');

$id = array_reduce($request->getHeader('Cookie'), function($accumulator, $cookie) use ($sessionName) {
if ($accumulator) {
Expand All @@ -90,9 +97,9 @@ public function onOpen(ConnectionInterface $conn, RequestInterface $request = nu
$saveHandler = $this->_handler;
}

$conn->Session = new Session(new VirtualSessionStorage($saveHandler, $id, $this->_serializer));
$conn->Session = new Session(new VirtualSessionStorage($saveHandler, $id, $this->_serializer, $this->optionsHandler));

if (ini_get('session.auto_start')) {
if ($this->optionsHandler->get('session.auto_start')) {
$conn->Session->start();
}

Expand Down Expand Up @@ -125,8 +132,8 @@ function onError(ConnectionInterface $conn, \Exception $e) {
/**
* Set all the php session. ini options
* © Symfony
* @param array $options
* @return array
* @param array<string, mixed> $options
* @return array<string, mixed>
*/
protected function setOptions(array $options) {
$all = array(
Expand All @@ -143,9 +150,9 @@ protected function setOptions(array $options) {

foreach ($all as $key) {
if (!array_key_exists($key, $options)) {
$options[$key] = ini_get("session.{$key}");
$options[$key] = $this->optionsHandler->get("session.{$key}");
} else {
ini_set("session.{$key}", $options[$key]);
$this->optionsHandler->set("session.{$key}", $options[$key]);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Ratchet/Session/Storage/Proxy/VirtualProxy.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
namespace Ratchet\Session\Storage\Proxy;

use Ratchet\Session\OptionsHandlerInterface;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;

class VirtualProxy extends SessionHandlerProxy {
Expand All @@ -16,11 +18,11 @@ class VirtualProxy extends SessionHandlerProxy {
/**
* {@inheritdoc}
*/
public function __construct(\SessionHandlerInterface $handler) {
public function __construct(\SessionHandlerInterface $handler, OptionsHandlerInterface $optionsHandler) {
parent::__construct($handler);

$this->saveHandlerName = 'user';
$this->_sessionName = ini_get('session.name');
$this->_sessionName = $optionsHandler->get('session.name');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Ratchet/Session/Storage/VirtualSessionStorage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace Ratchet\Session\Storage;
use Ratchet\Session\OptionsHandlerInterface;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Ratchet\Session\Storage\Proxy\VirtualProxy;
use Ratchet\Session\Serialize\HandlerInterface;
Expand All @@ -10,12 +11,16 @@ class VirtualSessionStorage extends NativeSessionStorage {
*/
protected $_serializer;

/** @var OptionsHandlerInterface */
private $optionsHandler;

/**
* @param \SessionHandlerInterface $handler
* @param string $sessionId The ID of the session to retrieve
* @param \Ratchet\Session\Serialize\HandlerInterface $serializer
*/
public function __construct(\SessionHandlerInterface $handler, $sessionId, HandlerInterface $serializer) {
public function __construct(\SessionHandlerInterface $handler, $sessionId, HandlerInterface $serializer, OptionsHandlerInterface $optionsHandler) {
$this->optionsHandler = $optionsHandler;
$this->setSaveHandler($handler);
$this->saveHandler->setId($sessionId);
$this->_serializer = $serializer;
Expand Down Expand Up @@ -80,7 +85,7 @@ public function setSaveHandler($saveHandler = null) {
}

if (!($saveHandler instanceof VirtualProxy)) {
$saveHandler = new VirtualProxy($saveHandler);
$saveHandler = new VirtualProxy($saveHandler, $this->optionsHandler);
}

$this->saveHandler = $saveHandler;
Expand Down
5 changes: 3 additions & 2 deletions src/Ratchet/Wamp/WampConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Ratchet\ConnectionInterface;
use Ratchet\AbstractConnectionDecorator;
use Ratchet\Wamp\ServerProtocol as WAMP;
use stdClass;

/**
* A ConnectionInterface object wrapper that is passed to your WAMP application
Expand All @@ -16,7 +17,7 @@ class WampConnection extends AbstractConnectionDecorator {
public function __construct(ConnectionInterface $conn) {
parent::__construct($conn);

$this->WAMP = new \StdClass;
$this->WAMP = new stdClass;
$this->WAMP->sessionId = str_replace('.', '', uniqid(mt_rand(), true));
$this->WAMP->prefixes = array();

Expand Down Expand Up @@ -85,7 +86,7 @@ public function getUri($uri) {

if (preg_match('/http(s*)\:\/\//', $uri) == false) {
if (strpos($uri, $curieSeperator) !== false) {
list($prefix, $action) = explode($curieSeperator, $uri);
[$prefix, $action] = explode($curieSeperator, $uri);

if(isset($this->WAMP->prefixes[$prefix]) === true){
return $this->WAMP->prefixes[$prefix] . '#' . $action;
Expand Down
2 changes: 1 addition & 1 deletion src/Ratchet/WebSocket/WsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* {@inheritdoc}
* @property \StdClass $WebSocket
* @property \stdClass $WebSocket
*/
class WsConnection extends AbstractConnectionDecorator {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Ratchet/WebSocket/WsServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function onOpen(ConnectionInterface $conn, RequestInterface $request = nu

$conn->httpRequest = $request;

$conn->WebSocket = new \StdClass;
$conn->WebSocket = new \stdClass;
$conn->WebSocket->closing = false;

$response = $this->handshakeNegotiator->handshake($request)->withHeader('X-Powered-By', \Ratchet\VERSION);
Expand Down
29 changes: 29 additions & 0 deletions tests/Session/InMemoryOptionsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Ratchet\Tests\Session;

use Ratchet\Session\OptionsHandlerInterface;

final class InMemoryOptionsHandler implements OptionsHandlerInterface
{
/** @var array<string, mixed> */
private $options;

/**
* @param array<string, mixed> $options
*/
public function __construct(array $options = [])
{
$this->options = $options;
}

public function get(string $name) : string
{
return $this->options[$name] ?? '';
}

public function set(string $name, $value) : void
{
$this->options[$name] = $value;
}
}
17 changes: 11 additions & 6 deletions tests/helpers/Ratchet/AbstractMessageComponentTestCase.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace Ratchet;

abstract class AbstractMessageComponentTestCase extends \PHPUnit_Framework_TestCase {
use PHPUnit\Framework\Constraint\IsInstanceOf;
use PHPUnit\Framework\TestCase;

abstract class AbstractMessageComponentTestCase extends TestCase {
protected $_app;
protected $_serv;
protected $_conn;
Expand All @@ -10,26 +13,28 @@ abstract public function getConnectionClassString();
abstract public function getDecoratorClassString();
abstract public function getComponentClassString();

public function setUp() {
$this->_app = $this->getMock($this->getComponentClassString());
public function setUp() : void {
$this->_app = $this->createMock($this->getComponentClassString());
$decorator = $this->getDecoratorClassString();
$this->_serv = new $decorator($this->_app);
$this->_conn = $this->getMock('\Ratchet\ConnectionInterface');
$this->_conn = $this->createMock('\Ratchet\ConnectionInterface');

$this->doOpen($this->_conn);

parent::setUp();
}

protected function doOpen($conn) {
$this->_serv->onOpen($conn);
}

public function isExpectedConnection() {
return new \PHPUnit_Framework_Constraint_IsInstanceOf($this->getConnectionClassString());
return new IsInstanceOf($this->getConnectionClassString());
}

public function testOpen() {
$this->_app->expects($this->once())->method('onOpen')->with($this->isExpectedConnection());
$this->doOpen($this->getMock('\Ratchet\ConnectionInterface'));
$this->doOpen($this->createMock('\Ratchet\ConnectionInterface'));
}

public function testOnClose() {
Expand Down
Loading