Skip to content

Commit

Permalink
Merge pull request #10 from geerteltink/feat/coding-standard-2.2.0
Browse files Browse the repository at this point in the history
feat: laminas coding standard 2.2.0
  • Loading branch information
weierophinney committed May 26, 2021
2 parents e40ebe6 + dbd2f94 commit 5f331e4
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 153 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"psr/http-message": "^1.0.1"
},
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"laminas/laminas-coding-standard": "~2.2.0",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.5"
},
Expand Down
335 changes: 279 additions & 56 deletions composer.lock

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?xml version="1.0"?>
<ruleset name="Laminas Coding Standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>

<!-- Show progress -->
<arg value="p"/>

<!-- Paths to check -->
<file>src</file>
<file>test</file>

<!-- Include all rules from the Laminas Coding Standard -->
<rule ref="LaminasCodingStandard"/>
</ruleset>
18 changes: 5 additions & 13 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
<?php

// @codingStandardsIgnoreStart
/**
* @see https://github.com/mezzio/mezzio-authentication-laminasauthentication for the canonical source repository
* @copyright https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/COPYRIGHT.md
* @license https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/LICENSE.md New BSD License
*/
// @codingStandardsIgnoreEnd

declare(strict_types=1);

namespace Mezzio\Authentication\LaminasAuthentication;

