Skip to content

Commit 30084fb

Browse files
committed
Merge pull request #12 from php-http/immutable_authentication
Make authentication methods immutable
2 parents 8a29f85 + 24a874c commit 30084fb

File tree

9 files changed

+12
-164
lines changed

9 files changed

+12
-164
lines changed

spec/Authentication/BasicAuthSpec.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,11 @@ function it_has_a_username()
2424
$this->getUsername()->shouldReturn('john.doe');
2525
}
2626

27-
function it_accepts_a_username()
28-
{
29-
$this->setUsername('jane.doe');
30-
31-
$this->getUsername()->shouldReturn('jane.doe');
32-
}
33-
3427
function it_has_a_password()
3528
{
3629
$this->getPassword()->shouldReturn('secret');
3730
}
3831

39-
function it_accepts_a_password()
40-
{
41-
$this->setPassword('very_secret');
42-
43-
$this->getPassword()->shouldReturn('very_secret');
44-
}
45-
4632
function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
4733
{
4834
$request->withHeader('Authorization', 'Basic '.base64_encode('john.doe:secret'))->willReturn($newRequest);

spec/Authentication/BearerSpec.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ function it_has_a_token()
2424
$this->getToken()->shouldReturn('token');
2525
}
2626

27-
function it_accepts_a_token()
28-
{
29-
$this->setToken('another_token');
30-
31-
$this->getToken()->shouldReturn('another_token');
32-
}
33-
3427
function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
3528
{
3629
$request->withHeader('Authorization', 'Bearer token')->willReturn($newRequest);

spec/Authentication/ChainSpec.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,11 @@ function it_accepts_an_authentication_chain_in_the_constructor(Authentication $a
2424
$this->getAuthenticationChain()->shouldReturn($chain);
2525
}
2626

27-
function it_sets_the_authentication_chain(Authentication $auth1, Authentication $auth2)
27+
function it_throws_an_exception_when_non_authentication_is_passed()
2828
{
29-
// This SHOULD be replaced
30-
$this->beConstructedWith([$auth1]);
29+
$this->beConstructedWith(['authentication']);
3130

32-
$this->setAuthenticationChain([$auth2]);
33-
34-
$this->getAuthenticationChain()->shouldReturn([$auth2]);
35-
}
36-
37-
function it_adds_an_authentication_method(Authentication $auth1, Authentication $auth2)
38-
{
39-
// This SHOULD NOT be replaced
40-
$this->beConstructedWith([$auth1]);
41-
42-
$this->addAuthentication($auth2);
43-
44-
$this->getAuthenticationChain()->shouldReturn([$auth1, $auth2]);
45-
}
46-
47-
function it_clears_the_authentication_chain(Authentication $auth1, Authentication $auth2)
48-
{
49-
// This SHOULD be replaced
50-
$this->beConstructedWith([$auth1]);
51-
52-
$this->clearAuthenticationChain();
53-
54-
$this->addAuthentication($auth2);
55-
56-
$this->getAuthenticationChain()->shouldReturn([$auth2]);
31+
$this->shouldThrow('InvalidArgumentException')->duringInstantiation();
5732
}
5833

5934
function it_authenticates_a_request(

spec/Authentication/MatchingSpec.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,11 @@ function it_has_an_authentication(Authentication $authentication)
2929
$this->getAuthentication()->shouldReturn($authentication);
3030
}
3131

32-
function it_accepts_an_authentication(Authentication $anotherAuthentication)
33-
{
34-
$this->setAuthentication($anotherAuthentication);
35-
36-
$this->getAuthentication()->shouldReturn($anotherAuthentication);
37-
}
38-
3932
function it_has_a_matcher()
4033
{
4134
$this->getMatcher()->shouldReturn($this->matcher);
4235
}
4336

44-
function it_accepts_a_matcher()
45-
{
46-
$matcher = function($request) { return false; };
47-
48-
$this->setMatcher($matcher);
49-
50-
$this->getMatcher()->shouldReturn($matcher);
51-
}
52-
5337
function it_authenticates_a_request(Authentication $authentication, RequestInterface $request, RequestInterface $newRequest)
5438
{
5539
$authentication->authenticate($request)->willReturn($newRequest);
@@ -61,7 +45,7 @@ function it_does_not_authenticate_a_request(Authentication $authentication, Requ
6145
{
6246
$matcher = function($request) { return false; };
6347

64-
$this->setMatcher($matcher);
48+
$this->beConstructedWith($authentication, $matcher);
6549

6650
$authentication->authenticate($request)->shouldNotBeCalled();
6751

spec/Authentication/WsseSpec.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,11 @@ function it_has_a_username()
2525
$this->getUsername()->shouldReturn('john.doe');
2626
}
2727

28-
function it_accepts_a_username()
29-
{
30-
$this->setUsername('jane.doe');
31-
32-
$this->getUsername()->shouldReturn('jane.doe');
33-
}
34-
3528
function it_has_a_password()
3629
{
3730
$this->getPassword()->shouldReturn('secret');
3831
}
3932

40-
function it_accepts_a_password()
41-
{
42-
$this->setPassword('very_secret');
43-
44-
$this->getPassword()->shouldReturn('very_secret');
45-
}
46-
4733
function it_authenticates_a_request(
4834
RequestInterface $request,
4935
RequestInterface $newRequest,

src/Authentication/Bearer.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ public function getToken()
3535
return $this->token;
3636
}
3737

38-
/**
39-
* Sets the token.
40-
*
41-
* @param string $token
42-
*/
43-
public function setToken($token)
44-
{
45-
$this->token = $token;
46-
}
47-
4838
/**
4939
* {@inheritdoc}
5040
*/

src/Authentication/Chain.php

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ final class Chain implements Authentication
2222
*/
2323
public function __construct(array $authenticationChain = [])
2424
{
25-
$this->setAuthenticationChain($authenticationChain);
26-
}
25+
foreach ($authenticationChain as $authentication) {
26+
if (!$authentication instanceof Authentication) {
27+
throw new \InvalidArgumentException(
28+
'Members of the authentication chain must be of type Http\Message\Authentication'
29+
);
30+
}
31+
}
2732

28-
/**
29-
* Adds an Authentication method to the chain.
30-
*
31-
* The order of authentication methods SHOULD NOT matter.
32-
*
33-
* @param Authentication $authentication
34-
*/
35-
public function addAuthentication(Authentication $authentication)
36-
{
37-
$this->authenticationChain[] = $authentication;
33+
$this->authenticationChain = $authenticationChain;
3834
}
3935

4036
/**
@@ -47,28 +43,6 @@ public function getAuthenticationChain()
4743
return $this->authenticationChain;
4844
}
4945

50-
/**
51-
* Replaces the current authentication chain.
52-
*
53-
* @param array $authenticationChain
54-
*/
55-
public function setAuthenticationChain(array $authenticationChain)
56-
{
57-
$this->clearAuthenticationChain();
58-
59-
foreach ($authenticationChain as $authentication) {
60-
$this->addAuthentication($authentication);
61-
}
62-
}
63-
64-
/**
65-
* Clears the authentication chain.
66-
*/
67-
public function clearAuthenticationChain()
68-
{
69-
$this->authenticationChain = [];
70-
}
71-
7246
/**
7347
* {@inheritdoc}
7448
*/

src/Authentication/Matching.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ public function getAuthentication()
4848
return $this->authentication;
4949
}
5050

51-
/**
52-
* Sets the authentication.
53-
*
54-
* @param Authentication $authentication
55-
*/
56-
public function setAuthentication(Authentication $authentication)
57-
{
58-
$this->authentication = $authentication;
59-
}
60-
6151
/**
6252
* Returns the matcher.
6353
*
@@ -68,16 +58,6 @@ public function getMatcher()
6858
return $this->matcher;
6959
}
7060

71-
/**
72-
* Sets the matcher.
73-
*
74-
* @param callable $matcher
75-
*/
76-
public function setMatcher(callable $matcher)
77-
{
78-
$this->matcher = $matcher;
79-
}
80-
8161
/**
8262
* {@inheritdoc}
8363
*/

src/Authentication/UserPasswordPair.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ public function getUsername()
2929
return $this->username;
3030
}
3131

32-
/**
33-
* Sets the username.
34-
*
35-
* @param string $username
36-
*/
37-
public function setUsername($username)
38-
{
39-
$this->username = $username;
40-
}
41-
4232
/**
4333
* Returns the password.
4434
*
@@ -48,14 +38,4 @@ public function getPassword()
4838
{
4939
return $this->password;
5040
}
51-
52-
/**
53-
* Sets the password.
54-
*
55-
* @param string $password
56-
*/
57-
public function setPassword($password)
58-
{
59-
$this->password = $password;
60-
}
6141
}

0 commit comments

Comments
 (0)