Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: psalm regression after updating to 1.2.0 #11

Merged
merged 2 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
6 changes: 4 additions & 2 deletions src/UserRepository/HtpasswdFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Mezzio\Authentication\UserRepository;

use ArrayAccess;
use Mezzio\Authentication\Exception;
use Mezzio\Authentication\UserInterface;
use Psr\Container\ContainerInterface;
Expand All @@ -24,9 +25,10 @@ class HtpasswdFactory
*/
public function __invoke(ContainerInterface $container): Htpasswd
{
$config = $container->get('config');
Assert::isMap($config);
/** @var ArrayAccess<array-key,mixed> $config */
$config = $container->get('config');
$authConfig = $config['authentication'] ?? [];
Assert::isMap($authConfig);

$htpasswd = $authConfig['htpasswd'] ?? null;
Assert::nullOrString($htpasswd);
Comment on lines +28 to 34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @var ArrayAccess<array-key,mixed> $config */
$config = $container->get('config');
$authConfig = $config['authentication'] ?? [];
Assert::isMap($authConfig);
$htpasswd = $authConfig['htpasswd'] ?? null;
Assert::nullOrString($htpasswd);
$config = $container->get('config');
Assert::isArrayAccessible($config);
$htpasswd = $config['authentication']['htpasswd'] ?? null;
Assert::nullOrString($htpasswd);

Will this work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If config may be set as null it would crash.
We can keep this in mind for the next major of this and probably want to add some kind of compatibility-layer in mezzio itself to wrap any container within a container delegator to ensure that config is always an array.

Gonna add this as an RFC in the mezzio project after @weierophinney merged the issue template for RFCs.

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 => [
boesing marked this conversation as resolved.
Show resolved Hide resolved
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,69 @@
<?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
* @param mixed $value
*/
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]);
}
}