oc-resizer-plugin/tests/ResizerTest.php

245 lines
8.7 KiB
PHP

<?php
namespace Aweos\Resizer\Tests;
use Aweos\Resizer\Exceptions\ResizerException;
use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Tests\TestCase;
use Event;
use Illuminate\Http\UploadedFile;
use Media\Classes\MediaLibrary;
use Storage;
class ResizerTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
Storage::fake('local');
$this->media = MediaLibrary::instance();
}
public function testDontCopyOriginalFileWhenSettingsAreNotSet(): void
{
Setting::set('folders', []);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
$this->media->put('/pages/test.jpg', $file);
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(0, '');
}
public function testThrowExceptionWhenOriginalFileNotFound(): void
{
$this->expectException(ResizerException::class);
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(0, '');
}
public function testCopyOriginalFileWithoutSizesWhenNoSizesAreSet(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
$this->media->put('/pages/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(1, 'pages');
$this->assertDoesntHaveFile('pages/test.jpg');
$this->assertHasFile('pages/test-500x600.jpg');
}
public function testCopyOriginalFileWithoutSizesWhenFilenameDoesntBeginWithASlash(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
$this->media->put('/pages/test.jpg', $file->get());
Event::fire('media.file.upload', [null, 'pages/test.jpg', null]);
$this->assertFileCount(1, 'pages');
$this->assertHasFile('pages/test-500x600.jpg');
}
public function testCopyTwoDirectoriesDeepButNotAnotherDirectory(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
$this->media->put('/pages-neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages-neu/test.jpg', null]);
$this->assertFileCount(0, 'pages');
$this->assertFileCount(0, 'pages-neu');
}
public function testCopySubdirectoriesOfSelectedPath(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', []);
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
$this->media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(1, 'pages/neu');
}
public function testGenerateSizeIfSizeIsSmallerWithSameAspectRatio(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', ['250']);
$file = UploadedFile::fake()->image('test.jpg', 500, 600);
$this->media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(2, 'pages/neu');
$this->assertHasFile('pages/neu/test-250x300.jpg');
$this->assertHasFile('pages/neu/test-500x600.jpg');
}
public function testGenerateSizeIfSizeIsSmallerWithDifferentAspectRatio(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', ['250']);
$file = UploadedFile::fake()->image('test.jpg', 500, 100);
$this->media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertHasFile('pages/neu/test-250x50.jpg');
}
public function testDontGenerateSizeIfOriginalFileIsSmaller(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', ['250']);
$file = UploadedFile::fake()->image('test.jpg', 250, 1000);
$this->media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(1, 'pages/neu');
}
public function testDontGenerateSizeIfImageWouldBeLarger(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', ['250']);
$file = UploadedFile::fake()->image('test.jpg', 249, 1000);
$this->media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(1, 'pages/neu');
}
public function testGenerateBreakpointImage(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '5x8']]);
Setting::set('breakpoints', ['100']);
$file = UploadedFile::fake()->image('test.jpg', 500, 400);
$this->media->put('/pages/neu/test.jpg', $file->get());
Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]);
$this->assertFileCount(4, 'pages/neu');
$this->assertHasFile('pages/neu/test-500x400.jpg');
$this->assertHasFile('pages/neu/test-250x400.jpg');
$this->assertHasFile('pages/neu/test-100x160.jpg');
$this->assertHasFile('pages/neu/test-100x80.jpg');
}
public function testDontGenerateSvgImageVesions(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', ['100']);
$this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg'));
Event::fire('media.file.upload', [null, '/pages/test.svg', null]);
$this->assertFileCount(0, 'pages');
}
public function testGeneratePngImage(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', ['100']);
$this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get());
Event::fire('media.file.upload', [null, '/pages/test.png', null]);
$this->assertHasFile('pages/test-100x100.png');
$this->assertHasFile('pages/test-500x500.png');
$this->assertFileCount(2, 'pages');
}
public function testDontUpsizeAJpgImage(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', ['100', '600']);
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
$this->assertFileCount(2, 'pages');
$this->assertHasFile('pages/test-100x100.jpg');
$this->assertHasFile('pages/test-500x500.jpg');
}
public function testDontUpsizeAPngImage(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', []);
Setting::set('breakpoints', ['100', '600']);
$this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get());
Event::fire('media.file.upload', [null, '/pages/test.png', null]);
$this->assertHasFile('pages/test-100x200.png');
$this->assertFileCount(2, 'pages');
}
public function testGeneratePdfImages(): void
{
Setting::set('folders', [['folder' => '/pages']]);
Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '1x1']]);
Setting::set('breakpoints', [200]);
$this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf'));
Event::fire('media.file.upload', [null, '/pages/test.pdf', null]);
$this->assertHasFile('pages/test-1275x1650.pdf.jpg');
$this->assertHasFile('pages/test-1275x1275.pdf.jpg');
$this->assertHasFile('pages/test-200x259.pdf.jpg');
$this->assertHasFile('pages/test-200x200.pdf.jpg');
$this->assertFileCount(4, 'pages');
}
}