Skip to content

Commit 5dd9692

Browse files
authored
Merge pull request #7 from Phauthentic/remove-zend
Remove zend/diactores from require-dev and the tests
2 parents c0b1f83 + b689cde commit 5dd9692

File tree

5 files changed

+178
-72
lines changed

5 files changed

+178
-72
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
],
1010
"require": {
1111
"php": "^7.1",
12-
"phauthentic/password-hashers": "dev-master",
12+
"phauthentic/password-hashers": "^1.0",
1313
"psr/http-factory": "^1.0",
1414
"psr/http-message": "~1.0",
1515
"psr/http-server-handler": "~1.0",
16-
"psr/http-server-middleware": "^1.0",
17-
"zendframework/zend-diactoros": "^1.4.0"
16+
"psr/http-server-middleware": "^1.0"
1817
},
1918
"require-dev": {
2019
"cakephp/cakephp-codesniffer": "^3.0",
2120
"firebase/php-jwt": "~4.0",
2221
"phpstan/phpstan": "^0.10.3",
2322
"phpunit/dbunit": "^4.0",
2423
"phpunit/phpunit": "^7.0",
25-
"squizlabs/php_codesniffer": "^3.3"
24+
"squizlabs/php_codesniffer": "^3.3",
25+
"zendframework/zend-diactoros": "^1.4.0"
2626
},
2727
"suggest": {
2828
"firebase/php-jwt": "If you want to use the JWT adapter add this dependency",

tests/TestCase/AuthenticationServiceTest.php

Lines changed: 109 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,23 @@
3535
use Phauthentic\Authentication\UrlChecker\DefaultUrlChecker;
3636
use Phauthentic\Authentication\UrlChecker\UrlCheckerInterface;
3737
use Phauthentic\PasswordHasher\DefaultPasswordHasher;
38+
use Psr\Http\Message\ResponseInterface;
39+
use Psr\Http\Message\ServerRequestInterface;
40+
use Psr\Http\Message\StreamInterface;
41+
use Psr\Http\Message\UriInterface;
3842
use RuntimeException;
3943
use Zend\Diactoros\Response;
40-
use Zend\Diactoros\ServerRequestFactory;
4144

45+
/**
46+
* Authentication Service Test
47+
*/
4248
class AuthenticationServiceTest extends TestCase
4349
{
50+
/**
51+
* Create a new PasswordIdentifier instance
52+
*
53+
* @return \Phauthentic\Authentication\Identifier\PasswordIdentifier
54+
*/
4455
protected function createPasswordIdentifier()
4556
{
4657
$resolver = new TestResolver($this->getConnection()->getConnection());
@@ -49,6 +60,11 @@ protected function createPasswordIdentifier()
4960
return new PasswordIdentifier($resolver, $passwordHasher);
5061
}
5162

63+
/**
64+
* Create a new SessionAuthenticator instance
65+
*
66+
* @return \Phauthentic\Authentication\Authenticator\SessionAuthenticator
67+
*/
5268
protected function createSessionAuthenticator(IdentifierInterface $identifier = null, StorageInterface $storage = null)
5369
{
5470
if (!$identifier) {
@@ -61,6 +77,11 @@ protected function createSessionAuthenticator(IdentifierInterface $identifier =
6177
return new SessionAuthenticator($identifier, $storage);
6278
}
6379

80+
/**
81+
* Create a new FormAuthenticator instance
82+
*
83+
* @return \Phauthentic\Authentication\Authenticator\FormAuthenticator
84+
*/
6485
protected function createFormAuthenticator(IdentifierInterface $identifier = null, UrlCheckerInterface $urlChecker = null)
6586
{
6687
if (!$identifier) {
@@ -74,6 +95,13 @@ protected function createFormAuthenticator(IdentifierInterface $identifier = nul
7495
return new FormAuthenticator($identifier, $urlChecker);
7596
}
7697

98+
/**
99+
* Create Authenticators
100+
*
101+
* @param \Phauthentic\Authentication\Identifier\IdentifierInterface
102+
* @param \Phauthentic\Authentication\Authenticator\Storage\StorageInterface
103+
* @return \Phauthentic\Authentication\Authenticator\AuthenticatorCollectionInterface
104+
*/
77105
protected function createAuthenticators(IdentifierInterface $identifier = null, StorageInterface $storage = null)
78106
{
79107
$authenticators = new AuthenticatorCollection();
@@ -87,16 +115,56 @@ protected function createAuthenticators(IdentifierInterface $identifier = null,
87115
return $authenticators;
88116
}
89117

118+
/**
119+
* Gets a mocked request
120+
*
121+
* @param string $path Path
122+
* @param array $body Parsed body data as array
123+
* @param array §server Server environment
124+
* @return mixed
125+
*/
126+
protected function getMockRequest($path, $body, $server = [])
127+
{
128+
$request = $this->getMockBuilder(ServerRequestInterface::class)
129+
->getMock();
130+
131+
$uri = $this->getMockBuilder(UriInterface::class)
132+
->getMock();
133+
134+
$uri->expects($this->any())
135+
->method('getPath')
136+
->willReturn($path);
137+
138+
$uri->expects($this->any())
139+
->method('__toString')
140+
->willReturn('http://localhost' . $path);
141+
142+
$request->expects($this->any())
143+
->method('getUri')
144+
->willReturn($uri);
145+
146+
$request->expects($this->any())
147+
->method('getParsedBody')
148+
->willReturn($body);
149+
150+
if (!empty($server)) {
151+
$request->expects($this->any())
152+
->method('getServerParams')
153+
->willReturn($server);
154+
}
155+
156+
return $request;
157+
}
158+
90159
/**
91160
* testAuthenticate
92161
*
93162
* @return void
94163
*/
95-
public function testAuthenticate()
164+
public function testAuthenticate(): void
96165
{
97-
$request = ServerRequestFactory::fromGlobals(
98-
['REQUEST_URI' => '/testpath'],
99-
[],
166+
$request = $this->getMockRequest(
167+
'/testpath',
100168
['username' => 'robert', 'password' => 'robert']
101169
);
102170

@@ -130,11 +198,10 @@ public function testAuthenticate()
130198
*
131199
* @return void
132200
*/
133-
public function testAuthenticateFailure()
201+
public function testAuthenticateFailure(): void
134202
{
135-
$request = ServerRequestFactory::fromGlobals(
136-
['REQUEST_URI' => '/testpath'],
137-
[],
203+
$request = $this->getMockRequest(
204+
'/testpath',
138205
['username' => 'robert', 'password' => 'invalid']
139206
);
140207

@@ -174,10 +241,11 @@ public function testAuthenticateFailure()
174241
*
175242
* @return void
176243
*/
177-
public function testAuthenticateStorage()
244+
public function testAuthenticateStorage(): void
178245
{
179-
$request = ServerRequestFactory::fromGlobals(
180-
['REQUEST_URI' => '/testpath']
246+
$request = $this->getMockRequest(
247+
'/testpath',
248+
[]
181249
);
182250

183251
$storage = $this->createMock(StorageInterface::class);
@@ -221,14 +289,18 @@ public function testAuthenticateStorage()
221289
*
222290
* @return void
223291
*/
224-
public function testAuthenticateWithChallenge()
292+
public function testAuthenticateWithChallenge(): void
225293
{
226-
$request = ServerRequestFactory::fromGlobals([
227-
'SERVER_NAME' => 'example.com',
228-
'REQUEST_URI' => '/testpath',
229-
'PHP_AUTH_USER' => 'robert',
230-
'PHP_AUTH_PW' => 'WRONG'
231-
]);
294+
$request = $this->getMockRequest(
295+
'/testpath',
296+
[],
297+
[
298+
'SERVER_NAME' => 'example.com',
299+
'REQUEST_URI' => '/testpath',
300+
'PHP_AUTH_USER' => 'robert',
301+
'PHP_AUTH_PW' => 'WRONG'
302+
]
303+
);
232304

233305
$identifier = $this->createPasswordIdentifier();
234306
$authenticators = new AuthenticatorCollection([
@@ -247,13 +319,13 @@ public function testAuthenticateWithChallenge()
247319
*
248320
* @return void
249321
*/
250-
public function testPersistAuthenticatedIdentity()
322+
public function testPersistAuthenticatedIdentity(): void
251323
{
252-
$request = ServerRequestFactory::fromGlobals(
253-
['REQUEST_URI' => '/testpath'],
254-
[],
324+
$request = $this->getMockRequest(
325+
'/testpath',
255326
['username' => 'robert', 'password' => 'robert']
256327
);
328+
257329
$response = new Response();
258330

259331
$storage = $this->createMock(StorageInterface::class);
@@ -286,13 +358,13 @@ public function testPersistAuthenticatedIdentity()
286358
*
287359
* @return void
288360
*/
289-
public function testPersistCustomIdentity()
361+
public function testPersistCustomIdentity(): void
290362
{
291-
$request = ServerRequestFactory::fromGlobals(
292-
['REQUEST_URI' => '/testpath'],
293-
[],
363+
$request = $this->getMockRequest(
364+
'/testpath',
294365
['username' => 'robert', 'password' => 'robert']
295366
);
367+
296368
$response = new Response();
297369

298370
$storage = $this->createMock(StorageInterface::class);
@@ -318,11 +390,10 @@ public function testPersistCustomIdentity()
318390
*
319391
* @return void
320392
*/
321-
public function testClearIdentity()
393+
public function testClearIdentity(): void
322394
{
323-
$request = ServerRequestFactory::fromGlobals(
324-
['REQUEST_URI' => '/testpath'],
325-
[],
395+
$request = $this->getMockRequest(
396+
'/testpath',
326397
['username' => 'robert', 'password' => 'robert']
327398
);
328399
$response = new Response();
@@ -352,11 +423,10 @@ public function testClearIdentity()
352423
*
353424
* @return void
354425
*/
355-
public function testNoAuthenticatorsLoadedException()
426+
public function testNoAuthenticatorsLoadedException(): void
356427
{
357-
$request = ServerRequestFactory::fromGlobals(
358-
['REQUEST_URI' => '/testpath'],
359-
[],
428+
$request = $this->getMockRequest(
429+
'/testpath',
360430
['username' => 'robert', 'password' => 'robert']
361431
);
362432

@@ -373,7 +443,7 @@ public function testNoAuthenticatorsLoadedException()
373443
*
374444
* @return void
375445
*/
376-
public function testBuildIdentity()
446+
public function testBuildIdentity(): void
377447
{
378448
$data = new ArrayObject(['username' => 'robert']);
379449
$identity = new Identity($data);
@@ -390,11 +460,10 @@ public function testBuildIdentity()
390460
$this->assertSame($identity, $result);
391461
}
392462

393-
public function testGetIdentity()
463+
public function testGetIdentity(): void
394464
{
395-
$request = ServerRequestFactory::fromGlobals(
396-
['REQUEST_URI' => '/testpath'],
397-
[],
465+
$request = $this->getMockRequest(
466+
'/testpath',
398467
['username' => 'robert', 'password' => 'robert']
399468
);
400469

tests/TestCase/UrlChecker/DefaultUrlCheckerTest.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
45
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
@@ -16,13 +17,13 @@
1617

1718
use Phauthentic\Authentication\UrlChecker\DefaultUrlChecker;
1819
use PHPUnit\Framework\TestCase;
19-
use Zend\Diactoros\ServerRequestFactory;
2020

2121
/**
2222
* DefaultUrlCheckerTest
2323
*/
2424
class DefaultUrlCheckerTest extends TestCase
2525
{
26+
use RequestMockTrait;
2627

2728
/**
2829
* testCheckFailure
@@ -33,10 +34,7 @@ public function testCheckFailure()
3334
{
3435
$checker = new DefaultUrlChecker();
3536

36-
$request = ServerRequestFactory::fromGlobals(
37-
['REQUEST_URI' => '/users/does-not-match']
38-
);
39-
37+
$request = $this->getMockRequest('/users/does-not-match');
4038
$result = $checker->check($request, '/users/login');
4139
$this->assertFalse($result);
4240
}
@@ -49,9 +47,8 @@ public function testCheckFailure()
4947
public function testCheck()
5048
{
5149
$checker = new DefaultUrlChecker();
52-
$request = ServerRequestFactory::fromGlobals(
53-
['REQUEST_URI' => '/users/login']
54-
);
50+
51+
$request = $this->getMockRequest('/users/login');
5552
$result = $checker->check($request, '/users/login');
5653
$this->assertTrue($result);
5754
}
@@ -65,10 +62,8 @@ public function testCheckFull()
6562
{
6663
$checker = new DefaultUrlChecker();
6764
$checker->checkFullUrl(true);
68-
$request = ServerRequestFactory::fromGlobals(
69-
['REQUEST_URI' => '/users/login', 'HTTP_HOST' => 'localhost']
70-
);
7165

66+
$request = $this->getMockRequest('/users/login');
7267
$result = $checker->check($request, 'http://localhost/users/login');
7368
$this->assertTrue($result);
7469
}
@@ -82,10 +77,8 @@ public function testCheckFullFailure()
8277
{
8378
$checker = new DefaultUrlChecker();
8479
$checker->checkFullUrl(true);
85-
$request = ServerRequestFactory::fromGlobals(
86-
['REQUEST_URI' => '/users/does-not-match']
87-
);
8880

81+
$request = $this->getMockRequest('/users/does-not-match');
8982
$result = $checker->check($request, 'http://localhost/users/login');
9083
$this->assertFalse($result);
9184
}

0 commit comments

Comments
 (0)