Skip to content

Commit befbe86

Browse files
authored
Merge pull request #156 from RonasIT/142-can-not-generate-nova-resource-in-laravel-11
142 can not generate nova resource in laravel 11
2 parents 614d650 + b7123d7 commit befbe86

File tree

11 files changed

+94
-96
lines changed

11 files changed

+94
-96
lines changed

src/Generators/NovaResourceGenerator.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RonasIT\Support\Generators;
44

5+
use Doctrine\DBAL\DriverManager;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Facades\DB;
78
use Laravel\Nova\NovaServiceProvider;
@@ -143,9 +144,7 @@ protected function getFieldsFromDatabase(): array
143144
$modelClass = $this->getModelClass($this->model);
144145
$model = app($modelClass);
145146

146-
$columns = DB::connection($model->getConnectionName())
147-
->getDoctrineSchemaManager()
148-
->listTableColumns($model->getTable());
147+
$columns = $this->getColumnList($model->getTable(), $model->getConnectionName());
149148

150149
$fields = array_map(function ($column) {
151150
return new DatabaseNovaField($column);
@@ -158,4 +157,21 @@ protected function commandFieldsExists(): bool
158157
{
159158
return !empty(Arr::flatten($this->fields));
160159
}
160+
161+
protected function getColumnList(string $table, ?string $connectionName = null): array
162+
{
163+
$config = DB::connection($connectionName)->getConfig();
164+
165+
$dbalConnection = DriverManager::getConnection([
166+
'dbname' => $config['database'],
167+
'user' => $config['username'],
168+
'password' => $config['password'],
169+
'host' => $config['host'],
170+
'driver' => "pdo_{$config['driver']}",
171+
]);
172+
173+
return $dbalConnection
174+
->createSchemaManager()
175+
->listTableColumns($table);
176+
}
161177
}

src/Generators/ServiceGenerator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace RonasIT\Support\Generators;
44

55
use Illuminate\Support\Arr;
6-
use Illuminate\Support\Str;
7-
use RonasIT\Support\DTO\RelationsDTO;
86
use RonasIT\Support\Exceptions\ClassNotExistsException;
97
use RonasIT\Support\Events\SuccessCreateMessage;
108

tests/CommandTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\Config;
77
use RonasIT\Support\Exceptions\ClassNotExistsException;
88
use RonasIT\Support\Tests\Support\Command\CommandMockTrait;
9+
use RonasIT\Support\Tests\Support\Command\Models\Post;
910
use UnexpectedValueException;
1011

1112
class CommandTest extends TestCase
@@ -49,7 +50,8 @@ public function testCallCommand()
4950
Carbon::setTestNow('2016-10-20 11:05:00');
5051

5152
$this->mockGenerator();
52-
$this->mockGettingModelInstance();
53+
$this->mockGettingModelInstance(new Post());
54+
$this->mockDBTransactionStartRollback(2);
5355

5456
$this
5557
->artisan('make:entity Post --methods=CRUD')

tests/MigrationGeneratorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace RonasIT\Support\Tests;
44

5-
use Illuminate\Support\Carbon;
65
use RonasIT\Support\DTO\RelationsDTO;
76
use RonasIT\Support\Events\WarningEvent;
87
use RonasIT\Support\Exceptions\UnknownFieldTypeException;

tests/NovaResourceGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use RonasIT\Support\Exceptions\ClassNotExistsException;
99
use RonasIT\Support\Generators\NovaResourceGenerator;
1010
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\NovaResourceGeneratorMockTrait;
11+
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\Post;
1112

1213
class NovaResourceGeneratorTest extends TestCase
1314
{
@@ -114,7 +115,7 @@ public function testSuccessWithoutCommandLineFields()
114115
{
115116
$this->mockNovaServiceProviderExists();
116117

117-
$this->mockGettingModelInstance();
118+
$this->mockGettingModelInstance(new Post());
118119

119120
app(NovaResourceGenerator::class)
120121
->setModel('Post')

tests/NovaTestGeneratorTest.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use RonasIT\Support\Generators\NovaTestGenerator;
1111
use RonasIT\Support\Tests\Support\NovaTestGeneratorTest\NovaTestGeneratorMockTrait;
1212
use Laravel\Nova\NovaServiceProvider;
13-
use Mockery;
1413

1514
class NovaTestGeneratorTest extends TestCase
1615
{
@@ -76,10 +75,7 @@ public function testNovaTestStubNotExist()
7675
'entity-generator.stubs.nova_test' => 'incorrect_stub',
7776
]);
7877

79-
$mock = Mockery::mock('alias:Illuminate\Support\Facades\DB');
80-
$mock
81-
->shouldReceive('beginTransaction', 'rollBack')
82-
->once();
78+
$this->mockDBTransactionStartRollback();
8379

8480
app(NovaTestGenerator::class)
8581
->setModel('WelcomeBonus')
@@ -130,10 +126,7 @@ public function testSuccess()
130126
'entity-generator.paths.models' => 'RonasIT/Support/Tests/Support/Models',
131127
]);
132128

133-
$mock = Mockery::mock('alias:Illuminate\Support\Facades\DB');
134-
$mock
135-
->shouldReceive('beginTransaction', 'rollBack')
136-
->once();
129+
$this->mockDBTransactionStartRollback();
137130

138131
$this->mockNativeGeneratorFunctions(
139132
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),

tests/Support/Command/CommandMockTrait.php

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

