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

orchestral / testbench-core / 7148162141

09 Dec 2023 01:33AM UTC coverage: 92.02%. Remained the same
7148162141

push

github

crynobone
wip

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

1130 of 1228 relevant lines covered (92.02%)

44.71 hits per line

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

87.23
/src/Concerns/HandlesDatabases.php
1
<?php
2

3
namespace Orchestra\Testbench\Concerns;
4

5
use Closure;
6
use Illuminate\Database\Events\DatabaseRefreshed;
7
use Orchestra\Testbench\Attributes\DefineDatabase;
8
use Orchestra\Testbench\Attributes\ResetRefreshDatabaseState;
9
use Orchestra\Testbench\Attributes\WithMigration;
10
use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException;
11
use Orchestra\Testbench\Foundation\Bootstrap\SyncDatabaseEnvironmentVariables;
12

13
/**
14
 * @internal
15
 */
16
trait HandlesDatabases
17
{
18
    /**
19
     * Setup database requirements.
20
     *
21
     * @param  \Closure():void  $callback
22
     */
23
    protected function setUpDatabaseRequirements(Closure $callback): void
24
    {
25
        if (\is_null($app = $this->app)) {
113✔
26
            throw ApplicationNotAvailableException::make(__METHOD__);
×
27
        }
28

29
        (new SyncDatabaseEnvironmentVariables())->bootstrap($app);
113✔
30

31
        $app['events']->listen(DatabaseRefreshed::class, function () {
113✔
32
            $this->defineDatabaseMigrationsAfterDatabaseRefreshed();
8✔
33
        });
113✔
34

35
        if (static::usesTestingConcern(WithLaravelMigrations::class)) {
113✔
36
            $this->setUpWithLaravelMigrations(); /** @phpstan-ignore-line */
4✔
37
        }
38

39
        if (static::usesTestingConcern(HandlesAttributes::class)) {
113✔
40
            $this->parseTestMethodAttributes($app, ResetRefreshDatabaseState::class);
113✔
41
        }
42

43
        if (static::usesTestingConcern(HandlesAttributes::class)) {
113✔
44
            $this->parseTestMethodAttributes($app, WithMigration::class);
113✔
45
        }
46

47
        $attributeCallbacks = $this->resolveTestbenchTestingFeature(
113✔
48
            default: function () {
113✔
49
                $this->defineDatabaseMigrations();
113✔
50

51
                $this->beforeApplicationDestroyed(function () {
113✔
52
                    $this->destroyDatabaseMigrations();
113✔
53
                });
113✔
54
            },
113✔
55
            annotation: fn () => $this->parseTestMethodAnnotations($app, 'define-db'),
113✔
56
            attribute: fn () => $this->parseTestMethodAttributes($app, DefineDatabase::class),
113✔
57
            pest: function () {
113✔
58
                $this->defineDatabaseMigrationsUsingPest(); /** @phpstan-ignore-line */
×
59
                $this->beforeApplicationDestroyed(function () {
×
60
                    $this->destroyDatabaseMigrationsUsingPest(); /** @phpstan-ignore-line */
×
61
                });
×
62
            }
113✔
63
        )->get('attribute');
113✔
64

65
        $callback();
113✔
66

67
        if (isset($attributeCallbacks) && $attributeCallbacks->isNotEmpty()) {
113✔
68
            $attributeCallbacks->each(function ($callback) {
2✔
69
                value($callback);
2✔
70
            });
2✔
71
        }
72

73
        $this->resolveTestbenchTestingFeature(
113✔
74
            default: fn () => $this->defineDatabaseSeeders(),
113✔
75
            pest: fn () => $this->defineDatabaseSeedersUsingPest() /** @phpstan-ignore-line */
113✔
76
        );
113✔
77
    }
78

79
    /**
80
     * Determine if using in-memory SQLite database connection
81
     *
82
     * @param  string|null  $connection
83
     * @return bool
84
     */
85
    protected function usesSqliteInMemoryDatabaseConnection(?string $connection = null): bool
86
    {
87
        if (\is_null($app = $this->app)) {
1✔
88
            throw ApplicationNotAvailableException::make(__METHOD__);
×
89
        }
90

91
        /** @var \Illuminate\Contracts\Config\Repository $config */
92
        $config = $app->make('config');
1✔
93

94
        /** @var string $connection */
95
        $connection = ! \is_null($connection) ? $connection : $config->get('database.default');
1✔
96

97
        /** @var array{driver: string, database: string}|null $database */
98
        $database = $config->get("database.connections.{$connection}");
1✔
99

100
        return ! \is_null($database) && $database['driver'] === 'sqlite' && $database['database'] == ':memory:';
1✔
101
    }
102

103
    /**
104
     * Define database migrations.
105
     *
106
     * @return void
107
     */
108
    protected function defineDatabaseMigrations()
109
    {
110
        // Define database migrations.
111
    }
108✔
112

113
    /**
114
     * Define database migrations after database refreshed.
115
     *
116
     * @return void
117
     */
118
    protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
119
    {
120
        // Define database migrations after database refreshed.
121
    }
8✔
122

123
    /**
124
     * Destroy database migrations.
125
     *
126
     * @return void
127
     */
128
    protected function destroyDatabaseMigrations()
129
    {
130
        // Destroy database migrations.
131
    }
113✔
132

133
    /**
134
     * Define database seeders.
135
     *
136
     * @return void
137
     */
138
    protected function defineDatabaseSeeders()
139
    {
140
        // Define database seeders.
141
    }
113✔
142
}
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