|
10 | 10 | use Psr\Http\Message\RequestInterface; |
11 | 11 | use Psr\Http\Message\ResponseInterface; |
12 | 12 |
|
| 13 | +/** |
| 14 | + * @covers \Acquia\Cli\CloudApi\PathRewriteConnector |
| 15 | + * |
| 16 | + * Unit tests for the PathRewriteConnector decorator. Ensures all path rewriting logic, |
| 17 | + * delegation, and error handling are correct. |
| 18 | + */ |
13 | 19 | class PathRewriteConnectorTest extends TestCase |
14 | 20 | { |
| 21 | + /** |
| 22 | + * Mocked inner connector to verify delegation and path rewriting. |
| 23 | + */ |
15 | 24 | private ConnectorInterface $inner; |
| 25 | + |
| 26 | + /** |
| 27 | + * The PathRewriteConnector under test. |
| 28 | + */ |
16 | 29 | private ConnectorInterface $connector; |
17 | 30 |
|
| 31 | + /** |
| 32 | + * Stores the original value of AH_CODEBASE_UUID to restore after each test. |
| 33 | + */ |
| 34 | + private string|bool $originalEnv; |
| 35 | + |
| 36 | + /** |
| 37 | + * Sets up a fresh PathRewriteConnector and mocks before each test. |
| 38 | + * Ensures AH_CODEBASE_UUID is set for tests that require it. |
| 39 | + */ |
18 | 40 | protected function setUp(): void |
19 | 41 | { |
20 | 42 | parent::setUp(); |
| 43 | + $this->originalEnv = getenv('AH_CODEBASE_UUID'); |
| 44 | + putenv('AH_CODEBASE_UUID=1234-5678-uuid'); |
21 | 45 | $this->inner = $this->createMock(ConnectorInterface::class); |
22 | 46 | $this->connector = new PathRewriteConnector($this->inner); |
23 | 47 | } |
24 | 48 |
|
25 | 49 | /** |
26 | | - * @runInSeparateProcess |
| 50 | + * @dataProvider createRequestProvider |
| 51 | + * @param string $verb The HTTP verb to test. |
| 52 | + * @param string $inputPath The input path to test. |
| 53 | + * @param string $expectedPath The expected path after rewriting. |
27 | 54 | */ |
28 | | - public function testCreateRequestRewritesMatchingPath(): void |
| 55 | + public function testCreateRequestPathRewriting(string $verb, string $inputPath, string $expectedPath): void |
29 | 56 | { |
30 | | - putenv('AH_CODEBASE_UUID=1234-5678-uuid'); |
31 | | - $expectedPath = '/translation/codebases/1234-5678-uuid/environments'; |
32 | | - $request = $this->createMock(RequestInterface::class); |
| 57 | + $mock = $this->createMock(RequestInterface::class); |
33 | 58 | $this->inner->expects($this->once()) |
34 | 59 | ->method('createRequest') |
35 | | - ->with('GET', $expectedPath) |
36 | | - ->willReturn($request); |
37 | | - $result = $this->connector->createRequest('GET', '/applications/abcd-ef01/environments'); |
38 | | - $this->assertSame($request, $result); |
| 60 | + ->with($verb, $expectedPath) |
| 61 | + ->willReturn($mock); |
| 62 | + $result = $this->connector->createRequest($verb, $inputPath); |
| 63 | + $this->assertSame($mock, $result); |
39 | 64 | } |
40 | 65 |
|
| 66 | + |
| 67 | + |
41 | 68 | /** |
42 | | - * @runInSeparateProcess |
| 69 | + * @dataProvider sendRequestProvider |
| 70 | + * @param string $verb The HTTP verb to test. |
| 71 | + * @param string $inputPath The input path to test. |
| 72 | + * @param string $expectedPath The expected path after rewriting. |
| 73 | + * @param array $options The options to pass to sendRequest. |
43 | 74 | */ |
44 | | - public function testCreateRequestDoesNotRewriteUnmatchedPath(): void |
| 75 | + public function testSendRequestPathRewriting(string $verb, string $inputPath, string $expectedPath, array $options): void |
45 | 76 | { |
46 | | - putenv('AH_CODEBASE_UUID=1234-5678-uuid'); |
47 | | - $request = $this->createMock(RequestInterface::class); |
| 77 | + $mock = $this->createMock(ResponseInterface::class); |
48 | 78 | $this->inner->expects($this->once()) |
49 | | - ->method('createRequest') |
50 | | - ->with('GET', '/other/path') |
51 | | - ->willReturn($request); |
52 | | - $result = $this->connector->createRequest('GET', '/other/path'); |
53 | | - $this->assertSame($request, $result); |
| 79 | + ->method('sendRequest') |
| 80 | + ->with($verb, $expectedPath, $options) |
| 81 | + ->willReturn($mock); |
| 82 | + $result = $this->connector->sendRequest($verb, $inputPath, $options); |
| 83 | + $this->assertSame($mock, $result); |
54 | 84 | } |
55 | 85 |
|
56 | 86 | /** |
57 | | - * @runInSeparateProcess |
| 87 | + * @dataProvider delegationProvider |
| 88 | + * @param string $method The method to test delegation for. |
| 89 | + * @param mixed $expected The expected return value from the inner connector. |
58 | 90 | */ |
59 | | - public function testSendRequestRewritesMatchingPath(): void |
| 91 | + public function testDelegation(string $method, string $expected): void |
60 | 92 | { |
61 | | - putenv('AH_CODEBASE_UUID=1234-5678-uuid'); |
62 | | - $expectedPath = '/translation/codebases/1234-5678-uuid/permissions'; |
63 | | - $response = $this->createMock(ResponseInterface::class); |
64 | 93 | $this->inner->expects($this->once()) |
65 | | - ->method('sendRequest') |
66 | | - ->with('POST', $expectedPath, ['foo' => 'bar']) |
67 | | - ->willReturn($response); |
68 | | - $result = $this->connector->sendRequest('POST', '/applications/abcd-ef01/permissions', ['foo' => 'bar']); |
69 | | - $this->assertSame($response, $result); |
| 94 | + ->method($method) |
| 95 | + ->willReturn($expected); |
| 96 | + $this->assertTrue(method_exists($this->connector, $method)); |
| 97 | + $this->assertSame($expected, $this->connector->{$method}()); |
70 | 98 | } |
71 | 99 |
|
72 | 100 | /** |
73 | | - * @runInSeparateProcess |
| 101 | + * Ensures an exception is thrown if AH_CODEBASE_UUID is not set when required. |
74 | 102 | */ |
75 | | - public function testSendRequestDoesNotRewriteUnmatchedPath(): void |
| 103 | + public function testThrowsIfCodebaseUuidNotSet(): void |
76 | 104 | { |
77 | | - putenv('AH_CODEBASE_UUID=1234-5678-uuid'); |
78 | | - $response = $this->createMock(ResponseInterface::class); |
79 | | - $this->inner->expects($this->once()) |
80 | | - ->method('sendRequest') |
81 | | - ->with('GET', '/other/path', []) |
82 | | - ->willReturn($response); |
83 | | - $result = $this->connector->sendRequest('GET', '/other/path', []); |
84 | | - $this->assertSame($response, $result); |
| 105 | + putenv('AH_CODEBASE_UUID'); |
| 106 | + $connector = new PathRewriteConnector($this->inner); |
| 107 | + $this->expectException(\RuntimeException::class); |
| 108 | + $this->expectExceptionMessage('Environment variable AH_CODEBASE_UUID is not set.'); |
| 109 | + // This will trigger getCodeBaseUuid() |
| 110 | + $connector->createRequest('GET', '/applications/abcd-ef01/environments'); |
85 | 111 | } |
86 | 112 |
|
87 | 113 | /** |
88 | | - * @runInSeparateProcess |
| 114 | + * Data provider for createRequest tests. Ensures that paths are rewritten |
| 115 | + * correctly based on the presence of the code base environment variable. |
| 116 | + * |
| 117 | + * @return array<int, array{0: string, 1: string, 2: string}> |
89 | 118 | */ |
90 | | - public function testGetBaseUriDelegates(): void |
| 119 | + public static function createRequestProvider(): array |
91 | 120 | { |
92 | | - putenv('AH_CODEBASE_UUID=1234-5678-uuid'); |
93 | | - $this->inner->expects($this->once()) |
94 | | - ->method('getBaseUri') |
95 | | - ->willReturn('https://api.example.com'); |
96 | | - $this->assertSame('https://api.example.com', $this->connector->getBaseUri()); |
| 121 | + return [ |
| 122 | + // Rewrite. |
| 123 | + ['GET', '/applications/abcd-ef01/environments', '/translation/codebases/1234-5678-uuid/environments'], |
| 124 | + // No rewrite. |
| 125 | + ['GET', '/other/path', '/other/path'], |
| 126 | + ]; |
97 | 127 | } |
98 | 128 |
|
99 | 129 | /** |
100 | | - * @runInSeparateProcess |
| 130 | + * Data provider for sendRequest tests. Ensures that both path rewriting |
| 131 | + * and options are handled correctly. |
| 132 | + * |
| 133 | + * @return array<int, array{0: string, 1: string, 2: string, 3: array<string, mixed>}> |
101 | 134 | */ |
102 | | - public function testGetUrlAccessTokenDelegates(): void |
| 135 | + public static function sendRequestProvider(): array |
103 | 136 | { |
104 | | - putenv('AH_CODEBASE_UUID=1234-5678-uuid'); |
105 | | - $this->inner->expects($this->once()) |
106 | | - ->method('getUrlAccessToken') |
107 | | - ->willReturn('token123'); |
108 | | - $this->assertSame('token123', $this->connector->getUrlAccessToken()); |
| 137 | + return [ |
| 138 | + // Rewrite. |
| 139 | + ['POST', '/applications/abcd-ef01/permissions', '/translation/codebases/1234-5678-uuid/permissions', ['foo' => 'bar']], |
| 140 | + // No rewrite. |
| 141 | + ['GET', '/other/path', '/other/path', []], |
| 142 | + ]; |
109 | 143 | } |
110 | 144 |
|
111 | 145 | /** |
112 | | - * @runInSeparateProcess |
| 146 | + * Data provider for delegation tests. Ensures that methods not related to |
| 147 | + * path rewriting are properly delegated to the inner connector. |
| 148 | + * |
| 149 | + * @return array<int, array{0: string, 1: mixed}> |
113 | 150 | */ |
114 | | - public function testThrowsIfCodebaseUuidNotSet(): void |
| 151 | + public static function delegationProvider(): array |
115 | 152 | { |
116 | | - putenv('AH_CODEBASE_UUID'); |
117 | | - $connector = new PathRewriteConnector($this->inner); |
118 | | - $this->expectException(\RuntimeException::class); |
119 | | - $this->expectExceptionMessage('Environment variable AH_CODEBASE_UUID is not set.'); |
120 | | - // This will trigger getCodeBaseUuid() |
121 | | - $connector->createRequest('GET', '/applications/abcd-ef01/environments'); |
| 153 | + return [ |
| 154 | + ['getBaseUri', 'https://api.example.com'], |
| 155 | + ['getUrlAccessToken', 'token123'], |
| 156 | + ]; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Restores the original AH_CODEBASE_UUID environment variable after each test. |
| 161 | + */ |
| 162 | + protected function tearDown(): void |
| 163 | + { |
| 164 | + if ($this->originalEnv === false) { |
| 165 | + putenv('AH_CODEBASE_UUID'); |
| 166 | + } else { |
| 167 | + putenv('AH_CODEBASE_UUID=' . $this->originalEnv); |
| 168 | + } |
| 169 | + parent::tearDown(); |
122 | 170 | } |
123 | 171 | } |
0 commit comments