|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RequestMethod; |
| 6 | + |
| 7 | +use Karser\Recaptcha3Bundle\RequestMethod\SymfonyHttpClient; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use ReCaptcha\RequestParameters; |
| 10 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 11 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 12 | + |
| 13 | +final class SymfonyHttpClientTest extends TestCase |
| 14 | +{ |
| 15 | + public function testSubmit(): void |
| 16 | + { |
| 17 | + $httpClient = new MockHttpClient(function (string $method, string $url, array $options) { |
| 18 | + self::assertSame('POST', $method); |
| 19 | + self::assertSame('https://www.google.com/recaptcha/api/siteverify', $url); |
| 20 | + self::assertSame('secret=secret&response=response', $options['body']); |
| 21 | + |
| 22 | + return new MockResponse('RESPONSEBODY'); |
| 23 | + }); |
| 24 | + |
| 25 | + $method = new SymfonyHttpClient($httpClient); |
| 26 | + $response = $method->submit(new RequestParameters('secret', 'response')); |
| 27 | + |
| 28 | + self::assertSame('RESPONSEBODY', $response); |
| 29 | + } |
| 30 | + |
| 31 | + public function testOverrideSiteVerifyUrl() |
| 32 | + { |
| 33 | + $httpClient = new MockHttpClient(function (string $method, string $url, array $options) { |
| 34 | + self::assertSame('POST', $method); |
| 35 | + self::assertSame('http://override/', $url); |
| 36 | + self::assertSame('secret=secret&response=response', $options['body']); |
| 37 | + |
| 38 | + return new MockResponse('RESPONSEBODY'); |
| 39 | + }); |
| 40 | + |
| 41 | + $method = new SymfonyHttpClient($httpClient, 'http://override/'); |
| 42 | + $response = $method->submit(new RequestParameters('secret', 'response')); |
| 43 | + |
| 44 | + self::assertSame('RESPONSEBODY', $response); |
| 45 | + } |
| 46 | + |
| 47 | + public function testResponseError() |
| 48 | + { |
| 49 | + $httpClient = new MockHttpClient(function (string $method, string $url, array $options) { |
| 50 | + self::assertSame('POST', $method); |
| 51 | + self::assertSame('https://www.google.com/recaptcha/api/siteverify', $url); |
| 52 | + self::assertSame('secret=secret&response=response', $options['body']); |
| 53 | + |
| 54 | + return new MockResponse('fail', ['http_code' => 400]); |
| 55 | + }); |
| 56 | + |
| 57 | + $method = new SymfonyHttpClient($httpClient); |
| 58 | + $response = $method->submit(new RequestParameters('secret', 'response')); |
| 59 | + |
| 60 | + self::assertSame('{"success": false, "error-codes": ["bad-response"]}', $response); |
| 61 | + } |
| 62 | +} |
0 commit comments