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

orchestral / testbench-core / 12324437332

13 Dec 2024 11:13PM UTC coverage: 91.777% (-0.5%) from 92.296%
12324437332

Pull #278

github

web-flow
Merge 82ecf708a into b1b7dd073
Pull Request #278: [7.x] Fixes database seeding using `WithWorkbench` and `RefreshDatabase`

6 of 14 new or added lines in 1 file covered. (42.86%)

25 existing lines in 8 files now uncovered.

1250 of 1362 relevant lines covered (91.78%)

64.18 hits per line

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

90.57
/src/Foundation/Application.php
1
<?php
2

3
namespace Orchestra\Testbench\Foundation;
4

5
use Illuminate\Console\Application as Artisan;
6
use Illuminate\Console\Scheduling\ScheduleListCommand;
7
use Illuminate\Console\Signals;
8
use Illuminate\Foundation\Bootstrap\HandleExceptions;
9
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
10
use Illuminate\Foundation\Console\RouteListCommand;
11
use Illuminate\Http\Resources\Json\JsonResource;
12
use Illuminate\Queue\Queue;
13
use Illuminate\Support\Arr;
14
use Illuminate\View\Component;
15
use Orchestra\Testbench\Concerns\CreatesApplication;
16
use Orchestra\Testbench\Contracts\Config as ConfigContract;
17
use Orchestra\Testbench\Workbench\Workbench;
18

19
/**
20
 * @api
21
 *
22
 * @phpstan-import-type TExtraConfig from \Orchestra\Testbench\Foundation\Config
23
 * @phpstan-import-type TOptionalExtraConfig from \Orchestra\Testbench\Foundation\Config
24
 *
25
 * @phpstan-type TConfig array{
26
 *   extra?: TOptionalExtraConfig,
27
 *   load_environment_variables?: bool,
28
 *   enabled_package_discoveries?: bool
29
 * }
30
 */
