Skip to content

Commit 2809d43

Browse files
committed
Always use connection string in tests
1 parent dbde512 commit 2809d43

File tree

6 files changed

+90
-74
lines changed

6 files changed

+90
-74
lines changed

.github/workflows/build-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ jobs:
8888
run: |
8989
./vendor/bin/phpunit --coverage-clover coverage.xml
9090
env:
91-
MONGO_HOST: 0.0.0.0
91+
MONGODB_URI: 'mongodb://127.0.0.1/'
9292
MYSQL_HOST: 0.0.0.0
9393
MYSQL_PORT: 3307
9494
- uses: codecov/codecov-action@v1

phpunit.xml.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@
3535
</testsuite>
3636
</testsuites>
3737
<php>
38-
<env name="MONGO_HOST" value="mongodb"/>
38+
<env name="MONGODB_URI" value="mongodb://127.0.0.1/" />
3939
<env name="MONGO_DATABASE" value="unittest"/>
40-
<env name="MONGO_PORT" value="27017"/>
4140
<env name="MYSQL_HOST" value="mysql"/>
4241
<env name="MYSQL_PORT" value="3306"/>
4342
<env name="MYSQL_DATABASE" value="unittest"/>

tests/ConnectionTest.php

Lines changed: 86 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,93 @@ public function testDb()
3737
$this->assertInstanceOf(Client::class, $connection->getMongoClient());
3838
}
3939

40-
public function testDsnDb()
40+
public function dataConnectionConfig(): Generator
4141
{
42-
$connection = DB::connection('dsn_mongodb_db');
43-
$this->assertInstanceOf(Database::class, $connection->getMongoDB());
44-
$this->assertInstanceOf(Client::class, $connection->getMongoClient());
42+
yield 'Single host' => [
43+
'expectedUri' => 'mongodb://some-host',
44+
'expectedDatabaseName' => 'tests',
45+
'config' => [
46+
'host' => 'some-host',
47+
'database' => 'tests',
48+
],
49+
];
50+
51+
yield 'Host and port' => [
52+
'expectedUri' => 'mongodb://some-host:12345',
53+
'expectedDatabaseName' => 'tests',
54+
'config' => [
55+
'host' => 'some-host',
56+
'port' => 12345,
57+
'database' => 'tests',
58+
],
59+
];
60+
61+
yield 'Port in host name takes precedence' => [
62+
'expectedUri' => 'mongodb://some-host:12345',
63+
'expectedDatabaseName' => 'tests',
64+
'config' => [
65+
'host' => 'some-host:12345',
66+
'port' => 54321,
67+
'database' => 'tests',
68+
],
69+
];
70+
71+
yield 'Multiple hosts' => [
72+
'expectedUri' => 'mongodb://host-1,host-2',
73+
'expectedDatabaseName' => 'tests',
74+
'config' => [
75+
'host' => ['host-1', 'host-2'],
76+
'database' => 'tests',
77+
],
78+
];
79+
80+
yield 'Multiple hosts with same port' => [
81+
'expectedUri' => 'mongodb://host-1:12345,host-2:12345',
82+
'expectedDatabaseName' => 'tests',
83+
'config' => [
84+
'host' => ['host-1', 'host-2'],
85+
'port' => 12345,
86+
'database' => 'tests',
87+
],
88+
];
89+
90+
yield 'Multiple hosts with port' => [
91+
'expectedUri' => 'mongodb://host-1:12345,host-2:54321',
92+
'expectedDatabaseName' => 'tests',
93+
'config' => [
94+
'host' => ['host-1:12345', 'host-2:54321'],
95+
'database' => 'tests',
96+
],
97+
];
98+
99+
yield 'DSN takes precedence over host/port config' => [
100+
'expectedUri' => 'mongodb://some-host:12345/auth-database',
101+
'expectedDatabaseName' => 'tests',
102+
'config' => [
103+
'dsn' => 'mongodb://some-host:12345/auth-database',
104+
'host' => 'wrong-host',
105+
'port' => 54321,
106+
'database' => 'tests',
107+
],
108+
];
109+
}
110+
111+
/** @dataProvider dataConnectionConfig */
112+
public function testConnectionConfig(string $expectedUri, string $expectedDatabaseName, array $config): void
113+
{
114+
$connection = new Connection($config);
115+
$client = $connection->getMongoClient();
116+
117+
$this->assertSame($expectedUri, (string) $client);
118+
$this->assertSame($expectedDatabaseName, $connection->getMongoDB()->getDatabaseName());
119+
}
120+
121+
public function testConnectionWithoutConfiguredDatabase(): void
122+
{
123+
$this->expectException(InvalidArgumentException::class);
124+
$this->expectExceptionMessage('Database is not properly configured.');
125+
126+
new Connection(['dsn' => 'mongodb://some-host']);
45127
}
46128

