Skip to content

Commit

Permalink
Merge 287b0a9 into 676bafd
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Oct 8, 2020
2 parents 676bafd + 287b0a9 commit ae7c5b5
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 141 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/.phpcs-cache
/.phpunit.result.cache
/clover.xml
/composer.lock
/coveralls-upload.json
Expand Down
12 changes: 9 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cache:

env:
global:
- COMPOSER_ARGS="--no-interaction"
- COMPOSER_ARGS="--no-interaction --ignore-platform-reqs"
- COVERAGE_DEPS="php-coveralls/php-coveralls"

matrix:
Expand All @@ -18,14 +18,20 @@ matrix:
- php: 7.3
env:
- DEPS=latest
- CS_CHECK=true
- TEST_COVERAGE=true
- php: 7.4
env:
- DEPS=lowest
- php: 7.4
env:
- DEPS=latest
- CS_CHECK=true
- TEST_COVERAGE=true
- php: nightly
env:
- DEPS=lowest
- php: nightly
env:
- DEPS=latest

before_install:
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
}
},
"require": {
"php": "^7.3",
"php": "^7.3 || ~8.0.0",
"ext-session": "*",
"dflydev/fig-cookies": "^2.0.1",
"laminas/laminas-zendframework-bridge": "^1.0",
"mezzio/mezzio-session": "^1.4"
},
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"laminas/laminas-coding-standard": "~2.1.0",
"laminas/laminas-diactoros": "^1.6",
"phpunit/phpunit": "9.0.*"
"phpunit/phpunit": "^9.3"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 14 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?xml version="1.0"?>
<ruleset name="Laminas Coding Standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>

<!-- Show progress -->
<arg value="p"/>

<!-- Paths to check -->
<file>src</file>
<file>test</file>

<!-- Include all rules from the Laminas Coding Standard -->
<rule ref="LaminasCodingStandard"/>
</ruleset>
33 changes: 14 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="mezzio-session-ext">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<php>
<ini name="session.save_handler" value="files" />
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="mezzio-session-ext">
<directory>./test</directory>
</testsuite>
</testsuites>
<php>
<ini name="session.save_handler" value="files"/>
</php>
</phpunit>
12 changes: 7 additions & 5 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@
namespace Mezzio\Session\Ext;

use Mezzio\Session\SessionPersistenceInterface;
use Zend\Expressive\Session\Ext\PhpSessionPersistence as LegacyPhpSessionPersistence;
use Zend\Expressive\Session\SessionPersistenceInterface as LegacySessionPersistenceInterface;

class ConfigProvider
{
public function __invoke() : array
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}

