Spiral provides is a convenient way to test the spiral/storage component.
The fakeStorage method allows you to create a fake storage that mimics the behavior of a real storage, but doesn't
actually send any files to the cloud. This way, you can test file uploads without worrying about accidentally sending
real files to the cloud.
use Tests\TestCase;
final class UserServiceTest extends TestCase
{
private \Spiral\Testing\Storage\FakeBucket $bucket;
protected function setUp(): void
{
parent::setUp();
$this->bucket = $this->fakeStorage()->bucket('avatars');
}
public function testUserShouldBeRegistered(): void
{
// Perform user registration ...
$this->bucket->assertCreated('avatars/john_smith.jpg');
}
}
You can then use the assertion methods provided by the class, such as assertExists, assertCreated,
assertNotExist, assertNotCreated, etc to check if specific file exists in the bucket.
The assertExists method asserts that the specified file exists in the bucket.
$uploads = $storage->bucket('uploads');
$uploads->assertExists('image.jpg');
The assertNotExist method asserts that the specified file does not exist in the bucket.
$uploads = $storage->bucket('uploads');
$uploads->assertNotExist('image.jpg');
The assertCreated method asserts that the specified file was created in the bucket.
$uploads = $storage->bucket('uploads');
$uploads->assertCreated('image.jpg');
The assertNotCreated method asserts that the specified file was not created in the bucket.
$uploads = $storage->bucket('uploads');
$uploads->assertNotCreated('image.jpg');
The assertDeleted method asserts that the specified file was deleted from the bucket.
$uploads = $storage->bucket('uploads');
$uploads->assertDeleted('image.jpg');
The assertNotDeleted method asserts that the specified file was not deleted from the bucket.
$uploads = $storage->bucket('uploads');
$uploads->assertNotDeleted('image.jpg');
The assertMoved method asserts that the specified file was moved from one location to another.
$uploads = $storage->bucket('uploads');
$uploads->assertMoved('file.txt', 'folder/file.txt');
The assertNotMoved method asserts that the specified file was not moved from one location to another.
$uploads = $storage->bucket('uploads');
$uploads->assertNotMoved('file.txt', 'folder/file.txt');
The assertCopied method asserts that the specified file was copied from one location to another.
$uploads = $storage->bucket('uploads');
$uploads->assertCopied('file.txt', 'folder/file.txt');
The assertNotCopied method asserts that the specified file was not copied from one location to another.
$uploads = $storage->bucket('uploads');
$uploads->assertNotCopied('file.txt', 'folder/file.txt');
The assertVisibilityChanged method asserts that the specified file has the given visibility.
$uploads = $storage->bucket('uploads');
$uploads->assertVisibilityChanged('file.txt');
The assertVisibilityNotChanged method asserts that the specified file has not changed its visibility.
$uploads = $storage->bucket('uploads');
$uploads->assertVisibilityNotChanged('file.txt');