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

orchestral / testbench-core / 19793339660

30 Nov 2025 03:42AM UTC coverage: 92.568% (-0.06%) from 92.629%
19793339660

push

github

web-flow
[7.x] Add `Orchestra\Testbench\package_version_compare()` function (#376)

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

0 of 1 new or added line in 1 file covered. (0.0%)

1420 of 1534 relevant lines covered (92.57%)

66.11 hits per line

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

93.55
/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

19
/**
20
 * Create Laravel application instance.
21
 *
22
 * @api
23
 *
24
 * @param  string|null  $basePath
25
 * @param  (callable(\Illuminate\Foundation\Application):(void))|null  $resolvingCallback
26
 * @param  array{extra?: array{providers?: array, dont-discover?: array, env?: array}, load_environment_variables?: bool, enabled_package_discoveries?: bool}  $options
27
 * @return \Orchestra\Testbench\Foundation\Application
28
 */
29
function container(?string $basePath = null, ?callable $resolvingCallback = null, array $options = []): Foundation\Application
30
{
31
    return Foundation\Application::make($basePath, $resolvingCallback, $options);
3✔
32
}
33

34
/**
35
 * Run artisan command.
36
 *
37
 * @api
38
 *
39
 * @param  \Orchestra\Testbench\Contracts\TestCase|\Illuminate\Contracts\Foundation\Application  $context
40
 * @param  string  $command
41
 * @param  array<string, mixed>  $parameters
42
 * @return int
43
 */
44
function artisan(Contracts\TestCase|ApplicationContract $context, string $command, array $parameters = []): int
45
{
46
    if ($context instanceof ApplicationContract) {
10✔
47
        return $context->make(ConsoleKernel::class)->call($command, $parameters);
1✔
48
    }
49

50
    $command = $context->artisan($command, $parameters);
10✔
51

52
    return $command instanceof PendingCommand ? $command->run() : $command;
10✔
53
}
54

55
/**
56
 * Run remote action using Testbench CLI.
57
 *
58
 * @api
59
 *
60
 * @param  array<int, string>|string  $command
61
 * @param  array<string, mixed>|string  $env
62
 * @param  bool|null  $tty
63
 * @return \Orchestra\Testbench\Foundation\Process\ProcessDecorator
64
 */
65
function remote(array|string $command, array|string $env = [], ?bool $tty = null): Foundation\Process\ProcessDecorator
66
{
67
    $remote = new Foundation\Process\RemoteCommand(
11✔
68
        package_path(), $env, $tty
11✔
69
    );
11✔
70

71
    $binary = Sidekick\is_testbench_cli(dusk: true) ? 'testbench-dusk' : 'testbench';
11✔
72

73
    $commander = is_file($vendorBinary = package_path('vendor', 'bin', $binary))
11✔
74
        ? $vendorBinary
×
75
        : $binary;
11✔
76

77
    return $remote->handle($commander, $command);
11✔
78
}
79

80
/**
81
 * Run callback only once.
82
 *
83
 * @api
84
 *
85
 * @param  mixed  $callback
86
 * @return \Closure():mixed
87
 *
88
 * @deprecated 7.55.0 Use `Orchestra\Sidekick\once()` instead.
89
 *
90
 * @codeCoverageIgnore
91
 */
92
function once($callback): Closure
93
{
94
    return Sidekick\once($callback);
95
}
96

97
/**
98
 * Register after resolving callback.
99
 *
100
 * @api
101
 *
102
 * @template TLaravel of \Illuminate\Contracts\Foundation\Application
103
 *
104
 * @param  TLaravel  $app
105
 * @param  class-string|string  $name
106
 * @param  (\Closure(object, TLaravel):(mixed))|null  $callback
107
 * @return void
108
 */
109
function after_resolving(ApplicationContract $app, string $name, ?Closure $callback = null): void
110
{
111
    $app->afterResolving($name, $callback);
173✔
112

113
    if ($app->resolved($name)) {
173✔
114
        value($callback, $app->make($name), $app);
5✔
115
    }
116
}
117

118
/**
119
 * Load migration paths.
120
 *
121
 * @api
122
 *
123
 * @param  \Illuminate\Contracts\Foundation\Application  $app
124
 * @param  array<int, string>|string  $paths
125
 * @return void
126
 */
127
function load_migration_paths(ApplicationContract $app, array|string $paths): void
128
{
129
    after_resolving($app, 'migrator', static function ($migrator) use ($paths) {
38✔
130
        foreach (Arr::wrap($paths) as $path) {
15✔
131
            /** @var \Illuminate\Database\Migrations\Migrator $migrator */
132
            $migrator->path($path);
15✔
133
        }
134
    });
38✔
135
}
136

137
/**
138
 * Get default environment variables.
139
 *
140
 * @return array<int, string>
141
 *
142
 * @deprecated
143
 *
144
 * @codeCoverageIgnore
145
 */
146
function default_environment_variables(): array
147
{
148
    return [];
149
}
150

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

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

188
            return "{$key}={$value}";
4✔
189
        })->values()->all();
4✔
190
}
191

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

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

218
    if (! \is_null($workingPath)) {
13✔
219
        return str_replace(rtrim($workingPath, $separator).$separator, $prefix.$separator, $path);
1✔
220
    }
221

222
    $laravelPath = base_path();
12✔
223
    $workbenchPath = workbench_path();
12✔
224
    $packagePath = package_path();
12✔
225

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

