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

orchestral / testbench-core / 20611431580

31 Dec 2025 03:39AM UTC coverage: 91.948% (-0.1%) from 92.057%
20611431580

Pull #378

github

web-flow
Merge 67b6f089b into 26f382a1f
Pull Request #378: [10.x] Supports PHPUnit 13

1553 of 1689 relevant lines covered (91.95%)

78.62 hits per line

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

90.0
/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\Database\Eloquent\Factories\Factory;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Migrations\Migrator;
11
use Illuminate\Database\Schema\Builder as SchemaBuilder;
12
use Illuminate\Foundation\Bootstrap\HandleExceptions;
13
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
14
use Illuminate\Foundation\Bootstrap\RegisterProviders;
15
use Illuminate\Foundation\Console\AboutCommand;
16
use Illuminate\Foundation\Console\ChannelListCommand;
17
use Illuminate\Foundation\Console\RouteListCommand;
18
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
19
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance;
20
use Illuminate\Foundation\Http\Middleware\TrimStrings;
21
use Illuminate\Foundation\Http\Middleware\ValidateCsrfToken;
22
use Illuminate\Http\Middleware\TrustHosts;
23
use Illuminate\Http\Middleware\TrustProxies;
24
use Illuminate\Http\Resources\Json\JsonResource;
25
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
26
use Illuminate\Mail\Markdown;
27
use Illuminate\Queue\Console\WorkCommand;
28
use Illuminate\Queue\Queue;
29
use Illuminate\Routing\Middleware\ThrottleRequests;
30
use Illuminate\Support\Arr;
31
use Illuminate\Support\EncodedHtmlString;
32
use Illuminate\Support\Once;
33
use Illuminate\Support\Sleep;
34
use Illuminate\Support\Str;
35
use Illuminate\Validation\Validator;
36
use Illuminate\View\Component;
37
use Orchestra\Testbench\Concerns\CreatesApplication;
38
use Orchestra\Testbench\Console\Commander;
39
use Orchestra\Testbench\Contracts\Config as ConfigContract;
40
use Orchestra\Testbench\Workbench\Workbench;
41

42
use function Orchestra\Sidekick\join_paths;
43

44
/**
45
 * @api
46
 *
47
 * @phpstan-import-type TExtraConfig from \Orchestra\Testbench\Foundation\Config
48
 * @phpstan-import-type TOptionalExtraConfig from \Orchestra\Testbench\Foundation\Config
49
 *
50
 * @phpstan-type TConfig array{
51
 *   extra?: TOptionalExtraConfig,
52
 *   load_environment_variables?: bool,
53
 *   enabled_package_discoveries?: bool
54
 * }
55
 */
56
class Application
57
{
58
    use CreatesApplication {
59
        resolveApplicationResolvingCallback as protected resolveApplicationResolvingCallbackFromTrait;
60
        resolveApplicationConfiguration as protected resolveApplicationConfigurationFromTrait;
61
    }
62

63
    /**
64
     * The Illuminate application instance.
65
     *
66
     * @var \Illuminate\Foundation\Application|null
67
     */
68
    protected $app;
69

70
    /**
71
     * List of configurations.
72
     *
73
     * @var array<string, mixed>
74
     *
75
     * @phpstan-var TExtraConfig
76
     */
77
    protected array $config = [
78
        'env' => [],
79
        'providers' => [],
80
        'dont-discover' => [],
81
        'bootstrappers' => [],
82
    ];
83

84
    /**
85
     * The application resolving callback.
86
     *
87
     * @var (callable(\Illuminate\Foundation\Application):(void))|null
88
     */
89
    protected $resolvingCallback;
90

91
    /**
92
     * Load Environment variables.
93
     *
94
     * @var bool
95
     */
96
    protected bool $loadEnvironmentVariables = false;
97

98
    /**
99
     * Create new application resolver.
100
     *
101
     * @param  string|null  $basePath
102
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
103
     */
104
    public function __construct(
105
        protected readonly ?string $basePath = null,
106
        ?callable $resolvingCallback = null
107
    ) {
108
        $this->resolvingCallback = $resolvingCallback;
16✔
109
    }
110

111
    /**
112
     * Create new application resolver.
113
     *
114
     * @param  string|null  $basePath
115
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
116
     * @param  array<string, mixed>  $options
117
     *
118
     * @phpstan-param TConfig  $options
119
     *
120
     * @return static
121
     */
122
    public static function make(?string $basePath = null, ?callable $resolvingCallback = null, array $options = [])
123
    {
124
        return (new static($basePath, $resolvingCallback))->configure($options);
14✔
125
    }
126

127
    /**
128
     * Create new application resolver from configuration file.
129
     *
130
     * @param  \Orchestra\Testbench\Contracts\Config  $config
131
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
132
     * @param  array<string, mixed>  $options
133
     *
134
     * @phpstan-param TConfig  $options
135
     *
136
     * @return static
137
     */
138
    public static function makeFromConfig(ConfigContract $config, ?callable $resolvingCallback = null, array $options = [])
139
    {
140
        $basePath = $config['laravel'] ?? static::applicationBasePath();
1✔
141

142
        return (new static($config['laravel'], $resolvingCallback))->configure(array_merge($options, [
1✔
143
            'load_environment_variables' => is_file("{$basePath}/.env"),
1✔
144
            'extra' => $config->getExtraAttributes(),
1✔
145
        ]));
1✔
146
    }
147

148
    /**
149
     * Create symlink to vendor path via new application instance.
150
     *
151
     * @param  string|null  $basePath
152
     * @param  string  $workingVendorPath
153
     * @return \Illuminate\Foundation\Application
154
     *
155
     * @codeCoverageIgnore
156
     */
157
    public static function createVendorSymlink(?string $basePath, string $workingVendorPath)
158
    {
159
        $app = static::create(basePath: $basePath, options: ['extra' => ['dont-discover' => ['*']]]);
160

161
        (new Actions\CreateVendorSymlink($workingVendorPath))->handle($app);
162

163
        return $app;
164
    }
165

166
    /**
167
     * Delete symlink to vendor path via new application instance.
168
     *
169
     * @param  string|null  $basePath
170
     * @return \Illuminate\Foundation\Application
171
     *
172
     * @codeCoverageIgnore
173
     */
174
    public static function deleteVendorSymlink(?string $basePath)
175
    {
176
        $app = static::create(basePath: $basePath, options: ['extra' => ['dont-discover' => ['*']]]);
177

178
        (new Actions\DeleteVendorSymlink)->handle($app);
179

180
        return $app;
181
    }
182

183
    /**
184
     * Create new application instance.
185
     *
186
     * @param  string|null  $basePath
187
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
188
     * @param  array<string, mixed>  $options
189
     *
190
     * @phpstan-param TConfig  $options
191
     *
192
     * @return \Illuminate\Foundation\Application
193
     */
194
    public static function create(?string $basePath = null, ?callable $resolvingCallback = null, array $options = [])
195
    {
196
        return static::make($basePath, $resolvingCallback, $options)->createApplication();
11✔
197
    }
198

199
    /**
200
     * Create new application instance from configuration file.
201
     *
202
     * @param  \Orchestra\Testbench\Contracts\Config  $config
203
     * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
204
     * @param  array<string, mixed>  $options
205
     *
206
     * @phpstan-param TConfig  $options
207
     *
208
     * @return \Illuminate\Foundation\Application
209
     */
210
    public static function createFromConfig(ConfigContract $config, ?callable $resolvingCallback = null, array $options = [])
211
    {
212
        return static::makeFromConfig($config, $resolvingCallback, $options)->createApplication();
1✔
213
    }
214

215
    /**
216
     * Flush the application states.
217
     *
218
     * @param  \Orchestra\Testbench\Console\Commander|\Orchestra\Testbench\PHPUnit\TestCase  $instance
219
     * @return void
220
     */
221
    public static function flushState(object $instance): void
222
    {
223
        AboutCommand::flushState();
198✔
224
        Artisan::forgetBootstrappers();
198✔
225
        ChannelListCommand::resolveTerminalWidthUsing(null);
198✔
226
        Component::flushCache();
198✔
227
        Component::forgetComponentsResolver();
198✔
228
        Component::forgetFactory();
198✔
229
        ConvertEmptyStringsToNull::flushState();
198✔
230
        EncodedHtmlString::flushState();
198✔
231
        Factory::flushState();
198✔
232

233
        if (! $instance instanceof Commander) {
198✔
234
            HandleExceptions::flushState($instance);
198✔
235
        }
236

237
        if (class_exists(JsonApiResource::class)) {
198✔
238
            JsonResource::flushState(); // @phpstan-ignore staticMethod.notFound
×
239
            JsonApiResource::flushState();
×
240
        } else {
241
            JsonResource::wrap('data');
198✔
242
        }
243

244
        Markdown::flushState();
198✔
245
        Migrator::withoutMigrations([]);
198✔
246
        Model::handleDiscardedAttributeViolationUsing(null);
198✔
247
        Model::handleLazyLoadingViolationUsing(null);
198✔
248
        Model::handleMissingAttributeViolationUsing(null);
198✔
249
        Model::automaticallyEagerLoadRelationships(false);
198✔
250
        Model::preventAccessingMissingAttributes(false);
198✔
251
        Model::preventLazyLoading(false);
198✔
252
        Model::preventSilentlyDiscardingAttributes(false);
198✔
253
        Once::flush();
198✔
254
        PreventRequestsDuringMaintenance::flushState();
198✔
255
        Queue::createPayloadUsing(null);
198✔
256
        RegisterProviders::flushState();
198✔
257
        RouteListCommand::resolveTerminalWidthUsing(null);
198✔
258
        ScheduleListCommand::resolveTerminalWidthUsing(null);
198✔
259
        SchemaBuilder::$defaultStringLength = 255;
198✔
260
        SchemaBuilder::$defaultMorphKeyType = 'int';
198✔
261
        Signals::resolveAvailabilityUsing(null); // @phpstan-ignore argument.type
198✔
262
        Sleep::fake(false);
198✔
263
        Str::createRandomStringsNormally();
198✔
264
        Str::createUlidsNormally();
198✔
265
        Str::createUuidsNormally();
198✔
266
        ThrottleRequests::shouldHashKeys();
198✔
267
        TrimStrings::flushState();
198✔
268
        TrustProxies::flushState();
198✔
269
        TrustHosts::flushState();
198✔
270
        Validator::flushState();
198✔
271
        ValidateCsrfToken::flushState();
198✔
272
        WorkCommand::flushState();
198✔
273
    }
274

275
    /**
276
     * Configure the application options.
277
     *
278
     * @param  array<string, mixed>  $options
279
     *
280
     * @phpstan-param TConfig  $options
281
     *
282
     * @return $this
283
     */
284
    public function configure(array $options)
285
    {
286
        if (isset($options['load_environment_variables']) && \is_bool($options['load_environment_variables'])) {
15✔
287
            $this->loadEnvironmentVariables = $options['load_environment_variables'];
1✔
288
        }
289

290
        if (isset($options['enables_package_discoveries']) && \is_bool($options['enables_package_discoveries'])) {
15✔
291
            Arr::set($options, 'extra.dont-discover', []);
×
292
        }
293

294
        /** @var TExtraConfig $config */
295
        $config = Arr::only($options['extra'] ?? [], array_keys($this->config));
15✔
296

297
        $this->config = $config;
15✔
298

299
        return $this;
15✔
300
    }
301

302
    /**
303
     * Ignore package discovery from.
304
     *
305
     * @api
306
     *
307
     * @return array<int, string>
308
     */
309
    public function ignorePackageDiscoveriesFrom()
310
    {
311
        return $this->config['dont-discover'] ?? [];
16✔
312
    }
313

314
    /**
315
     * Get package providers.
316
     *
317
     * @api
318
     *
319
     * @param  \Illuminate\Foundation\Application  $app
320
     * @return array<int, class-string>
321
     */
322
    protected function getPackageProviders($app)
323
    {
324
        return $this->config['providers'] ?? [];
16✔
325
    }
326

327
    /**
328
     * Get package bootstrapper.
329
     *
330
     * @api
331
     *
332
     * @param  \Illuminate\Foundation\Application  $app
333
     * @return array<int, class-string>
334
     */
335
    protected function getPackageBootstrappers($app)
336
    {
337
        if (\is_null($bootstrappers = ($this->config['bootstrappers'] ?? null))) {
16✔
338
            return [];
14✔
339
        }
340

341
        return Arr::wrap($bootstrappers);
2✔
342
    }
343

344
    /**
345
     * Resolve application resolving callback.
346
     *
347
     * @internal
348
     *
349
     * @param  \Illuminate\Foundation\Application  $app
350
     * @return void
351
     */
352
    protected function resolveApplicationResolvingCallback($app): void
353
    {
354
        $this->resolveApplicationResolvingCallbackFromTrait($app);
16✔
355

356
        if (\is_callable($this->resolvingCallback)) {
16✔
357
            \call_user_func($this->resolvingCallback, $app);
×
358
        }
359
    }
360

361
    /**
362
     * Resolve the application's base path.
363
     *
364
     * @api
365
     *
366
     * @return string
367
     */
368
    protected function getApplicationBasePath()
369
    {
370
        return $this->basePath ?? static::applicationBasePath();
16✔
371
    }
372

373
    /**
374
     * Resolve application core environment variables implementation.
375
     *
376
     * @internal
377
     *
378
     * @param  \Illuminate\Foundation\Application  $app
379
     * @return void
380
     */
381
    protected function resolveApplicationEnvironmentVariables($app)
382
    {
383
        Env::disablePutenv();
16✔
384

385
        $app->terminating(static function () {
16✔
386
            Env::enablePutenv();
1✔
387
        });
16✔
388

389
        if ($this->loadEnvironmentVariables === true) {
16✔
390
            $app->make(LoadEnvironmentVariables::class)->bootstrap($app);
×
391
        }
392

393
        (new Bootstrap\LoadEnvironmentVariablesFromArray($this->config['env'] ?? []))->bootstrap($app);
16✔
394
    }
395

396
    /**
397
     * Resolve application core configuration implementation.
398
     *
399
     * @internal
400
     *
401
     * @param  \Illuminate\Foundation\Application  $app
402
     * @return void
403
     */
404
    protected function resolveApplicationConfiguration($app)
405
    {
406
        $this->resolveApplicationConfigurationFromTrait($app);
16✔
407

408
        (new Bootstrap\EnsuresDefaultConfiguration)->bootstrap($app);
16✔
409
    }
410

411
    /**
412
     * Resolve application Console Kernel implementation.
413
     *
414
     * @api
415
     *
416
     * @param  \Illuminate\Foundation\Application  $app
417
     * @return void
418
     */
419
    protected function resolveApplicationConsoleKernel($app)
420
    {
421
        if ($this->hasCustomApplicationKernels() === true) {
16✔
422
            return;
×
423
        }
424

425
        $kernel = Workbench::applicationConsoleKernel() ?? 'Orchestra\Testbench\Console\Kernel';
16✔
426

427
        if (is_file($app->basePath(join_paths('app', 'Console', 'Kernel.php'))) && class_exists('App\Console\Kernel')) {
16✔
428
            $kernel = 'App\Console\Kernel';
×
429
        }
430

431
        $app->singleton('Illuminate\Contracts\Console\Kernel', $kernel);
16✔
432
    }
433

434
    /**
435
     * Resolve application HTTP Kernel implementation.
436
     *
437
     * @api
438
     *
439
     * @param  \Illuminate\Foundation\Application  $app
440
     * @return void
441
     */
442
    protected function resolveApplicationHttpKernel($app)
443
    {
444
        if ($this->hasCustomApplicationKernels() === true) {
16✔
445
            return;
×
446
        }
447

448
        $kernel = Workbench::applicationHttpKernel() ?? 'Orchestra\Testbench\Http\Kernel';
16✔
449

450
        if (is_file($app->basePath(join_paths('app', 'Http', 'Kernel.php'))) && class_exists('App\Http\Kernel')) {
16✔
451
            $kernel = 'App\Http\Kernel';
×
452
        }
453

454
        $app->singleton('Illuminate\Contracts\Http\Kernel', $kernel);
16✔
455
    }
456
}
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