Skip to content

Commit 835c4c3

Browse files
committed
fix: test suite models bootstrap
1 parent c082a93 commit 835c4c3

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

tests/Pest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3+
use Illuminate\Foundation\Testing\RefreshDatabase;
34
use Illuminate\Support\Facades\Mail;
45
use Backstage\Mails\Tests\TestCase;
56

67
uses(TestCase::class)
8+
->use(RefreshDatabase::class)
79
->in(__DIR__);
810

911
beforeEach(function () {

tests/TestCase.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,73 @@
22

33
namespace Backstage\Mails\Tests;
44

5+
use Backstage\Mails\MailsServiceProvider;
56
use Illuminate\Database\Eloquent\Factories\Factory;
67
use Illuminate\Filesystem\Filesystem;
78
use NotificationChannels\Discord\DiscordServiceProvider;
89
use Orchestra\Testbench\TestCase as Orchestra;
9-
use SplFileInfo;
10-
use Backstage\Mails\MailsServiceProvider;
1110

1211
class TestCase extends Orchestra
1312
{
13+
14+
protected static array $migrations = [];
15+
1416
protected function setUp(): void
1517
{
1618
parent::setUp();
1719

1820
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'
2022
);
23+
24+
$this->loadMigrations();
2125
}
2226

23-
protected function getPackageProviders($app)
27+
protected function getPackageProviders($app): array
2428
{
2529
return [
2630
DiscordServiceProvider::class,
2731
MailsServiceProvider::class,
2832
];
2933
}
3034

35+
/**
36+
* Set up the environment for testing.
37+
*
38+
* @param \Illuminate\Foundation\Application $app
39+
*/
3140
public function getEnvironmentSetUp($app)
3241
{
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:',
3646
]);
3747

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+
}
4073
}
4174
}

0 commit comments

Comments
 (0)