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

orchestral / testbench-core / 7147650994

09 Dec 2023 12:17AM UTC coverage: 91.87% (-0.8%) from 92.706%
7147650994

Pull #166

github

crynobone
wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Pull Request #166: PestPHP Improvements

74 of 88 new or added lines in 7 files covered. (84.09%)

1 existing line in 1 file now uncovered.

1130 of 1230 relevant lines covered (91.87%)

44.64 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
            /** @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
            default: function () {
113✔
50
                $this->defineDatabaseMigrations();
113✔
51

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

NEW
62
                $this->beforeApplicationDestroyed(function () {
×
63
                    /** @phpstan-ignore-next-line */
NEW
64
                    $this->destroyDatabaseMigrationsUsingPest();
×
NEW
65
                });
×
66
            }
113✔
67
        )->get('attribute');
113✔
68

69
        $callback();
113✔
70

71
        if (isset($attributeCallbacks) && $attributeCallbacks->isNotEmpty()) {
113✔
72
            $attributeCallbacks->each(function ($callback) {
2✔
73
                value($callback);
2✔
74
            });
2✔
75
        }
76

77
        $this->resolveTestbenchTestingFeature(
113✔
78
            default: fn () => $this->defineDatabaseSeeders(),
113✔
79
            pest: fn () => $this->defineDatabaseSeedersUsingPest() /** @phpstan-ignore-line */
113✔
80
        );
113✔
81
    }
82

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

95
        /** @var \Illuminate\Contracts\Config\Repository $config */
96
        $config = $app->make('config');
1✔
97

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

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

104
        return ! \is_null($database) && $database['driver'] === 'sqlite' && $database['database'] == ':memory:';
1✔
105
    }
106

107
    /**
108
     * Define database migrations.
109
     *
110
     * @return void
111
     */
112
    protected function defineDatabaseMigrations()
113
    {
114
        // Define database migrations.
115
    }
108✔
116

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

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

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