47129
public function testCollection()
@@ -89,33 +171,4 @@ public function testDriverName()
89171
$driver = DB::connection('mongodb')->getDriverName();
90172
$this->assertEquals('mongodb', $driver);
91173
}
92-
93-
public function testAuth()
94-
{
95-
$host = Config::get('database.connections.mongodb.host');
96-
Config::set('database.connections.mongodb.username', 'foo');
97-
Config::set('database.connections.mongodb.password', 'bar');
98-
Config::set('database.connections.mongodb.options.database', 'custom');
99-
100-
$connection = DB::connection('mongodb');
101-
$this->assertEquals('mongodb://'.$host.'/custom', (string) $connection->getMongoClient());
102-
}
103-
104-
public function testCustomHostAndPort()
105-
{
106-
Config::set('database.connections.mongodb.host', 'db1');
107-
Config::set('database.connections.mongodb.port', 27000);
108-
109-
$connection = DB::connection('mongodb');
110-
$this->assertEquals('mongodb://db1:27000', (string) $connection->getMongoClient());
111-
}
112-
113-
public function testHostWithPorts()
114-
{
115-
Config::set('database.connections.mongodb.port', 27000);
116-
Config::set('database.connections.mongodb.host', ['db1:27001', 'db2:27002', 'db3:27000']);
117-
118-
$connection = DB::connection('mongodb');
119-
$this->assertEquals('mongodb://db1:27001,db2:27002,db3:27000', (string) $connection->getMongoClient());
120-
}
121174
}

tests/DsnTest.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/TestCase.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ protected function getEnvironmentSetUp($app)
5656
$app['config']->set('database.connections.mysql', $config['connections']['mysql']);
5757
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
5858
$app['config']->set('database.connections.mongodb2', $config['connections']['mongodb']);
59-
$app['config']->set('database.connections.dsn_mongodb', $config['connections']['dsn_mongodb']);
60-
$app['config']->set('database.connections.dsn_mongodb_db', $config['connections']['dsn_mongodb_db']);
6159

6260
$app['config']->set('auth.model', 'User');
6361
$app['config']->set('auth.providers.users.model', 'User');

tests/config/database.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
11
<?php
22

3-
$mongoHost = env('MONGO_HOST', 'mongodb');
4-
$mongoPort = env('MONGO_PORT') ? (int) env('MONGO_PORT') : 27017;
5-
$mysqlPort = env('MYSQL_PORT') ? (int) env('MYSQL_PORT') : 3306;
6-
73
return [
8-
94
'connections' => [
10-
115
'mongodb' => [
126
'name' => 'mongodb',
137
'driver' => 'mongodb',
14-
'host' => $mongoHost,
8+
'dsn' => env('MONGODB_URI', 'mongodb://127.0.0.1/'),
159
'database' => env('MONGO_DATABASE', 'unittest'),
1610
],
1711

18-
'dsn_mongodb' => [
19-
'driver' => 'mongodb',
20-
'dsn' => "mongodb://$mongoHost:$mongoPort",
21-
'database' => env('MONGO_DATABASE', 'unittest'),
22-
],
23-
24-
'dsn_mongodb_db' => [
25-
'driver' => 'mongodb',
26-
'dsn' => "mongodb://$mongoHost:$mongoPort/".env('MONGO_DATABASE', 'unittest'),
27-
],
28-
2912
'mysql' => [
3013
'driver' => 'mysql',
3114
'host' => env('MYSQL_HOST', 'mysql'),
32-
'port' => $mysqlPort,
15+
'port' => env('MYSQL_PORT') ? (int) env('MYSQL_PORT') : 3306,
3316
'database' => env('MYSQL_DATABASE', 'unittest'),
3417
'username' => env('MYSQL_USERNAME', 'root'),
3518
'password' => env('MYSQL_PASSWORD', ''),
@@ -38,5 +21,4 @@
3821
'prefix' => '',
3922
],
4023
],
41-
4224
];

0 commit comments

Comments
 (0)