• 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.32
/src/functions.php
1
<?php
2

3
namespace Orchestra\Testbench;
4

5
use Closure;
6
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
7
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Routing\Router;
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\Collection;
13
use Illuminate\Support\ProcessUtils;
14
use Illuminate\Support\Str;
15
use Illuminate\Testing\PendingCommand;
16
use InvalidArgumentException;
17
use Orchestra\Sidekick;
18
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
19
use PHPUnit\Runner\ShutdownHandler;
20

21
/**
22
 * Create Laravel application instance.
23
 *
24
 * @api
25
 *
26
 * @param  string|null  $basePath
27
 * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
28
 * @param  array{extra?: array{providers?: array, dont-discover?: array, env?: array}, load_environment_variables?: bool, enabled_package_discoveries?: bool}  $options
29
 * @param  \Orchestra\Testbench\Foundation\Config|null  $config
30
 * @return \Orchestra\Testbench\Foundation\Application
31
 */
32
function container(
33
    ?string $basePath = null,
34
    ?callable $resolvingCallback = null,
35
    array $options = [],
36
    ?Foundation\Config $config = null
37
): Foundation\Application {
38
    if ($config instanceof Foundation\Config) {
3✔
39
        return Foundation\Application::makeFromConfig($config, $resolvingCallback, $options);
×
40
    }
41

42
    return Foundation\Application::make($basePath, $resolvingCallback, $options);
3✔
43
}
44

45
/**
46
 * Run artisan command.
47
 *
48
 * @api
49
 *
50
 * @param  \Orchestra\Testbench\Contracts\TestCase|\Illuminate\Contracts\Foundation\Application  $context
51
 * @param  string  $command
52
 * @param  array<string, mixed>  $parameters
53
 * @return int
54
 */
55
function artisan(Contracts\TestCase|ApplicationContract $context, string $command, array $parameters = []): int
56
{
57
    if ($context instanceof ApplicationContract) {
15✔
58
        return $context->make(ConsoleKernel::class)->call($command, $parameters);
1✔
59
    }
60

61
    $command = $context->artisan($command, $parameters);
15✔
62

63
    return $command instanceof PendingCommand ? $command->run() : $command;
15✔
64
}
65

66
/**
67
 * Emit an exit event within a test.
68
 *
69
 * @param  \PHPUnit\Framework\TestCase|object|null  $testCase
70
 * @param  string|int  $status
71
 * @return never
72
 */
73
function bail(?object $testCase, string|int $status = 0): never
74
{
75
    if ($testCase instanceof PHPUnitTestCase && Sidekick\phpunit_version_compare('12.3.5', '>=')) {
×
76
        ShutdownHandler::resetMessage();
×
77
    }
78

79
    exit($status);
×
80
}
81

82
/**
83
 * Emit an exit event within a test.
84
 *
85
 * @param  \PHPUnit\Framework\TestCase|object|null  $testCase
86
 * @param  string|int  $status
87
 * @return never
88
 */
89
function terminate(?object $testCase, string|int $status = 0): never
90
{
91
    bail($testCase, $status);
×
92
}
93

94
/**
95
 * Run remote action using Testbench CLI.
96
 *
97
 * @api
98
 *
99
 * @param  (\Closure():(mixed))|array<int, string>|string  $command
100
 * @param  array<string, mixed>|string  $env
101
 * @param  bool|null  $tty
102
 * @return \Orchestra\Testbench\Foundation\Process\ProcessDecorator
103
 */
104
function remote(Closure|array|string $command, array|string $env = [], ?bool $tty = null): Foundation\Process\ProcessDecorator
105
{
106
    $remote = new Foundation\Process\RemoteCommand(
12✔
107
        package_path(), $env, $tty
12✔
108
    );
12✔
109

110
    $binary = Sidekick\is_testbench_cli(dusk: true) ? 'testbench-dusk' : 'testbench';
12✔
111

112
    $commander = is_file($vendorBinary = package_path('vendor', 'bin', $binary))
12✔
113
        ? $vendorBinary
×
114
        : $binary;
12✔
115

116
    return $remote->handle($commander, $command);
12✔
117
}
118

119
/**
120
 * Register after resolving callback.
121
 *
122
 * @api
123
 *
124
 * @template TLaravel of \Illuminate\Contracts\Foundation\Application
125
 *
126
 * @param  TLaravel  $app
127
 * @param  class-string|string  $name
128
 * @param  (\Closure(object, TLaravel):(mixed))|null  $callback
129
 * @return void
130
 */
