22
33namespace DutchCodingCompany \LivewireRecaptcha \Tests ;
44
5+ use DutchCodingCompany \LivewireRecaptcha \Tests \Fixtures \MyCustomAttributeComponent ;
56use DutchCodingCompany \LivewireRecaptcha \Tests \Fixtures \MyTestComponent ;
67use Illuminate \Http \Client \Request ;
78use Illuminate \Support \Facades \Http ;
@@ -13,7 +14,7 @@ class CaptchaTest extends TestCase
1314 /**
1415 * @param array{0: bool, 1: array{'success': bool}} $captchaResponse
1516 */
16- #[DataProvider('provideCaptchaData ' )]
17+ #[DataProvider('provideDefaultAttributeData ' )]
1718 public function testInvalidCaptchaResponse (bool $ isValid , array $ captchaResponse ): void
1819 {
1920 Http::fake ([
@@ -45,10 +46,52 @@ public function testInvalidCaptchaResponse(bool $isValid, array $captchaResponse
4546 );
4647 }
4748
49+ /**
50+ * @param array{0: bool, 1: array{'success': bool}} $captchaResponse
51+ */
52+ #[DataProvider('provideCustomAttributeData ' )]
53+ public function testCustomAttributeProperties (bool $ isValid , array $ captchaResponse ): void
54+ {
55+ Http::fake ([
56+ 'https://custom.example.com/verify ' => Http::response ($ captchaResponse ),
57+ ]);
58+
59+ $ testable = Livewire::test (MyCustomAttributeComponent::class)
60+ ->set ('gRecaptchaResponse ' , $ captcha = 'mygrecaptcharesponse ' )
61+ ->call ('save ' );
62+
63+ if ($ isValid ) {
64+ $ testable ->assertHasNoErrors ();
65+ } else {
66+ $ testable ->assertHasErrors ([
67+ 'gRecaptchaResponse ' ,
68+ ]);
69+ }
70+
71+ Http::assertSent (fn (Request $ request ) => $ request ->url () === 'https://custom.example.com/verify ' &&
72+ $ request ['secret ' ] === 'custom-secret-key ' &&
73+ $ request ['response ' ] === $ captcha &&
74+ array_key_exists ('remoteip ' , $ request ->data ())
75+ );
76+ }
77+
78+ /**
79+ * @return array<string, array{0: bool, 1: array{'success': bool}}>
80+ */
81+ public static function provideCustomAttributeData (): array
82+ {
83+ return [
84+ 'valid response ' => [true , ['success ' => true , 'score ' => 0.9 ]],
85+ 'valid response, score at threshold ' => [true , ['success ' => true , 'score ' => 0.7 ]],
86+ 'valid response, score below threshold ' => [false , ['success ' => true , 'score ' => 0.5 ]],
87+ 'invalid response ' => [false , ['success ' => false ]],
88+ ];
89+ }
90+
4891 /**
4992 * @return array<string, array{0: bool, 1: array{'success': bool}}>
5093 */
51- public static function provideCaptchaData (): array
94+ public static function provideDefaultAttributeData (): array
5295 {
5396 return [
5497 'valid response ' => [true , ['success ' => true , 'score ' => 0.9 ]],
0 commit comments