Skip to content

Commit 22e06cc

Browse files
committed
refactor: remove db wrapper
1 parent a860cfe commit 22e06cc

File tree

5 files changed

+80
-88
lines changed

5 files changed

+80
-88
lines changed

src/Generators/NovaResourceGenerator.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace RonasIT\Support\Generators;
44

5+
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\DriverManager;
57
use Illuminate\Support\Arr;
6-
use RonasIT\Support\Support\DB;
8+
use Illuminate\Support\Facades\DB;
79
use Laravel\Nova\NovaServiceProvider;
810
use RonasIT\Support\Events\SuccessCreateMessage;
911
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
@@ -143,7 +145,7 @@ protected function getFieldsFromDatabase(): array
143145
$modelClass = $this->getModelClass($this->model);
144146
$model = app($modelClass);
145147

146-
$columns = DB::connection($model->getConnectionName())
148+
$columns = $this->connection($model->getConnectionName())
147149
->createSchemaManager()
148150
->listTableColumns($model->getTable());
149151

@@ -158,4 +160,19 @@ protected function commandFieldsExists(): bool
158160
{
159161
return !empty(Arr::flatten($this->fields));
160162
}
163+
164+
protected function connection(?string $name = null): Connection
165+
{
166+
$config = DB::connection($name)->getConfig();
167+
168+
$name = empty($name) ? "pdo_{$config['driver']}" : "pdo_{$name}";
169+
170+
return DriverManager::getConnection([
171+
'dbname' => $config['database'],
172+
'user' => $config['username'],
173+
'password' => $config['password'],
174+
'host' => $config['host'],
175+
'driver' => $name,
176+
]);
177+
}
161178
}

src/Support/DB.php

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

tests/DBTest.php

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

tests/Support/Command/CommandMockTrait.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace RonasIT\Support\Tests\Support\Command;
44

55
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\DriverManager;
67
use Doctrine\DBAL\Schema\AbstractSchemaManager;
78
use Doctrine\DBAL\Schema\Column;
89
use Doctrine\DBAL\Types\DateTimeType;
910
use Doctrine\DBAL\Types\IntegerType;
1011
use Doctrine\DBAL\Types\StringType;
12+
use Illuminate\Database\Connection as LaravelConnection;
1113
use RonasIT\Support\Generators\NovaTestGenerator;
12-
use RonasIT\Support\Support\DB;
14+
use Illuminate\Support\Facades\DB;
1315
use RonasIT\Support\Tests\Support\Command\Models\Post;
1416
use RonasIT\Support\Tests\Support\FileSystemMock;
1517
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
@@ -68,6 +70,23 @@ public function mockGenerator(): void
6870

6971
public function mockGettingModelInstance(): void
7072
{
73+
$config = [
74+
'database' => 'my_db',
75+
'username' => 'my_user',
76+
'password' => 'secret',
77+
'host' => '127.0.0.1',
78+
'driver' => 'pgsql',
79+
];
80+
81+
$laravelConnectionMock = Mockery::mock(LaravelConnection::class);
82+
$laravelConnectionMock
83+
->shouldReceive('getConfig')
84+
->andReturn($config);
85+
86+
DB::shouldReceive('connection')
87+
->with('pgsql')
88+
->andReturn($laravelConnectionMock);
89+
7190
$schemaManagerMock = Mockery::mock(AbstractSchemaManager::class);
7291
$schemaManagerMock
7392
->shouldReceive('listTableColumns')
@@ -84,10 +103,16 @@ public function mockGettingModelInstance(): void
84103
->expects('createSchemaManager')
85104
->andReturn($schemaManagerMock);
86105

87-
$mock = Mockery::mock('alias:' . DB::class);
88-
$mock
89-
->expects('connection')
90-
->with('pgsql')
106+
$driverManagerMock = Mockery::mock('alias:' . DriverManager::class);
107+
$driverManagerMock
108+
->shouldReceive('getConnection')
109+
->with([
110+
'dbname' => 'my_db',
111+
'user' => 'my_user',
112+
'password' => 'secret',
113+
'host' => '127.0.0.1',
114+
'driver' => 'pdo_pgsql',
115+
])
91116
->andReturn($connectionMock);
92117

93118
$this->app->instance('App\\Models\\Post', new Post());

tests/Support/NovaResourceGeneratorTest/NovaResourceGeneratorMockTrait.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
namespace RonasIT\Support\Tests\Support\NovaResourceGeneratorTest;
44

55
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\DriverManager;
67
use Doctrine\DBAL\Schema\Column;
78
use Doctrine\DBAL\Types\DateTimeType;
89
use Doctrine\DBAL\Types\IntegerType;
910
use Doctrine\DBAL\Types\StringType;
10-
use RonasIT\Support\Support\DB;
1111
use Mockery;
12+
use Illuminate\Support\Facades\DB;
1213
use RonasIT\Support\Tests\Support\FileSystemMock;
1314
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
1415
use Doctrine\DBAL\Schema\AbstractSchemaManager;
16+
use Illuminate\Database\Connection as LaravelConnection;
1517

1618
trait NovaResourceGeneratorMockTrait
1719
{
@@ -30,6 +32,23 @@ public function mockFilesystem(): void
3032

3133
public function mockGettingModelInstance(): void
3234
{
35+
$config = [
36+
'database' => 'my_db',
37+
'username' => 'my_user',
38+
'password' => 'secret',
39+
'host' => '127.0.0.1',
40+
'driver' => 'pgsql',
41+
];
42+
43+
$laravelConnectionMock = Mockery::mock(LaravelConnection::class);
44+
$laravelConnectionMock
45+
->shouldReceive('getConfig')
46+
->andReturn($config);
47+
48+
DB::shouldReceive('connection')
49+
->with('pgsql')
50+
->andReturn($laravelConnectionMock);
51+
3352
$schemaManagerMock = Mockery::mock(AbstractSchemaManager::class);
3453
$schemaManagerMock
3554
->shouldReceive('listTableColumns')
@@ -46,13 +65,19 @@ public function mockGettingModelInstance(): void
4665
->expects('createSchemaManager')
4766
->andReturn($schemaManagerMock);
4867

49-
$mock = Mockery::mock('alias:' . DB::class);
50-
$mock
51-
->expects('connection')
52-
->with('pgsql')
68+
$driverManagerMock = Mockery::mock('alias:' . DriverManager::class);
69+
$driverManagerMock
70+
->shouldReceive('getConnection')
71+
->with([
72+
'dbname' => 'my_db',
73+
'user' => 'my_user',
74+
'password' => 'secret',
75+
'host' => '127.0.0.1',
76+
'driver' => 'pdo_pgsql',
77+
])
5378
->andReturn($connectionMock);
5479

55-
$this->app->instance('App\\Models\\Post', new Post);
80+
$this->app->instance('App\\Models\\Post', new Post());
5681
}
5782

5883
public function mockFileSystemWithoutPostModel(): void

0 commit comments

Comments
 (0)