diff options
| -rw-r--r-- | src/Assets.php | 37 | ||||
| -rw-r--r-- | tests/Feature/AssetsTest.php | 78 | 
2 files changed, 114 insertions, 1 deletions
| diff --git a/src/Assets.php b/src/Assets.php index 78cbe59..a431ffb 100644 --- a/src/Assets.php +++ b/src/Assets.php @@ -4,19 +4,54 @@ namespace Lightscale\LaralightAssets;  class Assets  { -    private string $manifestParser; +    private string $defaultManifest;      private array $manifests = [];      private array $manifestsParsers = [];      private array $files = [];      private array $queuedHeadFiles = [];      private array $queuedFooterFiles = []; +    public function addManifest( +        string $name, +        Manifest $manifest, +        bool $isDefault = false +    ): void +    { +        $this->manifests[$name] = $manifest; + +        if ($isDefault) { +            $this->defaultManifest = $name; +        } +    } +      public function registerManifest(          string $name,          string $path,          string $baseUrl, +        string $parserClass = JsonManifestParser::class, +        bool $isDefault = false,      ): void      { +        $this->addManifest( +            $name, +            new Manifest($path, $baseUrl, $parserClass), +            $isDefault +        ); +    } +    public function getManifests(): array +    { +        return $this->manifests;      } + +    public function getManifest(string $name): Manifest +    { +        return $this->getManifests()[$name]; +    } + +    public function getDefaultManifest(): Manifest +    { +        return $this->getManifest($this->defaultManifest); +    } +  } diff --git a/tests/Feature/AssetsTest.php b/tests/Feature/AssetsTest.php new file mode 100644 index 0000000..887142c --- /dev/null +++ b/tests/Feature/AssetsTest.php @@ -0,0 +1,78 @@ +<?php + +use Lightscale\LaralightAssets\Assets; +use Lightscale\LaralightAssets\Manifest; + +it('instantiates', function() { +    new Assets(); +})->throwsNoExceptions(); + +it('instantiates from container', function() { +    app(Assets::class); +})->throwsNoExceptions(); + +it('is singleton', function() { +    expect(app(Assets::class))->toBe(app(Assets::class)); +}); + +it('adds manifests', function() { +    app(Assets::class)->addManifest( +        'main', +        new Manifest( +            public_path('dist/manifest.json'), +            '/dist', +        ), +        isDefault: true, +    ); +})->throwsNoExceptions(); + +it('registers manifests', function() { +    app(Assets::class)->registerManifest( +        'main', public_path('dist/manifest.json'), '/dist', isDefault: true, +    ); +})->throwsNoExceptions(); + +it('gets manifests', function() { +    $assets = app(Assets::class); +    $assets->registerManifest( +        'main', public_path('dist/manifest.json'), '/dist', isDefault: true, +    ); + +    expect($assets->getManifests()) +        ->toHaveLength(1) +        ->toContainOnlyInstancesOf(Manifest::class); +}); + +it('gets manifest', function() { +    $assets = app(Assets::class); +    $manifest = new Manifest( +        public_path('dist/manifest.json'), +        '/dist', +    ); +    $name = 'main'; +    $assets->addManifest($name, $manifest, true); + +    expect($assets->getManifest($name))->toBe($manifest); +}); + +it('gets default manifest', function() { +    $assets = app(Assets::class); +    $manifest = new Manifest( +        public_path('dist/manifest.json'), +        '/dist', +    ); +    $name = 'main'; +    $assets->addManifest($name, $manifest, true); + +    expect($assets->getDefaultManifest())->toBe($manifest); +}); + +it('throws exception missing manifest', function() { +    $assets = app(Assets::class); +    $assets->getManifest('main'); +})->throws(Exception::class); + +it('throws exception without default', function() { +    $assets = app(Assets::class); +    $assets->getDefaultManifest(); +})->throws(Error::class); | 
