Skip to content

Commit

Permalink
Merge d165efa into 1e82ed5
Browse files Browse the repository at this point in the history
  • Loading branch information
boesing committed Nov 3, 2020
2 parents 1e82ed5 + d165efa commit 58c9441
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#11](https://github.com/mezzio/mezzio-authentication/pull/11) Fixes an assertion error which was caused due to the implementation of Psalm in [#6](https://github.com/mezzio/mezzio-authentication/pull/6) and reported with [#10](https://github.com/mezzio/mezzio-authentication/issues/10).

Mezzio has support for multiple containers and some of those containers may return `ArrayObject` instead of an array.

## 1.2.0 - 2020-10-20

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"psr/container": "^1.0",
"psr/http-message": "^1.0.1",
"psr/http-server-middleware": "^1.0",
"symfony/polyfill-php80": "^1.20",
"webmozart/assert": "^1.9"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions src/UserRepository/HtpasswdFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class HtpasswdFactory
*/
public function __invoke(ContainerInterface $container): Htpasswd
{
$config = $container->get('config');
Assert::isMap($config);
$config = $container->get('config');
$authConfig = $config['authentication'] ?? [];
Assert::isMap($authConfig);

$htpasswd = $authConfig['htpasswd'] ?? null;
Assert::nullOrString($htpasswd);
Expand Down
45 changes: 39 additions & 6 deletions test/UserRepository/HtpasswdFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@

namespace MezzioTest\Authentication\UserRepository;

use ArrayAccess;
use ArrayObject;
use Generator;
use Mezzio\Authentication\Exception\InvalidConfigException;
use Mezzio\Authentication\UserInterface;
use Mezzio\Authentication\UserRepository\Htpasswd;
use Mezzio\Authentication\UserRepository\HtpasswdFactory;
use MezzioTest\Authentication\UserRepository\HtpasswdFactoryTest\ConfigImplementingArrayAccess;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
Expand All @@ -32,6 +36,34 @@ class HtpasswdFactoryTest extends TestCase
/** @var HtpasswdFactory */
private $factory;

/**
* @psalm-return Generator<string,array{0:mixed,1:non-empty-string}>
*/
public function validConfigs(): Generator
{
$filename = __DIR__ . '/../TestAssets/htpasswd';
$config = [
'authentication' => [
'htpasswd' => $filename,
],
];

yield 'array' => [
$config,
$filename,
];

yield ArrayObject::class => [
new ArrayObject($config),
$filename,
];

yield ArrayAccess::class => [
new ConfigImplementingArrayAccess($config),
$filename,
];
}

protected function setUp(): void
{
$this->container = $this->prophesize(ContainerInterface::class);
Expand Down Expand Up @@ -80,13 +112,14 @@ function () {
($this->factory)($this->container->reveal());
}

public function testInvokeWithValidConfig(): void
/**
* @psalm-param mixed $validConfig
* @psalm-param non-empty-string $filename
* @dataProvider validConfigs
*/
public function testInvokeWithValidConfig($validConfig, string $filename): void
{
$this->container->get('config')->willReturn([
'authentication' => [
'htpasswd' => $filename = __DIR__ . '/../TestAssets/htpasswd',
],
]);
$this->container->get('config')->willReturn($validConfig);
$this->container->get(UserInterface::class)->willReturn(
function () {
return $this->user->reveal();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace MezzioTest\Authentication\UserRepository\HtpasswdFactoryTest;

use ArrayAccess;
use OutOfBoundsException;

use function array_key_exists;
use function assert;
use function is_string;

/**
* @template-implements ArrayAccess<string,mixed>
*/
final class ConfigImplementingArrayAccess implements ArrayAccess
{
/** @var array<array-key,mixed> */
private $data;

/**
* @param array<string,mixed> $data
*/
public function __construct(array $data)
{
$this->data = $data;
}

/**
* @psalm-param array-key $offset
* @return bool
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->data);
}

/**
* @psalm-param array-key $offset
* @return mixed
*/
public function offsetGet($offset)
{
if (! $this->offsetExists($offset)) {
throw new OutOfBoundsException();
}

return $this->data[$offset];
}

/**
* @psalm-param array-key $offset
*/
public function offsetSet($offset, $value)
{
assert(is_string($offset));
$this->data[$offset] = $value;
}

/**
* @psalm-param array-key $offset
*/
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
}

0 comments on commit 58c9441

Please sign in to comment.