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

orchestral / testbench-core / 20941274095

13 Jan 2026 01:31AM UTC coverage: 91.924% (-0.01%) from 91.934%
20941274095

push

github

crynobone
Release 9.18.0

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

1548 of 1684 relevant lines covered (91.92%)

77.47 hits per line

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

90.43
/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
 * Run callback only once.
121
 *
122
 * @api
123
 *
124
 * @param  mixed  $callback
125
 * @return \Closure():mixed
126
 *
127
 * @deprecated 7.55.0 Use `Orchestra\Sidekick\once()` instead.
128
 *
129
 * @codeCoverageIgnore
130
 */
131
#[\Deprecated(message: 'Use `Orchestra\Sidekick\once()` instead', since: '7.55.0')]
132
function once($callback): Closure
133
{
134
    return Sidekick\once($callback);
135
}
136

137
/**
138
 * Register after resolving callback.
139
 *
140
 * @api
141
 *
142
 * @template TLaravel of \Illuminate\Contracts\Foundation\Application
143
 *
144
 * @param  TLaravel  $app
145
 * @param  class-string|string  $name
146
 * @param  (\Closure(object, TLaravel):(mixed))|null  $callback
147
 * @return void
148
 */
149
function after_resolving(ApplicationContract $app, string $name, ?Closure $callback = null): void
150
{
151
    Sidekick\after_resolving($app, $name, $callback);
195✔
152
}
153

154
/**
155
 * Load migration paths.
156
 *
157
 * @api
158
 *
159
 * @param  \Illuminate\Contracts\Foundation\Application  $app
160
 * @param  array<int, string>|string  $paths
161
 * @return void
162
 */
163
function load_migration_paths(ApplicationContract $app, array|string $paths): void
164
{
165
    after_resolving($app, 'migrator', static function ($migrator) use ($paths) {
51✔
166
        foreach (Arr::wrap($paths) as $path) {
15✔
167
            /** @var \Illuminate\Database\Migrations\Migrator $migrator */
168
            $migrator->path($path);
15✔
169
        }
170
    });
51✔
171
}
172

173
/**
174
 * Get defined environment variables.
175
 *
176
 * @api
177
 *
178
 * @return array<string, mixed>
179
 */
180
function defined_environment_variables(): array
181
{
182
    return (new Collection(array_merge($_SERVER, $_ENV)))
12✔
183
        ->keys()
12✔
184
        ->mapWithKeys(static fn (string $key) => [$key => Sidekick\Env::forward($key)])
12✔
185
        ->unless(
12✔
186
            Sidekick\Env::has('TESTBENCH_WORKING_PATH'), static fn ($env) => $env->put('TESTBENCH_WORKING_PATH', package_path())
12✔
187
        )->all();
12✔
188
}
189

190
/**
191
 * Get default environment variables.
192
 *
193
 * @api
194
 *
195
 * @param  iterable<string, mixed>  $variables
196
 * @return array<int, string>
197
 */
198
function parse_environment_variables($variables): array
199
{
200
    return (new Collection($variables))
4✔
201
        ->transform(static function ($value, $key) {
4✔
202
            if (\is_bool($value) || \in_array($value, ['true', 'false'])) {
4✔
203
                $value = \in_array($value, [true, 'true']) ? '(true)' : '(false)';
4✔
204
            } elseif (\is_null($value) || \in_array($value, ['null'])) {
1✔
205
                $value = '(null)';
1✔
206
            } else {
207
                $value = $key === 'APP_DEBUG' ? \sprintf('(%s)', Str::of($value)->ltrim('(')->rtrim(')')) : "'{$value}'";
1✔
208
            }
209

210
            return "{$key}={$value}";
4✔
211
        })->values()->all();
4✔
212
}
213

214
/**
215
 * Refresh router lookups.
216
 *
217
 * @api
218
 *
219
 * @param  \Illuminate\Routing\Router  $router
220
 * @return void
221
 */
222
function refresh_router_lookups(Router $router): void
223
{
224
    $router->getRoutes()->refreshNameLookups();
195✔
225
}
226

227
/**
228
 * Transform realpath to alias path.
229
 *
230
 * @api
231
 *
232
 * @param  string  $path
233
 * @param  string|null  $workingPath
234
 * @return string
235
 */
236
function transform_realpath_to_relative(string $path, ?string $workingPath = null, string $prefix = ''): string
237
{
238
    $separator = DIRECTORY_SEPARATOR;
13✔
239

240
    if (! \is_null($workingPath)) {
13✔
241
        return str_replace(rtrim($workingPath, $separator).$separator, $prefix.$separator, $path);
1✔
242
    }
243

244
    $laravelPath = base_path();
12✔
245
    $workbenchPath = workbench_path();
12✔
246
    $packagePath = package_path();
12✔
247

248
    return match (true) {
249
        str_starts_with($path, $laravelPath) => str_replace($laravelPath.$separator, '@laravel'.$separator, $path),
12✔
250
        str_starts_with($path, $workbenchPath) => str_replace($workbenchPath.$separator, '@workbench'.$separator, $path),
8✔
251
        str_starts_with($path, $packagePath) => str_replace($packagePath.$separator, '.'.$separator, $path),
8✔
252
        ! empty($prefix) => implode($separator, [$prefix, ltrim($path, $separator)]),
8✔
253
        default => $path,
12✔
254
    };
255
}
256

