Skip to content

Commit 93d2687

Browse files
committed
IDE-4857: Fixed the co-polit suggestions.
1 parent 1955adf commit 93d2687

4 files changed

Lines changed: 164 additions & 75 deletions

File tree

src/CloudApi/ConnectorFactory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ public function createConnector(): ConnectorInterface
2323
$connector = $this->buildConnector();
2424

2525
// If the AH_CODEBASE_UUID environment variable is set, that means
26-
// it's a MEO subscription and for MEO we need to rewrite the path to
27-
// include the codebase UUID. This is because MEO's uses different
28-
// endpoints, and the codebase UUID is used to determine which endpoint
29-
// to use.
26+
// it's a MEO subscription. For MEO, we need to rewrite the API request
27+
// path so that MEO-specific endpoints are used and the correct
28+
// endpoint can be selected based on the codebase.
3029
if (getenv('AH_CODEBASE_UUID')) {
3130
return new PathRewriteConnector($connector);
3231
}

src/CloudApi/PathRewriteConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function rewritePath(string $path): string
9999
* Returns an array of regex patterns and their corresponding replacement paths for rewriting API request paths.
100100
*
101101
* @return array<string, string> An array of regex patterns and their corresponding replacement paths.
102-
* The replacement paths may use the codebaseUuid property.
102+
* The replacement paths may include the codebase UUID obtained from the AH_CODEBASE_UUID environment variable.
103103
*/
104104
private function getPathsToRewrite(): array
105105
{

tests/phpunit/src/CloudApi/ConnectorFactoryTest.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,69 @@
99
use AcquiaCloudApi\Connector\Connector;
1010
use PHPUnit\Framework\TestCase;
1111

12+
/**
13+
* @covers \Acquia\Cli\CloudApi\ConnectorFactory
14+
*
15+
* Unit tests for the ConnectorFactory. Ensures that the factory returns the correct
16+
* connector type depending on the presence of the AH_CODEBASE_UUID environment variable.
17+
*/
1218
class ConnectorFactoryTest extends TestCase
1319
{
1420
/**
15-
* @runInSeparateProcess
21+
* Stores the original value of AH_CODEBASE_UUID to restore after each test.
1622
*/
17-
public function testCreateConnectorReturnsPathRewriteConnectorIfEnvSet(): void
23+
private string|false $originalEnv;
24+
25+
/**
26+
* Saves the original environment variable before each test.
27+
*/
28+
protected function setUp(): void
1829
{
19-
putenv('AH_CODEBASE_UUID=1234-5678-uuid');
20-
$factory = new ConnectorFactory(['key' => 'k', 'secret' => 's'], 'https://api.example.com');
21-
$connector = $factory->createConnector();
22-
$this->assertInstanceOf(PathRewriteConnector::class, $connector);
30+
parent::setUp();
31+
$this->originalEnv = getenv('AH_CODEBASE_UUID');
2332
}
2433

34+
2535
/**
26-
* @runInSeparateProcess
36+
* @dataProvider connectorFactoryProvider
2737
*/
28-
public function testCreateConnectorReturnsRegularConnectorIfEnvNotSet(): void
38+
public function testCreateConnectorFactoryBehavior(?string $envValue, string $expectedClass): void
2939
{
30-
putenv('AH_CODEBASE_UUID');
40+
if ($envValue !== null) {
41+
putenv("AH_CODEBASE_UUID=$envValue");
42+
} else {
43+
putenv('AH_CODEBASE_UUID');
44+
}
3145
$factory = new ConnectorFactory(['key' => 'k', 'secret' => 's'], 'https://api.example.com');
3246
$connector = $factory->createConnector();
33-
$this->assertInstanceOf(Connector::class, $connector);
47+
$this->assertInstanceOf($expectedClass, $connector);
48+
}
49+
50+
/**
51+
* Data provider for testCreateConnectorFactoryBehavior() test.
52+
*
53+
* @return array<int, array{0: string|null, 1: class-string}>
54+
*/
55+
public static function connectorFactoryProvider(): array
56+
{
57+
return [
58+
// Env set: should return PathRewriteConnector.
59+
['1234-5678-uuid', PathRewriteConnector::class],
60+
// Env not set: should return Connector.
61+
[null, Connector::class],
62+
];
63+
}
64+
65+
/**
66+
* Restores the original environment variable after each test.
67+
*/
68+
protected function tearDown(): void
69+
{
70+
if ($this->originalEnv === false) {
71+
putenv('AH_CODEBASE_UUID');
72+
} else {
73+
putenv('AH_CODEBASE_UUID=' . $this->originalEnv);
74+
}
75+
parent::tearDown();
3476
}
3577
}

tests/phpunit/src/CloudApi/PathRewriteConnectorTest.php

Lines changed: 108 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,114 +10,162 @@
1010
use Psr\Http\Message\RequestInterface;
1111
use Psr\Http\Message\ResponseInterface;
1212

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+
*/
1319
class PathRewriteConnectorTest extends TestCase
1420
{
21+
/**
22+
* Mocked inner connector to verify delegation and path rewriting.
23+
*/
1524
private ConnectorInterface $inner;
25+
26+
/**
27+
* The PathRewriteConnector under test.
28+
*/
1629
private ConnectorInterface $connector;
1730

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+
*/
1840
protected function setUp(): void
1941
{
2042
parent::setUp();
43+
$this->originalEnv = getenv('AH_CODEBASE_UUID');
44+
putenv('AH_CODEBASE_UUID=1234-5678-uuid');
2145
$this->inner = $this->createMock(ConnectorInterface::class);
2246
$this->connector = new PathRewriteConnector($this->inner);
2347
}
2448

2549
/**
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.
2754
*/
28-
public function testCreateRequestRewritesMatchingPath(): void
55+
public function testCreateRequestPathRewriting(string $verb, string $inputPath, string $expectedPath): void
2956
{
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);
3358
$this->inner->expects($this->once())
3459
->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);
3964
}
4065

