1616 * limitations under the License.
1717 */
1818
19- use Mockery as m ;
20- use Mockery \Adapter \Phpunit \MockeryPHPUnitIntegration ;
2119use PHPUnit \Framework \TestCase ;
2220use SAML2 \AuthnRequest ;
21+ use Symfony \Component \HttpFoundation \Exception \SessionNotFoundException ;
22+ use Symfony \Component \HttpFoundation \RequestStack ;
23+ use Symfony \Component \HttpFoundation \Session \Session ;
24+ use Symfony \Component \HttpFoundation \Session \Storage \MockArraySessionStorage ;
2325
2426class EngineBlock_Test_Saml2_AuthnRequestSessionRepositoryTest extends TestCase
2527{
26- use MockeryPHPUnitIntegration;
28+ private Session $ session ;
29+ private EngineBlock_Saml2_AuthnRequestSessionRepository $ repo ;
2730
2831 protected function setUp (): void
2932 {
30- $ _SESSION = [];
31- }
32-
33- protected function tearDown (): void
34- {
35- $ _SESSION = [];
33+ $ this ->session = new Session (new MockArraySessionStorage ());
34+ $ requestStack = $ this ->createMock (RequestStack::class);
35+ $ requestStack ->method ('getSession ' )->willReturn ($ this ->session );
36+ $ this ->repo = new EngineBlock_Saml2_AuthnRequestSessionRepository ($ requestStack );
3637 }
3738
3839 private function makeRequest (string $ id ): EngineBlock_Saml2_AuthnRequestAnnotationDecorator
@@ -42,33 +43,101 @@ private function makeRequest(string $id): EngineBlock_Saml2_AuthnRequestAnnotati
4243 return new EngineBlock_Saml2_AuthnRequestAnnotationDecorator ($ authnRequest );
4344 }
4445
45- private function makeRepository (): EngineBlock_Saml2_AuthnRequestSessionRepository
46+ public function test_store_saves_request (): void
47+ {
48+ $ request = $ this ->makeRequest ('_sp-request-A ' );
49+
50+ $ this ->repo ->store ($ request );
51+
52+ $ this ->assertSame ($ request , $ this ->repo ->findRequestById ('_sp-request-A ' ));
53+ }
54+
55+ public function test_find_request_by_id_returns_null_for_unknown_id (): void
4656 {
47- $ logger = m::mock (Psr \Log \LoggerInterface::class);
48- return new EngineBlock_Saml2_AuthnRequestSessionRepository ($ logger );
57+ $ this ->assertNull ($ this ->repo ->findRequestById ('_unknown ' ));
4958 }
5059
51- public function test_store_saves_request ()
60+ public function test_link_stores_request_mapping (): void
5261 {
53- $ repository = $ this ->makeRepository ( );
54- $ request = $ this ->makeRequest ('_sp -request-A ' );
62+ $ spRequest = $ this ->makeRequest ( ' _sp-request-A ' );
63+ $ idpRequest = $ this ->makeRequest ('_idp -request-B ' );
5564
56- $ repository ->store ($ request );
65+ $ this ->repo ->store ($ spRequest );
66+ $ this ->repo ->link ($ idpRequest , $ spRequest );
5767
58- $ storedRequest = $ repository ->findRequestById ('_sp-request-A ' );
59- $ this ->assertSame ($ request , $ storedRequest );
68+ $ this ->assertSame ('_sp-request-A ' , $ this ->repo ->findLinkedRequestId ('_idp-request-B ' ));
6069 }
6170
62- public function test_link_stores_request_mapping ()
71+ public function test_find_linked_request_id_returns_null_for_unknown_id (): void
6372 {
64- $ repository = $ this ->makeRepository ();
65- $ spRequest = $ this ->makeRequest ('_sp-request-A ' );
66- $ idpRequest = $ this ->makeRequest ('_idp-request-B ' );
73+ $ this ->assertNull ($ this ->repo ->findLinkedRequestId ('_unknown ' ));
74+ }
75+
76+ public function test_find_linked_request_id_returns_null_for_null_input (): void
77+ {
78+ $ this ->assertNull ($ this ->repo ->findLinkedRequestId (null ));
79+ }
80+
81+ public function test_store_and_find_multiple_requests (): void
82+ {
83+ $ req1 = $ this ->makeRequest ('_req-1 ' );
84+ $ req2 = $ this ->makeRequest ('_req-2 ' );
85+
86+ $ this ->repo ->store ($ req1 );
87+ $ this ->repo ->store ($ req2 );
88+
89+ $ this ->assertSame ($ req1 , $ this ->repo ->findRequestById ('_req-1 ' ));
90+ $ this ->assertSame ($ req2 , $ this ->repo ->findRequestById ('_req-2 ' ));
91+ }
92+
93+ public function test_store_is_noop_when_no_session_available (): void
94+ {
95+ $ requestStack = $ this ->createMock (RequestStack::class);
96+ $ requestStack ->method ('getSession ' )
97+ ->willThrowException (new SessionNotFoundException ());
98+
99+ $ repo = new EngineBlock_Saml2_AuthnRequestSessionRepository ($ requestStack );
100+ $ request = $ this ->makeRequest ('_req-A ' );
101+
102+ $ repo ->store ($ request ); // must not throw
103+
104+ $ this ->assertNull ($ repo ->findRequestById ('_req-A ' ));
105+ }
106+
107+ public function test_link_is_noop_when_no_session_available (): void
108+ {
109+ $ requestStack = $ this ->createMock (RequestStack::class);
110+ $ requestStack ->method ('getSession ' )
111+ ->willThrowException (new SessionNotFoundException ());
112+
113+ $ repo = new EngineBlock_Saml2_AuthnRequestSessionRepository ($ requestStack );
114+ $ spRequest = $ this ->makeRequest ('_sp-A ' );
115+ $ idpRequest = $ this ->makeRequest ('_idp-B ' );
116+
117+ $ repo ->link ($ idpRequest , $ spRequest ); // must not throw
118+
119+ $ this ->assertNull ($ repo ->findLinkedRequestId ('_idp-B ' ));
120+ }
121+
122+ public function test_find_request_is_noop_when_no_session_available (): void
123+ {
124+ $ requestStack = $ this ->createMock (RequestStack::class);
125+ $ requestStack ->method ('getSession ' )
126+ ->willThrowException (new SessionNotFoundException ());
127+
128+ $ repo = new EngineBlock_Saml2_AuthnRequestSessionRepository ($ requestStack );
129+
130+ $ this ->assertNull ($ repo ->findRequestById ('_req-A ' ));
131+ }
132+
133+ public function test_find_linked_is_noop_when_no_session_available (): void
134+ {
135+ $ requestStack = $ this ->createMock (RequestStack::class);
136+ $ requestStack ->method ('getSession ' )
137+ ->willThrowException (new SessionNotFoundException ());
67138
68- $ repository ->store ($ spRequest );
69- $ repository ->link ($ idpRequest , $ spRequest );
139+ $ repo = new EngineBlock_Saml2_AuthnRequestSessionRepository ($ requestStack );
70140
71- $ linkedRequestId = $ repository ->findLinkedRequestId ('_idp-request-B ' );
72- $ this ->assertSame ('_sp-request-A ' , $ linkedRequestId );
141+ $ this ->assertNull ($ repo ->findLinkedRequestId ('_req-A ' ));
73142 }
74143}
0 commit comments