Skip to content

Commit 089951c

Browse files
committed
Support passing authentication options to test framework
1 parent 29fddd8 commit 089951c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ this configuration by creating your own `phpunit.xml` file based on the
3737
`phpunit.xml.dist` file we provide. To run the tests in serverless mode, set the
3838
`MONGODB_IS_SERVERLESS` environment variable to `on`.
3939

40+
To run tests against a cluster that requires authentication, either include the
41+
credentials in the connection string given in the `MONGODB_URI` environment
42+
variable, or set the `MONGODB_USERNAME` and `MONGODB_PASSWORD` environment
43+
variables accordingly.
44+
4045
By default, the `simple-phpunit` binary chooses the correct PHPUnit version for
4146
the PHP version you are running. To run tests against a specific PHPUnit version,
4247
use the `SYMFONY_PHPUNIT_VERSION` environment variable:

tests/FunctionalTestCase.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,20 @@ public function tearDown(): void
6969

7070
public static function createTestClient(?string $uri = null, array $options = [], array $driverOptions = []): Client
7171
{
72-
return new Client($uri ?? static::getUri(), $options, static::appendServerApiOption($driverOptions));
72+
return new Client(
73+
$uri ?? static::getUri(),
74+
static::appendAuthenticationOptions($options),
75+
static::appendServerApiOption($driverOptions)
76+
);
7377
}
7478

7579
public static function createTestManager(?string $uri = null, array $options = [], array $driverOptions = []): Manager
7680
{
77-
return new Manager($uri ?? static::getUri(), $options, static::appendServerApiOption($driverOptions));
81+
return new Manager(
82+
$uri ?? static::getUri(),
83+
static::appendAuthenticationOptions($options),
84+
static::appendServerApiOption($driverOptions)
85+
);
7886
}
7987

8088
public static function getUri($allowMultipleMongoses = false): string
@@ -513,6 +521,26 @@ protected function skipIfTransactionsAreNotSupported(): void
513521
}
514522
}
515523

524+
private static function appendAuthenticationOptions(array $options): array
525+
{
526+
if (isset($options['username']) || isset($options['password'])) {
527+
return $options;
528+
}
529+
530+
$username = getenv('MONGODB_USERNAME') ?: null;
531+
$password = getenv('MONGODB_PASSWORD') ?: null;
532+
533+
if ($username !== null) {
534+
$options['username'] = $username;
535+
}
536+
537+
if ($password !== null) {
538+
$options['password'] = $password;
539+
}
540+
541+
return $options;
542+
}
543+
516544
private static function appendServerApiOption(array $driverOptions): array
517545
{
518546
if (getenv('API_VERSION') && ! isset($driverOptions['serverApi'])) {

0 commit comments

Comments
 (0)