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

orchestral / testbench-core / 7147240326

08 Dec 2023 11:11PM UTC coverage: 92.439% (-0.07%) from 92.508%
7147240326

push

github

crynobone
wip

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

2 of 2 new or added lines in 1 file covered. (100.0%)

2 existing lines in 2 files now uncovered.

1137 of 1230 relevant lines covered (92.44%)

45.29 hits per line

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

95.35
/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
            /** @phpstan-ignore-next-line */
37
            $this->setUpWithLaravelMigrations();
4✔
38
        }
39

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

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

48
        $attributeCallbacks = $this->resolveTestbenchTestingFeature(
113✔
49
            testCase: function () {
113✔
50
                $this->defineDatabaseMigrations();
113✔
51
            },
113✔
52
            annotation: function () use ($app) {
113✔
53
                $this->parseTestMethodAnnotations($app, 'define-db');
113✔
54
            },
113✔
55
            attribute: function () use ($app) {
113✔
56
                return $this->parseTestMethodAttributes($app, DefineDatabase::class);
113✔
57
            },
113✔
58
            pest: '@defineEnvironment',
113✔
59
        )->get('attribute');
113✔
60

61
        $callback();
113✔
62

63
        if (isset($attributeCallbacks) && $attributeCallbacks->isNotEmpty()) {
113✔
64
            $attributeCallbacks->each(function ($callback) {
2✔
65
                value($callback);
2✔
66
            });
2✔
67
        }
68

69
        $this->defineDatabaseSeeders();
113✔
70

71
        $this->beforeApplicationDestroyed(function () {
113✔
72
            $this->destroyDatabaseMigrations();
113✔
73
        });
113✔
74
    }
75

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

88
        /** @var \Illuminate\Contracts\Config\Repository $config */
89
        $config = $app->make('config');
1✔
90

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

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

97
        return ! \is_null($database) && $database['driver'] === 'sqlite' && $database['database'] == ':memory:';
1✔
98
    }
99

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

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

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

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