Skip to content

Commit de765a5

Browse files
author
Andrey Helldar
committed
Fixed testing bugs
1 parent 2363574 commit de765a5

File tree

7 files changed

+117
-49
lines changed

7 files changed

+117
-49
lines changed

tests/Concerns/Connections.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ protected function setDatabaseConnection($app, string $connection, string $defau
4848
$app->config->set('database.connections.' . $connection, $configurator->toArray());
4949
}
5050

51-
protected function getConfigurator(string $connection): BaseConfiguration
51+
protected function getConfigurator(string $driver): BaseConfiguration
5252
{
53-
return Manager::make()->get($connection);
53+
return Manager::make()->get($driver);
5454
}
5555
}

tests/Concerns/Database.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ protected function freshDatabase(): void
3636
{
3737
$this->createDatabases();
3838

39+
$this->runMigrations();
40+
3941
$this->fillTables();
4042
}
4143

@@ -45,14 +47,14 @@ protected function createDatabases(): void
4547
$this->createDatabase($this->target_connection, $this->defaultTargetConnectionName());
4648
}
4749

48-
protected function createDatabase(string $database, string $connection): void
50+
protected function createDatabase(string $database, string $driver): void
4951
{
50-
$instance = $this->getDatabaseConnector($connection);
52+
$instance = $this->getDatabaseConnector($driver);
5153

52-
$config = $this->getConnectionConfiguration($connection);
54+
$config = $this->getConnectionConfiguration($database, $driver);
5355

5456
$instance::make()
55-
->of($database, $connection)
57+
->of($database, $driver)
5658
->configuration($config)
5759
->dropDatabase()
5860
->createDatabase();
@@ -68,11 +70,18 @@ protected function getDatabaseConnector(string $connection): string
6870
return $this->connectors[$connection];
6971
}
7072

71-
protected function getConnectionConfiguration(string $connection): BaseConfiguration
73+
protected function getConnectionConfiguration(string $connection, string $driver): BaseConfiguration
7274
{
73-
$driver = Config::get('database.connections.' . $connection . '.driver');
7475
$config = Config::get('database.connections.' . $connection);
7576

76-
return Manager::make()->get($driver)->merge($config);
77+
return Manager::make()
78+
->get($driver)
79+
->merge($config)
80+
->setDatabase();
81+
}
82+
83+
protected function runMigrations(): void
84+
{
85+
$this->artisan('migrate', ['--database' => $this->source_connection])->run();
7786
}
7887
}

tests/Configurations/BaseConfiguration.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ abstract class BaseConfiguration implements Arrayable
99
{
1010
use Makeable;
1111

12+
protected $config = [];
13+
1214
protected $configuration;
1315

1416
public function __construct(Configuration $configuration)
1517
{
16-
$this->configuration = $configuration;
18+
$this->configuration = $configuration->merge($this->config);
1719
}
1820

1921
public function merge(array $config): self
@@ -23,7 +25,7 @@ public function merge(array $config): self
2325
return $this;
2426
}
2527

26-
public function setDatabase(?string $name): self
28+
public function setDatabase(string $name = null): self
2729
{
2830
$this->configuration->setDatabase($name);
2931

@@ -39,9 +41,19 @@ public function toArray(): array
3941

4042
protected function fill(): void
4143
{
42-
$this->configuration->setDatabase('default');
44+
$this->fillDatabase();
45+
$this->fillPassword();
46+
}
47+
48+
protected function fillDatabase(): void
49+
{
50+
if ($this->configuration->doesntDatabase()) {
51+
$this->configuration->setDatabase('default');
52+
}
53+
}
4354

44-
$this->configuration->setUsername(env('DB_USERNAME'));
55+
protected function fillPassword(): void
56+
{
4557
$this->configuration->setPassword(env('DB_PASSWORD'));
4658
}
4759
}

tests/Configurations/Configuration.php

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,39 @@
66
use Illuminate\Contracts\Support\Arrayable;
77
use Illuminate\Support\Arr;
88
use Illuminate\Support\Str;
9+
use InvalidArgumentException;
910

