1414use chillerlan \Authenticator \AuthenticatorOptions ;
1515use chillerlan \Authenticator \Authenticators \AuthenticatorInterface ;
1616use chillerlan \Authenticator \Common \Base32 ;
17- use InvalidArgumentException ;
1817use PHPUnit \Framework \TestCase ;
19- use RuntimeException ;
20- use function strlen ;
18+ use PHPUnit \Framework \Attributes \{DataProvider , Test };
19+ use InvalidArgumentException , RuntimeException ;
20+ use function rawurlencode , sprintf , strlen ;
2121
2222abstract class AuthenticatorInterfaceTestAbstract extends TestCase{
2323
@@ -27,14 +27,18 @@ abstract class AuthenticatorInterfaceTestAbstract extends TestCase{
2727 protected const rawsecret = '12345678901234567890 ' ;
2828 protected const secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ ' ;
2929
30+ protected const label = 'some test-label ' ;
31+ protected const issuer = 'chillerlan.net ' ;
32+
3033 abstract protected function getInstance (AuthenticatorOptions $ options ):AuthenticatorInterface ;
3134
3235 protected function setUp ():void {
3336 $ this ->options = new AuthenticatorOptions ;
3437 $ this ->authenticatorInterface = $ this ->getInstance ($ this ->options );
3538 }
3639
37- public function testSetGetSecret ():void {
40+ #[Test]
41+ public function setGetSecret ():void {
3842 $ this ->authenticatorInterface ->setSecret ($ this ::secret);
3943
4044 $ secret = $ this ->authenticatorInterface ->getSecret ();
@@ -43,34 +47,39 @@ public function testSetGetSecret():void{
4347 $ this ::assertSame ($ this ::rawsecret, Base32::decode ($ secret ));
4448 }
4549
46- public function testSetEmptySecretException ():void {
50+ #[Test]
51+ public function setEmptySecretException ():void {
4752 $ this ->expectException (InvalidArgumentException::class);
4853 $ this ->expectExceptionMessage ('The given secret string is empty ' );
4954
5055 $ this ->authenticatorInterface ->setSecret ('' );
5156 }
5257
53- public function testSetInvalidSecretException ():void {
58+ #[Test]
59+ public function setInvalidSecretException ():void {
5460 $ this ->expectException (InvalidArgumentException::class);
5561
5662 $ this ->authenticatorInterface ->setSecret ('This_is_an_invalid_secret_phrase! ' );
5763 }
5864
59- public function testGetSecretException ():void {
65+ #[Test]
66+ public function getSecretException ():void {
6067 $ this ->expectException (RuntimeException::class);
6168 $ this ->expectExceptionMessage ('No secret set ' );
6269
6370 $ this ->authenticatorInterface ->getSecret ();
6471 }
6572
66- public function testCreateSecretDefaultLength ():void {
73+ #[Test]
74+ public function createSecretDefaultLength ():void {
6775 $ this ::assertSame (
6876 $ this ->options ->secret_length ,
6977 strlen (Base32::decode ($ this ->authenticatorInterface ->createSecret ())),
7078 );
7179 }
7280
73- public function testCreateSecretWithLength ():void {
81+ #[Test]
82+ public function createSecretWithLength ():void {
7483
7584 for ($ secretLength = 16 ; $ secretLength <= 512 ; $ secretLength += 8 ){
7685 $ secret = Base32::decode ($ this ->authenticatorInterface ->createSecret ($ secretLength ));
@@ -80,28 +89,32 @@ public function testCreateSecretWithLength():void{
8089
8190 }
8291
83- public function testCreateSecretCheckCharacterSet ():void {
92+ #[Test]
93+ public function createSecretCheckCharacterSet ():void {
8494 $ secret = $ this ->authenticatorInterface ->createSecret (32 );
8595
8696 $ this ::assertMatchesRegularExpression ('/^[ ' .Base32::CHARSET .']+$/ ' , $ secret );
8797 }
8898
89- public function testCreateSecretException ():void {
99+ #[Test]
100+ public function createSecretException ():void {
90101 $ this ->expectException (InvalidArgumentException::class);
91102 $ this ->expectExceptionMessage ('Invalid secret length ' );
92103
93104 $ this ->authenticatorInterface ->createSecret (10 );
94105 }
95106
96- public function testGetHMACWithoutSecretException ():void {
107+ #[Test]
108+ public function getHMACWithoutSecretException ():void {
97109 $ this ->expectException (RuntimeException::class);
98110 $ this ->expectExceptionMessage ('No secret given ' );
99111
100112 $ this ->authenticatorInterface ->getHMAC (69 );
101113 }
102114
103115 // https://github.com/PHPGangsta/GoogleAuthenticator/pull/25
104- public function testVerifyCodeWithLeadingZero ():void {
116+ #[Test]
117+ public function verifyCodeWithLeadingZero ():void {
105118 $ this ->authenticatorInterface ->setSecret ($ this ::secret);
106119
107120 $ code = $ this ->authenticatorInterface ->code ();
@@ -111,7 +124,8 @@ public function testVerifyCodeWithLeadingZero():void{
111124 }
112125
113126 // coverage
114- public function testGetServertime ():void {
127+ #[Test]
128+ public function getServertime ():void {
115129 $ this ->options ->useLocalTime = false ;
116130
117131 $ servertime = $ this ->authenticatorInterface ->getServerTime ();
@@ -123,4 +137,32 @@ public function testGetServertime():void{
123137 $ this ::assertMatchesRegularExpression ('/^\d+$/ ' , (string )$ servertime );
124138 }
125139
140+ abstract public static function uriSettingsProvider ():array ;
141+
142+ /**
143+ * @param array<string, mixed> $options
144+ */
145+ #[Test]
146+ #[DataProvider('uriSettingsProvider ' )]
147+ public function getUri (array $ options , string $ expected ):void {
148+
149+ $ this ->authenticatorInterface
150+ ->setOptions ($ options )
151+ ->setSecret (self ::secret)
152+ ;
153+
154+ $ this ::assertSame (
155+ sprintf (
156+ 'otpauth://%s/%s?secret=%s&issuer=%s%s ' ,
157+ $ this ->authenticatorInterface ::MODE ,
158+ rawurlencode (self ::label),
159+ self ::secret,
160+ rawurlencode (self ::issuer),
161+ $ expected ,
162+ ),
163+ $ this ->authenticatorInterface ->getUri (self ::label, self ::issuer, 42 ),
164+ );
165+
166+ }
167+
126168}
0 commit comments