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

orchestral / testbench-core / 13171917748

06 Feb 2025 03:52AM UTC coverage: 92.94%. Remained the same
13171917748

push

github

crynobone
wip

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

1277 of 1374 relevant lines covered (92.94%)

66.71 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()) {
164✔
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()) {
164✔
49
            ResetRefreshDatabaseState::run();
1✔
50
        }
51

52
        foreach ($this->cachedTestMigratorProcessors as $migrator) {
164✔
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
            /** @var array<int, string>|string $paths */
74
            load_migration_paths($app, $paths);
×
75

76
            return;
×
77
        }
78

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

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

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

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

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

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

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

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

121
        return $options;
2✔
122
    }
123

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

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

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

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

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

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

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

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

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

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

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

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

200
        return $options;
6✔
201
    }
202
}
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