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

orchestral / testbench-core / 11985075971

23 Nov 2024 06:56AM UTC coverage: 91.991%. Remained the same
11985075971

push

github

crynobone
Merge remote-tracking branch 'origin/7.x' into 7.x

56 of 65 new or added lines in 23 files covered. (86.15%)

31 existing lines in 9 files now uncovered.

1206 of 1311 relevant lines covered (91.99%)

61.99 hits per line

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

92.71
/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\Testbench\Foundation\Env;
18
use PHPUnit\Runner\Version;
19
use RuntimeException;
20
use Symfony\Component\Process\Process;
21

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

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

53
    $command = $context->artisan($command, $parameters);
10✔
54

55
    return $command instanceof PendingCommand ? $command->run() : $command;
10✔
56
}
57

58
/**
59
 * Run remote action using Testbench CLI.
60
 *
61
 * @api
62
 *
63
 * @param  array<int, string>|string  $command
64
 * @param  array<string, mixed>|string  $env
65
 * @param  bool|null  $tty
66
 * @return \Symfony\Component\Process\Process
67
 */
68
function remote(array|string $command, array|string $env = [], ?bool $tty = null): Process
69
{
70
    $binary = \defined('TESTBENCH_DUSK') ? 'testbench-dusk' : 'testbench';
10✔
71

72
    $commander = is_file($vendorBin = package_path('vendor', 'bin', $binary))
10✔
UNCOV
73
        ? ProcessUtils::escapeArgument((string) $vendorBin)
×
74
        : $binary;
10✔
75

76
    if (\is_string($env)) {
10✔
UNCOV
77
        $env = ['APP_ENV' => $env];
×
78
    }
79

80
    Arr::add($env, 'TESTBENCH_PACKAGE_REMOTE', '(true)');
10✔
81

82
    $process = Process::fromShellCommandline(
10✔
83
        command: Arr::join([php_binary(true), $commander, ...Arr::wrap($command)], ' '),
10✔
84
        cwd: package_path(),
10✔
85
        env: array_merge(defined_environment_variables(), $env)
10✔
86
    );
10✔
87

88
    if (\is_bool($tty)) {
10✔
NEW
89
        $process->setTty($tty);
×
90
    }
91

92
    return $process;
10✔
93
}
94

95
/**
96
 * Run callback only once.
97
 *
98
 * @api
99
 *
100
 * @param  mixed  $callback
101
 * @return \Closure():mixed
102
 */
103
function once($callback): Closure
104
{
105
    $response = new Foundation\UndefinedValue;
157✔
106

107
    return function () use ($callback, &$response) {
157✔
108
        if ($response instanceof Foundation\UndefinedValue) {
157✔
109
            $response = value($callback) ?? null;
157✔
110
        }
111

112
        return $response;
157✔
113
    };
157✔
114
}
115

116
/**
117
 * Register after resolving callback.
118
 *
119
 * @api
120
 *
121
 * @param  \Illuminate\Contracts\Foundation\Application  $app
122
 * @param  string  $name
123
 * @param  (\Closure(object, \Illuminate\Contracts\Foundation\Application):(mixed))|null  $callback
124
 * @return void
125
 */
126
function after_resolving(ApplicationContract $app, string $name, ?Closure $callback = null): void
127
{
128
    $app->afterResolving($name, $callback);
157✔
129

130
    if ($app->resolved($name)) {
157✔
131
        value($callback, $app->make($name), $app);
5✔
132
    }
133
}
134

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

154
/**
155
 * Get default environment variables.
156
 *
157
 * @return array<int, string>
158
 *
159
 * @deprecated
160
 *
161
 * @codeCoverageIgnore
162
 */
163
function default_environment_variables(): array
164
{
165
    return [];
166
}
167

168
/**
169
 * Get defined environment variables.
170
 *
171
 * @api
172
 *
173
 * @return array<string, mixed>
174
 */
