|
2 | 2 |
|
3 | 3 | namespace Backstage\Mails\Tests; |
4 | 4 |
|
| 5 | +use Backstage\Mails\MailsServiceProvider; |
5 | 6 | use Illuminate\Database\Eloquent\Factories\Factory; |
6 | 7 | use Illuminate\Filesystem\Filesystem; |
7 | 8 | use NotificationChannels\Discord\DiscordServiceProvider; |
8 | 9 | use Orchestra\Testbench\TestCase as Orchestra; |
9 | | -use SplFileInfo; |
10 | | -use Backstage\Mails\MailsServiceProvider; |
11 | 10 |
|
12 | 11 | class TestCase extends Orchestra |
13 | 12 | { |
| 13 | + |
| 14 | + protected static array $migrations = []; |
| 15 | + |
14 | 16 | protected function setUp(): void |
15 | 17 | { |
16 | 18 | parent::setUp(); |
17 | 19 |
|
18 | 20 | Factory::guessFactoryNamesUsing( |
19 | | - fn (string $modelName) => 'Backstage\\Mails\\Database\\Factories\\'.class_basename($modelName).'Factory' |
| 21 | + fn(string $modelName) => 'Backstage\\Mails\\Database\\Factories\\' . class_basename($modelName) . 'Factory' |
20 | 22 | ); |
| 23 | + |
| 24 | + $this->loadMigrations(); |
21 | 25 | } |
22 | 26 |
|
23 | | - protected function getPackageProviders($app) |
| 27 | + protected function getPackageProviders($app): array |
24 | 28 | { |
25 | 29 | return [ |
26 | 30 | DiscordServiceProvider::class, |
27 | 31 | MailsServiceProvider::class, |
28 | 32 | ]; |
29 | 33 | } |
30 | 34 |
|
| 35 | + /** |
| 36 | + * Set up the environment for testing. |
| 37 | + * |
| 38 | + * @param \Illuminate\Foundation\Application $app |
| 39 | + */ |
31 | 40 | public function getEnvironmentSetUp($app) |
32 | 41 | { |
33 | | - config([ |
34 | | - 'database.default' => 'testing', |
35 | | - 'queue.default' => 'sync', |
| 42 | + $app['config']->set('database.default', 'sqlite'); |
| 43 | + $app['config']->set('database.connections.sqlite', [ |
| 44 | + 'driver' => 'sqlite', |
| 45 | + 'database' => ':memory:', |
36 | 46 | ]); |
37 | 47 |
|
38 | | - collect(app(Filesystem::class)->files(__DIR__.'/../database/migrations/')) |
39 | | - ->map(fn (SplFileInfo $file) => require __DIR__.'/../database/migrations/'.$file->getBasename()); |
| 48 | + $app['config']->set('queue.default', 'sync'); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Load and run migrations from stub files |
| 53 | + */ |
| 54 | + protected function loadMigrations(): void |
| 55 | + { |
| 56 | + $filesystem = new Filesystem(); |
| 57 | + $migrationFiles = $filesystem->files(__DIR__ . '/../database/migrations/'); |
| 58 | + |
| 59 | + // Sorting to ensure migrations run in the correct order |
| 60 | + usort($migrationFiles, function ($a, $b) { |
| 61 | + return strcmp($a->getFilename(), $b->getFilename()); |
| 62 | + }); |
| 63 | + |
| 64 | + foreach ($migrationFiles as $file) { |
| 65 | + // Skip if not a stub file |
| 66 | + if ($file->getExtension() !== 'stub') { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + $migration = include $file->getPathname(); |
| 71 | + $migration->up(); |
| 72 | + } |
40 | 73 | } |
41 | 74 | } |
0 commit comments