33
namespace RonasIT\Support\Tests\Support\Command;
44

5-
use Illuminate\Database\Connection;
6-
use Illuminate\Support\Facades\DB;
75
use RonasIT\Support\Generators\NovaTestGenerator;
8-
use RonasIT\Support\Tests\Support\Command\Models\Post;
96
use RonasIT\Support\Tests\Support\FileSystemMock;
107
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
118
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\SchemaManager;
12-
use Mockery;
139

1410
trait CommandMockTrait
1511
{
@@ -60,22 +56,4 @@ public function mockGenerator(): void
6056
$this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Command\Models\Post', true]),
6157
);
6258
}
63-
64-
public function mockGettingModelInstance(): void
65-
{
66-
$connectionMock = Mockery::mock(Connection::class)->makePartial();
67-
$connectionMock
68-
->expects('getDoctrineSchemaManager')
69-
->andReturn(new SchemaManager());
70-
71-
$mock = Mockery::mock('alias:' . DB::class);
72-
$mock
73-
->expects('connection')
74-
->with('pgsql')
75-
->andReturn($connectionMock);
76-
77-
$mock->shouldReceive('beginTransaction', 'rollBack');
78-
79-
$this->app->instance('App\\Models\\Post', new Post());
80-
}
8159
}

tests/Support/GeneratorMockTrait.php

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

33
namespace RonasIT\Support\Tests\Support;
44

5+
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\DriverManager;
7+
use Doctrine\DBAL\Schema\AbstractSchemaManager;
8+
use Doctrine\DBAL\Schema\Column;
9+
use Doctrine\DBAL\Types\DateTimeType;
10+
use Doctrine\DBAL\Types\IntegerType;
11+
use Doctrine\DBAL\Types\StringType;
12+
use Illuminate\Database\Connection as LaravelConnection;
13+
use Illuminate\Support\Facades\DB;
514
use Laravel\Nova\NovaServiceProvider;
615
use RonasIT\Support\Traits\MockTrait;
16+
use Mockery;
17+
use Illuminate\Support\Str;
718

819
trait GeneratorMockTrait
920
{
@@ -43,4 +54,57 @@ public function mockPhpFileContent(): string
4354
{
4455
return '<?php';
4556
}
57+
58+
public function mockDBTransactionStartRollback(int $count = 1): void
59+
{
60+
DB::shouldReceive('beginTransaction')->times($count);
61+
DB::shouldReceive('rollBack')->times($count);
62+
}
63+
64+
public function mockGettingModelInstance(object $model): void
65+
{
66+
$laravelConnectionMock = Mockery::mock(LaravelConnection::class);
67+
$laravelConnectionMock
68+
->shouldReceive('getConfig')
69+
->andReturn([
70+
'database' => 'my_db',
71+
'username' => 'my_user',
72+
'password' => 'secret',
73+
'host' => '127.0.0.1',
74+
'driver' => 'pgsql',
75+
]);
76+
77+
DB::shouldReceive('connection')
78+
->with('pgsql')
79+
->andReturn($laravelConnectionMock);
80+
81+
$schemaManagerMock = Mockery::mock(AbstractSchemaManager::class);
82+
$schemaManagerMock
83+
->shouldReceive('listTableColumns')
84+
->andReturn([
85+
new Column('id', new IntegerType()),
86+
new Column('title', new StringType()),
87+
new Column('created_at', new DateTimeType()),
88+
]);
89+
90+
$connectionMock = Mockery::mock(Connection::class);
91+
$connectionMock->makePartial()
92+
->expects('createSchemaManager')
93+
->andReturn($schemaManagerMock);
94+
95+
Mockery::mock('alias:' . DriverManager::class)
96+
->shouldReceive('getConnection')
97+
->with([
98+
'dbname' => 'my_db',
99+
'user' => 'my_user',
100+
'password' => 'secret',
101+
'host' => '127.0.0.1',
102+
'driver' => 'pdo_pgsql',
103+
])
104+
->andReturn($connectionMock);
105+
106+
$modelName = Str::afterLast(get_class($model), '\\');
107+
108+
$this->app->instance("App\\Models\\{$modelName}", $model);
109+
}
46110
}

tests/Support/NovaResourceGeneratorTest/NovaResourceGeneratorMockTrait.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace RonasIT\Support\Tests\Support\NovaResourceGeneratorTest;
44

5-
use Illuminate\Database\Connection;
6-
use Illuminate\Support\Facades\DB;
7-
use Mockery;
85
use RonasIT\Support\Tests\Support\FileSystemMock;
96
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
107

@@ -23,22 +20,6 @@ public function mockFilesystem(): void
2320
$fileSystemMock->setStructure();
2421
}
2522

26-
public function mockGettingModelInstance(): void
27-
{
28-
$connectionMock = Mockery::mock(Connection::class)->makePartial();
29-
$connectionMock
30-
->expects('getDoctrineSchemaManager')
31-
->andReturn(new SchemaManager);
32-
33-
$mock = Mockery::mock('alias:' . DB::class);
34-
$mock
35-
->expects('connection')
36-
->with('pgsql')
37-
->andReturn($connectionMock);
38-
39-
$this->app->instance('App\\Models\\Post', new Post);
40-
}
41-
4223
public function mockFileSystemWithoutPostModel(): void
4324
{
4425
$fileSystemMock = new FileSystemMock();

tests/Support/NovaResourceGeneratorTest/SchemaManager.php

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

0 commit comments

Comments
 (0)