131
function after_resolving(ApplicationContract $app, string $name, ?Closure $callback = null): void
132
{
133
    Sidekick\after_resolving($app, $name, $callback);
198✔
134
}
135

136
/**
137
 * Load migration paths.
138
 *
139
 * @api
140
 *
141
 * @param  \Illuminate\Contracts\Foundation\Application  $app
142
 * @param  array<int, string>|string  $paths
143
 * @return void
144
 */
145
function load_migration_paths(ApplicationContract $app, array|string $paths): void
146
{
147
    after_resolving($app, 'migrator', static function ($migrator) use ($paths) {
51✔
148
        foreach (Arr::wrap($paths) as $path) {
15✔
149
            /** @var \Illuminate\Database\Migrations\Migrator $migrator */
150
            $migrator->path($path);
15✔
151
        }
152
    });
51✔
153
}
154

155
/**
156
 * Get defined environment variables.
157
 *
158
 * @api
159
 *
160
 * @return array<string, mixed>
161
 */
162
function defined_environment_variables(): array
163
{
164
    return (new Collection(array_merge($_SERVER, $_ENV)))
12✔
165
        ->keys()
12✔
166
        ->mapWithKeys(static fn (string $key) => [$key => Sidekick\Env::forward($key)])
12✔
167
        ->unless(
12✔
168
            Sidekick\Env::has('TESTBENCH_WORKING_PATH'), static fn ($env) => $env->put('TESTBENCH_WORKING_PATH', package_path())
12✔
169
        )->all();
12✔
170
}
171

172
/**
173
 * Get default environment variables.
174
 *
175
 * @api
176
 *
177
 * @param  iterable<string, mixed>  $variables
178
 * @return array<int, string>
179
 */
180
function parse_environment_variables($variables): array
181
{
182
    return (new Collection($variables))
4✔
183
        ->transform(static function ($value, $key) {
4✔
184
            if (\is_bool($value) || \in_array($value, ['true', 'false'])) {
4✔
185
                $value = \in_array($value, [true, 'true']) ? '(true)' : '(false)';
4✔
186
            } elseif (\is_null($value) || \in_array($value, ['null'])) {
1✔
187
                $value = '(null)';
1✔
188
            } else {
189
                $value = $key === 'APP_DEBUG' ? \sprintf('(%s)', Str::of($value)->ltrim('(')->rtrim(')')) : "'{$value}'";
1✔
190
            }
191

192
            return "{$key}={$value}";
4✔
193
        })->values()->all();
4✔
194
}
195

196
/**
197
 * Refresh router lookups.
198
 *
199
 * @api
200
 *
201
 * @param  \Illuminate\Routing\Router  $router
202
 * @return void
203
 */
204
function refresh_router_lookups(Router $router): void
205
{
206
    $router->getRoutes()->refreshNameLookups();
198✔
207
}
208

209
/**
210
 * Transform realpath to alias path.
211
 *
212
 * @api
213
 *
214
 * @param  string  $path
215
 * @param  string|null  $workingPath
216
 * @return string
217
 */
218
function transform_realpath_to_relative(string $path, ?string $workingPath = null, string $prefix = ''): string
219
{
220
    $separator = DIRECTORY_SEPARATOR;
13✔
221

222
    if (! \is_null($workingPath)) {
13✔
223
        return str_replace(rtrim($workingPath, $separator).$separator, $prefix.$separator, $path);
1✔
224
    }
225

226
    $laravelPath = base_path();
12✔
227
    $workbenchPath = workbench_path();
12✔
228
    $packagePath = package_path();
12✔
229

230
    return match (true) {
231
        str_starts_with($path, $laravelPath) => str_replace($laravelPath.$separator, '@laravel'.$separator, $path),
12✔
232
        str_starts_with($path, $workbenchPath) => str_replace($workbenchPath.$separator, '@workbench'.$separator, $path),
8✔
233
        str_starts_with($path, $packagePath) => str_replace($packagePath.$separator, '.'.$separator, $path),
8✔
234
        ! empty($prefix) => implode($separator, [$prefix, ltrim($path, $separator)]),
8✔
235
        default => $path,
12✔
236
    };
237
}
238

239
/**
240
 * Get the default skeleton path.
241
 *
242
 * @api
243
 *
244
 * @no-named-arguments
245
 *
246
 * @param  array<int, string|null>|string  ...$path
247
 * @return ($path is '' ? string : string|false)
248
 */
249
function default_skeleton_path(array|string $path = ''): string|false
250
{
251
    return realpath(
195✔
252
        Sidekick\join_paths(__DIR__, '..', 'laravel', ...Arr::wrap(\func_num_args() > 1 ? \func_get_args() : $path))
195✔
253
    );
195✔
254
}
255

256
/**
257
 * Determine if application is bootstrapped using Testbench's default skeleton.
258
 *
259
 * @param  string|null  $basePath
260
 * @return bool
261
 */
262
function uses_default_skeleton(?string $basePath = null): bool
263
{
264
    $basePath ??= base_path();
198✔
265

266
    return realpath(Sidekick\join_paths($basePath, 'bootstrap', '.testbench-default-skeleton')) !== false;
198✔
267
}
268

269
/**
270
 * Get the migration path by type.
271
 *
272
 * @api
273
 *
274
 * @param  string|null  $type
275
 * @return string
276
 *
277
 * @throws \InvalidArgumentException
278
 */
279
function default_migration_path(?string $type = null): string
280
{
281
    $path = realpath(
59✔
282
        \is_null($type) ? base_path('migrations') : base_path(Sidekick\join_paths('migrations', $type))
59✔
283
    );
59✔
284

285
    if ($path === false) {
59✔
286
        throw new InvalidArgumentException(\sprintf('Unable to resolve migration path for type [%s]', $type ?? 'laravel'));
×
287
    }
288

289
    return $path;
59✔
290
}
291

292
/**
293
 * Get the path to the package folder.
294
 *
295
 * @api
296
 *
297
 * @no-named-arguments
298
 *
299
 * @param  array<int, string|null>|string  ...$path
300
 * @return string
301
 */
302
function package_path(array|string $path = ''): string
303
{
304
    $argumentCount = \func_num_args();
96✔
305

306
    $workingPath = \defined('TESTBENCH_WORKING_PATH')
96✔
307
        ? TESTBENCH_WORKING_PATH
56✔
308
        : Sidekick\Env::get('TESTBENCH_WORKING_PATH', getcwd());
40✔
309

310
    if ($argumentCount === 1 && \is_string($path) && str_starts_with($path, './')) {
96✔
311
        return Sidekick\transform_relative_path($path, $workingPath);
7✔
312
    }
313

314
    $path = Sidekick\join_paths(...Arr::wrap($argumentCount > 1 ? \func_get_args() : $path));
96✔
315

316
    return str_starts_with($path, './')
96✔
317
        ? Sidekick\transform_relative_path($path, $workingPath)
×
318
        : Sidekick\join_paths(rtrim($workingPath, DIRECTORY_SEPARATOR), $path);
96✔
319
}
320

321
/**
322
 * Get the workbench configuration.
323
 *
324
 * @api
325
 *
326
 * @return array<string, mixed>
327
 */
328
function workbench(): array
329
{
330
    /** @var \Orchestra\Testbench\Contracts\Config $config */
331
    $config = app()->bound(Contracts\Config::class)
48✔
332
        ? app()->make(Contracts\Config::class)
46✔
333
        : new Foundation\Config;
2✔
334

335
    return $config->getWorkbenchAttributes();
48✔
336
}
337

338
/**
339
 * Get the path to the workbench folder.
340
 *
341
 * @api
342
 *
343
 * @no-named-arguments
344
 *
345
 * @param  array<int, string|null>|string  ...$path
346
 * @return string
347
 */
348
function workbench_path(array|string $path = ''): string
349
{
350
    return package_path('workbench', ...Arr::wrap(\func_num_args() > 1 ? \func_get_args() : $path));
77✔
351
}
352

353
/**
354
 * Get the migration path by type.
355
 *
356
 * @api
357
 *
358
 * @param  string|null  $type
359
 * @return string
360
 *
361
 * @throws \InvalidArgumentException
362
 *
363
 * @deprecated
364
 *
365
 * @codeCoverageIgnore
366
 */
367
#[\Deprecated(message: 'Use `Orchestra\Testbench\default_migration_path()` instead', since: '9.5.1')]
368
function laravel_migration_path(?string $type = null): string
369
{
370
    return default_migration_path($type);
371
}
372

373
/**
374
 * Determine if vendor symlink exists on the laravel application.
375
 *
376
 * @api
377
 *
378
 * @param  \Illuminate\Contracts\Foundation\Application  $app
379
 * @param  string|null  $workingPath
380
 * @return bool
381
 */
382
function laravel_vendor_exists(ApplicationContract $app, ?string $workingPath = null): bool
383
{
384
    $filesystem = new Filesystem;
12✔
385

386
    $appVendorPath = $app->basePath('vendor');
12✔
387
    $workingPath ??= package_path('vendor');
12✔
388

389
    return $filesystem->isFile(Sidekick\join_paths($appVendorPath, 'autoload.php')) &&
12✔
390
        $filesystem->hash(Sidekick\join_paths($appVendorPath, 'autoload.php')) === $filesystem->hash(Sidekick\join_paths($workingPath, 'autoload.php'));
12✔
391
}
392

393
/**
394
 * Laravel version compare.
395
 *
396
 * @api
397
 *
398
 * @template TOperator of string|null
399
 *
400
 * @param  string  $version
401
 * @param  string|null  $operator
402
 *
403
 * @phpstan-param  TOperator  $operator
404
 *
405
 * @return int|bool
406
 *
407
 * @phpstan-return (TOperator is null ? int : bool)
408
 *
409
 * @codeCoverageIgnore
410
 */
411
function laravel_version_compare(string $version, ?string $operator = null): int|bool
412
{
413
    return Sidekick\laravel_version_compare($version, $operator);
414
}
415

416
/**
417
 * Package version compare.
418
 *
419
 * @api
420
 *
421
 * @template TOperator of string|null
422
 *
423
 * @param  string  $package
424
 * @param  string  $version
425
 * @param  string|null  $operator
426
 *
427
 * @phpstan-param  TOperator  $operator
428
 *
429
 * @return int|bool
430
 *
431
 * @phpstan-return (TOperator is null ? int : bool)
432
 *
433
 * @throws \OutOfBoundsException
434
 * @throws \RuntimeException
435
 *
436
 * @codeCoverageIgnore
437
 */
438
function package_version_compare(string $package, string $version, ?string $operator = null)
439
{
440
    return Sidekick\package_version_compare($package, $version, $operator);
441
}
442

443
/**
444
 * PHPUnit version compare.
445
 *
446
 * @api
447
 *
448
 * @template TOperator of string|null
449
 *
450
 * @param  string  $version
451
 * @param  string|null  $operator
452
 *
453
 * @phpstan-param  TOperator  $operator
454
 *
455
 * @return int|bool
456
 *
457
 * @phpstan-return (TOperator is null ? int : bool)
458
 *
459
 * @throws \OutOfBoundsException
460
 * @throws \RuntimeException
461
 *
462
 * @codeCoverageIgnore
463
 */
464
function phpunit_version_compare(string $version, ?string $operator = null): int|bool
465
{
466
    return Sidekick\phpunit_version_compare($version, $operator);
467
}
468

469
/**
470
 * Determine the PHP Binary.
471
 *
472
 * @api
473
 *
474
 * @param  bool  $escape
475
 * @return string
476
 */
477
function php_binary(bool $escape = false): string
478
{
479
    $phpBinary = Sidekick\php_binary();
12✔
480

481
    return $escape === true ? ProcessUtils::escapeArgument((string) $phpBinary) : $phpBinary;
12✔
482
}
483

484
/**
485
 * Join the given paths together.
486
 *
487
 * @param  string|null  $basePath
488
 * @param  string  ...$paths
489
 * @return string
490
 *
491
 * @codeCoverageIgnore
492
 */
493
function join_paths(?string $basePath, string ...$paths): string
494
{
495
    return Sidekick\join_paths($basePath, ...$paths);
496
}
497

498
/**
499
 * Ensure the provided `$app` return an instance of Laravel application or throw an exception.
500
 *
501
 * @internal
502
 *
503
 * @param  \Illuminate\Foundation\Application|null  $app
504
 * @param  string|null  $caller
505
 * @return \Illuminate\Foundation\Application
506
 *
507
 * @throws \Orchestra\Testbench\Exceptions\ApplicationNotAvailableException
508
 */
509
function laravel_or_fail($app, ?string $caller = null): Application
510
{
511
    if ($app instanceof Application) {
192✔
512
        return $app;
192✔
513
    }
514

515
    if (\is_null($caller)) {
1✔
516
        $caller = transform(debug_backtrace()[1] ?? null, function ($debug) {
1✔
517
            /** @phpstan-ignore isset.offset */
518
            if (isset($debug['class']) && isset($debug['function'])) {
1✔
519
                return \sprintf('%s::%s', $debug['class'], $debug['function']);
1✔
520
            }
521

522
            /** @phpstan-ignore offsetAccess.notFound */
523
            return $debug['function'];
×
524
        });
1✔
525
    }
526

527
    throw Exceptions\ApplicationNotAvailableException::make($caller);
1✔
528
}
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