public function getDependencies() : array
public function getDependencies(): array
{
return [
'aliases' => [
'aliases' => [
SessionPersistenceInterface::class => PhpSessionPersistence::class,

// Legacy Zend Framework aliases
\Zend\Expressive\Session\SessionPersistenceInterface::class => SessionPersistenceInterface::class,
\Zend\Expressive\Session\Ext\PhpSessionPersistence::class => PhpSessionPersistence::class,
LegacySessionPersistenceInterface::class => SessionPersistenceInterface::class,
LegacyPhpSessionPersistence::class => PhpSessionPersistence::class,
],
'factories' => [
PhpSessionPersistence::class => PhpSessionPersistenceFactory::class,
Expand Down
20 changes: 11 additions & 9 deletions src/PhpSessionPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class PhpSessionPersistence implements InitializePersistenceIdInterface, Session
*/
public function __construct(bool $nonLocking = false, bool $deleteCookieOnEmptySession = false)
{
$this->nonLocking = $nonLocking;
$this->nonLocking = $nonLocking;
$this->deleteCookieOnEmptySession = $deleteCookieOnEmptySession;

// Get session cache ini settings
Expand All @@ -99,14 +99,15 @@ public function __construct(bool $nonLocking = false, bool $deleteCookieOnEmptyS

/**
* @internal
*
* @return bool the non-locking mode used during initialization
*/
public function isNonLocking() : bool
public function isNonLocking(): bool
{
return $this->nonLocking;
}

public function initializeSessionFromRequest(ServerRequestInterface $request) : SessionInterface
public function initializeSessionFromRequest(ServerRequestInterface $request): SessionInterface
{
$sessionId = $this->getSessionCookieValueFromRequest($request);
if ($sessionId) {
Expand All @@ -117,14 +118,15 @@ public function initializeSessionFromRequest(ServerRequestInterface $request) :
return new Session($_SESSION ?? [], $sessionId);
}

public function persistSession(SessionInterface $session, ResponseInterface $response) : ResponseInterface
public function persistSession(SessionInterface $session, ResponseInterface $response): ResponseInterface
{
$id = $session->getId();

// Regenerate if:
// - the session is marked as regenerated
// - the id is empty, but the data has changed (new session)
if ($session->isRegenerated()
if (
$session->isRegenerated()
|| ($id === '' && $session->hasChanged())
) {
$id = $this->regenerateSession();
Expand Down Expand Up @@ -156,7 +158,7 @@ public function persistSession(SessionInterface $session, ResponseInterface $res
return $response;
}

public function initializeId(SessionInterface $session) : SessionInterface
public function initializeId(SessionInterface $session): SessionInterface
{
$id = $session->getId();
if ($id === '' || $session->isRegenerated()) {
Expand All @@ -171,7 +173,7 @@ public function initializeId(SessionInterface $session) : SessionInterface
/**
* @param array $options Additional options to pass to `session_start()`.
*/
private function startSession(string $id, array $options = []) : void
private function startSession(string $id, array $options = []): void
{
session_id($id);
session_start([
Expand All @@ -184,7 +186,7 @@ private function startSession(string $id, array $options = []) : void
/**
* Regenerates the session safely.
*/
private function regenerateSession() : string
private function regenerateSession(): string
{
if (session_status() === PHP_SESSION_ACTIVE) {
session_destroy();
Expand All @@ -200,7 +202,7 @@ private function regenerateSession() : string
/**
* Generate a session identifier.
*/
private function generateSessionId() : string
private function generateSessionId(): string
{
return bin2hex(random_bytes(16));
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSessionPersistenceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
class PhpSessionPersistenceFactory
{
public function __invoke(ContainerInterface $container) : PhpSessionPersistence
public function __invoke(ContainerInterface $container): PhpSessionPersistence
{
$config = $container->has('config') ? $container->get('config') : null;
$config = $config['session']['persistence']['ext'] ?? null;
Expand Down
6 changes: 3 additions & 3 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class ConfigProviderTest extends TestCase
/** @var ConfigProvider */
private $provider;

protected function setUp() : void
protected function setUp(): void
{
$this->provider = new ConfigProvider();
}

public function testInvocationReturnsArray() : array
public function testInvocationReturnsArray(): array
{
$config = ($this->provider)();
$this->assertIsArray($config);
Expand All @@ -34,7 +34,7 @@ public function testInvocationReturnsArray() : array
/**
* @depends testInvocationReturnsArray
*/
public function testReturnedArrayContainsDependencies(array $config) : void
public function testReturnedArrayContainsDependencies(array $config): void
{
$this->assertArrayHasKey('dependencies', $config);
$this->assertIsArray($config['dependencies']);
Expand Down
90 changes: 60 additions & 30 deletions test/PhpSessionPersistenceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,78 @@

class PhpSessionPersistenceFactoryTest extends TestCase
{
public function testFactoryConfigProducesPhpSessionPersistenceInterfaceService() : void
public function testFactoryProducesPhpSessionPersistenceServiceWithDefaultsInAbsenceOfConfig(): void
{
$container = $this->prophesize(ContainerInterface::class);
$factory = new PhpSessionPersistenceFactory();
$container = $this->createMock(ContainerInterface::class);
$factory = new PhpSessionPersistenceFactory();

// test php-session-persistence with missing config
$container->has('config')->willReturn(false);
$persistence = $factory($container->reveal());
$container
->expects($this->once())
->method('has')
->with('config')
->willReturn(false);

$persistence = $factory($container);
$this->assertInstanceOf(PhpSessionPersistence::class, $persistence);
$this->assertFalse($persistence->isNonLocking());
$this->assertFalse($persistence->isDeleteCookieOnEmptySession());
}

// test php-session-persistence with non-locking config set to false and true
foreach ([false, true] as $nonLocking) {
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
'session' => [
'persistence' => [
'ext' => [
'non_locking' => $nonLocking,
],
],
],
]);
$persistence = $factory($container->reveal());
$this->assertSame($nonLocking, $persistence->isNonLocking());
}
public function configProvider(): iterable
{
yield 'non_locking disabled' => [
'config' => ['non_locking' => false],
'expected' => false,
'methodToTest' => 'isNonLocking',
];
yield 'non_locking enabled' => [
'config' => ['non_locking' => true],
'expected' => true,
'methodToTest' => 'isNonLocking',
];
yield 'delete_cookie_on_empty_session disabled' => [
'config' => ['delete_cookie_on_empty_session' => false],
'expected' => false,
'methodToTest' => 'isDeleteCookieOnEmptySession',
];
yield 'delete_cookie_on_empty_session enabled' => [
'config' => ['delete_cookie_on_empty_session' => true],
'expected' => true,
'methodToTest' => 'isDeleteCookieOnEmptySession',
];
}

/**
* @dataProvider configProvider
*/
public function testFactoryConfigProducesPhpSessionPersistenceInterfaceService(
array $config,
bool $expected,
string $methodToTest
): void {
$container = $this->createMock(ContainerInterface::class);
$factory = new PhpSessionPersistenceFactory();

// test php-session-persistence with delete_cookie_on_empty_session config set to false and true
foreach ([false, true] as $deleteCookieOnEmptySession) {
$container->has('config')->willReturn(true);
$container->get('config')->willReturn([
// test php-session-persistence with missing config
$container
->expects($this->once())
->method('has')
->with('config')
->willReturn(true);
$container
->expects($this->once())
->method('get')
->with('config')
->willReturn([
'session' => [
'persistence' => [
'ext' => [
'delete_cookie_on_empty_session' => $deleteCookieOnEmptySession,
],
'ext' => $config,
],
],
]);
$persistence = $factory($container->reveal());
$this->assertSame($deleteCookieOnEmptySession, $persistence->isDeleteCookieOnEmptySession());
}

$persistence = $factory($container);
$this->assertSame($expected, $persistence->$methodToTest());
}
}

0 comments on commit ae7c5b5

Please sign in to comment.