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

orchestral / testbench-core / 7147460739

08 Dec 2023 11:48PM UTC coverage: 92.071% (-0.6%) from 92.706%
7147460739

Pull #166

github

crynobone
wip

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

78 of 91 new or added lines in 7 files covered. (85.71%)

5 existing lines in 3 files now uncovered.

1138 of 1236 relevant lines covered (92.07%)

45.17 hits per line

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

93.33
/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
            },
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: function () {
113✔
59
                /** @phpstan-ignore-next-line */
NEW
60
                $this->defineDatabaseMigrationsUsingPest();
×
61
            }
113✔
62
        )->get('attribute');
113✔
63

64
        $callback();
113✔
65

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

72
        $this->defineDatabaseSeeders();
113✔
73

74
        $this->beforeApplicationDestroyed(function () {
113✔
75
            $this->destroyDatabaseMigrations();
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✔
UNCOV
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