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

orchestral / testbench-core / 13172071658

06 Feb 2025 04:52AM UTC coverage: 91.702%. Remained the same
13172071658

push

github

crynobone
Merge branch '8.x' into 9.x

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

1525 of 1663 relevant lines covered (91.7%)

74.11 hits per line

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

78.72
/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()) {
186✔
35
            $this->afterApplicationCreated(static function () {
88✔
36
                static::usesTestingFeature(new ResetRefreshDatabaseState);
88✔
37
            });
88✔
38
        }
39
    }
40

41
    /**
42
     * Teardown the test environment.
43
     *
44
     * @return void
45
     */
46
    protected function tearDownInteractsWithMigrations(): void
47
    {
48
        if (
49
            (\count($this->cachedTestMigratorProcessors) > 0 && static::usesRefreshDatabaseTestingConcern())
186✔
50
            || ($this->usesSqliteInMemoryDatabaseConnection() && ! empty(RefreshDatabaseState::$inMemoryConnections))
186✔
51
        ) {
52
            ResetRefreshDatabaseState::run();
7✔
53
        }
54

55
        foreach ($this->cachedTestMigratorProcessors as $migrator) {
186✔
56
            $migrator->rollback();
12✔
57
        }
58
    }
59

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

72
        if (
73
            (\is_string($paths) || Arr::isList($paths))
2✔
74
            && static::usesRefreshDatabaseTestingConcern()
2✔
75
            && RefreshDatabaseState::$migrated === false
2✔
76
            && RefreshDatabaseState::$lazilyRefreshed === false
2✔
77
        ) {
78
            /** @var array<int, string>|string $paths */
79
            load_migration_paths($app, $paths);
×
80

81
            return;
×
82
        }
83

84
        /** @var array<string, mixed>|string $paths */
85
        $migrator = new MigrateProcessor($this, $this->resolvePackageMigrationsOptions($paths));
2✔
86
        $migrator->up();
2✔
87

88
        array_unshift($this->cachedTestMigratorProcessors, $migrator);
2✔
89

90
        $this->resetApplicationArtisanCommands($app);
2✔
91
    }
92

93
    /**
94
     * Define hooks to migrate the database before each test without rollback after.
95
     *
96
     * @api
97
     *
98
     * @param  array<string, mixed>|string  $paths
99
     * @return void
100
     *
101
     * @deprecated
102
     */
103
    #[\Deprecated(message: 'Use `loadMigrationsFrom()` instead', since: '9.0.7')]
104
    protected function loadMigrationsWithoutRollbackFrom(array|string $paths): void
105
    {
106
        $app = laravel_or_fail($this->app);
×
107

108
        $migrator = new MigrateProcessor($this, $this->resolvePackageMigrationsOptions($paths));
×
109
        $migrator->up();
×
110

111
        array_unshift($this->cachedTestMigratorProcessors, $migrator);
×
112

113
        $this->resetApplicationArtisanCommands($app);
×
114
    }
115

116
    /**
117
     * Resolve Package Migrations Artisan command options.
118
     *
119
     * @internal
120
     *
121
     * @param  array<string, mixed>|string  $paths
122
     * @return array<string, mixed>
123
     *
124
     * @throws \InvalidArgumentException
125
     */
126
    protected function resolvePackageMigrationsOptions(array|string $paths = []): array
127
    {
128
        $options = \is_array($paths) ? $paths : ['--path' => $paths];
2✔
129

130
        if (isset($options['--realpath']) && ! \is_bool($options['--realpath'])) {
2✔
131
            throw new InvalidArgumentException('Expect --realpath to be a boolean.');
×
132
        }
133

134
        $options['--realpath'] = true;
2✔
135

136
        return $options;
2✔
137
    }
138

139
    /**
140
     * Migrate Laravel's default migrations.
141
     *
142
     * @api
143
     *
144
     * @param  array<string, mixed>|string  $database
145
     * @return void
146
     */
147
    protected function loadLaravelMigrations(array|string $database = []): void
148
    {
149
        $app = laravel_or_fail($this->app);
10✔
150

151
        $options = $this->resolveLaravelMigrationsOptions($database);
10✔
152
        $options['--path'] = default_migration_path();
10✔
153
        $options['--realpath'] = true;
10✔
154

155
        $migrator = new MigrateProcessor($this, $this->resolveLaravelMigrationsOptions($options));
10✔
156
        $migrator->up();
10✔
157

158
        array_unshift($this->cachedTestMigratorProcessors, $migrator);
10✔
159

160
        $this->resetApplicationArtisanCommands($app);
10✔
161
    }
162

163
    /**
164
     * Migrate Laravel's default migrations without rollback.
165
     *
166
     * @api
167
     *
168
     * @param  array<string, mixed>|string  $database
169
     * @return void
170
     *
171
     * @deprecated
172
     */
173
    #[\Deprecated(message: 'Use `loadLaravelMigrations()` instead', since: '9.0.7')]
174
    protected function loadLaravelMigrationsWithoutRollback(array|string $database = []): void
175
    {
176
        $this->loadLaravelMigrations($database);
×
177
    }
178

179
    /**
180
     * Migrate all Laravel's migrations.
181
     *
182
     * @api
183
     *
184
     * @param  array<string, mixed>|string  $database
185
     * @return void
186
     */
187
    protected function runLaravelMigrations(array|string $database = []): void
188
    {
189
        $app = laravel_or_fail($this->app);
1✔
190

191
        $migrator = new MigrateProcessor($this, $this->resolveLaravelMigrationsOptions($database));
1✔
192
        $migrator->up();
1✔
193

194
        array_unshift($this->cachedTestMigratorProcessors, $migrator);
1✔
195

196
        $this->resetApplicationArtisanCommands($app);
1✔
197
    }
198

199
    /**
200
     * Migrate all Laravel's migrations without rollback.
201
     *
202
     * @api
203
     *
204
     * @param  array<string, mixed>|string  $database
205
     * @return void
206
     *
207
     * @deprecated
208
     */
209
    #[\Deprecated(message: 'Use `runLaravelMigrations()` method instead', since: '9.0.7')]
210
    protected function runLaravelMigrationsWithoutRollback(array|string $database = []): void
211
    {
212
        $this->runLaravelMigrations($database);
×
213
    }
214

215
    /**
216
     * Resolve Laravel Migrations Artisan command options.
217
     *
218
     * @internal
219
     *
220
     * @param  array<string, mixed>|string  $database
221
     * @return array<string, mixed>
222
     */
223
    protected function resolveLaravelMigrationsOptions(array|string $database = []): array
224
    {
225
        $options = \is_array($database) ? $database : ['--database' => $database];
11✔
226

227
        return $options;
11✔
228
    }
229
}
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

© 2026 Coveralls, Inc