1717use Scheb \TwoFactorBundle \Security \TwoFactor \Event \TwoFactorCodeReusedEvent ;
1818use Scheb \TwoFactorBundle \Tests \TestCase ;
1919use stdClass ;
20- use Symfony \Component \Cache \CacheItem ;
2120use Symfony \Component \Security \Core \User \UserInterface ;
2221use Symfony \Contracts \EventDispatcher \EventDispatcherInterface ;
2322
2726class CheckTwoFactorCodeReuseListenerTest extends TestCase
2827{
2928 private const string MFA_CODE = '123456 ' ;
29+ private const string CACHE_KEY = 'scheb_two_factor_code_reuse.3468ccbd044e2fdfb0769b68b5c85d7660c6c12c ' ;
3030 private const string USER_IDENTIFIER = 'jdoe@example.com ' ;
3131
3232 private MockObject &EventDispatcherInterface $ eventDispatcher ;
@@ -44,7 +44,21 @@ protected function setUp(): void
4444 $ this ->logger = $ this ->createMock (LoggerInterface::class);
4545 }
4646
47- protected function expectDoNothing (): void
47+ private function stubCacheItemIsHit (CacheItemPoolInterface &MockObject $ cacheProvider , bool $ isHit ): void
48+ {
49+ $ cacheItem = $ this ->createMock (CacheItemInterface::class);
50+ $ cacheItem
51+ ->expects ($ this ->any ())
52+ ->method ('isHit ' )
53+ ->willReturn ($ isHit );
54+
55+ $ cacheProvider ->expects ($ this ->once ())
56+ ->method ('getItem ' )
57+ ->with (self ::CACHE_KEY )
58+ ->willReturn ($ cacheItem );
59+ }
60+
61+ private function expectDoNothing (): void
4862 {
4963 $ this ->eventDispatcher
5064 ->expects ($ this ->never ())
@@ -79,13 +93,7 @@ public function checkForCodeReuse_invalidCacheProvider_nothingHappensButLogged()
7993 public function checkForCodeReuse_validCacheProviderAndNoHit_nothingHappens (): void
8094 {
8195 $ cacheProvider = $ this ->createMock (CacheItemPoolInterface::class);
82- $ cacheItem = new CacheItem ();
83- $ cacheItem ->set (true );
84- $ cacheItem ->expiresAfter (60 );
85- $ cacheProvider ->expects ($ this ->once ())
86- ->method ('getItem ' )
87- ->with ('scheb_two_factor_code_reuse.3468ccbd044e2fdfb0769b68b5c85d7660c6c12c ' )
88- ->willReturn ($ cacheItem );
96+ $ this ->stubCacheItemIsHit ($ cacheProvider , false );
8997
9098 $ listener = new CheckTwoFactorCodeReuseListener ($ this ->eventDispatcher , $ cacheProvider , 20 , $ this ->logger );
9199 $ this ->expectDoNothing ();
@@ -97,13 +105,7 @@ public function checkForCodeReuse_validCacheProviderAndNoHit_nothingHappens(): v
97105 public function checkForCodeReuse_validCacheProviderWithCacheHit_eventIsDispatched (): void
98106 {
99107 $ cacheProvider = $ this ->createMock (CacheItemPoolInterface::class);
100-
101- $ cacheItem = $ this ->createMock (CacheItemInterface::class);
102- $ cacheItem ->expects ($ this ->once ())->method ('isHit ' )->willReturn (true );
103-
104- $ cacheProvider ->expects ($ this ->once ())
105- ->method ('getItem ' )
106- ->willReturn ($ cacheItem );
108+ $ this ->stubCacheItemIsHit ($ cacheProvider , true );
107109
108110 $ this ->eventDispatcher ->expects ($ this ->once ())
109111 ->method ('dispatch ' )
@@ -115,7 +117,7 @@ public function checkForCodeReuse_validCacheProviderWithCacheHit_eventIsDispatch
115117 }
116118
117119 #[Test]
118- public function checkForCodeReuse_validCacheProviderNoCacheHit_cacheItemIsSaved (): void
120+ public function rememberCode_validCacheProvider_cacheItemIsSaved (): void
119121 {
120122 $ cacheItem = new class implements CacheItemInterface {
121123 private mixed $ value ;
@@ -171,12 +173,12 @@ public function expiresAfter(DateInterval|int|null $time): static
171173 $ cacheItem ->expiresAfter (60 );
172174 $ cacheProvider ->expects ($ this ->once ())
173175 ->method ('getItem ' )
174- ->with (' scheb_two_factor_code_reuse.3468ccbd044e2fdfb0769b68b5c85d7660c6c12c ' )
176+ ->with (self :: CACHE_KEY )
175177 ->willReturn ($ cacheItem );
176178
177179 $ listener = new CheckTwoFactorCodeReuseListener ($ this ->eventDispatcher , $ cacheProvider , 20 , $ this ->logger );
178180
179- $ listener ->checkForCodeReuse (new TwoFactorCodeEvent ($ this ->user , self ::MFA_CODE ));
181+ $ listener ->rememberCode (new TwoFactorCodeEvent ($ this ->user , self ::MFA_CODE ));
180182
181183 $ this ->assertSame (20 , $ cacheItem ->expiresAfter );
182184 $ this ->assertSame (true , $ cacheItem ->get ());
0 commit comments