|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Two\Gateway\Test\E2E\Service\Api; |
| 5 | + |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Two\Gateway\Api\Config\RepositoryInterface as ConfigRepository; |
| 8 | +use Two\Gateway\Api\Log\RepositoryInterface as LogRepository; |
| 9 | +use Two\Gateway\Service\Api\Adapter; |
| 10 | +use Two\Gateway\Test\E2E\Http\RealCurl; |
| 11 | + |
| 12 | +/** |
| 13 | + * End-to-end tests for Service\Api\Adapter against the real Two API. |
| 14 | + * Defaults to staging unless TWO_API_BASE_URL overrides it. |
| 15 | + * |
| 16 | + * Run via: TWO_API_KEY=xxx make test-e2e |
| 17 | + */ |
| 18 | +class ApiAdapterTest extends TestCase |
| 19 | +{ |
| 20 | + private Adapter $adapter; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $apiKey = (string)getenv('TWO_API_KEY'); |
| 25 | + $baseUrl = getenv('TWO_API_BASE_URL') ?: 'https://api.staging.two.inc'; |
| 26 | + |
| 27 | + $config = $this->createMock(ConfigRepository::class); |
| 28 | + $config->method('getCheckoutApiUrl')->willReturn($baseUrl); |
| 29 | + $config->method('addVersionDataInURL')->willReturnArgument(0); |
| 30 | + $config->method('getApiKey')->willReturn($apiKey); |
| 31 | + |
| 32 | + $log = $this->createMock(LogRepository::class); |
| 33 | + |
| 34 | + $this->adapter = new Adapter($config, new RealCurl(), $log); |
| 35 | + } |
| 36 | + |
| 37 | + public function testApiKeyIsValid(): void |
| 38 | + { |
| 39 | + $result = $this->adapter->execute('/v1/merchant/verify_api_key', [], 'GET'); |
| 40 | + |
| 41 | + $this->assertIsArray($result); |
| 42 | + $this->assertArrayNotHasKey('error_code', $result); |
| 43 | + } |
| 44 | + |
| 45 | + public function testInvalidApiKeyReturns401WithStructuredError(): void |
| 46 | + { |
| 47 | + $baseUrl = getenv('TWO_API_BASE_URL') ?: 'https://api.staging.two.inc'; |
| 48 | + |
| 49 | + $config = $this->createMock(ConfigRepository::class); |
| 50 | + $config->method('getCheckoutApiUrl')->willReturn($baseUrl); |
| 51 | + $config->method('addVersionDataInURL')->willReturnArgument(0); |
| 52 | + $config->method('getApiKey')->willReturn('invalid-key'); |
| 53 | + |
| 54 | + $log = $this->createMock(LogRepository::class); |
| 55 | + |
| 56 | + $adapter = new Adapter($config, new RealCurl(), $log); |
| 57 | + $result = $adapter->execute('/v1/merchant/verify_api_key', [], 'GET'); |
| 58 | + |
| 59 | + $this->assertEquals(401, $result['http_status']); |
| 60 | + } |
| 61 | + |
| 62 | + public function testBadOrderPayloadReturnsStructuredError(): void |
| 63 | + { |
| 64 | + $result = $this->adapter->execute('/v1/order', []); |
| 65 | + |
| 66 | + $this->assertArrayHasKey('http_status', $result); |
| 67 | + $this->assertGreaterThanOrEqual(400, $result['http_status']); |
| 68 | + } |
| 69 | +} |
0 commit comments