class ConfigProvider
{
public function __invoke() : array
public function __invoke(): array
{
return [
'authentication' => $this->getAuthenticationConfig(),
'dependencies' => $this->getDependencies(),
'dependencies' => $this->getDependencies(),
];
}

public function getAuthenticationConfig() : array
public function getAuthenticationConfig(): array
{
return [
'redirect' => '', // URL to which to redirect for invalid credentials
];
}

public function getDependencies() : array
public function getDependencies(): array
{
return [
// Legacy Zend Framework aliases
'aliases' => [
'aliases' => [
// @codingStandardsIgnoreStart
\Zend\Expressive\Authentication\ZendAuthentication\ZendAuthentication::class => LaminasAuthentication::class,
// @codingStandardsIgnoreEnd
Expand Down
38 changes: 11 additions & 27 deletions src/LaminasAuthentication.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

// @codingStandardsIgnoreStart
/**
* @see https://github.com/mezzio/mezzio-authentication-laminasauthentication for the canonical source repository
* @copyright https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/COPYRIGHT.md
* @license https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/LICENSE.md New BSD License
*/
// @codingStandardsIgnoreEnd

declare(strict_types=1);

namespace Mezzio\Authentication\LaminasAuthentication;
Expand All @@ -22,24 +14,16 @@

class LaminasAuthentication implements AuthenticationInterface
{
/**
* @var AuthenticationService
*/
/** @var AuthenticationService */
protected $auth;

/**
* @var array
*/
/** @var array */
protected $config;

/**
* @var callable
*/
/** @var callable */
protected $responseFactory;

/**
* @var callable
*/
/** @var callable */
protected $userFactory;

public function __construct(
Expand All @@ -48,11 +32,11 @@ public function __construct(
callable $responseFactory,
callable $userFactory
) {
$this->auth = $auth;
$this->auth = $auth;
$this->config = $config;

// Ensures type safety of the composed factory
$this->responseFactory = function () use ($responseFactory) : ResponseInterface {
$this->responseFactory = function () use ($responseFactory): ResponseInterface {
return $responseFactory();
};

Expand All @@ -61,12 +45,12 @@ public function __construct(
string $identity,
array $roles = [],
array $details = []
) use ($userFactory) : UserInterface {
) use ($userFactory): UserInterface {
return $userFactory($identity, $roles, $details);
};
}

public function authenticate(ServerRequestInterface $request) : ?UserInterface
public function authenticate(ServerRequestInterface $request): ?UserInterface
{
if (! $this->auth->hasIdentity()) {
if ('POST' === strtoupper($request->getMethod())) {
Expand All @@ -78,7 +62,7 @@ public function authenticate(ServerRequestInterface $request) : ?UserInterface
return ($this->userFactory)($this->auth->getIdentity());
}

public function unauthorizedResponse(ServerRequestInterface $request) : ResponseInterface
public function unauthorizedResponse(ServerRequestInterface $request): ResponseInterface
{
return ($this->responseFactory)()
->withHeader(
Expand All @@ -88,9 +72,9 @@ public function unauthorizedResponse(ServerRequestInterface $request) : Response
->withStatus(301);
}

private function initiateAuthentication(ServerRequestInterface $request) : ?UserInterface
private function initiateAuthentication(ServerRequestInterface $request): ?UserInterface
{
$params = $request->getParsedBody();
$params = $request->getParsedBody();
$username = $this->config['username'] ?? 'username';
$password = $this->config['password'] ?? 'password';

Expand Down
13 changes: 3 additions & 10 deletions src/LaminasAuthenticationFactory.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

// @codingStandardsIgnoreStart
/**
* @see https://github.com/mezzio/mezzio-authentication-laminasauthentication for the canonical source repository
* @copyright https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/COPYRIGHT.md
* @license https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/LICENSE.md New BSD License
*/
// @codingStandardsIgnoreEnd

declare(strict_types=1);

namespace Mezzio\Authentication\LaminasAuthentication;
Expand All @@ -22,7 +14,7 @@

class LaminasAuthenticationFactory
{
public function __invoke(ContainerInterface $container) : LaminasAuthentication
public function __invoke(ContainerInterface $container): LaminasAuthentication
{
$auth = $container->has(AuthenticationService::class)
? $container->get(AuthenticationService::class)
Expand All @@ -45,7 +37,8 @@ public function __invoke(ContainerInterface $container) : LaminasAuthentication
);
}

if (! $container->has(UserInterface::class)
if (
! $container->has(UserInterface::class)
&& ! $container->has(\Zend\Expressive\Authentication\UserInterface::class)
) {
throw new Exception\InvalidConfigException(
Expand Down
11 changes: 2 additions & 9 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

// @codingStandardsIgnoreStart
/**
* @see https://github.com/mezzio/mezzio-authentication-laminasauthentication for the canonical source repository
* @copyright https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/COPYRIGHT.md
* @license https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/LICENSE.md New BSD License
*/
// @codingStandardsIgnoreEnd

declare(strict_types=1);

namespace MezzioTest\Authentication\LaminasAuthentication;
Expand All @@ -23,7 +15,7 @@ public function setUp(): void
$this->provider = new ConfigProvider();
}

public function testInvocationReturnsArray()
public function testInvocationReturnsArray(): array
{
$config = ($this->provider)();
$this->assertInternalType('array', $config);
Expand All @@ -48,6 +40,7 @@ public function testReturnedArrayContainsAuthenticationConfig(array $config)
$this->assertInternalType('array', $config['authentication']);
}

/** @param mixed $actual */
private static function assertInternalType(string $expected, $actual, string $message = ''): void
{
static::assertThat(
Expand Down
24 changes: 8 additions & 16 deletions test/LaminasAuthenticationFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

// @codingStandardsIgnoreStart
/**
* @see https://github.com/mezzio/mezzio-authentication-laminasauthentication for the canonical source repository
* @copyright https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/COPYRIGHT.md
* @license https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/LICENSE.md New BSD License
*/
// @codingStandardsIgnoreEnd

declare(strict_types=1);

namespace MezzioTest\Authentication\LaminasAuthentication;
Expand All @@ -19,11 +11,11 @@
use Mezzio\Authentication\UserInterface;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use ReflectionProperty;
use Prophecy\PhpUnit\ProphecyTrait;

class LaminasAuthenticationFactoryTest extends TestCase
{
Expand Down Expand Up @@ -52,15 +44,15 @@ class LaminasAuthenticationFactoryTest extends TestCase

public function setUp(): void
{
$this->container = $this->prophesize(ContainerInterface::class);
$this->factory = new LaminasAuthenticationFactory();
$this->authService = $this->prophesize(AuthenticationService::class);
$this->container = $this->prophesize(ContainerInterface::class);
$this->factory = new LaminasAuthenticationFactory();
$this->authService = $this->prophesize(AuthenticationService::class);
$this->responsePrototype = $this->prophesize(ResponseInterface::class);
$this->responseFactory = function () {
$this->responseFactory = function () {
return $this->responsePrototype->reveal();
};
$this->userPrototype = $this->prophesize(UserInterface::class);
$this->userFactory = function () {
$this->userPrototype = $this->prophesize(UserInterface::class);
$this->userFactory = function () {
return $this->userPrototype->reveal();
};
}
Expand Down Expand Up @@ -133,7 +125,7 @@ public function testInvokeWithContainerAndConfig()
public static function assertResponseFactoryReturns(
ResponseInterface $expected,
LaminasAuthentication $service
) : void {
): void {
$r = new ReflectionProperty($service, 'responseFactory');
$r->setAccessible(true);
$responseFactory = $r->getValue($service);
Expand Down
26 changes: 7 additions & 19 deletions test/LaminasAuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

// @codingStandardsIgnoreStart
/**
* @see https://github.com/mezzio/mezzio-authentication-laminasauthentication for the canonical source repository
* @copyright https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/COPYRIGHT.md
* @license https://github.com/mezzio/mezzio-authentication-laminasauthentication/blob/master/LICENSE.md New BSD License
*/
// @codingStandardsIgnoreEnd

declare(strict_types=1);

namespace MezzioTest\Authentication\Adapter;
Expand All @@ -34,9 +26,6 @@ class LaminasAuthenticationTest extends TestCase
/** @var AuthenticationService|ObjectProphecy */
private $authService;

/** @var UserInterface|ObjectProphecy */
private $authenticatedUser;

/** @var callable */
private $responseFactory;

Expand All @@ -48,14 +37,13 @@ class LaminasAuthenticationTest extends TestCase

public function setUp(): void
{
$this->request = $this->prophesize(ServerRequestInterface::class);
$this->authService = $this->prophesize(AuthenticationService::class);
$this->authenticatedUser = $this->prophesize(UserInterface::class);
$this->request = $this->prophesize(ServerRequestInterface::class);
$this->authService = $this->prophesize(AuthenticationService::class);
$this->responseFactory = function () {
return $this->prophesize(ResponseInterface::class)->reveal();
};
$this->userPrototype = $this->prophesize(UserInterface::class);
$this->userFactory = function () {
$this->userPrototype = $this->prophesize(UserInterface::class);
$this->userFactory = function () {
return $this->userPrototype->reveal();
};
}
Expand Down Expand Up @@ -83,7 +71,7 @@ public function testAuthenticateWithGetMethodAndIdentity()
$this->responseFactory,
$this->userFactory
);
$result = $laminasAuthentication->authenticate($this->request->reveal());
$result = $laminasAuthentication->authenticate($this->request->reveal());
$this->assertInstanceOf(UserInterface::class, $result);
}

Expand Down Expand Up @@ -179,7 +167,7 @@ public function testAuthenticateWithPostMethodAndValidCredential()
$this->responseFactory,
$this->userFactory
);
$result = $laminasAuthentication->authenticate($this->request->reveal());
$result = $laminasAuthentication->authenticate($this->request->reveal());
$this->assertInstanceOf(UserInterface::class, $result);
}

Expand Down Expand Up @@ -215,7 +203,7 @@ public function testAuthenticateWithPostMethodAndNoValidCredentialAndAlreadyAuth
$this->responseFactory,
$this->userFactory
);
$identity = $laminasAuthentication->authenticate($this->request->reveal());
$identity = $laminasAuthentication->authenticate($this->request->reveal());
$this->assertInstanceOf(UserInterface::class, $identity);
$this->assertEquals('string', $identity->getIdentity());
}
Expand Down

0 comments on commit 5f331e4

Please sign in to comment.