• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orchestral / testbench-core / 12324437332

13 Dec 2024 11:13PM UTC coverage: 91.777% (-0.5%) from 92.296%
12324437332

Pull #278

github

web-flow
Merge 82ecf708a into b1b7dd073
Pull Request #278: [7.x] Fixes database seeding using `WithWorkbench` and `RefreshDatabase`

6 of 14 new or added lines in 1 file covered. (42.86%)

25 existing lines in 8 files now uncovered.

1250 of 1362 relevant lines covered (91.78%)

64.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.02
/src/Concerns/InteractsWithMigrations.php
1
<?php
2

3
namespace Orchestra\Testbench\Concerns;
4

5
use Illuminate\Foundation\Testing\RefreshDatabaseState;
6
use Illuminate\Support\Arr;
7
use InvalidArgumentException;
8
use Orchestra\Testbench\Attributes\ResetRefreshDatabaseState;
9
use Orchestra\Testbench\Database\MigrateProcessor;
10

11
use function Orchestra\Testbench\default_migration_path;
12
use function Orchestra\Testbench\laravel_or_fail;
13
use function Orchestra\Testbench\load_migration_paths;
14

15
/**
16
 * @internal
17
 */
18
trait InteractsWithMigrations
19
{
20
    /**
21
     * List of cached migrators instances.
22
     *
23
     * @var array<int, \Orchestra\Testbench\Database\MigrateProcessor>
24
     */
25
    protected $cachedTestMigratorProcessors = [];
26

27
    /**
28
     * Setup the test environment.
29
     *
30
     * @return void
31
     */
32
    protected function setUpInteractsWithMigrations(): void
33
    {
34
        if ($this->usesSqliteInMemoryDatabaseConnection()) {
158✔
35
            $this->afterApplicationCreated(static function () {
32✔
36
                static::usesTestingFeature(new ResetRefreshDatabaseState);
32✔
37
            });
32✔
38
        }
39
    }
40

41
    /**
42
     * Teardown the test environment.
43
     *
44
     * @return void
45
     */
46
    protected function tearDownInteractsWithMigrations(): void
47
    {
48
        if (\count($this->cachedTestMigratorProcessors) > 0 && static::usesRefreshDatabaseTestingConcern()) {
158✔
49
            ResetRefreshDatabaseState::run();
1✔
50
        }
51

52
        foreach ($this->cachedTestMigratorProcessors as $migrator) {
158✔
53
            $migrator->rollback();
7✔
54
        }
55
    }
56

57
    /**
58
     * Define hooks to migrate the database before and after each test.
59
     *
60
     * @param  array<int|string, mixed>|string  $paths
61
     * @return void
62
     */
63
    protected function loadMigrationsFrom($paths): void
64
    {
65
        $app = laravel_or_fail($this->app);
2✔
66

67
        if (
68
            (\is_string($paths) || Arr::isList($paths))
2✔
69
            && static::usesRefreshDatabaseTestingConcern()
2✔
70
            && RefreshDatabaseState::$migrated === false
2✔
71
            && RefreshDatabaseState::$lazilyRefreshed === false
2✔
72
        ) {
73

74
            /** @var array<int, string>|string $paths */
75
            load_migration_paths($app, $paths);
×
76

UNCOV
77
            return;
×
78
        }
79

80
        /** @var array<string, mixed>|string $paths */
81
        $this->loadMigrationsWithoutRollbackFrom($paths);
2✔
82
    }
83

84
    /**
85
     * Define hooks to migrate the database before each test without rollback after.
86
     *
87
     * @param  array<string, mixed>|string  $paths
88
     * @return void
89
     *
90
     * @deprecated
91
     */
92
    protected function loadMigrationsWithoutRollbackFrom($paths): void
93
    {
94
        $app = laravel_or_fail($this->app);
2✔
95

96
        $migrator = new MigrateProcessor($this, $this->resolvePackageMigrationsOptions($paths));
2✔
97
        $migrator->up();
2✔
98

99
        array_unshift($this->cachedTestMigratorProcessors, $migrator);
2✔
100

101
        $this->resetApplicationArtisanCommands($app);
2✔
102
    }
103

104
    /**
105
     * Resolve Package Migrations Artisan command options.
106
     *
107
     * @param  array<string, mixed>|string  $paths
108
     * @return array<string, mixed>
109
     *
110
     * @throws \InvalidArgumentException
111
     */
112
    protected function resolvePackageMigrationsOptions($paths = []): array
113
    {
114
        $options = \is_array($paths) ? $paths : ['--path' => $paths];
2✔
115

116
        if (isset($options['--realpath']) && ! \is_bool($options['--realpath'])) {
2✔
UNCOV
117
            throw new InvalidArgumentException('Expect --realpath to be a boolean.');
×
118
        }
119

120
        $options['--realpath'] = true;
2✔
121

122
        return $options;
2✔
123
    }
124

125
    /**
126
     * Migrate Laravel's default migrations.
127
     *
128
     * @param  array<string, mixed>|string  $database
129
     * @return void
130
     */
131
    protected function loadLaravelMigrations($database = []): void
132
    {
133
        $this->loadLaravelMigrationsWithoutRollback($database);
5✔
134
    }
135

136
    /**
137
     * Migrate Laravel's default migrations without rollback.
138
     *
139
     * @param  array<string, mixed>|string  $database
140
     * @return void
141
     *
142
     * @deprecated
143
     */
144
    protected function loadLaravelMigrationsWithoutRollback($database = []): void
145
    {
146
        $app = laravel_or_fail($this->app);
5✔
147

148
        $options = $this->resolveLaravelMigrationsOptions($database);
5✔
149
        $options['--path'] = default_migration_path();
5✔
150
        $options['--realpath'] = true;
5✔
151

152
        $migrator = new MigrateProcessor($this, $this->resolveLaravelMigrationsOptions($options));
5✔
153
        $migrator->up();
5✔
154

155
        array_unshift($this->cachedTestMigratorProcessors, $migrator);
5✔
156

157
        $this->resetApplicationArtisanCommands($app);
5✔
158
    }
159

160
    /**
161
     * Migrate all Laravel's migrations.
162
     *
163
     * @param  array<string, mixed>|string  $database
164
     * @return void
165
     */
166
    protected function runLaravelMigrations($database = []): void
167
    {
168
        $this->runLaravelMigrationsWithoutRollback($database);
1✔
169
    }
170

171
    /**
172
     * Migrate all Laravel's migrations without rollback.
173
     *
174
     * @param  array<string, mixed>|string  $database
175
     * @return void
176
     *
177
     * @deprecated
178
     */
179
    protected function runLaravelMigrationsWithoutRollback($database = []): void
180
    {
181
        $app = laravel_or_fail($this->app);
1✔
182

183
        $migrator = new MigrateProcessor($this, $this->resolveLaravelMigrationsOptions($database));
1✔
184
        $migrator->up();
1✔
185

186
        array_unshift($this->cachedTestMigratorProcessors, $migrator);
1✔
187

188
        $this->resetApplicationArtisanCommands($app);
1✔
189
    }
190

191
    /**
192
     * Resolve Laravel Migrations Artisan command options.
193
     *
194
     * @param  array<string, mixed>|string  $database
195
     * @return array<string, mixed>
196
     */
197
    protected function resolveLaravelMigrationsOptions($database = []): array
198
    {
199
        $options = \is_array($database) ? $database : ['--database' => $database];
6✔
200

201
        return $options;
6✔
202
    }
203
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc