Skip to content

Commit 19729da

Browse files
committed
🐛 use match properly
1 parent 87ca21f commit 19729da

File tree

7 files changed

+75
-11
lines changed

7 files changed

+75
-11
lines changed

src/Roots/Acorn/Bootloader.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class Bootloader
2626
*/
2727
protected $app;
2828

29+
/**
30+
* Filesystem helper
31+
*
32+
* @var \Roots\Acorn\Filesystem\Filesystem
33+
*/
34+
protected $files;
35+
2936
/**
3037
* Base path for the application
3138
*
@@ -66,9 +73,10 @@ public static function getInstance(?ApplicationContract $app = null)
6673
*
6774
* @param \Illuminate\Contracts\Foundation\Application $app
6875
*/
69-
public function __construct(?ApplicationContract $app = null)
76+
public function __construct(ApplicationContract $app = null, Filesystem $files = null)
7077
{
7178
$this->app = $app;
79+
$this->files = $files ?? new Filesystem;
7280

7381
static::$instance ??= $this;
7482
}
@@ -310,7 +318,7 @@ protected function basePath(): string
310318

311319
is_dir($app_path = get_theme_file_path('app')) => dirname($app_path),
312320

313-
$vendor_path = (new Filesystem())->closest(dirname(__DIR__, 4), 'composer.json') => dirname($vendor_path),
321+
is_file($vendor_path = $this->files->closest(dirname(__DIR__, 4), 'composer.json')) => dirname($vendor_path),
314322

315323
default => dirname(__DIR__, 3)
316324
};
@@ -418,12 +426,11 @@ protected function fallbackPath(string $path): string
418426
*/
419427
protected function fallbackStoragePath()
420428
{
421-
$files = new Filesystem();
422-
$path = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'acorn';
423-
$files->ensureDirectoryExists($path . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'data', 0755, true);
424-
$files->ensureDirectoryExists($path . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'views', 0755, true);
425-
$files->ensureDirectoryExists($path . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR . 'sessions', 0755, true);
426-
$files->ensureDirectoryExists($path . DIRECTORY_SEPARATOR . 'logs', 0755, true);
429+
$path = WP_CONTENT_DIR.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'acorn';
430+
$this->files->ensureDirectoryExists($path.DIRECTORY_SEPARATOR.'framework'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'data', 0755, true);
431+
$this->files->ensureDirectoryExists($path.DIRECTORY_SEPARATOR.'framework'.DIRECTORY_SEPARATOR.'views', 0755, true);
432+
$this->files->ensureDirectoryExists($path.DIRECTORY_SEPARATOR.'framework'.DIRECTORY_SEPARATOR.'sessions', 0755, true);
433+
$this->files->ensureDirectoryExists($path.DIRECTORY_SEPARATOR.'logs', 0755, true);
427434

428435
return $path;
429436
}

tests/Application/BootloaderTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,63 @@
33
use Roots\Acorn\Bootloader;
44
use Roots\Acorn\Tests\Test\TestCase;
55

6+
use function Roots\Acorn\Tests\mock;
7+
68
uses(TestCase::class);
79

10+
beforeEach(function () {
11+
unset($_ENV['APP_BASE_PATH']);
12+
});
13+
814
it('should get a new instance', function () {
915
expect(Bootloader::getInstance())->toBeInstanceOf(Bootloader::class);
1016
});
17+
18+
it('should reuse the same instance', function () {
19+
expect(Bootloader::getInstance())->toBe(Bootloader::getInstance());
20+
});
21+
22+
it('should get a new application instance', function () {
23+
expect((new Bootloader)->getApplication())->toBeInstanceOf(\Illuminate\Contracts\Foundation\Application::class);
24+
});
25+
26+
it('should set the basePath if env var is set', function () {
27+
$_ENV['APP_BASE_PATH'] = $path = $this->fixture('base_path/base_empty');
28+
29+
$app = (new Bootloader)->getApplication();
30+
31+
expect($app->basePath())->toBe($path);
32+
});
33+
34+
it('should set the basePath if composer.json exists in theme', function () {
35+
$composer_path = $this->fixture('base_path/base_composer');
36+
37+
$this->stub('get_theme_file_path', fn ($path) => "{$composer_path}/{$path}");
38+
39+
$app = (new Bootloader)->getApplication();
40+
41+
expect($app->basePath())->toBe($composer_path);
42+
});
43+
44+
it('should set the basePath if app exists in theme', function () {
45+
$app_path = $this->fixture('base_path/base_app');
46+
$this->stub('get_theme_file_path', fn () => $app_path);
47+
48+
$app = Bootloader::getInstance()->getApplication();
49+
50+
expect($app->basePath())->toBe(dirname($app_path));
51+
});
52+
53+
it('should set the basePath if composer.json exists as ancestor of ../../../', function () {
54+
$files = mock(\Roots\Acorn\Filesystem\Filesystem::class);
55+
$this->stub('get_theme_file_path', fn () => '');
56+
57+
$composer_path = $this->fixture('base_path/base_composer');
58+
59+
$files->shouldReceive('closest')->andReturn("{$composer_path}/composer.json");
60+
$files->shouldReceive('ensureDirectoryExists');
61+
62+
$app = (new Bootloader(null, $files))->getApplication();
63+
64+
expect($app->basePath())->toBe($composer_path);
65+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/Application/__fixtures__/base_path/base_composer/composer.json

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/Application/__fixtures__/use_paths/composer.json

Whitespace-only changes.

tests/Helpers.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ function temp(?string $path = null)
2929
if (! $temp) {
3030
$temp = (new TemporaryDirectory())->create();
3131

32-
register_shutdown_function(function () use ($temp) {
33-
$temp->delete();
34-
});
32+
register_shutdown_function(fn () => $temp->delete());
3533
}
3634

3735
if ($path !== null) {

0 commit comments

Comments
 (0)