235
/**
236
 * Transform relative path.
237
 *
238
 * @api
239
 *
240
 * @param  string  $path
241
 * @param  string  $workingPath
242
 * @return string
243
 *
244
 * @deprecated 7.55.0 Use `Orchestra\Sidekick\transform_relative_path()` instead.
245
 *
246
 * @codeCoverageIgnore
247
 */
248
function transform_relative_path(string $path, string $workingPath): string
249
{
250
    return Sidekick\transform_relative_path($path, $workingPath);
251
}
252

253
/**
254
 * Get the default skeleton path.
255
 *
256
 * @api
257
 *
258
 * @no-named-arguments
259
 *
260
 * @param  array<int, string|null>|string  ...$path
261
 * @return string
262
 */
263
function default_skeleton_path(array|string $path = ''): string
264
{
265
    return (string) realpath(
170✔
266
        Sidekick\join_paths(__DIR__, '..', 'laravel', ...Arr::wrap(\func_num_args() > 1 ? \func_get_args() : $path))
170✔
267
    );
170✔
268
}
269

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

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

290
    return $path;
44✔
291
}
292

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

307
    $workingPath = \defined('TESTBENCH_WORKING_PATH')
69✔
308
        ? TESTBENCH_WORKING_PATH
34✔
309
        : Sidekick\Env::get('TESTBENCH_WORKING_PATH', getcwd());
36✔
310

311
    if ($argumentCount === 1 && \is_string($path) && str_starts_with($path, './')) {
69✔
312
        return transform_relative_path($path, $workingPath);
×
313
    }
314

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

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

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

336
    return $config->getWorkbenchAttributes();
35✔
337
}
338

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

354
/**
355
 * Get the migration path by type.
356
 *
357
 * @api
358
 *
359
 * @param  string|null  $type
360
 * @return string
361
 *
362
 * @throws \InvalidArgumentException
363
 *
364
 * @deprecated
365
 */
366
function laravel_migration_path(?string $type = null): string
367
{
368
    return default_migration_path($type);
1✔
369
}
370

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

384
    $appVendorPath = $app->basePath('vendor');
9✔
385
    $workingPath ??= package_path('vendor');
9✔
386

387
    return $filesystem->isFile(join_paths($appVendorPath, 'autoload.php')) &&
9✔
388
        $filesystem->hash(join_paths($appVendorPath, 'autoload.php')) === $filesystem->hash(join_paths($workingPath, 'autoload.php'));
9✔
389
}
390

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

412
/**
413
 * Package version compare.
414
 *
415
 * @api
416
 *
417
 * @template TOperator of string|null
418
 *
419
 * @param  string  $version
420
 * @param  string|null  $operator
421
 *
422
 * @phpstan-param  TOperator  $operator
423
 *
424
 * @return int|bool
425
 *
426
 * @phpstan-return (TOperator is null ? int : bool)
427
 *
428
 * @throws \OutOfBoundsException
429
 * @throws \RuntimeException
430
 */
431
function package_version_compare(string $version, ?string $operator = null)
432
{
NEW
433
    return Sidekick\package_version_compare($version, $operator);
×
434
}
435

436
/**
437
 * PHPUnit version compare.
438
 *
439
 * @api
440
 *
441
 * @template TOperator of string|null
442
 *
443
 * @param  string  $version
444
 * @param  string|null  $operator
445
 *
446
 * @phpstan-param  TOperator  $operator
447
 *
448
 * @return int|bool
449
 *
450
 * @phpstan-return (TOperator is null ? int : bool)
451
 *
452
 * @throws \OutOfBoundsException
453
 * @throws \RuntimeException
454
 */
455
function phpunit_version_compare(string $version, ?string $operator = null)
456
{
457
    return Sidekick\phpunit_version_compare($version, $operator);
1✔
458
}
459

460
/**
461
 * Determine the PHP Binary.
462
 *
463
 * @api
464
 *
465
 * @param  bool  $escape
466
 * @return string
467
 */
468
function php_binary(bool $escape = false): string
469
{
470
    $phpBinary = Sidekick\php_binary();
11✔
471

472
    return $escape === true ? ProcessUtils::escapeArgument((string) $phpBinary) : $phpBinary;
11✔
473
}
474

475
/**
476
 * Join the given paths together.
477
 *
478
 * @param  string|null  $basePath
479
 * @param  string  ...$paths
480
 * @return string
481
 *
482
 * @codeCoverageIgnore
483
 */
484
function join_paths(?string $basePath, string ...$paths): string
485
{
486
    return Sidekick\join_paths($basePath, ...$paths);
487
}
488

489
/**
490
 * Ensure the provided `$app` return an instance of Laravel application or throw an exception.
491
 *
492
 * @internal
493
 *
494
 * @param  \Illuminate\Foundation\Application|null  $app
495
 * @param  string|null  $caller
496
 * @return \Illuminate\Foundation\Application
497
 *
498
 * @throws \Orchestra\Testbench\Exceptions\ApplicationNotAvailableException
499
 */
500
function laravel_or_fail($app, ?string $caller = null): Application
501
{
502
    if ($app instanceof Application) {
167✔
503
        return $app;
167✔
504
    }
505

506
    if (\is_null($caller)) {
1✔
507
        $caller = transform(debug_backtrace()[1] ?? null, function ($debug) {
1✔
508
            if (isset($debug['class']) && isset($debug['function'])) {
1✔
509
                return \sprintf('%s::%s', $debug['class'], $debug['function']);
1✔
510
            }
511

512
            return $debug['function'];
×
513
        });
1✔
514
    }
515

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