257
/**
258
 * Transform relative path.
259
 *
260
 * @api
261
 *
262
 * @param  string  $path
263
 * @param  string  $workingPath
264
 * @return string
265
 *
266
 * @deprecated 7.55.0 Use `Orchestra\Sidekick\transform_relative_path()` instead.
267
 *
268
 * @codeCoverageIgnore
269
 */
270
#[\Deprecated(message: 'Use `Orchestra\Sidekick\transform_relative_path()` instead', since: '7.55.0')]
271
function transform_relative_path(string $path, string $workingPath): string
272
{
273
    return Sidekick\transform_relative_path($path, $workingPath);
274
}
275

276
/**
277
 * Get the default skeleton path.
278
 *
279
 * @api
280
 *
281
 * @no-named-arguments
282
 *
283
 * @param  array<int, string|null>|string  ...$path
284
 * @return ($path is '' ? string : string|false)
285
 */
286
function default_skeleton_path(array|string $path = ''): string|false
287
{
288
    return realpath(
192✔
289
        Sidekick\join_paths(__DIR__, '..', 'laravel', ...Arr::wrap(\func_num_args() > 1 ? \func_get_args() : $path))
192✔
290
    );
192✔
291
}
292

293
/**
294
 * Determine if application is bootstrapped using Testbench's default skeleton.
295
 *
296
 * @param  string|null  $basePath
297
 * @return bool
298
 */
299
function uses_default_skeleton(?string $basePath = null): bool
300
{
301
    $basePath ??= base_path();
195✔
302

303
    return realpath(Sidekick\join_paths($basePath, 'bootstrap', '.testbench-default-skeleton')) !== false;
195✔
304
}
305

306
/**
307
 * Get the migration path by type.
308
 *
309
 * @api
310
 *
311
 * @param  string|null  $type
312
 * @return string
313
 *
314
 * @throws \InvalidArgumentException
315
 */
316
function default_migration_path(?string $type = null): string
317
{
318
    $path = realpath(
58✔
319
        \is_null($type) ? base_path('migrations') : base_path(Sidekick\Filesystem\join_paths('migrations', $type))
58✔
320
    );
58✔
321

322
    if ($path === false) {
58✔
323
        throw new InvalidArgumentException(\sprintf('Unable to resolve migration path for type [%s]', $type ?? 'laravel'));
×
324
    }
325

326
    return $path;
58✔
327
}
328

329
/**
330
 * Get the path to the package folder.
331
 *
332
 * @api
333
 *
334
 * @no-named-arguments
335
 *
336
 * @param  array<int, string|null>|string  ...$path
337
 * @return string
338
 */
339
function package_path(array|string $path = ''): string
340
{
341
    $argumentCount = \func_num_args();
94✔
342

343
    $workingPath = Sidekick\package_path();
94✔
344

345
    if ($argumentCount === 1 && \is_string($path) && str_starts_with($path, './')) {
94✔
346
        return Sidekick\transform_relative_path($path, $workingPath);
7✔
347
    }
348

349
    $path = Sidekick\Filesystem\join_paths(...Arr::wrap($argumentCount > 1 ? \func_get_args() : $path));
94✔
350

351
    return str_starts_with($path, './')
94✔
352
        ? Sidekick\transform_relative_path($path, $workingPath)
×
353
        : Sidekick\Filesystem\join_paths(rtrim($workingPath, DIRECTORY_SEPARATOR), $path);
94✔
354
}
355

356
/**
357
 * Get the workbench configuration.
358
 *
359
 * @api
360
 *
361
 * @return array<string, mixed>
362
 */
363
function workbench(): array
364
{
365
    /** @var \Orchestra\Testbench\Contracts\Config $config */
366
    $config = app()->bound(Contracts\Config::class)
48✔
367
        ? app()->make(Contracts\Config::class)
46✔
368
        : new Foundation\Config;
2✔
369

370
    return $config->getWorkbenchAttributes();
48✔
371
}
372

373
/**
374
 * Get the path to the workbench folder.
375
 *
376
 * @api
377
 *
378
 * @no-named-arguments
379
 *
380
 * @param  array<int, string|null>|string  ...$path
381
 * @return string
382
 */
383
function workbench_path(array|string $path = ''): string
384
{
385
    return package_path('workbench', ...Arr::wrap(\func_num_args() > 1 ? \func_get_args() : $path));
75✔
386
}
387

388
/**
389
 * Get the migration path by type.
390
 *
391
 * @api
392
 *
393
 * @param  string|null  $type
394
 * @return string
395
 *
396
 * @throws \InvalidArgumentException
397
 *
398
 * @deprecated
399
 *
400
 * @codeCoverageIgnore
401
 */