175
function defined_environment_variables(): array
176
{
177
    return Collection::make(array_merge($_SERVER, $_ENV))
10✔
178
        ->keys()
10✔
179
        ->mapWithKeys(static fn (string $key) => [$key => Env::forward($key)])
10✔
180
        ->unless(
10✔
181
            Env::has('TESTBENCH_WORKING_PATH'), static fn ($env) => $env->put('TESTBENCH_WORKING_PATH', package_path())
10✔
182
        )->all();
10✔
183
}
184

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

205
            return "{$key}={$value}";
4✔
206
        })->values()->all();
4✔
207
}
208

209
/**
210
 * Refresh router lookups.
211
 *
212
 * @api
213
 *
214
 * @param  \Illuminate\Routing\Router  $router
215
 * @return void
216
 */
217
function refresh_router_lookups(Router $router): void
218
{
219
    $router->getRoutes()->refreshNameLookups();
157✔
220
}
221

222
/**
223
 * Transform relative path.
224
 *
225
 * @api
226
 *
227
 * @param  string  $path
228
 * @param  string  $workingPath
229
 * @return string
230
 */
231
function transform_relative_path(string $path, string $workingPath): string
232
{
233
    return str_starts_with($path, './')
24✔
234
        ? rtrim($workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.mb_substr($path, 2)
1✔
235
        : $path;
24✔
236
}
237

238
/**
239
 * Get the default skeleton path.
240
 *
241
 * @api
242
 *
243
 * @param  array|string  $path
244
 * @return string
245
 */
246
function default_skeleton_path(array|string $path = ''): string
247
{
248
    return (string) realpath(join_paths(__DIR__, '..', 'laravel', ...Arr::wrap(\func_num_args() > 1 ? \func_get_args() : $path)));
154✔
249
}
250

251
/**
252
 * Get the migration path by type.
253
 *
254
 * @api
255
 *
256
 * @param  string|null  $type
257
 * @return string
258
 *
259
 * @throws \InvalidArgumentException
260
 */
261
function default_migration_path(?string $type = null): string
262
{
263
    $path = realpath(
34✔
264
        \is_null($type) ? base_path('migrations') : base_path(join_paths('migrations', $type))
34✔
265
    );
34✔
266

267
    if ($path === false) {
34✔
UNCOV
268
        throw new InvalidArgumentException(\sprintf('Unable to resolve migration path for type [%s]', $type ?? 'laravel'));
×
269
    }
270

271
    return $path;
34✔
272
}
273

274
/**
275
 * Get the path to the package folder.
276
 *
277
 * @api
278
 *
279
 * @param  array|string  $path
280
 * @return string
281
 */
282
function package_path(array|string $path = ''): string
283
{
284
    $argumentCount = \func_num_args();
54✔
285

286
    $workingPath = \defined('TESTBENCH_WORKING_PATH')
54✔
287
        ? TESTBENCH_WORKING_PATH
20✔
288
        : Env::get('TESTBENCH_WORKING_PATH', getcwd());
34✔
289

290
    if ($argumentCount === 1 && \is_string($path) && str_starts_with($path, './')) {
54✔
UNCOV
291
        return transform_relative_path($path, $workingPath);
×
292
    }
293

294
    $path = join_paths(...Arr::wrap($argumentCount > 1 ? \func_get_args() : $path));
54✔
295

296
    return str_starts_with($path, './')
54✔
UNCOV
297
        ? transform_relative_path($path, $workingPath)
×
298
        : join_paths(rtrim($workingPath, DIRECTORY_SEPARATOR), $path);
54✔
299
}
300

301
/**
302
 * Get the workbench configuration.
303
 *
304
 * @api
305
 *
306
 * @return array<string, mixed>
307
 */
308
function workbench(): array
309
{
310
    /** @var \Orchestra\Testbench\Contracts\Config $config */
311
    $config = app()->bound(Contracts\Config::class)
25✔
312
        ? app()->make(Contracts\Config::class)
23✔
313
        : new Foundation\Config;
2✔
314

315
    return $config->getWorkbenchAttributes();
25✔
316
}
317

318
/**
319
 * Get the path to the workbench folder.
320
 *
321
 * @api
322
 *
323
 * @param  array|string  $path
324
 * @return string
325
 */
326
function workbench_path(array|string $path = ''): string
327
{
328
    return package_path('workbench', ...Arr::wrap(\func_num_args() > 1 ? \func_get_args() : $path));
31✔
329
}
330

331
/**
332
 * Get the migration path by type.
333
 *
334
 * @api
335
 *
336
 * @param  string|null  $type
337
 * @return string
338
 *
339
 * @throws \InvalidArgumentException
340
 *
341
 * @deprecated
342
 */
343
function laravel_migration_path(?string $type = null): string
344
{
345
    return default_migration_path($type);
1✔
346
}
347

348
/**
349
 * Determine if vendor symlink exists on the laravel application.
350
 *
351
 * @api
352
 *
353
 * @param  \Illuminate\Contracts\Foundation\Application  $app
354
 * @param  string|null  $workingPath
355
 * @return bool
356
 */
357
function laravel_vendor_exists(ApplicationContract $app, ?string $workingPath = null): bool
358
{
359
    $filesystem = new Filesystem;
3✔
360

361
    $appVendorPath = $app->basePath('vendor');
3✔
362
    $workingPath ??= package_path('vendor');
3✔
363

364
    return $filesystem->isFile(join_paths($appVendorPath, 'autoload.php')) &&
3✔
365
        $filesystem->hash(join_paths($appVendorPath, 'autoload.php')) === $filesystem->hash(join_paths($workingPath, 'autoload.php'));
3✔
366
}
367

368
/**
369
 * Laravel version compare.
370
 *
371
 * @api
372
 *
373
 * @param  string  $version
374
 * @param  string|null  $operator
375
 * @return int|bool
376
 */
377
function laravel_version_compare(string $version, ?string $operator = null)
378
{
379
    /** @phpstan-ignore identical.alwaysFalse */
380
    $laravel = Application::VERSION === '9.x-dev' ? '9.0.0' : Application::VERSION;
5✔
381

382
    if (\is_null($operator)) {
5✔
383
        return version_compare($laravel, $version);
1✔
384
    }
385

386
    return version_compare($laravel, $version, $operator);
5✔
387
}
388

389
/**
390
 * PHPUnit version compare.
391
 *
392
 * @api
393
 *
394
 * @param  string  $version
395
 * @param  string|null  $operator
396
 * @return int|bool
397
 *
398
 * @throws \RuntimeException
399
 */
400
function phpunit_version_compare(string $version, ?string $operator = null)
401
{
402
    if (! class_exists(Version::class)) {
3✔
UNCOV
403
        throw new RuntimeException('Unable to verify PHPUnit version');
×
404
    }
405

406
    if (\is_null($operator)) {
3✔
407
        return version_compare(Version::id(), $version);
1✔
408
    }
409

410
    return version_compare(Version::id(), $version, $operator);
3✔
411
}
412

413
/**
414
 * Determine the PHP Binary.
415
 *
416
 * @api
417
 *
418
 * @param  bool  $escape
419
 * @return string
420
 */
421
function php_binary(bool $escape = false): string
422
{
423
    $phpBinary = (new Support\PhpExecutableFinder)->find(false) ?: 'php';
10✔
424

425
    return $escape === true ? ProcessUtils::escapeArgument((string) $phpBinary) : $phpBinary;
10✔
426
}
427

428
/**
429
 * Join the given paths together.
430
 *
431
 * @param  string|null  $basePath
432
 * @param  string  ...$paths
433
 * @return string
434
 */
435
function join_paths(?string $basePath, string ...$paths): string
436
{
437
    foreach ($paths as $index => $path) {
160✔
438
        if (empty($path) && $path !== '0') {
160✔
439
            unset($paths[$index]);
154✔
440
        } else {
441
            $paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR);
160✔
442
        }
443
    }
444

445
    return $basePath.implode('', $paths);
160✔
446
}
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