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

Commit f8e478d

Browse files
committed
#8 refactored tests
* use dataProviders * added more test-cases * added return-type hints * remove unnecessary assertion
1 parent e1ec4ca commit f8e478d

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

test/BasicAccessTest.php

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function setUp()
4646
};
4747
}
4848

49-
public function testConstructor()
49+
public function testConstructor(): void
5050
{
5151
$basicAccess = new BasicAccess(
5252
$this->userRepository->reveal(),
@@ -56,49 +56,45 @@ public function testConstructor()
5656
$this->assertInstanceOf(AuthenticationInterface::class, $basicAccess);
5757
}
5858

59-
public function testIsAuthenticatedWithoutHeader()
60-
{
61-
$this->request
62-
->getHeader('Authorization')
63-
->willReturn([]);
64-
65-
$basicAccess = new BasicAccess(
66-
$this->userRepository->reveal(),
67-
'test',
68-
$this->responseFactory
69-
);
70-
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
71-
}
7259

73-
public function testIsAuthenticatedWithoutBasic()
60+
/**
61+
* @param array $authHeaderContent
62+
* @dataProvider provideInvalidAuthenticationHeader
63+
*/
64+
public function testIsAuthenticatedWithInvalidData(array $authHeaderContent): void
7465
{
7566
$this->request
7667
->getHeader('Authorization')
77-
->willReturn(['foo']);
68+
->willReturn($authHeaderContent);
7869

7970
$basicAccess = new BasicAccess(
8071
$this->userRepository->reveal(),
8172
'test',
8273
$this->responseFactory
8374
);
84-
8575
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
8676
}
8777

88-
public function testIsAuthenticatedWithValidCredential()
78+
/**
79+
* @param string $username
80+
* @param string $password
81+
* @param array $header
82+
* @dataProvider provideValidAuthentication
83+
*/
84+
public function testIsAuthenticatedWithValidCredential(string $username, string $password, array $header): void
8985
{
9086
$this->request
9187
->getHeader('Authorization')
92-
->willReturn(['Basic QWxhZGRpbjpPcGVuU2VzYW1l']);
88+
->willReturn($header);
9389
$this->request
9490
->withAttribute(UserInterface::class, Argument::type(UserInterface::class))
9591
->willReturn($this->request->reveal());
9692

9793
$this->authenticatedUser
9894
->getIdentity()
99-
->willReturn('Aladdin');
95+
->willReturn($username);
10096
$this->userRepository
101-
->authenticate('Aladdin', 'OpenSesame')
97+
->authenticate($username, $password)
10298
->willReturn($this->authenticatedUser->reveal());
10399

104100
$basicAccess = new BasicAccess(
@@ -112,7 +108,7 @@ public function testIsAuthenticatedWithValidCredential()
112108
$this->assertEquals('Aladdin', $user->getIdentity());
113109
}
114110

115-
public function testIsAuthenticatedWithNoCredential()
111+
public function testIsAuthenticatedWithNoCredential(): void
116112
{
117113
$this->request
118114
->getHeader('Authorization')
@@ -131,7 +127,7 @@ public function testIsAuthenticatedWithNoCredential()
131127
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
132128
}
133129

134-
public function testGetUnauthenticatedResponse()
130+
public function testGetUnauthenticatedResponse(): void
135131
{
136132
$this->responsePrototype
137133
->getHeader('WWW-Authenticate')
@@ -151,7 +147,38 @@ public function testGetUnauthenticatedResponse()
151147

152148
$response = $basicAccess->unauthorizedResponse($this->request->reveal());
153149

154-
$this->assertInstanceOf(ResponseInterface::class, $response);
155150
$this->assertEquals(['Basic realm="test"'], $response->getHeader('WWW-Authenticate'));
156151
}
152+
153+
public function provideInvalidAuthenticationHeader(): array
154+
{
155+
return [
156+
'empty-header' => [[]],
157+
'missing-basic-prefix' => [['foo']],
158+
'only-username' => [['Basic ' . base64_encode('Aladdin')]],
159+
'username-with-colon' => [['Basic ' . base64_encode('Aladdin:')]],
160+
'password-without-username' => [['Basic ' . base64_encode(':OpenSesame')]],
161+
'base64-encoded-pile-of-poo-emoji' => [['Basic ' . base64_encode('💩')]],
162+
'password-containing-colon' => [['Basic ' . base64_encode('username:password:containing:colons:')]],
163+
'only-one-colon' => [['Basic ' . base64_encode(':')]],
164+
'multiple-colons' => [['Basic ' . base64_encode(':::::::')]],
165+
'pile-of-poo-emoji' => [['Basic 💩']],
166+
'only-pile-of-poo-emoji' => [['💩']],
167+
'basic-prefix-without-content' => [['Basic ']],
168+
'only-basic' => [['Basic']],
169+
];
170+
}
171+
172+
public function provideValidAuthentication(): array
173+
{
174+
return [
175+
'aladdin' => ['Aladdin', 'OpenSesame', ['Basic ' . base64_encode('Aladdin:OpenSesame')]],
176+
'passwords-with-colon' => ['Aladdin', 'Open:Sesame', ['Basic ' . base64_encode('Aladdin:Open:Sesame')]],
177+
'passwords-with-multiple-colons' => [
178+
'Aladdin',
179+
':Open:Sesame:',
180+
['Basic ' . base64_encode('Aladdin::Open:Sesame:')]
181+
],
182+
];
183+
}
157184
}

0 commit comments

Comments
 (0)