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

Commit 1f04d88

Browse files
committed
ResponseFactory is passed to service
Before the response prototype was passed to the service so the second fetch of the service used the same response. The brand new response should be created each time the service is used.
1 parent edec9b1 commit 1f04d88

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

src/BasicAccess.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication-basic for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication-basic/blob/master/LICENSE.md
66
* New BSD License
77
*/
@@ -27,30 +27,24 @@ class BasicAccess implements AuthenticationInterface
2727
protected $realm;
2828

2929
/**
30-
* @var ResponseInterface
30+
* @var callable
3131
*/
32-
protected $responsePrototype;
32+
protected $responseFactory;
3333

34-
/**
35-
* Constructor
36-
*
37-
* @param UserRepositoryInterface $repository
38-
* @param string $realm
39-
* @param ResponseInterface $responsePrototype
40-
*/
4134
public function __construct(
4235
UserRepositoryInterface $repository,
4336
string $realm,
44-
ResponseInterface $responsePrototype
37+
callable $responseFactory
4538
) {
4639
$this->repository = $repository;
4740
$this->realm = $realm;
48-
$this->responsePrototype = $responsePrototype;
41+
42+
// Ensures type safety of the composed factory
43+
$this->responseFactory = function () use ($responseFactory) : ResponseInterface {
44+
return $responseFactory();
45+
};
4946
}
5047

51-
/**
52-
* {@inheritDoc}
53-
*/
5448
public function authenticate(ServerRequestInterface $request) : ?UserInterface
5549
{
5650
$authHeader = $request->getHeader('Authorization');
@@ -67,12 +61,9 @@ public function authenticate(ServerRequestInterface $request) : ?UserInterface
6761
return $this->repository->authenticate($username, $password);
6862
}
6963

70-
/**
71-
* {@inheritDoc}
72-
*/
7364
public function unauthorizedResponse(ServerRequestInterface $request) : ResponseInterface
7465
{
75-
return $this->responsePrototype
66+
return ($this->responseFactory)()
7667
->withHeader(
7768
'WWW-Authenticate',
7869
sprintf('Basic realm="%s"', $this->realm)

src/BasicAccessFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication-basic for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication-basic/blob/master/LICENSE.md
66
* New BSD License
77
*/
88

99
namespace Zend\Expressive\Authentication\Basic;
1010

1111
use Psr\Container\ContainerInterface;
12+
use Psr\Http\Message\ResponseInterface;
1213
use Zend\Expressive\Authentication\Exception;
13-
use Zend\Expressive\Authentication\ResponsePrototypeTrait;
1414
use Zend\Expressive\Authentication\UserRepositoryInterface;
1515

1616
class BasicAccessFactory
1717
{
18-
use ResponsePrototypeTrait;
19-
2018
public function __invoke(ContainerInterface $container) : BasicAccess
2119
{
2220
$userRegister = $container->has(UserRepositoryInterface::class)
@@ -40,7 +38,7 @@ public function __invoke(ContainerInterface $container) : BasicAccess
4038
return new BasicAccess(
4139
$userRegister,
4240
$realm,
43-
$this->getResponsePrototype($container)
41+
$container->get(ResponseInterface::class)
4442
);
4543
}
4644
}

test/BasicAccessFactoryTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication-basic for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication-basic/blob/master/LICENSE.md
66
* New BSD License
77
*/
88

99
namespace ZendTest\Expressive\Authentication\Basic;
1010

11+
use PHPUnit\Framework\Assert;
1112
use PHPUnit\Framework\TestCase;
12-
use Prophecy\Argument;
1313
use Prophecy\Prophecy\ObjectProphecy;
1414
use Psr\Container\ContainerInterface;
1515
use Psr\Http\Message\ResponseInterface;
16+
use ReflectionProperty;
1617
use Zend\Expressive\Authentication\Basic\BasicAccess;
1718
use Zend\Expressive\Authentication\Basic\BasicAccessFactory;
1819
use Zend\Expressive\Authentication\Exception\InvalidConfigException;
@@ -29,6 +30,9 @@ class BasicAccessFactoryTest extends TestCase
2930
/** @var UserRepositoryInterface|ObjectProphecy */
3031
private $userRegister;
3132

33+
/** @var ResponseInterface|ObjectProphecy */
34+
private $responsePrototype;
35+
3236
/** @var callback */
3337
private $responseFactory;
3438

@@ -37,8 +41,9 @@ protected function setUp()
3741
$this->container = $this->prophesize(ContainerInterface::class);
3842
$this->factory = new BasicAccessFactory();
3943
$this->userRegister = $this->prophesize(UserRepositoryInterface::class);
44+
$this->responsePrototype = $this->prophesize(ResponseInterface::class);
4045
$this->responseFactory = function () {
41-
return $this->prophesize(ResponseInterface::class)->reveal();
46+
return $this->responsePrototype->reveal();
4247
};
4348
}
4449

@@ -92,5 +97,14 @@ public function testInvokeWithContainerAndConfig()
9297

9398
$basicAccess = ($this->factory)($this->container->reveal());
9499
$this->assertInstanceOf(BasicAccess::class, $basicAccess);
100+
$this->assertResponseFactoryReturns($this->responsePrototype->reveal(), $basicAccess);
101+
}
102+
103+
public static function assertResponseFactoryReturns(ResponseInterface $expected, BasicAccess $service) : void
104+
{
105+
$r = new ReflectionProperty($service, 'responseFactory');
106+
$r->setAccessible(true);
107+
$responseFactory = $r->getValue($service);
108+
Assert::assertSame($expected, $responseFactory());
95109
}
96110
}

