From 9f9ad32ae8db944b6c2d58d933a330992729558e Mon Sep 17 00:00:00 2001 From: Tyson Andre Date: Mon, 16 Sep 2019 21:12:50 -0400 Subject: [PATCH 1/6] Fix incorrect check if $options['input'] was ArrayAccess. Previously, $input was always null, and that should have checked `$options['input']` instead. Move the assignment to $input above the conditions. --- src/Storage/Factory.php | 8 ++++---- test/Service/StorageFactoryTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Storage/Factory.php b/src/Storage/Factory.php index caebee9f..edb405fa 100644 --- a/src/Storage/Factory.php +++ b/src/Storage/Factory.php @@ -131,17 +131,17 @@ protected static function createSessionArrayStorage($type, array $options) { $input = null; if (isset($options['input'])) { - if (null !== $options['input'] - && ! is_array($options['input']) + $input = $options['input']; + if (null !== $input + && ! is_array($input) && ! $input instanceof ArrayAccess ) { throw new Exception\InvalidArgumentException(sprintf( '%s expects the "input" option to be null, an array, or to implement ArrayAccess; received "%s"', $type, - (is_object($options['input']) ? get_class($options['input']) : gettype($options['input'])) + (is_object($input) ? get_class($input) : gettype($input)) )); } - $input = $options['input']; } return new $type($input); diff --git a/test/Service/StorageFactoryTest.php b/test/Service/StorageFactoryTest.php index 52dabd57..8efee546 100644 --- a/test/Service/StorageFactoryTest.php +++ b/test/Service/StorageFactoryTest.php @@ -68,6 +68,16 @@ public function sessionStorageConfig() ], ], ], SessionArrayStorage::class], + 'session-array-storage-arrayobject' => [[ + 'session_storage' => [ + 'type' => 'SessionArrayStorage', + 'options' => [ + 'input' => new \ArrayObject([ + 'foo' => 'bar', + ]), + ], + ], + ], SessionArrayStorage::class], 'session-array-storage-fqcn' => [[ 'session_storage' => [ 'type' => SessionArrayStorage::class, From a8becf8f9ad07db887e700d2db4ad182503aa5fc Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 19 Sep 2019 20:42:26 +0100 Subject: [PATCH 2/6] Import ArrayObject --- test/Service/StorageFactoryTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Service/StorageFactoryTest.php b/test/Service/StorageFactoryTest.php index 8efee546..a00d3773 100644 --- a/test/Service/StorageFactoryTest.php +++ b/test/Service/StorageFactoryTest.php @@ -9,6 +9,7 @@ namespace ZendTest\Session\Service; +use ArrayObject; use PHPUnit\Framework\TestCase; use Zend\ServiceManager\Config; use Zend\ServiceManager\Exception\ServiceNotCreatedException; @@ -72,7 +73,7 @@ public function sessionStorageConfig() 'session_storage' => [ 'type' => 'SessionArrayStorage', 'options' => [ - 'input' => new \ArrayObject([ + 'input' => new ArrayObject([ 'foo' => 'bar', ]), ], From 4cf1f1f6a8396ac264ff4c8e9be337695601e807 Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 19 Sep 2019 20:42:40 +0100 Subject: [PATCH 3/6] Added case without input in configuration --- test/Service/StorageFactoryTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/Service/StorageFactoryTest.php b/test/Service/StorageFactoryTest.php index a00d3773..d5da75fe 100644 --- a/test/Service/StorageFactoryTest.php +++ b/test/Service/StorageFactoryTest.php @@ -104,6 +104,21 @@ public function testUsesConfigurationToCreateStorage($config, $class) $this->assertEquals($config['session_storage']['options']['input'], $test); } + public function testConfigurationWithoutInputIsValid() + { + $this->services->setService('config', [ + 'session_storage' => [ + 'type' => SessionArrayStorage::class, + 'options' => [], + ], + ]); + + $storage = $this->services->get(StorageInterface::class); + + $this->assertInstanceOf(SessionArrayStorage::class, $storage); + $this->assertSame([], $storage->toArray()); + } + public function invalidSessionStorageConfig() { return [ From ed94d0d5935d7bbbe7ae3a569da20aceae090d57 Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 19 Sep 2019 20:45:04 +0100 Subject: [PATCH 4/6] Removed redundant condition $input must be different than null, as just before we check isset. --- src/Storage/Factory.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Storage/Factory.php b/src/Storage/Factory.php index edb405fa..5613f40d 100644 --- a/src/Storage/Factory.php +++ b/src/Storage/Factory.php @@ -132,8 +132,7 @@ protected static function createSessionArrayStorage($type, array $options) $input = null; if (isset($options['input'])) { $input = $options['input']; - if (null !== $input - && ! is_array($input) + if (! is_array($input) && ! $input instanceof ArrayAccess ) { throw new Exception\InvalidArgumentException(sprintf( From 55f6c65d0d94a5133ae06e5b59ac05792feecef6 Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 19 Sep 2019 20:45:16 +0100 Subject: [PATCH 5/6] Removed redundant brackets --- src/Storage/Factory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/Factory.php b/src/Storage/Factory.php index 5613f40d..ca39538f 100644 --- a/src/Storage/Factory.php +++ b/src/Storage/Factory.php @@ -138,7 +138,7 @@ protected static function createSessionArrayStorage($type, array $options) throw new Exception\InvalidArgumentException(sprintf( '%s expects the "input" option to be null, an array, or to implement ArrayAccess; received "%s"', $type, - (is_object($input) ? get_class($input) : gettype($input)) + is_object($input) ? get_class($input) : gettype($input) )); } } From d0ee91010062385b90dec512e4a780a6b39669a3 Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 19 Sep 2019 20:47:18 +0100 Subject: [PATCH 6/6] Adds CHANGELOG for #122 --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64e99aba..69b7cd31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#122](https://github.com/zendframework/zend-session/pull/122) fixes + type check for configuration of session storage. Allows input to be + an instance of ArrayAccess or an array. ## 2.8.6 - 2019-08-11