FileHelperTest.php in SVG Upload Sanitizer 8
File
tests/src/Unit/Helper/FileHelperTest.php
View source
<?php
namespace Drupal\Tests\svg_upload_sanitizer\Unit\Helper;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\FileInterface;
use Drupal\svg_upload_sanitizer\Helper\FileHelper;
use Drupal\Tests\svg_upload_sanitizer\TestLogger;
use Drupal\Tests\UnitTestCase;
class FileHelperTest extends UnitTestCase {
private $fileSystem;
private $logger;
private $fileHelper;
public function setUp() {
parent::setUp();
$this->fileSystem = $this
->createMock(FileSystemInterface::class);
$this->logger = new TestLogger();
$this->fileHelper = new FileHelper($this->fileSystem);
$this->fileHelper
->setLogger($this->logger);
}
public function testUpdateSizeWhenTheFilePathCouldNotBeResolved() {
list($file) = $this
->prepareUpdateSize(FALSE);
$this
->assertFalse($this->fileHelper
->updateSize($file));
$logs = $this->logger
->getLogs('error');
$this
->assertCount(1, $logs);
$this
->assertSame('Could not resolve the path of the file (URI: "public://fileuri").', reset($logs));
}
public function testUpdateSizeWhenTheFileSizeCouldNotBeGotten() {
list($file) = $this
->prepareUpdateSize(TRUE, FALSE);
$this
->assertFalse($this->fileHelper
->updateSize($file));
$logs = $this->logger
->getLogs('error');
$this
->assertCount(1, $logs);
$this
->assertSame('Could not get the file size (path: "something/that/will/never/exists.casper").', reset($logs));
}
public function testUpdateSizeWhenTheFileCouldNotBeSaved() {
list($file, $filePath) = $this
->prepareUpdateSize(TRUE, TRUE, FALSE);
$file
->expects($this
->atLeastOnce())
->method('id')
->willReturn(28);
$this
->assertTrue($this->fileHelper
->updateSize($file));
$logs = $this->logger
->getLogs('error');
$this
->assertCount(1, $logs);
$this
->assertSame(sprintf('Could not save the file (fid: "28", path: "%s").', $filePath), reset($logs));
}
public function testUpdateSize() {
list($file) = $this
->prepareUpdateSize(TRUE, TRUE, TRUE);
$this
->assertTrue($this->fileHelper
->updateSize($file));
}
private function prepareUpdateSize($pathIsResolved, $filePathExists = FALSE, $fileSaveIsSuccessful = FALSE) {
$filePath = $filePathExists ? sprintf('%s/fixtures/image.svg', __DIR__) : 'something/that/will/never/exists.casper';
$this->fileSystem
->expects($this
->atLeastOnce())
->method('realpath')
->with('public://fileuri')
->willReturn($pathIsResolved ? $filePath : FALSE);
$file = $this
->createMock(FileInterface::class);
$file
->expects($this
->atLeastOnce())
->method('getFileUri')
->willReturn('public://fileuri');
if ($filePathExists) {
$file
->expects($this
->atLeastOnce())
->method('setSize')
->with(filesize($filePath));
$mocker = $file
->expects($this
->atLeastOnce())
->method('save');
if (!$fileSaveIsSuccessful) {
$mocker
->willThrowException(new EntityStorageException());
}
}
return [
$file,
$filePath,
];
}
}