402
#[\Deprecated(message: 'Use `Orchestra\Testbench\default_migration_path()` instead', since: '9.5.1')]
403
function laravel_migration_path(?string $type = null): string
404
{
405
    return default_migration_path($type);
406
}
407

408
/**
409
 * Determine if vendor symlink exists on the laravel application.
410
 *
411
 * @api
412
 *
413
 * @param  \Illuminate\Contracts\Foundation\Application  $app
414
 * @param  string|null  $workingPath
415
 * @return bool
416
 */
417
function laravel_vendor_exists(ApplicationContract $app, ?string $workingPath = null): bool
418
{
419
    $filesystem = new Filesystem;
12✔
420

421
    $appVendorPath = $app->basePath('vendor');
12✔
422
    $workingPath ??= package_path('vendor');
12✔
423

424
    return $filesystem->isFile(join_paths($appVendorPath, 'autoload.php')) &&
12✔
425
        $filesystem->hash(join_paths($appVendorPath, 'autoload.php')) === $filesystem->hash(join_paths($workingPath, 'autoload.php'));
12✔
426
}
427

428
/**
429
 * Laravel version compare.
430
 *
431
 * @api
432
 *
433
 * @template TOperator of string|null
434
 *
435
 * @param  string  $version
436
 * @param  string|null  $operator
437
 *
438
 * @phpstan-param  TOperator  $operator
439
 *
440
 * @return int|bool
441
 *
442
 * @phpstan-return (TOperator is null ? int : bool)
443
 */
444
function laravel_version_compare(string $version, ?string $operator = null): int|bool
445
{
446
    return Sidekick\laravel_version_compare($version, $operator);
1✔
447
}
448

449
/**
450
 * Package version compare.
451
 *
452
 * @api
453
 *
454
 * @template TOperator of string|null
455
 *
456
 * @param  string  $package
457
 * @param  string  $version
458
 * @param  string|null  $operator
459
 *
460
 * @phpstan-param  TOperator  $operator
461
 *
462
 * @return int|bool
463
 *
464
 * @phpstan-return (TOperator is null ? int : bool)
465
 *
466
 * @throws \OutOfBoundsException
467
 * @throws \RuntimeException
468
 */
469
function package_version_compare(string $package, string $version, ?string $operator = null)
470
{
471
    return Sidekick\package_version_compare($package, $version, $operator);
1✔
472
}
473

474
/**
475
 * PHPUnit version compare.
476
 *
477
 * @api
478
 *
479
 * @template TOperator of string|null
480
 *
481
 * @param  string  $version
482
 * @param  string|null  $operator
483
 *
484
 * @phpstan-param  TOperator  $operator
485
 *
486
 * @return int|bool
487
 *
488
 * @phpstan-return (TOperator is null ? int : bool)
489
 *
490
 * @throws \OutOfBoundsException
491
 * @throws \RuntimeException
492
 */
493
function phpunit_version_compare(string $version, ?string $operator = null): int|bool
494
{
495
    return Sidekick\phpunit_version_compare($version, $operator);
2✔
496
}
497

498
/**
499
 * Determine the PHP Binary.
500
 *
501
 * @api
502
 *
503
 * @param  bool  $escape
504
 * @return string
505
 */
506
function php_binary(bool $escape = false): string
507
{
508
    $phpBinary = Sidekick\php_binary();
12✔
509

510
    return $escape === true ? ProcessUtils::escapeArgument((string) $phpBinary) : $phpBinary;
12✔
511
}
512

513
/**
514
 * Join the given paths together.
515
 *
516
 * @param  string|null  $basePath
517
 * @param  string  ...$paths
518
 * @return string
519
 *
520
 * @codeCoverageIgnore
521
 */
522
function join_paths(?string $basePath, string ...$paths): string
523
{
524
    return Sidekick\Filesystem\join_paths($basePath, ...$paths);
525
}
526

527
/**
528
 * Ensure the provided `$app` return an instance of Laravel application or throw an exception.
529
 *
530
 * @internal
531
 *
532
 * @param  \Illuminate\Foundation\Application|null  $app
533
 * @param  string|null  $caller
534
 * @return \Illuminate\Foundation\Application
535
 *
536
 * @throws \Orchestra\Testbench\Exceptions\ApplicationNotAvailableException
537
 */
538
function laravel_or_fail($app, ?string $caller = null): Application
539
{
540
    if ($app instanceof Application) {
189✔
541
        return $app;
189✔
542
    }
543

544
    if (\is_null($caller)) {
1✔
545
        $caller = transform(debug_backtrace()[1] ?? null, function ($debug) {
1✔
546
            /** @phpstan-ignore isset.offset */
547
            if (isset($debug['class']) && isset($debug['function'])) {
1✔
548
                return \sprintf('%s::%s', $debug['class'], $debug['function']);
1✔
549
            }
550

551
            /** @phpstan-ignore offsetAccess.notFound */
552
            return $debug['function'];
×
553
        });
1✔
554
    }
555

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