2424use CodeIgniter \Test \Mock \MockSecurity ;
2525use Config \Security as SecurityConfig ;
2626use PHPUnit \Framework \Attributes \BackupGlobals ;
27+ use PHPUnit \Framework \Attributes \DataProvider ;
2728use PHPUnit \Framework \Attributes \Group ;
2829
2930/**
@@ -42,13 +43,23 @@ protected function setUp(): void
4243 $ this ->resetServices ();
4344 }
4445
45- private function createMockSecurity (? SecurityConfig $ config = null ): MockSecurity
46+ private static function createMockSecurity (SecurityConfig $ config = new SecurityConfig () ): MockSecurity
4647 {
47- $ config ??= new SecurityConfig ();
48-
4948 return new MockSecurity ($ config );
5049 }
5150
51+ private static function createIncomingRequest (): IncomingRequest
52+ {
53+ $ config = new MockAppConfig ();
54+
55+ return new IncomingRequest (
56+ $ config ,
57+ new SiteURI ($ config ),
58+ null ,
59+ new UserAgent (),
60+ );
61+ }
62+
5263 public function testBasicConfigIsSaved (): void
5364 {
5465 $ security = $ this ->createMockSecurity ();
@@ -108,18 +119,6 @@ public function testCSRFVerifyPostThrowsExceptionOnNoMatch(): void
108119 $ security ->verify ($ request );
109120 }
110121
111- private function createIncomingRequest (): IncomingRequest
112- {
113- $ config = new MockAppConfig ();
114-
115- return new IncomingRequest (
116- $ config ,
117- new SiteURI ($ config ),
118- null ,
119- new UserAgent (),
120- );
121- }
122-
123122 public function testCSRFVerifyPostReturnsSelfOnMatch (): void
124123 {
125124 $ _SERVER ['REQUEST_METHOD ' ] = 'POST ' ;
@@ -354,24 +353,34 @@ public function testGetPostedTokenReturnsTokenFromFormBody(): void
354353 $ this ->assertSame ('8b9218a55906f9dcc1dc263dce7f005a ' , $ method ($ request ));
355354 }
356355
357- public function testGetPostedTokenReturnsNullForInvalidInputs (): void
356+ #[DataProvider('provideGetPostedTokenReturnsNullForInvalidInputs ' )]
357+ public function testGetPostedTokenReturnsNullForInvalidInputs (string $ case , IncomingRequest $ request ): void
358+ {
359+ $ method = $ this ->getPrivateMethodInvoker ($ this ->createMockSecurity (), 'getPostedToken ' );
360+
361+ $ this ->assertNull (
362+ $ method ($ request ),
363+ sprintf ('Failed asserting that %s returns null on invalid input. ' , $ case ),
364+ );
365+ }
366+
367+ /**
368+ * @return iterable<string, array{string, IncomingRequest}>
369+ */
370+ public static function provideGetPostedTokenReturnsNullForInvalidInputs (): iterable
358371 {
359- $ method = $ this ->getPrivateMethodInvoker ($ this ->createMockSecurity (), 'getPostedToken ' );
360372 $ testCases = [
361- 'empty_post ' => $ this -> createIncomingRequest (),
362- 'invalid_post_data ' => $ this -> createIncomingRequest ()->setGlobal ('post ' , ['csrf_test_name ' => ['invalid ' => 'data ' ]]),
363- 'empty_header ' => $ this -> createIncomingRequest ()->setHeader ('X-CSRF-TOKEN ' , '' ),
364- 'invalid_json_data ' => $ this -> createIncomingRequest ()->setBody (json_encode (['csrf_test_name ' => ['invalid ' => 'data ' ]])),
365- 'invalid_json ' => $ this -> createIncomingRequest ()->setBody ('{invalid json} ' ),
366- 'missing_token_in_body ' => $ this -> createIncomingRequest ()->setBody ('other=value&another=test ' ),
367- 'invalid_form_data ' => $ this -> createIncomingRequest ()->setBody ('csrf_test_name[]=invalid ' ),
373+ 'empty_post ' => self :: createIncomingRequest (),
374+ 'invalid_post_data ' => self :: createIncomingRequest ()->setGlobal ('post ' , ['csrf_test_name ' => ['invalid ' => 'data ' ]]),
375+ 'empty_header ' => self :: createIncomingRequest ()->setHeader ('X-CSRF-TOKEN ' , '' ),
376+ 'invalid_json_data ' => self :: createIncomingRequest ()->setBody (json_encode (['csrf_test_name ' => ['invalid ' => 'data ' ]])),
377+ 'invalid_json ' => self :: createIncomingRequest ()->setBody ('{invalid json} ' ),
378+ 'missing_token_in_body ' => self :: createIncomingRequest ()->setBody ('other=value&another=test ' ),
379+ 'invalid_form_data ' => self :: createIncomingRequest ()->setBody ('csrf_test_name[]=invalid ' ),
368380 ];
369381
370382 foreach ($ testCases as $ case => $ request ) {
371- $ this ->assertNull (
372- $ method ($ request ),
373- "Failed asserting that {$ case } returns null "
374- );
383+ yield $ case => [$ case , $ request ];
375384 }
376385 }
377386}
0 commit comments