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

orchestral / testbench-core / 8336040821

19 Mar 2024 01:19AM UTC coverage: 92.45% (+0.01%) from 92.437%
8336040821

push

github

crynobone
wip

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

1102 of 1192 relevant lines covered (92.45%)

50.99 hits per line

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

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

3
namespace Orchestra\Testbench\Concerns;
4

5
use Closure;
6
use Illuminate\Database\Events\DatabaseRefreshed;
7
use Illuminate\Foundation\Testing\RefreshDatabase;
8
use Orchestra\Testbench\Attributes\DefineDatabase;
9
use Orchestra\Testbench\Attributes\ResetRefreshDatabaseState;
10
use Orchestra\Testbench\Attributes\WithMigration;
11
use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException;
12
use Orchestra\Testbench\Features\TestingFeature;
13

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

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

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

39
        TestingFeature::run(
122✔
40
            testCase: $this,
122✔
41
            attribute: fn () => $this->parseTestMethodAttributes($app, WithMigration::class),
122✔
42
        );
122✔
43

44
        $attributeCallbacks = TestingFeature::run(
122✔
45
            testCase: $this,
122✔
46
            default: function () {
122✔
47
                $this->defineDatabaseMigrations();
122✔
48
                $this->beforeApplicationDestroyed(fn () => $this->destroyDatabaseMigrations());
122✔
49
            },
122✔
50
            annotation: fn () => $this->parseTestMethodAnnotations($app, 'define-db'),
122✔
51
            attribute: fn () => $this->parseTestMethodAttributes($app, DefineDatabase::class)
122✔
52
        )->get('attribute');
122✔
53

54
        $callback();
122✔
55

56
        $attributeCallbacks->handle();
122✔
57

58
        $this->defineDatabaseSeeders();
122✔
59

60
        if (static::usesRefreshDatabaseTestingConcern()) {
122✔
61
            $this->beforeApplicationDestroyed(static fn () => ResetRefreshDatabaseState::run());
4✔
62
        }
63
    }
64

65
    /**
66
     * Determine if using in-memory SQLite database connection
67
     *
68
     * @param  string|null  $connection
69
     * @return bool
70
     */
71
    protected function usesSqliteInMemoryDatabaseConnection(?string $connection = null): bool
72
    {
73
        if (\is_null($app = $this->app)) {
1✔
74
            throw ApplicationNotAvailableException::make(__METHOD__);
×
75
        }
76

77
        /** @var \Illuminate\Contracts\Config\Repository $config */
78
        $config = $app->make('config');
1✔
79

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

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

86
        return ! \is_null($database) && $database['driver'] === 'sqlite' && $database['database'] == ':memory:';
1✔
87
    }
88

89
    /**
90
     * Define database migrations.
91
     *
92
     * @return void
93
     */
94
    protected function defineDatabaseMigrations()
95
    {
96
        // Define database migrations.
97
    }
118✔
98

99
    /**
100
     * Define database migrations after database refreshed.
101
     *
102
     * @return void
103
     */
104
    protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
105
    {
106
        // Define database migrations after database refreshed.
107
    }
9✔
108

109
    /**
110
     * Destroy database migrations.
111
     *
112
     * @return void
113
     */
114
    protected function destroyDatabaseMigrations()
115
    {
116
        // Destroy database migrations.
117
    }
122✔
118

119
    /**
120
     * Define database seeders.
121
     *
122
     * @return void
123
     */
124
    protected function defineDatabaseSeeders()
125
    {
126
        // Define database seeders.
127
    }
122✔
128
}
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