diff options
| author | Sam Light <sam@lightscale.co.uk> | 2025-03-31 00:49:43 +0100 | 
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2025-03-31 00:49:43 +0100 | 
| commit | c85ac39b61875f6b570612739b0244160243d263 (patch) | |
| tree | d90b0955b11aabe26a8fa218e021cc5751939265 | |
| parent | 28e2f2c50c902d6ea09b2d6941c9e71ea07b23eb (diff) | |
Manifest testing
| -rw-r--r-- | src/Manifest.php | 21 | ||||
| -rw-r--r-- | tests/Feature/ManifestTest.php | 26 | 
2 files changed, 43 insertions, 4 deletions
| diff --git a/src/Manifest.php b/src/Manifest.php index b7c5e96..655945b 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -2,19 +2,26 @@  namespace Lightscale\LaralightAssets; +use Illuminate\Support\Str; +  class Manifest  { -    private ?array $data; +    private ?array $data = null; +    private string $basePath;      public function __construct(          private string $path,          private string $baseUrl, +        ?string $basePath = null,          private string $parserClass = JsonManifestParser::class, -    ) {} +    ) { +        $fileName = Str::afterLast($path, '/'); +        $this->basePath = Str::replaceLast("/{$fileName}", '', $path); +    }      private function readFile(): string      { -        return filegetcontents($this->path); +        return file_get_contents($this->path);      }      private function loadData(): void @@ -30,6 +37,12 @@ class Manifest          return $this->data;      } + +    public function getDir(): string +    { +        return $this->basePath; +    } +      public function getFile(string $file): ?string      {          return $this->getData()[$file] ?? null; @@ -38,7 +51,7 @@ class Manifest      public function getUrl(string $file): ?string      {          $file = $this->getFile($file); -        return $file === null ? null : "{$this->baseUrl}/{$file}"; +        return $file === null ? null : asset("{$this->baseUrl}/{$file}");      }      public function getPath(string $file): ?string diff --git a/tests/Feature/ManifestTest.php b/tests/Feature/ManifestTest.php index a9e1108..5a05764 100644 --- a/tests/Feature/ManifestTest.php +++ b/tests/Feature/ManifestTest.php @@ -2,6 +2,32 @@  use Lightscale\LaralightAssets\Manifest; +$path = fn() => public_path('dist/manifest.json'); +  it('instantiates', function() {      new Manifest('test.json', '/dist');  })->throwsNoExceptions(); + +it('generates dir from file', function() use ($path) { +    $manifest = new Manifest($path(), '/dist'); +    expect($manifest->getDir()) +        ->toBe(public_path('/dist')); +}); + +it('gets file', function() use ($path) { +    $manifest = new Manifest($path(), '/dist'); +    expect($manifest->getFile('main.css')) +        ->toBe('main.testing.css'); +}); + +it('gets url', function() use ($path) { +    $manifest = new Manifest($path(), '/dist'); +    expect($manifest->getUrl('main.css')) +        ->toBe(asset('/dist/main.testing.css')); +}); + +it('gets paths', function() use ($path) { +    $manifest = new Manifest($path(), '/dist'); +    expect($manifest->getPath('main.css')) +        ->toBe(public_path('/dist/main.testing.css')); +}); | 