66+
67+
4168
/**
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.
4374
*/
44-
public function testCreateRequestDoesNotRewriteUnmatchedPath(): void
75+
public function testSendRequestPathRewriting(string $verb, string $inputPath, string $expectedPath, array $options): void
4576
{
46-
putenv('AH_CODEBASE_UUID=1234-5678-uuid');
47-
$request = $this->createMock(RequestInterface::class);
77+
$mock = $this->createMock(ResponseInterface::class);
4878
$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);
5484
}
5585

5686
/**
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.
5890
*/
59-
public function testSendRequestRewritesMatchingPath(): void
91+
public function testDelegation(string $method, string $expected): void
6092
{
61-
putenv('AH_CODEBASE_UUID=1234-5678-uuid');
62-
$expectedPath = '/translation/codebases/1234-5678-uuid/permissions';
63-
$response = $this->createMock(ResponseInterface::class);
6493
$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}());
7098
}
7199

72100
/**
73-
* @runInSeparateProcess
101+
* Ensures an exception is thrown if AH_CODEBASE_UUID is not set when required.
74102
*/
75-
public function testSendRequestDoesNotRewriteUnmatchedPath(): void
103+
public function testThrowsIfCodebaseUuidNotSet(): void
76104
{
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');
85111
}
86112

87113
/**
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}>
89118
*/
90-
public function testGetBaseUriDelegates(): void
119+
public static function createRequestProvider(): array
91120
{
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+
];
97127
}
98128

99129
/**
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>}>
101134
*/
102-
public function testGetUrlAccessTokenDelegates(): void
135+
public static function sendRequestProvider(): array
103136
{
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+
];
109143
}
110144

111145
/**
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}>
113150
*/
114-
public function testThrowsIfCodebaseUuidNotSet(): void
151+
public static function delegationProvider(): array
115152
{
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();
122170
}
123171
}

0 commit comments

Comments
 (0)