|
2 | 2 |
|
3 | 3 | namespace RonasIT\Support\Tests\Support; |
4 | 4 |
|
| 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; |
5 | 14 | use Laravel\Nova\NovaServiceProvider; |
6 | 15 | use RonasIT\Support\Traits\MockTrait; |
| 16 | +use Mockery; |
| 17 | +use Illuminate\Support\Str; |
7 | 18 |
|
8 | 19 | trait GeneratorMockTrait |
9 | 20 | { |
@@ -43,4 +54,57 @@ public function mockPhpFileContent(): string |
43 | 54 | { |
44 | 55 | return '<?php'; |
45 | 56 | } |
| 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 | + } |
46 | 110 | } |
0 commit comments