Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 6f11811

Browse files
committed
Review for consistency
1 parent 0dffea5 commit 6f11811

File tree

5 files changed

+102
-62
lines changed

5 files changed

+102
-62
lines changed

src/BasicAccess.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,32 @@ public function __construct(
5151
/**
5252
* {@inheritDoc}
5353
*/
54-
public function authenticate(ServerRequestInterface $request): ?UserInterface
54+
public function authenticate(ServerRequestInterface $request) : ?UserInterface
5555
{
5656
$authHeader = $request->getHeader('Authorization');
5757
if (empty($authHeader)) {
5858
return null;
5959
}
60-
if (! preg_match('/Basic ([a-zA-Z0-9\+\/\=]+)/', $authHeader[0], $match)) {
60+
61+
if (! preg_match('/Basic (?P<credentials>[a-zA-Z0-9\+\/\=]+)/', $authHeader[0], $match)) {
6162
return null;
6263
}
63-
[$username, $password] = explode(':', base64_decode($match[1]));
64+
65+
[$username, $password] = explode(':', base64_decode($match['credentials']));
6466

6567
return $this->repository->authenticate($username, $password);
6668
}
6769

6870
/**
6971
* {@inheritDoc}
7072
*/
71-
public function unauthorizedResponse(ServerRequestInterface $request): ResponseInterface
73+
public function unauthorizedResponse(ServerRequestInterface $request) : ResponseInterface
7274
{
73-
return $this->responsePrototype->withHeader(
74-
'WWW-Authenticate',
75-
sprintf("Basic realm=\"%s\"", $this->realm)
76-
)->withStatus(401);
75+
return $this->responsePrototype
76+
->withHeader(
77+
'WWW-Authenticate',
78+
sprintf('Basic realm="%s"', $this->realm)
79+
)
80+
->withStatus(401);
7781
}
7882
}

src/BasicAccessFactory.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ class BasicAccessFactory
1717
{
1818
use ResponsePrototypeTrait;
1919

20-
public function __invoke(ContainerInterface $container): BasicAccess
20+
public function __invoke(ContainerInterface $container) : BasicAccess
2121
{
22-
$userRegister = $container->has(UserRepositoryInterface::class) ?
23-
$container->get(UserRepositoryInterface::class) :
24-
null;
22+
$userRegister = $container->has(UserRepositoryInterface::class)
23+
? $container->get(UserRepositoryInterface::class)
24+
: null;
25+
2526
if (null === $userRegister) {
2627
throw new Exception\InvalidConfigException(
2728
'UserRepositoryInterface service is missing for authentication'
2829
);
2930
}
31+
3032
$realm = $container->get('config')['authentication']['realm'] ?? null;
33+
3134
if (null === $realm) {
3235
throw new Exception\InvalidConfigException(
3336
'Realm value is not present in authentication config'

test/BasicAccessFactoryTest.php

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Http\Message\ResponseInterface;
1515
use Zend\Expressive\Authentication\Basic\BasicAccess;
1616
use Zend\Expressive\Authentication\Basic\BasicAccessFactory;
17+
use Zend\Expressive\Authentication\Exception\InvalidConfigException;
1718
use Zend\Expressive\Authentication\UserRepositoryInterface;
1819

1920
class BasicAccessFactoryTest extends TestCase
@@ -26,47 +27,53 @@ protected function setUp()
2627
$this->responsePrototype = $this->prophesize(ResponseInterface::class);
2728
}
2829

29-
/**
30-
* @expectedException Zend\Expressive\Authentication\Exception\InvalidConfigException
31-
*/
3230
public function testInvokeWithEmptyContainer()
3331
{
32+
$this->expectException(InvalidConfigException::class);
3433
$basicAccess = ($this->factory)($this->container->reveal());
3534
}
3635

37-
/**
38-
* @expectedException Zend\Expressive\Authentication\Exception\InvalidConfigException
39-
*/
4036
public function testInvokeWithContainerEmptyConfig()
4137
{
42-
$this->container->has(UserRepositoryInterface::class)
43-
->willReturn(true);
44-
$this->container->get(UserRepositoryInterface::class)
45-
->willReturn($this->userRegister->reveal());
46-
$this->container->has(ResponseInterface::class)
47-
->willReturn(true);
48-
$this->container->get(ResponseInterface::class)
49-
->willReturn($this->responsePrototype->reveal());
50-
$this->container->get('config')
51-
->willReturn([]);
38+
$this->container
39+
->has(UserRepositoryInterface::class)
40+
->willReturn(true);
41+
$this->container
42+
->get(UserRepositoryInterface::class)
43+
->willReturn($this->userRegister->reveal());
44+
$this->container
45+
->has(ResponseInterface::class)
46+
->willReturn(true);
47+
$this->container
48+
->get(ResponseInterface::class)
49+
->willReturn($this->responsePrototype->reveal());
50+
$this->container
51+
->get('config')
52+
->willReturn([]);
5253

54+
$this->expectException(InvalidConfigException::class);
5355
$basicAccess = ($this->factory)($this->container->reveal());
5456
}
5557

5658
public function testInvokeWithContainerAndConfig()
5759
{
58-
$this->container->has(UserRepositoryInterface::class)
59-
->willReturn(true);
60-
$this->container->get(UserRepositoryInterface::class)
61-
->willReturn($this->userRegister->reveal());
62-
$this->container->has(ResponseInterface::class)
63-
->willReturn(true);
64-
$this->container->get(ResponseInterface::class)
65-
->willReturn($this->responsePrototype->reveal());
66-
$this->container->get('config')
67-
->willReturn([
68-
'authentication' => ['realm' => 'My page']
69-
]);
60+
$this->container
61+
->has(UserRepositoryInterface::class)
62+
->willReturn(true);
63+
$this->container
64+
->get(UserRepositoryInterface::class)
65+
->willReturn($this->userRegister->reveal());
66+
$this->container
67+
->has(ResponseInterface::class)
68+
->willReturn(true);
69+
$this->container
70+
->get(ResponseInterface::class)
71+
->willReturn($this->responsePrototype->reveal());
72+
$this->container
73+
->get('config')
74+
->willReturn([
75+
'authentication' => ['realm' => 'My page'],
76+
]);
7077

7178
$basicAccess = ($this->factory)($this->container->reveal());
7279
$this->assertInstanceOf(BasicAccess::class, $basicAccess);

test/BasicAccessTest.php

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public function testConstructor()
3939

4040
public function testIsAuthenticatedWithoutHeader()
4141
{
42-
$this->request->getHeader('Authorization')
43-
->willReturn([]);
42+
$this->request
43+
->getHeader('Authorization')
44+
->willReturn([]);
4445

4546
$basicAccess = new BasicAccess(
4647
$this->userRepository->reveal(),
@@ -52,67 +53,83 @@ public function testIsAuthenticatedWithoutHeader()
5253

5354
public function testIsAuthenticatedWithoutBasic()
5455
{
55-
$this->request->getHeader('Authorization')
56-
->willReturn(['foo']);
56+
$this->request
57+
->getHeader('Authorization')
58+
->willReturn(['foo']);
5759

5860
$basicAccess = new BasicAccess(
5961
$this->userRepository->reveal(),
6062
'test',
6163
$this->responsePrototype->reveal()
6264
);
65+
6366
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
6467
}
6568

6669
public function testIsAuthenticatedWithValidCredential()
6770
{
68-
$this->request->getHeader('Authorization')
69-
->willReturn(['Basic QWxhZGRpbjpPcGVuU2VzYW1l']);
70-
$this->request->withAttribute(UserInterface::class, Argument::type(UserInterface::class))
71-
->willReturn($this->request->reveal());
72-
73-
$this->authenticatedUser->getUsername()->willReturn('Aladdin');
74-
$this->userRepository->authenticate('Aladdin', 'OpenSesame')
75-
->willReturn($this->authenticatedUser->reveal());
71+
$this->request
72+
->getHeader('Authorization')
73+
->willReturn(['Basic QWxhZGRpbjpPcGVuU2VzYW1l']);
74+
$this->request
75+
->withAttribute(UserInterface::class, Argument::type(UserInterface::class))
76+
->willReturn($this->request->reveal());
77+
78+
$this->authenticatedUser
79+
->getUsername()
80+
->willReturn('Aladdin');
81+
$this->userRepository
82+
->authenticate('Aladdin', 'OpenSesame')
83+
->willReturn($this->authenticatedUser->reveal());
7684

7785
$basicAccess = new BasicAccess(
7886
$this->userRepository->reveal(),
7987
'test',
8088
$this->responsePrototype->reveal()
8189
);
90+
8291
$user = $basicAccess->authenticate($this->request->reveal());
8392
$this->assertInstanceOf(UserInterface::class, $user);
8493
$this->assertEquals('Aladdin', $user->getUsername());
8594
}
8695

8796
public function testIsAuthenticatedWithNoCredential()
8897
{
89-
$this->request->getHeader('Authorization')
90-
->willReturn(['Basic QWxhZGRpbjpPcGVuU2VzYW1l']);
98+
$this->request
99+
->getHeader('Authorization')
100+
->willReturn(['Basic QWxhZGRpbjpPcGVuU2VzYW1l']);
91101

92-
$this->userRepository->authenticate('Aladdin', 'OpenSesame')
93-
->willReturn(null);
102+
$this->userRepository
103+
->authenticate('Aladdin', 'OpenSesame')
104+
->willReturn(null);
94105

95106
$basicAccess = new BasicAccess(
96107
$this->userRepository->reveal(),
97108
'test',
98109
$this->responsePrototype->reveal()
99110
);
111+
100112
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
101113
}
102114

103115
public function testGetUnauthenticatedResponse()
104116
{
105-
$this->responsePrototype->getHeader('WWW-Authenticate')
106-
->willReturn(['Basic realm="test"']);
107-
$this->responsePrototype->withHeader('WWW-Authenticate', 'Basic realm="test"')
108-
->willReturn($this->responsePrototype->reveal());
109-
$this->responsePrototype->withStatus(401)
110-
->willReturn($this->responsePrototype->reveal());
117+
$this->responsePrototype
118+
->getHeader('WWW-Authenticate')
119+
->willReturn(['Basic realm="test"']);
120+
$this->responsePrototype
121+
->withHeader('WWW-Authenticate', 'Basic realm="test"')
122+
->willReturn($this->responsePrototype->reveal());
123+
$this->responsePrototype
124+
->withStatus(401)
125+
->willReturn($this->responsePrototype->reveal());
126+
111127
$basicAccess = new BasicAccess(
112128
$this->userRepository->reveal(),
113129
'test',
114130
$this->responsePrototype->reveal()
115131
);
132+
116133
$response = $basicAccess->unauthorizedResponse($this->request->reveal());
117134

118135
$this->assertInstanceOf(ResponseInterface::class, $response);

test/ConfigProviderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ public function testReturnedArrayContainsDependencies(array $config)
3333
$this->assertArrayHasKey('dependencies', $config);
3434
$this->assertInternalType('array', $config['dependencies']);
3535
}
36+
37+
/**
38+
* @depends testInvocationReturnsArray
39+
*/
40+
public function testReturnedArrayContainsAuthenticationConfig(array $config)
41+
{
42+
$this->assertArrayHasKey('authentication', $config);
43+
$this->assertInternalType('array', $config['authentication']);
44+
}
3645
}

0 commit comments

Comments
 (0)