Skip to content

Commit 6c9b34f

Browse files
Merge pull request #250 from alma/feature/ecom-4030-fix-php-client-to-avoid-api-mode-values-to-silently-fallback
feat: Fix PHP client to avoid API mode values to silently fallback to…
2 parents 5b32803 + baf291e commit 6c9b34f

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/Client/Domain/ValueObject/Environment.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ final class Environment {
2727
* @param string $customApiUrl The custom API URL, required if mode is custom
2828
*/
2929
public function __construct(string $mode, string $customApiUrl = '') {
30-
// Check mode
31-
if (!in_array($mode, [self::LIVE_MODE, self::TEST_MODE, self::CUSTOM_MODE])) {
32-
$mode = self::LIVE_MODE;
30+
if (!in_array($mode, [self::LIVE_MODE, self::TEST_MODE, self::CUSTOM_MODE], true)) {
31+
throw new InvalidArgumentException(
32+
sprintf(
33+
'Invalid environment mode "%s". Allowed values are: %s.',
34+
$mode,
35+
implode(', ', [self::LIVE_MODE, self::TEST_MODE, self::CUSTOM_MODE])
36+
)
37+
);
3338
}
3439
if ($mode === self::CUSTOM_MODE && empty($customApiUrl)) {
3540
throw new InvalidArgumentException('Custom API URL must be provided for custom mode.');

tests/Client/Unit/Application/ClientConfigurationTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Alma\Client\Application\ClientConfiguration;
66
use Alma\Client\Domain\ValueObject\Environment;
7+
use InvalidArgumentException;
78
use PHPUnit\Framework\TestCase;
89

910
class ClientConfigurationTest extends TestCase
@@ -24,14 +25,14 @@ public function testTestMode()
2425
}
2526

2627
/**
27-
* Ensure we can't create a ClientConfiguration with an invalid mode,
28-
* and it defaults to LIVE_MODE
28+
* Ensure that creating an Environment with an invalid mode throws an InvalidArgumentException.
2929
* @return void
3030
*/
3131
public function testInvalidMode()
3232
{
33-
$clientConfiguration = new ClientConfiguration('sk_test_xxxxxxxxxxxx', new Environment('INVALID_MODE'));
34-
$this->assertEquals(new Environment(Environment::LIVE_MODE), $clientConfiguration->getEnvironment());
33+
$this->expectException(InvalidArgumentException::class);
34+
$this->expectExceptionMessage('Invalid environment mode "INVALID_MODE". Allowed values are: live, test, custom.');
35+
new Environment('INVALID_MODE');
3536
}
3637

3738
/**

tests/Client/Unit/Domain/Entity/EnvironmentTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,25 @@ public function testEnvironmentModesAreSetCorrectly($mode, $expectedUrl)
2828
$this->assertEquals(Uri::fromString($expectedUrl), $environment->getBaseUri());
2929
}
3030

31-
public function testInvalidModeDefaultsToLive()
31+
public static function invalidModesProvider(): array
3232
{
33-
$environment = new Environment('invalid_mode');
34-
$this->assertEquals(Environment::LIVE_MODE, $environment->getMode());
35-
$this->assertEquals(Uri::fromString(Environment::LIVE_API_URL), $environment->getBaseUri());
33+
return [
34+
['invalid_mode'],
35+
['Live'],
36+
['Test'],
37+
['LIVE'],
38+
];
39+
}
40+
41+
/** @dataProvider invalidModesProvider */
42+
public function testInvalidModeThrowsException(string $invalidMode)
43+
{
44+
$this->expectException(InvalidArgumentException::class);
45+
$this->expectExceptionMessage(sprintf(
46+
'Invalid environment mode "%s". Allowed values are: live, test, custom.',
47+
$invalidMode
48+
));
49+
new Environment($invalidMode);
3650
}
3751

3852
public function testCustomModeWithoutUrlThrowsException()

0 commit comments

Comments
 (0)