HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/be.naniguide.com/tests/Unit/Assets/DirectoryAssetManagerTest.php
<?php

/**
 * DirectoryAssetManager unit test — filesystem-backed AssetManager.
 *
 * Each test builds a fresh temp directory and tears it down on teardown,
 * so nothing ever writes to the real base_path().
 */

use App\Library\Assets\DirectoryAssetManager;
use Illuminate\Http\UploadedFile;

uses(Tests\TestCase::class);

beforeEach(function () {
    $this->tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'dam_' . bin2hex(random_bytes(6));
    mkdir($this->tmp, 0777, true);
});

afterEach(function () {
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($this->tmp, FilesystemIterator::SKIP_DOTS),
        RecursiveIteratorIterator::CHILD_FIRST
    );
    foreach ($it as $file) {
        $file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname());
    }
    @rmdir($this->tmp);
});

it('lists files and folders at root with folders first', function () {
    file_put_contents($this->tmp . '/alpha.txt', 'hi');
    file_put_contents($this->tmp . '/beta.md', 'hi');
    mkdir($this->tmp . '/sub');

    $result = (new DirectoryAssetManager($this->tmp))->list([]);

    expect($result['total'])->toBe(3);
    expect($result['items'][0]->isFolder)->toBeTrue();
    expect($result['items'][0]->name)->toBe('sub');
    $names = array_map(fn ($e) => $e->name, $result['items']);
    expect($names)->toBe(['sub', 'alpha.txt', 'beta.md']);
});

it('filters listing by search term', function () {
    file_put_contents($this->tmp . '/report.md', '');
    file_put_contents($this->tmp . '/notes.txt', '');

    $result = (new DirectoryAssetManager($this->tmp))->list(['search' => 'rep']);

    expect($result['total'])->toBe(1);
    expect($result['items'][0]->name)->toBe('report.md');
});

it('navigates into a subdirectory via path', function () {
    mkdir($this->tmp . '/deep');
    file_put_contents($this->tmp . '/deep/inner.txt', '');

    $result = (new DirectoryAssetManager($this->tmp))->list(['path' => 'deep']);

    expect($result['total'])->toBe(1);
    expect($result['items'][0]->name)->toBe('inner.txt');
    expect($result['currentPath'])->toBe('deep');
});

it('rejects path traversal', function () {
    (new DirectoryAssetManager($this->tmp))->list(['path' => '..']);
})->throws(RuntimeException::class);

it('uploads a file into the root directory', function () {
    $entry = (new DirectoryAssetManager($this->tmp))
        ->upload(UploadedFile::fake()->create('hello.txt', 1));

    expect($entry->isFolder)->toBeFalse();
    expect($entry->name)->toBe('hello.txt');
    expect(file_exists($this->tmp . '/hello.txt'))->toBeTrue();
});

it('creates an empty file', function () {
    $entry = (new DirectoryAssetManager($this->tmp))->createFile('empty.txt');

    expect($entry->name)->toBe('empty.txt');
    expect($entry->size)->toBe(0);
    expect(file_exists($this->tmp . '/empty.txt'))->toBeTrue();
    expect(file_get_contents($this->tmp . '/empty.txt'))->toBe('');
});

it('refuses to create a file that already exists', function () {
    file_put_contents($this->tmp . '/already.txt', 'x');

    (new DirectoryAssetManager($this->tmp))->createFile('already.txt');
})->throws(RuntimeException::class);

it('renames a file', function () {
    file_put_contents($this->tmp . '/old.txt', 'hi');
    $mgr = new DirectoryAssetManager($this->tmp);
    $list = $mgr->list([]);
    $uid  = $list['items'][0]->id;

    $entry = $mgr->rename($uid, 'new.txt');

    expect($entry->name)->toBe('new.txt');
    expect(file_exists($this->tmp . '/new.txt'))->toBeTrue();
    expect(file_exists($this->tmp . '/old.txt'))->toBeFalse();
});

it('deletes a file', function () {
    file_put_contents($this->tmp . '/gone.txt', 'hi');
    $mgr = new DirectoryAssetManager($this->tmp);
    $uid = $mgr->list([])['items'][0]->id;

    $mgr->delete($uid);

    expect(file_exists($this->tmp . '/gone.txt'))->toBeFalse();
});

it('view throws because directory scope has no URL', function () {
    (new DirectoryAssetManager($this->tmp))->view('anything');
})->throws(BadMethodCallException::class);