Skip to content

Commit

Permalink
Merge branch 'lastKeyPairIteratorSupport'
Browse files Browse the repository at this point in the history
Closes #132
  • Loading branch information
akrabat committed Feb 4, 2021
2 parents 2c2949d + 01bad9a commit ee811a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"phpspec/prophecy": "^1.10",
"phpspec/prophecy": "^1.12",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.0",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.5.8"
},
"autoload": {
Expand Down
20 changes: 16 additions & 4 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,22 @@ 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) {
$this->storage->rewind();
while ($this->storage->valid()) {
$name = $this->storage->key();
$value = $this->storage->current();
$this->storage->next();
}
$this->storage->rewind();
}

return $name !== null && $value !== null
? [
Expand Down
20 changes: 20 additions & 0 deletions tests/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,24 @@ 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']);
}
}

0 comments on commit ee811a2

Please sign in to comment.