Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Jul 8, 2024
1 parent 3913a82 commit 1683986
Showing 1 changed file with 17 additions and 50 deletions.
67 changes: 17 additions & 50 deletions tests/ZipperTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Aerni\Zipper\Tests;

use Aerni\Zipper\Facades\ZipperStore;
use Aerni\Zipper\ZipperTags;
use Illuminate\Support\Str;
use Statamic\Assets\AssetContainer;
use Statamic\Facades\AssetContainer as AssetContainerFacade;
use Statamic\Assets\Asset;
use Statamic\Fields\Field;
use Statamic\Fields\Value;
use Illuminate\Support\Str;
use Aerni\Zipper\ZipperTags;
use Statamic\Facades\Antlers;
use Aerni\Zipper\Facades\ZipperStore;
use Statamic\Fieldtypes\Assets\Assets;
use Statamic\Assets\OrderedQueryBuilder;
use Statamic\Facades\AssetContainer;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;

class ZipperTagsTest extends TestCase
Expand All @@ -27,69 +29,34 @@ public function setUp(): void
'root' => __DIR__.'/__fixtures__/assets',
]]);

$this->container = (new AssetContainer)->handle('test')->disk('test');

AssetContainerFacade::shouldReceive('findByHandle')->andReturn($this->container);
AssetContainerFacade::shouldReceive('find')->andReturn($this->container);
AssetContainerFacade::shouldReceive('all')->andReturn(collect([$this->container]));

$this->tag = app(ZipperTags::class);
$this->container = AssetContainer::make('test')->disk('test')->save();
}

/** @test */
public function can_handle_a_single_asset()
{
$file = $this->container->files()->first();
$asset = $this->container->assets()->first();

$fieldtype = (new Assets)->setField(new Field('assets', [
'type' => 'assets',
'max_files' => 1,
]));
$url = Antlers::parse('{{ zip:assets }}', ['assets' => $asset]);

$value = new Value($file, 'assets', $fieldtype);
$id = Str::before(basename($url), '?signature');

$this->tag
->setContext(['assets' => $value])
->setParameters([]);
$files = ZipperStore::get($id)->files();

$this->tag->method = 'assets';

$url = $this->tag->wildcard();

$uri = Str::afterLast($url, '/');
$id = Str::before($uri, '?signature');

$file = ZipperStore::get($id)->files()[0];

$this->assertSame($value->value()->resolvedPath(), $file->resolvedPath());
$this->assertEquals($asset->resolvedPath(), $files->first()->resolvedPath());
}

/** @test */
public function can_handle_multiple_assets()
{
$files = $this->container->files()->all();

$fieldtype = (new Assets)->setField(new Field('assets', [
'type' => 'assets',
]));

$value = new Value($files, 'assets', $fieldtype);

$this->tag
->setContext(['assets' => $value])
->setParameters([]);

$this->tag->method = 'assets';
$assets = $this->container->queryAssets();

$url = $this->tag->wildcard();
$url = Antlers::parse('{{ zip:assets }}', ['assets' => $assets]);

$uri = Str::afterLast($url, '/');
$id = Str::before($uri, '?signature');
$id = Str::before(basename($url), '?signature');

$files = ZipperStore::get($id)->files()->map(fn ($file) => $file->resolvedPath());

$value->value()->get()->each(function ($file) use ($files) {
$this->assertContains($file->resolvedPath(), $files);
});
$assets->get()->each(fn ($asset) => $this->assertContains($asset->resolvedPath(), $files));
}
}

0 comments on commit 1683986

Please sign in to comment.