From 70f8347e85b99ac3c576769a47fdf7efc7b300e7 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 1 Feb 2021 10:53:14 +0000 Subject: [PATCH 1/6] Update phpunit/phpunit requirement from ^9.0 to ^9.5 Updates the requirements on [phpunit/phpunit](https://github.com/sebastianbergmann/phpunit) to permit the latest version. - [Release notes](https://github.com/sebastianbergmann/phpunit/releases) - [Changelog](https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-9.5.md) - [Commits](https://github.com/sebastianbergmann/phpunit/compare/9.0.0...9.5.1) Signed-off-by: dependabot-preview[bot] --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 65a2b3b..879fbb7 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "require-dev": { "phpspec/prophecy": "^1.10", "phpspec/prophecy-phpunit": "^2.0", - "phpunit/phpunit": "^9.0", + "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "^3.5.8" }, "autoload": { From e3ee43aa2ec2a725278354d37aa20e644f92af6d Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 1 Feb 2021 11:28:02 +0000 Subject: [PATCH 2/6] Update phpspec/prophecy requirement from ^1.10 to ^1.12 Updates the requirements on [phpspec/prophecy](https://github.com/phpspec/prophecy) to permit the latest version. - [Release notes](https://github.com/phpspec/prophecy/releases) - [Changelog](https://github.com/phpspec/prophecy/blob/master/CHANGES.md) - [Commits](https://github.com/phpspec/prophecy/compare/1.10.0...1.12.2) Signed-off-by: dependabot-preview[bot] --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 879fbb7..e5ee535 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "psr/http-server-middleware": "^1.0" }, "require-dev": { - "phpspec/prophecy": "^1.10", + "phpspec/prophecy": "^1.12", "phpspec/prophecy-phpunit": "^2.0", "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "^3.5.8" From 3b2093e220691394648e32d2d790bc5ad84a85d6 Mon Sep 17 00:00:00 2001 From: cwreden Date: Mon, 1 Feb 2021 21:36:03 +0100 Subject: [PATCH 3/6] Implement iterator support at getLastKeyPair --- src/Guard.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Guard.php b/src/Guard.php index c4eda33..a704a34 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -289,10 +289,20 @@ protected function getLastKeyPair(): ?array return null; } - end($this->storage); - $name = key($this->storage); - $value = $this->storage[$name]; - reset($this->storage); + $name = null; + $value = null; + if (is_array($this->storage)) { + end($this->storage); + $name = key($this->storage); + $value = $this->storage[$name]; + reset($this->storage); + } elseif ($this->storage instanceof Iterator) { + while ($this->storage->valid()) { + $this->storage->next(); + } + $name = $this->storage->key(); + $value = $this->storage->current(); + } return $name !== null && $value !== null ? [ From bcef5c1974da0bd4825e8a6b82b5c5f5bd41f73a Mon Sep 17 00:00:00 2001 From: cwreden Date: Mon, 1 Feb 2021 22:12:00 +0100 Subject: [PATCH 4/6] Implement phpunit test --- tests/GuardTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/GuardTest.php b/tests/GuardTest.php index f4cb45a..ee03965 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -425,4 +425,25 @@ public function testProcessAppendsNewTokensWhenPersistentTokenModeIsOn() $mw->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal()); } + + public function testCanGetLastKeyPairFromIterator() + { + + $storage = new ArrayIterator([ + 'test_key1' => 'value1', + 'test_key2' => 'value2', + ]); + $responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class); + $mw = new Guard($responseFactoryProphecy->reveal(), 'test', $storage, null, 1); + + $enforceStorageLimitMethod = new ReflectionMethod($mw, 'getLastKeyPair'); + $enforceStorageLimitMethod->setAccessible(true); + $keyPair = $enforceStorageLimitMethod->invoke($mw); + + $this->assertIsArray($keyPair); + $this->assertArrayHasKey('test_name', $keyPair); + $this->assertArrayHasKey('test_value', $keyPair); + $this->assertEquals('test_key2', $keyPair['test_name']); + $this->assertEquals('value2', $keyPair['test_value']); + } } From ad4e4b0d2458111405480712fe61528f82667d2c Mon Sep 17 00:00:00 2001 From: cwreden Date: Mon, 1 Feb 2021 22:29:03 +0100 Subject: [PATCH 5/6] Add rewind storage and move get key and value --- src/Guard.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Guard.php b/src/Guard.php index a704a34..0f0055b 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -297,11 +297,13 @@ protected function getLastKeyPair(): ?array $value = $this->storage[$name]; reset($this->storage); } elseif ($this->storage instanceof Iterator) { + $this->storage->rewind(); while ($this->storage->valid()) { + $name = $this->storage->key(); + $value = $this->storage->current(); $this->storage->next(); } - $name = $this->storage->key(); - $value = $this->storage->current(); + $this->storage->rewind(); } return $name !== null && $value !== null From eb4d7f1594d37ecbf53479fe5a9555ea98e66ce9 Mon Sep 17 00:00:00 2001 From: cwreden Date: Tue, 2 Feb 2021 18:05:09 +0100 Subject: [PATCH 6/6] Remove empty line --- tests/GuardTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/GuardTest.php b/tests/GuardTest.php index ee03965..a1edbde 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -428,7 +428,6 @@ public function testProcessAppendsNewTokensWhenPersistentTokenModeIsOn() public function testCanGetLastKeyPairFromIterator() { - $storage = new ArrayIterator([ 'test_key1' => 'value1', 'test_key2' => 'value2',