-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathStorageTest.php
More file actions
95 lines (64 loc) · 3.44 KB
/
StorageTest.php
File metadata and controls
95 lines (64 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
use Saloon\Helpers\Storage;
use League\Flysystem\Filesystem;
use Saloon\Exceptions\DirectoryNotFoundException;
use League\Flysystem\Local\LocalFilesystemAdapter;
test('it will throw an exception if the base directory does not exist', function () {
new Storage('example');
})->throws(DirectoryNotFoundException::class, 'The directory "example" does not exist or is not a valid directory.');
test('you can check if a file exists', function () {
$storage = new Storage('tests');
expect($storage->exists('Pest.php'))->toBeTrue();
expect($storage->missing('Pest.php'))->toBeFalse();
});
test('you can check if a file is missing', function () {
$storage = new Storage('tests');
expect($storage->exists('HelloWorld.php'))->toBeFalse();
expect($storage->missing('HelloWorld.php'))->toBeTrue();
});
test('you can retrieve a file from storage', function () {
$storage = new Storage('tests');
$file = $storage->get('Pest.php');
expect($file)->toEqual(file_get_contents('tests/Pest.php'));
});
test('you can put a file in storage', function () {
$filesystem = new Filesystem(new LocalFilesystemAdapter('tests/Fixtures/Saloon/Testing'));
$filesystem->deleteDirectory('/');
$filesystem->createDirectory('/');
$storage = new Storage('tests/Fixtures/Saloon/Testing');
expect($storage->exists('example.txt'))->toBeFalse();
$storage->put('example.txt', 'Hello World');
expect($storage->exists('example.txt'))->toBeTrue();
expect($storage->get('example.txt'))->toEqual('Hello World');
});
test('it will create a file with nested folders', function () {
$filesystem = new Filesystem(new LocalFilesystemAdapter('tests/Fixtures/Saloon/Testing'));
$filesystem->deleteDirectory('/');
$filesystem->createDirectory('/');
$path = 'Testing' . DIRECTORY_SEPARATOR . 'some' . DIRECTORY_SEPARATOR . 'other' . DIRECTORY_SEPARATOR . 'directories' . DIRECTORY_SEPARATOR . 'example.txt';
$storage = new Storage('tests' . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'Saloon');
expect($storage->exists($path))->toBeFalse();
$storage->put($path, 'Hello World');
expect($storage->exists($path))->toBeTrue();
expect($storage->get($path))->toEqual('Hello World');
});
test('you can get the base directory path from the storage class', function () {
$storage = new Storage('tests/Fixtures/Saloon');
expect($storage->getBaseDirectory())->toEqual('tests/Fixtures/Saloon');
});
test('when putting the path that escapes the base directory throws', function () {
$storage = new Storage('tests');
expect(fn () => $storage->put('..' . DIRECTORY_SEPARATOR . 'outside.txt', 'content'))
->toThrow(InvalidArgumentException::class, 'Path must remain inside the storage base directory');
});
test('when getting the path that escapes the base directory throws', function () {
$storage = new Storage('tests');
expect(fn () => $storage->get('..' . DIRECTORY_SEPARATOR . 'outside.txt'))
->toThrow(InvalidArgumentException::class, 'Path must remain inside the storage base directory');
});
test('when checking the existence of a path that escapes the base directory throws', function (string $method) {
$storage = new Storage('tests');
expect(fn () => $storage->$method('..' . DIRECTORY_SEPARATOR . 'outside.txt'))
->toThrow(InvalidArgumentException::class, 'Path must remain inside the storage base directory');
})->with(['exists', 'missing']);