31
class Application
32
{
33
    use CreatesApplication {
34
        resolveApplicationResolvingCallback as protected resolveApplicationResolvingCallbackFromTrait;
35
        resolveApplicationConfiguration as protected resolveApplicationConfigurationFromTrait;
36
    }
37

38
    /**
39
     * The Illuminate application instance.
40
     *
41
     * @var \Illuminate\Foundation\Application|null
42
     */
43
    protected $app;
44

45
    /**
46
     * The application base path.
47
     *
48
     * @var string|null
49
     */
50
    protected $basePath;
51

52
    /**
53
     * List of configurations.
54
     *
55
     * @var array<string, mixed>
56
     *
57
     * @phpstan-var TExtraConfig
58
     */
59
    protected $config = [
60
        'env' => [],
61
        'providers' => [],
62
        'dont-discover' => [],
63
        'bootstrappers' => [],
64
    ];
65

66
    /**
67
     * The application resolving callback.
68
     *
69
     * @var (callable(\Illuminate\Foundation\Application):(void))|null
70
     */
71
    protected $resolvingCallback;
72

73
    /**
74
     * Load Environment variables.
75
     *
76
     * @var bool
77
     */
78
    protected $loadEnvironmentVariables = false;
79

80
    /**
81
     * Create new application resolver.
82
     *
83
     * @param  string|null  $basePath
84
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
85
     */
86
    public function __construct(?string $basePath = null, ?callable $resolvingCallback = null)
87
    {
88
        $this->basePath = $basePath;
7✔
89
        $this->resolvingCallback = $resolvingCallback;
7✔
90
    }
91

92
    /**
93
     * Create new application resolver.
94
     *
95
     * @param  string|null  $basePath
96
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
97
     * @param  array<string, mixed>  $options
98
     * @return static
99
     *
100
     * @phpstan-param TConfig  $options
101
     */
102
    public static function make(?string $basePath = null, ?callable $resolvingCallback = null, array $options = [])
103
    {
104
        return (new static($basePath, $resolvingCallback))->configure($options);
5✔
105
    }
106

107
    /**
108
     * Create new application resolver from configuration file.
109
     *
110
     * @param  \Orchestra\Testbench\Contracts\Config  $config
111
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
112
     * @param  array<string, mixed>  $options
113
     * @return static
114
     *
115
     * @phpstan-param TConfig  $options
116
     */
117
    public static function makeFromConfig(ConfigContract $config, ?callable $resolvingCallback = null, array $options = [])
118
    {
119
        $basePath = $config['laravel'] ?? static::applicationBasePath();
1✔
120

121
        return (new static($config['laravel'], $resolvingCallback))->configure(array_merge($options, [
1✔
122
            'load_environment_variables' => is_file("{$basePath}/.env"),
1✔
123
            'extra' => $config->getExtraAttributes(),
1✔
124
        ]));
1✔
125
    }
126

127
    /**
128
     * Create symlink to vendor path via new application instance.
129
     *
130
     * @param  string|null  $basePath
131
     * @param  string  $workingVendorPath
132
     * @return \Illuminate\Foundation\Application
133
     *
134
     * @codeCoverageIgnore
135
     */
136
    public static function createVendorSymlink(?string $basePath, string $workingVendorPath)
137
    {
138
        $app = static::create(basePath: $basePath, options: ['extra' => ['dont-discover' => ['*']]]);
139

140
        (new Bootstrap\CreateVendorSymlink($workingVendorPath))->bootstrap($app);
141

142
        return $app;
143
    }
144

145
    /**
146
     * Create new application instance.
147
     *
148
     * @param  string|null  $basePath
149
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
150
     * @param  array<string, mixed>  $options
151
     * @return \Illuminate\Foundation\Application
152
     *
153
     * @phpstan-param TConfig  $options
154
     */
155
    public static function create(?string $basePath = null, ?callable $resolvingCallback = null, array $options = [])
156
    {
157
        return static::make($basePath, $resolvingCallback, $options)->createApplication();
2✔
158
    }
159

160
    /**
161
     * Create new application instance from configuration file.
162
     *
163
     * @param  \Orchestra\Testbench\Contracts\Config  $config
164
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
165
     * @param  array<string, mixed>  $options
166
     * @return \Illuminate\Foundation\Application
167
     *
168
     * @phpstan-param TConfig  $options
169
     */
170
    public static function createFromConfig(ConfigContract $config, ?callable $resolvingCallback = null, array $options = [])
171
    {
172
        return static::makeFromConfig($config, $resolvingCallback, $options)->createApplication();
1✔
173
    }
174

175
    /**
176
     * Flush the application states.
177
     *
178
     * @return void
179
     */
180
    public static function flushState(): void
181
    {
182
        Artisan::forgetBootstrappers();
158✔
183
        Component::flushCache();
158✔
184
        Component::forgetComponentsResolver();
158✔
185
        Component::forgetFactory();
158✔
186
        HandleExceptions::forgetApp();
158✔
187
        JsonResource::wrap('data');
158✔
188
        Queue::createPayloadUsing(null);
158✔
189
        RouteListCommand::resolveTerminalWidthUsing(null);
158✔
190
        ScheduleListCommand::resolveTerminalWidthUsing(null);
158✔
191
        Signals::resolveAvailabilityUsing(null);
158✔
192
    }
193

194
    /**
195
     * Configure the application options.
196
     *
197
     * @param  array<string, mixed>  $options
198
     * @return $this
199
     *
200
     * @phpstan-param TConfig  $options
201
     */
202
    public function configure(array $options)
203
    {
204
        if (isset($options['load_environment_variables']) && \is_bool($options['load_environment_variables'])) {
6✔
205
            $this->loadEnvironmentVariables = $options['load_environment_variables'];
1✔
206
        }
207

208
        if (isset($options['enables_package_discoveries']) && \is_bool($options['enables_package_discoveries'])) {
6✔
UNCOV
209
            Arr::set($options, 'extra.dont-discover', []);
×
210
        }
211

212
        /** @var TExtraConfig $config */
213
        $config = Arr::only($options['extra'] ?? [], array_keys($this->config));
6✔
214

215
        $this->config = $config;
6✔
216

217
        return $this;
6✔
218
    }
219

220
    /**
221
     * Ignore package discovery from.
222
     *
223
     * @return array
224
     */
225
    public function ignorePackageDiscoveriesFrom()
226
    {
227
        return $this->config['dont-discover'] ?? [];
7✔
228
    }
229

230
    /**
231
     * Get package providers.
232
     *
233
     * @param  \Illuminate\Foundation\Application  $app
234
     * @return array
235
     */
236
    protected function getPackageProviders($app)
237
    {
238
        return $this->config['providers'] ?? [];
7✔
239
    }
240

241
    /**
242
     * Get package bootstrapper.
243
     *
244
     * @param  \Illuminate\Foundation\Application  $app
245
     * @return array<int, class-string>
246
     */
247
    protected function getPackageBootstrappers($app)
248
    {
249
        if (\is_null($bootstrappers = ($this->config['bootstrappers'] ?? null))) {
7✔
250
            return [];
5✔
251
        }
252

253
        return Arr::wrap($bootstrappers);
2✔
254
    }
255

256
    /**
257
     * Resolve application resolving callback.
258
     *
259
     * @param  \Illuminate\Foundation\Application  $app
260
     * @return void
261
     */
262
    private function resolveApplicationResolvingCallback($app): void
263
    {
264
        $this->resolveApplicationResolvingCallbackFromTrait($app);
7✔
265

266
        if (\is_callable($this->resolvingCallback)) {
7✔
UNCOV
267
            \call_user_func($this->resolvingCallback, $app);
×
268
        }
269
    }
270

271
    /**
272
     * Get base path.
273
     *
274
     * @return string
275
     */
276
    protected function getBasePath()
277
    {
278
        return $this->basePath ?? static::applicationBasePath();
7✔
279
    }
280

281
    /**
282
     * Resolve application core environment variables implementation.
283
     *
284
     * @param  \Illuminate\Foundation\Application  $app
285
     * @return void
286
     */
287
    protected function resolveApplicationEnvironmentVariables($app)
288
    {
289
        Env::disablePutenv();
7✔
290

291
        $app->terminating(static function () {
7✔
292
            Env::enablePutenv();
1✔
293
        });
7✔
294

295
        if ($this->loadEnvironmentVariables === true) {
7✔
UNCOV
296
            $app->make(LoadEnvironmentVariables::class)->bootstrap($app);
×
297
        }
298

299
        (new Bootstrap\LoadEnvironmentVariablesFromArray($this->config['env'] ?? []))->bootstrap($app);
7✔
300
    }
301

302
    /**
303
     * Resolve application core configuration implementation.
304
     *
305
     * @param  \Illuminate\Foundation\Application  $app
306
     * @return void
307
     */
308
    protected function resolveApplicationConfiguration($app)
309
    {
310
        $this->resolveApplicationConfigurationFromTrait($app);
7✔
311

312
        (new Bootstrap\EnsuresDefaultConfiguration)->bootstrap($app);
7✔
313
    }
314

315
    /**
316
     * Resolve application Console Kernel implementation.
317
     *
318
     * @param  \Illuminate\Foundation\Application  $app
319
     * @return void
320
     */
321
    protected function resolveApplicationConsoleKernel($app)
322
    {
323
        $kernel = Workbench::applicationConsoleKernel() ?? 'Orchestra\Testbench\Console\Kernel';
7✔
324

325
        if (is_file($app->basePath('app/Console/Kernel.php')) && class_exists('App\Console\Kernel')) {
7✔
UNCOV
326
            $kernel = 'App\Console\Kernel';
×
327
        }
328

329
        $app->singleton('Illuminate\Contracts\Console\Kernel', $kernel);
7✔
330
    }
331

332
    /**
333
     * Resolve application HTTP Kernel implementation.
334
     *
335
     * @param  \Illuminate\Foundation\Application  $app
336
     * @return void
337
     */
338
    protected function resolveApplicationHttpKernel($app)
339
    {
340
        $kernel = Workbench::applicationHttpKernel() ?? 'Orchestra\Testbench\Http\Kernel';
7✔
341

342
        if (is_file($app->basePath('app/Http/Kernel.php')) && class_exists('App\Http\Kernel')) {
7✔
UNCOV
343
            $kernel = 'App\Http\Kernel';
×
344
        }
345

346
        $app->singleton('Illuminate\Contracts\Http\Kernel', $kernel);
7✔
347
    }
348
}
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

© 2025 Coveralls, Inc