test/BasicAccessTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication-basic for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication-basic/blob/master/LICENSE.md
66
* New BSD License
77
*/
@@ -32,20 +32,26 @@ class BasicAccessTest extends TestCase
3232
/** @var ResponseInterface|ObjectProphecy */
3333
private $responsePrototype;
3434

35+
/** @var callable */
36+
private $responseFactory;
37+
3538
protected function setUp()
3639
{
3740
$this->request = $this->prophesize(ServerRequestInterface::class);
3841
$this->userRepository = $this->prophesize(UserRepositoryInterface::class);
3942
$this->authenticatedUser = $this->prophesize(UserInterface::class);
4043
$this->responsePrototype = $this->prophesize(ResponseInterface::class);
44+
$this->responseFactory = function () {
45+
return $this->responsePrototype->reveal();
46+
};
4147
}
4248

4349
public function testConstructor()
4450
{
4551
$basicAccess = new BasicAccess(
4652
$this->userRepository->reveal(),
4753
'test',
48-
$this->responsePrototype->reveal()
54+
$this->responseFactory
4955
);
5056
$this->assertInstanceOf(AuthenticationInterface::class, $basicAccess);
5157
}
@@ -59,7 +65,7 @@ public function testIsAuthenticatedWithoutHeader()
5965
$basicAccess = new BasicAccess(
6066
$this->userRepository->reveal(),
6167
'test',
62-
$this->responsePrototype->reveal()
68+
$this->responseFactory
6369
);
6470
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
6571
}
@@ -73,7 +79,7 @@ public function testIsAuthenticatedWithoutBasic()
7379
$basicAccess = new BasicAccess(
7480
$this->userRepository->reveal(),
7581
'test',
76-
$this->responsePrototype->reveal()
82+
$this->responseFactory
7783
);
7884

7985
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
@@ -98,7 +104,7 @@ public function testIsAuthenticatedWithValidCredential()
98104
$basicAccess = new BasicAccess(
99105
$this->userRepository->reveal(),
100106
'test',
101-
$this->responsePrototype->reveal()
107+
$this->responseFactory
102108
);
103109

104110
$user = $basicAccess->authenticate($this->request->reveal());
@@ -119,7 +125,7 @@ public function testIsAuthenticatedWithNoCredential()
119125
$basicAccess = new BasicAccess(
120126
$this->userRepository->reveal(),
121127
'test',
122-
$this->responsePrototype->reveal()
128+
$this->responseFactory
123129
);
124130

125131
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
@@ -140,7 +146,7 @@ public function testGetUnauthenticatedResponse()
140146
$basicAccess = new BasicAccess(
141147
$this->userRepository->reveal(),
142148
'test',
143-
$this->responsePrototype->reveal()
149+
$this->responseFactory
144150
);
145151

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

0 commit comments

Comments
 (0)