diff --git a/.travis.yml b/.travis.yml index f6eefa5..203f6db 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,11 +8,14 @@ php: - 5.6 - 7.0 - 7.1 + - 7.2 + - nightly - hhvm matrix: fast_finish: true allow_failures: + - php: nightly - php: hhvm before_install: @@ -25,7 +28,7 @@ before_install: echo "extension = memcache.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini fi # Remove PHP CS Fixer for PHP 5.5 since it requires PHP 5.6 - - if [[ $TRAVIS_PHP_VERSION == "5.5" || $TRAVIS_PHP_VERSION == "hhvm" ]]; then composer remove friendsofphp/php-cs-fixer --dev --no-update; fi + - if [[ $TRAVIS_PHP_VERSION == "5.5" || $TRAVIS_PHP_VERSION == "nightly" || $TRAVIS_PHP_VERSION == "hhvm" ]]; then composer remove friendsofphp/php-cs-fixer --dev --no-update; fi before_script: - composer self-update || true @@ -37,7 +40,7 @@ script: # PHP CodeSniffer - vendor/bin/phpcs -p --report=summary # PHP CS Fixer (if >=5.6) - - if [[ $TRAVIS_PHP_VERSION != "5.5" && $TRAVIS_PHP_VERSION != "hhvm" ]]; then vendor/bin/php-cs-fixer fix --dry-run; fi + - if [[ $TRAVIS_PHP_VERSION != "5.5" && $TRAVIS_PHP_VERSION != "nightly" && $TRAVIS_PHP_VERSION != "hhvm" ]]; then vendor/bin/php-cs-fixer fix --dry-run; fi after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/composer.json b/composer.json index a2ded3a..064215e 100644 --- a/composer.json +++ b/composer.json @@ -15,23 +15,24 @@ "require": { "bolt/common": "^1.0", "php": "^5.5 || ^7.0", - "symfony/event-dispatcher": "^2.8 || ^3.0", - "symfony/http-foundation": "^2.8 || ^3.0", - "symfony/http-kernel": "^2.8 || ^3.0", + "symfony/event-dispatcher": "^2.8 || ^3.0 || ^4.0", + "symfony/http-foundation": "^2.8 || ^3.0 || ^4.0", + "symfony/http-kernel": "^2.8 || ^3.0 || ^4.0", "symfony/polyfill-php70": "^1.0" }, "require-dev": { "bolt/codingstyle": "^2.0@dev", "bolt/filesystem": "^2.0", - "escapestudios/symfony2-coding-standard": "^3.0@dev", - "friendsofphp/php-cs-fixer": "^2.4", + "escapestudios/symfony2-coding-standard": "^3.1", + "friendsofphp/php-cs-fixer": "^2.10", "mikey179/vfsstream": "^1.6", - "phpunit/phpunit": "^4.8", + "phpunit/phpunit": "^4.8 || ^5.7 || ^6.0", "predis/predis": "^1.1", "silex/silex": "^1.3 || ^2.0", - "symfony/cache": "^3.3", - "symfony/filesystem": "^2.8 || ^3.0", - "symfony/phpunit-bridge": "^3.3" + "squizlabs/php_codesniffer": "^3.0", + "symfony/cache": "^3.3 || ^4.0", + "symfony/filesystem": "^2.8 || ^3.0 || ^4.0", + "symfony/phpunit-bridge": "^3.3 || ^4.0" }, "autoload": { "psr-4": { @@ -44,15 +45,10 @@ } }, "scripts": { - "test": "phpunit", + "test": "vendor/bin/phpunit", "lint": [ - "phpcbf", - "php-cs-fixer fix" + "vendor/bin/phpcbf", + "vendor/bin/php-cs-fixer fix" ] - }, - "extra": { - "branch-alias": { - "dev-master": "1.x-dev" - } } } diff --git a/src/Handler/Factory/MemcachedFactory.php b/src/Handler/Factory/MemcachedFactory.php index a28608d..df7f607 100644 --- a/src/Handler/Factory/MemcachedFactory.php +++ b/src/Handler/Factory/MemcachedFactory.php @@ -102,7 +102,7 @@ protected function configure(Memcached $memcached, array $connections, OptionsBa $memcached->setOptions([ Memcached::OPT_BINARY_PROTOCOL => $binary, Memcached::OPT_LIBKETAMA_COMPATIBLE => $options->getBoolean('consistent_hash'), - Memcached::OPT_SERVER_FAILURE_LIMIT => $options->getInt('server_failure_limit'), + Memcached::OPT_SERVER_FAILURE_LIMIT => $options->getInt('server_failure_limit', 1), // See #3 Memcached::OPT_NUMBER_OF_REPLICAS => $options->getInt('number_of_replicas'), Memcached::OPT_RANDOMIZE_REPLICA_READ => $options->getBoolean('randomize_replica_read'), Memcached::OPT_REMOVE_FAILED_SERVERS => $options->getBoolean('remove_failed_servers'), diff --git a/src/Handler/FileHandler.php b/src/Handler/FileHandler.php index 06dfcb1..1af8e6d 100644 --- a/src/Handler/FileHandler.php +++ b/src/Handler/FileHandler.php @@ -2,6 +2,7 @@ namespace Bolt\Session\Handler; +use Bolt\Common\Thrower; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use Symfony\Component\Filesystem\Exception\IOException; @@ -16,8 +17,6 @@ */ class FileHandler extends AbstractHandler implements LazyWriteHandlerInterface { - /** @var int */ - protected $depth; /** @var int */ protected $mode; /** @var string */ @@ -41,7 +40,6 @@ public function __construct($savePath = null, LoggerInterface $logger = null, Fi $this->logger = $logger ?: new NullLogger(); // @see http://php.net/manual/en/session.configuration.php#ini.session.save-path - $depth = 1; $mode = 0600; $savePath = $savePath ?: sys_get_temp_dir(); @@ -53,20 +51,17 @@ public function __construct($savePath = null, LoggerInterface $logger = null, Fi $path = explode(';', $savePath); if ($count === 1) { - $depth = $path[0]; $savePath = $path[1]; } else { - $depth = $path[0]; $mode = intval($path[1], 8); $savePath = $path[2]; } } if (!is_dir($savePath)) { - $this->fs->mkdir($savePath, 0777); + $this->fs->mkdir($savePath); } - $this->depth = $depth; $this->mode = $mode; $this->savePath = $savePath; } @@ -76,7 +71,21 @@ public function __construct($savePath = null, LoggerInterface $logger = null, Fi */ public function read($sessionId) { - return file_get_contents($this->getSessionFileName($sessionId)); + $file = $this->getSessionFileName($sessionId); + + try { + if ($this->fs->exists($file)) { + try { + return Thrower::call('file_get_contents', $file); + } catch (\ErrorException $e) { + $this->logger->error(sprintf('Unable to read session file: %s', $file), ['exception' => $e]); + } + } + } catch (IOException $e) { + $this->logger->error($e->getMessage(), ['exception' => $e]); + } + + return ''; } /** @@ -84,8 +93,10 @@ public function read($sessionId) */ public function write($sessionId, $data) { + $file = $this->getSessionFileName($sessionId); + try { - $this->fs->dumpFile($this->getSessionFileName($sessionId), $data); + $this->fs->dumpFile($file, $data); } catch (IOException $e) { $this->logger->error('Unable to write session file to ' . $this->savePath); @@ -93,7 +104,7 @@ public function write($sessionId, $data) } try { - $this->fs->chmod($this->getSessionFileName($sessionId), $this->mode); + $this->fs->chmod($file, $this->mode); } catch (IOException $e) { $this->logger->error('Unable to set correct permissions on session file in ' . $this->savePath); } @@ -117,12 +128,14 @@ public function updateTimestamp($sessionId, $data) */ public function destroy($sessionId) { + $file = $this->getSessionFileName($sessionId); + try { - $this->fs->remove($this->getSessionFileName($sessionId)); + $this->fs->remove($file); return true; } catch (IOException $e) { - $this->logger->error('Unable to remove session file ' . $this->getSessionFileName($sessionId)); + $this->logger->error('Unable to remove session file ' . $file); return false; } diff --git a/tests/Handler/Factory/MemcacheFactoryTest.php b/tests/Handler/Factory/MemcacheFactoryTest.php index 825bdf7..056746b 100644 --- a/tests/Handler/Factory/MemcacheFactoryTest.php +++ b/tests/Handler/Factory/MemcacheFactoryTest.php @@ -5,7 +5,7 @@ use Bolt\Session\Handler\Factory\MemcacheFactory; use Bolt\Session\OptionsBag; use Bolt\Session\Tests\Handler\Factory\Mock\MockMemcache; -use PHPUnit_Framework_TestCase as TestCase; +use PHPUnit\Framework\TestCase; /** * @requires extension memcache diff --git a/tests/Handler/Factory/Mock/MockMemcached.php b/tests/Handler/Factory/Mock/MockMemcached.php index f6d8077..c412b52 100644 --- a/tests/Handler/Factory/Mock/MockMemcached.php +++ b/tests/Handler/Factory/Mock/MockMemcached.php @@ -48,7 +48,7 @@ public function setOption($option, $value) $this->options[$value]; } - public function setOptions(/* @noinspection PhpSignatureMismatchDuringInheritanceInspection */ $options) + public function setOptions($options) { $this->options = $options; } diff --git a/tests/Handler/FileHandlerTest.php b/tests/Handler/FileHandlerTest.php index 5e0a061..f6176c8 100644 --- a/tests/Handler/FileHandlerTest.php +++ b/tests/Handler/FileHandlerTest.php @@ -19,7 +19,7 @@ class FileHandlerTest extends TestCase /** @var string */ protected $savePath; /** @var string */ - protected $sessionName; + protected $sessionId; /** @var string */ protected $sessionFile; @@ -27,80 +27,85 @@ public function setUp() { $this->vfs = VfsStream::setup(); $this->savePath = $this->vfs->url(); - $this->sessionName = 'george'; - $this->sessionFile = $this->savePath . '/' . $this->sessionName . '.sess'; + $this->sessionId = 'george'; + $this->sessionFile = $this->savePath . '/' . $this->sessionId . '.sess'; } - public function testConstructor() + public function testOpen() { - $this->assertClassHasAttribute('savePath', FileHandler::class); - $this->assertClassHasAttribute('fs', FileHandler::class); + $fsh = new FileHandler($this->savePath); + + $result = $fsh->open(null, 'PHPSESSID'); + $this->assertTrue($result); + } + public function testClose() + { $fsh = new FileHandler($this->savePath); - $this->assertObjectHasAttribute('savePath', $fsh); - $this->assertObjectHasAttribute('fs', $fsh); + $result = $fsh->close(); + $this->assertTrue($result); + } + + public function testRead() + { + file_put_contents($this->sessionFile, 'kittens'); - $this->assertAttributeEquals($this->savePath, 'savePath', $fsh); + $fsh = new FileHandler($this->savePath); + + $result = $fsh->read($this->sessionId); + $this->assertSame('kittens', $result); } - public function testOpen() + public function testReadNew() { $fsh = new FileHandler($this->savePath); - $result = $fsh->open($this->savePath, $this->sessionName); - $this->assertTrue($result); + $result = $fsh->read('new'); + $this->assertSame('', $result); } - public function testClose() + public function testReadUnreadable() { + file_put_contents($this->savePath . '/unreadable.sess', ''); + chmod($this->savePath . '/unreadable.sess', 0); + $fsh = new FileHandler($this->savePath); - $result = $fsh->close(); - $this->assertTrue($result); + $result = $fsh->read('unreadable'); + $this->assertSame('', $result); } - /** - * @covers \Bolt\Session\Handler\FileHandler::write - * @covers \Bolt\Session\Handler\FileHandler::read - */ - public function testWriteRead() + public function testWrite() { $fsh = new FileHandler($this->savePath); - $fsh->open($this->savePath, $this->sessionName); - $result = $fsh->write($this->sessionName, 'kittens'); + $result = $fsh->write($this->sessionId, 'kittens'); $this->assertTrue($result); - - $result = $fsh->read($this->sessionName); - $this->assertSame('kittens', $result); + $this->assertFileExists($this->sessionFile); $this->assertStringEqualsFile($this->sessionFile, 'kittens'); } public function testDestroy() { - $fsh = new FileHandler($this->savePath); - $fsh->open($this->savePath, $this->sessionName); + file_put_contents($this->sessionFile, 'kittens'); - $fsh->write($this->sessionName, 'kittens'); - $this->assertFileExists($this->sessionFile); + $fsh = new FileHandler($this->savePath); - $result = $fsh->destroy($this->sessionName); + $result = $fsh->destroy($this->sessionId); $this->assertTrue($result); - $this->assertFileNotExists($this->sessionName); + $this->assertFileNotExists($this->sessionFile); } public function testGc() { - $fsh = new FileHandler($this->savePath); - $fsh->open($this->savePath, $this->sessionName); + file_put_contents($this->sessionFile, 'kittens'); + touch($this->sessionFile, time() - 5); - $fsh->write($this->sessionName, 'kittens'); - $this->assertFileExists($this->sessionFile); + $fsh = new FileHandler($this->savePath); - sleep(1); $result = $fsh->gc(1); $this->assertTrue($result); - $this->assertFileNotExists($this->sessionName); + $this->assertFileNotExists($this->sessionFile); } } diff --git a/tests/IniBagTest.php b/tests/IniBagTest.php index 0d9837c..1765829 100644 --- a/tests/IniBagTest.php +++ b/tests/IniBagTest.php @@ -132,6 +132,9 @@ public function testHas() $this->assertFalse($ini->has('kajhsdfakjsdfh')); } + /** + * @runInSeparateProcess + */ public function testSet() { $ini = new IniBag('session'); @@ -146,6 +149,9 @@ public function testSet() } } + /** + * @runInSeparateProcess + */ public function testAdd() { $ini = new IniBag('session'); @@ -160,6 +166,9 @@ public function testAdd() } } + /** + * @runInSeparateProcess + */ public function testReplace() { $ini = new IniBag('session');