Skip to content

Commit b610ea2

Browse files
committed
refactor: use new filesystem functions
1 parent 09fa013 commit b610ea2

File tree

10 files changed

+47
-51
lines changed

10 files changed

+47
-51
lines changed

src/Tempest/Framework/Testing/ViteTester.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
use InvalidArgumentException;
88
use Tempest\Container\Container;
99
use Tempest\Container\Exceptions\ContainerException;
10+
use Tempest\Core\FrameworkKernel;
1011
use Tempest\Core\Kernel;
11-
use Tempest\Filesystem\LocalFilesystem;
1212
use Tempest\Vite\TagsResolver\NullTagsResolver;
1313
use Tempest\Vite\TagsResolver\TagsResolver;
1414
use Tempest\Vite\Vite;
1515
use Tempest\Vite\ViteConfig;
1616

17+
use function Tempest\Support\Filesystem\delete_directory;
18+
use function Tempest\Support\Filesystem\ensure_directory_exists;
19+
use function Tempest\Support\Filesystem\write_file;
20+
1721
final class ViteTester
1822
{
1923
private ?string $root = null;
@@ -126,30 +130,29 @@ public function call(callable $callback, array $files, bool $manifest = false, ?
126130
$tagsResolver = null;
127131
}
128132

129-
$filesystem = new LocalFilesystem();
130-
$filesystem->deleteDirectory($temporaryRootDirectory, recursive: true);
131-
$filesystem->ensureDirectoryExists($temporaryRootDirectory);
133+
ensure_directory_exists($temporaryRootDirectory);
132134

133135
$paths = [];
134136

135137
foreach ($files as $path => $content) {
136138
$path = "{$temporaryRootDirectory}/{$path}";
137139
$paths[] = $path;
138-
$filesystem->ensureDirectoryExists(dirname($path));
139-
$filesystem->write($path, is_array($content) ? json_encode($content, flags: JSON_UNESCAPED_SLASHES) : $content);
140+
141+
ensure_directory_exists(dirname($path));
142+
write_file($path, is_array($content) ? json_encode($content, flags: JSON_UNESCAPED_SLASHES) : $content);
140143
}
141144

142-
$this->container->get(Kernel::class)->root = $temporaryRootDirectory;
145+
$this->container->get(FrameworkKernel::class)->root = $temporaryRootDirectory;
143146
$this->container->config($temporaryViteConfig);
144147
$this->container->unregister(TagsResolver::class);
145148
$callback(...$paths);
146-
$this->container->get(Kernel::class)->root = $actualRootDirectory;
149+
$this->container->get(FrameworkKernel::class)->root = $actualRootDirectory;
147150
$this->container->config($actualViteConfig);
148151

149152
if ($tagsResolver) {
150153
$this->container->register(TagsResolver::class, fn () => $tagsResolver);
151154
}
152155

153-
$filesystem->deleteDirectory($temporaryRootDirectory, recursive: true);
156+
delete_directory($temporaryRootDirectory);
154157
}
155158
}

