15
15
use Symfony \Component \Validator \Constraints \NotPwned ;
16
16
use Symfony \Component \Validator \Constraints \NotPwnedValidator ;
17
17
use Symfony \Component \Validator \Test \ConstraintValidatorTestCase ;
18
+ use Symfony \Contracts \HttpClient \Exception \ServerExceptionInterface ;
19
+ use Symfony \Contracts \HttpClient \HttpClientInterface ;
20
+ use Symfony \Contracts \HttpClient \ResponseInterface ;
18
21
19
22
/**
20
23
* @author Kévin Dunglas <[email protected] >
@@ -26,27 +29,35 @@ class NotPwnedValidatorTest extends ConstraintValidatorTestCase
26
29
private const PASSWORD_LEAKED = 'maman ' ;
27
30
private const PASSWORD_NOT_LEAKED = ']<0585"%sb^5aa$w6!b38",,72?dp3r4\45b28Hy ' ;
28
31
29
- private const RETURN = array (
32
+ private const RETURN = [
30
33
'35E033023A46402F94CFB4F654C5BFE44A1:1 ' ,
31
34
'35F079CECCC31812288257CD770AA7968D7:53 ' ,
32
35
'36039744C253F9B2A4E90CBEDB02EBFB82D:5 ' , // this is the matching line, password: maman
33
36
'3686792BBC66A72D40D928ED15621124CFE:7 ' ,
34
37
'36EEC709091B810AA240179A44317ED415C:2 ' ,
35
- ) ;
38
+ ] ;
36
39
37
40
protected function createValidator ()
38
41
{
39
- $ httpClient = function (string $ url ) {
40
- if (self ::PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL === $ url ) {
41
- // Simulate a connection error
42
- return false ;
43
- }
44
-
45
- return implode ("\r\n" , self ::RETURN );
46
- };
47
-
48
- // Pass null instead of this mock to run the tests against the real API
49
- return new NotPwnedValidator ($ httpClient );
42
+ $ httpClientStub = $ this ->createMock (HttpClientInterface::class);
43
+ $ httpClientStub ->method ('request ' )->will (
44
+ $ this ->returnCallback (function (string $ method , string $ url ): ResponseInterface {
45
+ if (self ::PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL === $ url ) {
46
+ throw new class ('Problem contacting the Have I been Pwned API. ' ) extends \Exception implements ServerExceptionInterface {
47
+ };
48
+ }
49
+
50
+ $ responseStub = $ this ->createMock (ResponseInterface::class);
51
+ $ responseStub
52
+ ->method ('getContent ' )
53
+ ->willReturn (implode ("\r\n" , self ::RETURN ));
54
+
55
+ return $ responseStub ;
56
+ })
57
+ );
58
+
59
+ // Pass HttpClient::create() instead of this mock to run the tests against the real API
60
+ return new NotPwnedValidator ($ httpClientStub );
50
61
}
51
62
52
63
public function testNullIsValid ()
@@ -75,7 +86,7 @@ public function testInvalidPassword()
75
86
76
87
public function testThresholdReached ()
77
88
{
78
- $ constraint = new NotPwned (array ( 'threshold ' => 3 ) );
89
+ $ constraint = new NotPwned ([ 'threshold ' => 3 ] );
79
90
$ this ->validator ->validate (self ::PASSWORD_LEAKED , $ constraint );
80
91
81
92
$ this ->buildViolation ($ constraint ->message )
@@ -85,7 +96,7 @@ public function testThresholdReached()
85
96
86
97
public function testThresholdNotReached ()
87
98
{
88
- $ constraint = new NotPwned (array ( 'threshold ' => 10 ) );
99
+ $ constraint = new NotPwned ([ 'threshold ' => 10 ] );
89
100
$ this ->validator ->validate (self ::PASSWORD_LEAKED , $ constraint );
90
101
91
102
$ this ->assertNoViolation ();
@@ -112,11 +123,11 @@ public function testInvalidConstraint()
112
123
*/
113
124
public function testInvalidValue ()
114
125
{
115
- $ this ->validator ->validate (array () , new NotPwned ());
126
+ $ this ->validator ->validate ([] , new NotPwned ());
116
127
}
117
128
118
129
/**
119
- * @expectedException \Symfony\Component\Validator \Exception\RuntimeException
130
+ * @expectedException \Symfony\Contracts\HttpClient \Exception\ExceptionInterface
120
131
* @expectedExceptionMessage Problem contacting the Have I been Pwned API.
121
132
*/
122
133
public function testApiError ()
0 commit comments