1011
/**
11-
* @method void setDriver(string $value)
1212
* @method void setHost(string $value)
13-
* @method void setPort(string $value)
1413
* @method void setDatabase(string|null $value)
1514
* @method void setUsername(string $value)
1615
* @method void setPassword(string $value)
17-
* @method void setSchema(string $value)
18-
* @method void setSslmode(string $value)
16+
* @method bool hasDatabase()
17+
* @method bool doesntDatabase()
1918
*/
2019
final class Configuration implements Arrayable
2120
{
2221
use Makeable;
2322

24-
protected $config = [
25-
'driver' => null,
26-
'url' => null,
27-
'host' => null,
28-
'port' => null,
29-
'database' => null,
30-
'username' => null,
31-
'password' => null,
32-
'unix_socket' => '',
33-
'charset' => 'utf8mb4',
34-
'collation' => 'utf8mb4_unicode_ci',
35-
'prefix' => '',
36-
'prefix_indexes' => true,
37-
'strict' => true,
38-
'engine' => null,
39-
'options' => [],
40-
];
41-
42-
public function __call(string $name, array $value): void
23+
protected $config = [];
24+
25+
public function __call(string $name, array $value)
4326
{
44-
Arr::set($this->config, $this->resolveKeyName($name), $this->castValue($value[0]));
27+
$key = $this->resolveKeyName($name);
28+
29+
switch (true) {
30+
case Str::startsWith($name, 'set'):
31+
return $this->set($key, $value);
32+
33+
case Str::startsWith($name, 'has'):
34+
return $this->has($key);
35+
36+
case Str::startsWith($name, 'doesnt'):
37+
return ! $this->has($key);
38+
39+
default:
40+
throw new InvalidArgumentException('Unknown method: ' . $name);
41+
}
4542
}
4643

4744
public function merge(array $config): self
@@ -56,6 +53,20 @@ public function toArray(): array
5653
return $this->config;
5754
}
5855

56+
protected function set(string $key, $value): self
57+
{
58+
Arr::set($this->config, $key, $this->castValue($value[0]));
59+
60+
return $this;
61+
}
62+
63+
protected function has(string $key): bool
64+
{
65+
$value = Arr::get($this->config, $key);
66+
67+
return ! empty($value);
68+
}
69+
5970
protected function resolveKeyName(string $name): string
6071
{
6172
return (string) Str::of($name)->snake()->after('_');

tests/Configurations/MySQL.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,30 @@
66

77
final class MySQL extends BaseConfiguration
88
{
9+
protected $config = [
10+
'driver' => Drivers::MYSQL,
11+
'url' => null,
12+
'host' => '127.0.0.1',
13+
'port' => '3306',
14+
'database' => 'forge',
15+
'username' => 'root',
16+
'password' => 'root',
17+
'unix_socket' => '',
18+
'charset' => 'utf8mb4',
19+
'collation' => 'utf8mb4_unicode_ci',
20+
'prefix' => '',
21+
'prefix_indexes' => true,
22+
'strict' => true,
23+
'engine' => null,
24+
'options' => [],
25+
];
26+
927
protected function fill(): void
1028
{
1129
parent::fill();
1230

1331
$this->configuration->setHost(env('MYSQL_HOST', 'mysql'));
1432

15-
$this->configuration->setDriver(Drivers::MYSQL);
16-
$this->configuration->setPort(3306);
33+
$this->configuration->setUsername(env('DB_USERNAME'));
1734
}
1835
}

tests/Configurations/Postgres.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,27 @@
66

77
final class Postgres extends BaseConfiguration
88
{
9+
protected $config = [
10+
'driver' => Drivers::POSTGRES,
11+
'url' => null,
12+
'host' => '127.0.0.1',
13+
'port' => '5432',
14+
'database' => 'forge',
15+
'username' => 'root',
16+
'password' => 'root',
17+
'charset' => 'utf8',
18+
'prefix' => '',
19+
'prefix_indexes' => true,
20+
'schema' => 'public',
21+
'sslmode' => 'prefer',
22+
];
23+
924
protected function fill(): void
1025
{
1126
parent::fill();
1227

1328
$this->configuration->setHost(env('PGSQL_HOST', 'postgres'));
1429

15-
$this->configuration->setDriver(Drivers::POSTGRES);
16-
$this->configuration->setPort(5432);
17-
18-
$this->configuration->setSchema('public');
19-
$this->configuration->setSslmode('prefer');
30+
$this->configuration->setUsername(env('DB_USERNAME'));
2031
}
2132
}

tests/Configurations/SqlServer.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@
66

77
final class SqlServer extends BaseConfiguration
88
{
9+
protected $config = [
10+
'driver' => Drivers::SQL_SERVER,
11+
'url' => null,
12+
'host' => '127.0.0.1',
13+
'port' => '1433',
14+
'database' => 'forge',
15+
'username' => 'sa',
16+
'password' => '',
17+
'charset' => 'utf8',
18+
'prefix' => '',
19+
'prefix_indexes' => true,
20+
];
21+
922
protected function fill(): void
1023
{
1124
parent::fill();
1225

1326
$this->configuration->setHost(env('SQLSRV_HOST', 'sqlsrv'));
14-
15-
$this->configuration->setDriver(Drivers::SQL_SERVER);
16-
$this->configuration->setPort(1433);
17-
18-
$this->configuration->setUsername('sa');
1927
}
2028
}

0 commit comments

Comments
 (0)