src/Tempest/Support/src/Arr/ManipulatesArray.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,11 @@ public function slice(int $offset, ?int $length = null): static
727727
/**
728728
* Returns a pair containing lists for which the given predicate returned `true` and `false`, respectively.
729729
*
730-
* @param (Closure(T): bool) $predicate
730+
* @param (Closure(TValue): bool) $predicate
731731
*
732-
* @return static<array<T>, array<T>>
732+
* @return static<int, array<array<TValue>, array<TValue>>>
733733
*/
734-
function partition(Closure $predicate): static
734+
public function partition(Closure $predicate): static
735735
{
736736
$success = [];
737737
$failure = [];

src/Tempest/Support/src/Filesystem/functions.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Tempest\Support\Filesystem {
66
use FilesystemIterator;
7-
use Tempest\Support\Filesystem\Exceptions\RuntimeException;
87

98
use function copy as php_copy;
109
use function dirname;
@@ -122,13 +121,13 @@ function create_directory(string $directory, int $permissions = 0o777): void
122121
return;
123122
}
124123

125-
[$result, $error_message] = box(static fn (): bool => mkdir($directory, $permissions, true));
124+
[$result, $errorMessage] = box(static fn (): bool => mkdir($directory, $permissions, recursive: true));
126125

127-
if (false === $result && ! namespace\is_directory($directory)) {
126+
if ($result === false && ! namespace\is_directory($directory)) { // @phpstan-ignore booleanNot.alwaysTrue
128127
throw new Exceptions\RuntimeException(sprintf(
129128
'Failed to create directory "%s": %s.',
130129
$directory,
131-
$error_message ?? 'internal error',
130+
$errorMessage ?? 'internal error',
132131
));
133132
}
134133
}
@@ -217,7 +216,7 @@ function delete_file(string $file): void
217216

218217
[$result, $errorMessage] = box(static fn (): bool => unlink($file));
219218

220-
if (false === $result && namespace\is_file($file)) {
219+
if ($result === false && namespace\is_file($file)) { // @phpstan-ignore booleanAnd.rightAlwaysTrue
221220
throw new Exceptions\RuntimeException(sprintf(
222221
'Failed to delete file "%s": %s.',
223222
$file,
@@ -253,13 +252,13 @@ function get_permissions(string $path): int
253252
/**
254253
* Cleans the specified `$directory` by deleting its contents, optionally creating it if it doesn't exist.
255254
*/
256-
function empty_directory(string $directory, bool $create = false): void
255+
function ensure_directory_empty(string $directory): void
257256
{
258-
if (! namespace\is_directory($directory) && ! $create) {
257+
if (namespace\exists($directory) && ! namespace\is_directory($directory)) {
259258
throw Exceptions\NotDirectoryException::for($directory);
260259
}
261260

262-
if (! namespace\is_directory($directory) && $create) {
261+
if (! namespace\is_directory($directory)) {
263262
namespace\create_directory($directory);
264263
return;
265264
}

src/Tempest/Support/src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function box(Closure $callback): array
6161
{
6262
$lastMessage = null;
6363

64-
set_error_handler(static function (int $_type, string $message) use (&$lastMessage): void {
64+
set_error_handler(static function (int $_type, string $message) use (&$lastMessage): void { // @phpstan-ignore argument.type
6565
$lastMessage = $message;
6666
});
6767

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Tempest\Support\Tests\Filesystem;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class FunctionsTest extends TestCase
8+
{
9+
}

tests/Integration/Auth/AuthorizerTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
use Tempest\Auth\Install\User;
1212
use Tempest\Clock\Clock;
1313
use Tempest\Core\FrameworkKernel;
14-
use Tempest\Core\Kernel;
1514
use Tempest\Database\Migrations\CreateMigrationsTable;
16-
use Tempest\Filesystem\LocalFilesystem;
1715
use Tempest\Router\Session\Managers\FileSessionManager;
1816
use Tempest\Router\Session\SessionConfig;
1917
use Tempest\Router\Session\SessionManager;
18+
use Tempest\Support\Filesystem;
2019
use Tests\Tempest\Fixtures\Controllers\AdminController;
2120
use Tests\Tempest\Integration\Auth\Fixtures\UserPermissionUnitEnum;
2221
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
@@ -30,8 +29,6 @@ final class AuthorizerTest extends FrameworkIntegrationTestCase
3029
{
3130
private string $path = __DIR__ . '/Fixtures/tmp';
3231

33-
private LocalFilesystem $filesystem;
34-
3532
protected function setUp(): void
3633
{
3734
parent::setUp();
@@ -44,9 +41,8 @@ protected function setUp(): void
4441
);
4542

4643
$this->path = __DIR__ . '/Fixtures/tmp';
47-
$this->filesystem = new LocalFilesystem();
48-
$this->filesystem->deleteDirectory($this->path, recursive: true);
49-
$this->filesystem->ensureDirectoryExists($this->path);
44+
45+
Filesystem\ensure_directory_empty($this->path);
5046

5147
$this->container->get(FrameworkKernel::class)->internalStorage = realpath($this->path);
5248

@@ -62,7 +58,7 @@ protected function setUp(): void
6258

6359
protected function tearDown(): void
6460
{
65-
$this->filesystem->deleteDirectory($this->path, recursive: true);
61+
Filesystem\delete_directory($this->path);
6662
}
6763

6864
public function test_authorize(): void

tests/Integration/Auth/SessionAuthenticatorTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
use Tempest\Clock\Clock;
1515
use Tempest\Core\FrameworkKernel;
1616
use Tempest\Database\Migrations\CreateMigrationsTable;
17-
use Tempest\Filesystem\LocalFilesystem;
1817
use Tempest\Router\Session\Managers\FileSessionManager;
1918
use Tempest\Router\Session\Session;
2019
use Tempest\Router\Session\SessionConfig;
2120
use Tempest\Router\Session\SessionManager;
21+
use Tempest\Support\Filesystem;
2222
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
2323

2424
/**
@@ -28,16 +28,13 @@ final class SessionAuthenticatorTest extends FrameworkIntegrationTestCase
2828
{
2929
private string $path = __DIR__ . '/Fixtures/tmp';
3030

31-
private LocalFilesystem $filesystem;
32-
3331
protected function setUp(): void
3432
{
3533
parent::setUp();
3634

3735
$this->path = __DIR__ . '/Fixtures/tmp';
38-
$this->filesystem = new LocalFilesystem();
39-
$this->filesystem->deleteDirectory($this->path, recursive: true);
40-
$this->filesystem->ensureDirectoryExists($this->path);
36+
37+
Filesystem\ensure_directory_empty($this->path);
4138

4239
$this->container->get(FrameworkKernel::class)->internalStorage = realpath($this->path);
4340

@@ -60,7 +57,7 @@ protected function setUp(): void
6057

6158
protected function tearDown(): void
6259
{
63-
$this->filesystem->deleteDirectory($this->path, recursive: true);
60+
Filesystem\delete_directory($this->path);
6461
}
6562

6663
public function test_authenticator(): void

tests/Integration/Http/FileSessionTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66

77
use Tempest\Clock\Clock;
88
use Tempest\Core\FrameworkKernel;
9-
use Tempest\Core\Kernel;
10-
use Tempest\Filesystem\LocalFilesystem;
119
use Tempest\Router\Session\Managers\FileSessionManager;
1210
use Tempest\Router\Session\Session;
1311
use Tempest\Router\Session\SessionConfig;
1412
use Tempest\Router\Session\SessionId;
1513
use Tempest\Router\Session\SessionManager;
14+
use Tempest\Support\Filesystem;
1615
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1716

18-
use function Tempest\internal_storage_path;
1917
use function Tempest\Support\path;
2018

2119
/**
@@ -25,20 +23,15 @@ final class FileSessionTest extends FrameworkIntegrationTestCase
2523
{
2624
private string $path = __DIR__ . '/Fixtures/tmp';
2725

28-
private LocalFilesystem $filesystem;
29-
3026
protected function setUp(): void
3127
{
3228
parent::setUp();
3329

34-
$this->filesystem = new LocalFilesystem();
35-
$this->filesystem->deleteDirectory($this->path, recursive: true);
36-
$this->filesystem->ensureDirectoryExists($this->path);
30+
Filesystem\ensure_directory_empty($this->path);
3731

3832
$this->path = realpath($this->path);
3933

4034
$this->container->get(FrameworkKernel::class)->internalStorage = realpath($this->path);
41-
4235
$this->container->config(new SessionConfig(path: 'sessions'));
4336
$this->container->singleton(
4437
SessionManager::class,
@@ -51,7 +44,7 @@ protected function setUp(): void
5144

5245
protected function tearDown(): void
5346
{
54-
$this->filesystem->deleteDirectory($this->path, recursive: true);
47+
Filesystem\delete_directory($this->path);
5548
}
5649

5750
public function test_create_session_from_container(): void

tests/Integration/Support/ArrayMutabilityTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ final class ArrayMutabilityTest extends TestCase
3232
'dd',
3333
'tap',
3434
'groupBy',
35+
'partition',
3536
];
3637

3738
public function test_immutable_array(): void

tests/Integration/Support/DependencyInstallerTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Closure;
88
use PHPUnit\Framework\Attributes\TestWith;
9-
use Tempest\Filesystem\LocalFilesystem;
9+
use Tempest\Support\Filesystem;
1010
use Tempest\Support\JavaScript\DependencyInstaller;
1111
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1212

@@ -78,10 +78,8 @@ private function callInTemporaryDirectory(Closure $callback): void
7878
{
7979
$directory = __DIR__ . '/Fixtures/tmp';
8080

81-
$filesystem = new LocalFilesystem();
82-
$filesystem->deleteDirectory($directory, recursive: true);
83-
$filesystem->ensureDirectoryExists($directory);
81+
Filesystem\ensure_directory_empty($directory);
8482
$callback($directory);
85-
$filesystem->deleteDirectory($directory, recursive: true);
83+
Filesystem\delete_directory($directory);
8684
}
8785
}

0 commit comments

Comments
 (0)