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

orchestral / testbench-core / 12171572045

05 Dec 2024 01:37AM UTC coverage: 90.937% (-0.04%) from 90.977%
12171572045

push

github

web-flow
[9.x] Add support for named in-memory SQLite connections (#271)

* [9.x] Add support for named in-memory SQLite connections

PR: https://github.com/laravel/framework/pull/53635

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

* wip

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

---------

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

4 of 5 new or added lines in 1 file covered. (80.0%)

1475 of 1622 relevant lines covered (90.94%)

71.17 hits per line

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

93.62
/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\RequiresDatabase;
9
use Orchestra\Testbench\Attributes\WithMigration;
10
use Orchestra\Testbench\Features\TestingFeature;
11

12
use function Orchestra\Testbench\laravel_or_fail;
13

14
/**
15
 * @internal
16
 */
17
trait HandlesDatabases
18
{
19
    /**
20
     * Setup database requirements.
21
     *
22
     * @internal
23
     *
24
     * @param  \Closure():void  $callback
25
     */
26
    protected function setUpDatabaseRequirements(Closure $callback): void
27
    {
28
        $app = laravel_or_fail($this->app);
178✔
29

30
        TestingFeature::run(
178✔
31
            testCase: $this,
178✔
32
            attribute: fn () => $this->parseTestMethodAttributes($app, RequiresDatabase::class),
178✔
33
        );
178✔
34

35
        $app['events']->listen(DatabaseRefreshed::class, function () {
178✔
36
            $this->defineDatabaseMigrationsAfterDatabaseRefreshed();
17✔
37
        });
178✔
38

39
        if (static::usesTestingConcern(WithLaravelMigrations::class)) {
178✔
40
            $this->setUpWithLaravelMigrations(); /** @phpstan-ignore method.notFound */
7✔
41
        }
42

43
        TestingFeature::run(
178✔
44
            testCase: $this,
178✔
45
            attribute: fn () => $this->parseTestMethodAttributes($app, WithMigration::class),
178✔
46
        );
178✔
47

48
        $attributeCallbacks = TestingFeature::run(
178✔
49
            testCase: $this,
178✔
50
            default: function () {
178✔
51
                $this->defineDatabaseMigrations();
178✔
52
                $this->beforeApplicationDestroyed(fn () => $this->destroyDatabaseMigrations());
178✔
53
            },
178✔
54
            annotation: fn () => $this->parseTestMethodAnnotations($app, 'define-db'),
178✔
55
            attribute: fn () => $this->parseTestMethodAttributes($app, DefineDatabase::class),
178✔
56
            pest: function () {
178✔
57
                $this->defineDatabaseMigrationsUsingPest(); /** @phpstan-ignore method.notFound */
×
58
                $this->beforeApplicationDestroyed(fn () => $this->destroyDatabaseMigrationsUsingPest()); /** @phpstan-ignore method.notFound */
×
59
            },
178✔
60
        )->get('attribute');
178✔
61

62
        $callback();
178✔
63

64
        $attributeCallbacks->handle();
178✔
65

66
        TestingFeature::run(
178✔
67
            testCase: $this,
178✔
68
            default: fn () => $this->defineDatabaseSeeders(),
178✔
69
            pest: fn () => $this->defineDatabaseSeedersUsingPest(), /** @phpstan-ignore method.notFound */
178✔
70
        );
178✔
71
    }
72

73
    /**
74
     * Determine if using in-memory SQLite database connection
75
     *
76
     * @api
77
     *
78
     * @param  string|null  $connection
79
     * @return bool
80
     */
81
    protected function usesSqliteInMemoryDatabaseConnection(?string $connection = null): bool
82
    {
83
        $app = laravel_or_fail($this->app);
178✔
84

85
        /** @var \Illuminate\Contracts\Config\Repository $config */
86
        $config = $app->make('config');
178✔
87

88
        /** @var string $connection */
89
        $connection ??= $config->get('database.default');
178✔
90

91
        /** @var array{driver: string, database: string}|null $database */
92
        $database = $config->get("database.connections.{$connection}");
178✔
93

94
        if (\is_null($database) || $database['driver'] !== 'sqlite') {
178✔
NEW
95
            return false;
×
96
        }
97

98
        return $database['database'] == ':memory:'
178✔
99
            || str_contains($database['database'], '?mode=memory')
178✔
100
            || str_contains($database['database'], '&mode=memory');
178✔
101
    }
102

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

115
    /**
116
     * Define database migrations after database refreshed.
117
     *
118
     * @api
119
     *
120
     * @return void
121
     */
122
    protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
123
    {
124
        // Define database migrations after database refreshed.
125
    }
17✔
126

127
    /**
128
     * Destroy database migrations.
129
     *
130
     * @api
131
     *
132
     * @return void
133
     */
134
    protected function destroyDatabaseMigrations()
135
    {
136
        // Destroy database migrations.
137
    }
177✔
138

139
    /**
140
     * Define database seeders.
141
     *
142
     * @api
143
     *
144
     * @return void
145
     */
146
    protected function defineDatabaseSeeders()
147
    {
148
        // Define database seeders.
149
    }
178✔
150
}
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