class FileHelperTest in SVG Upload Sanitizer 8
Unit test class for the FileHelper class.
@package Drupal\Tests\svg_upload_sanitizer\Unit\Helper
@internal
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\svg_upload_sanitizer\Unit\Helper\FileHelperTest
Expanded class hierarchy of FileHelperTest
File
- tests/
src/ Unit/ Helper/ FileHelperTest.php, line 19
Namespace
Drupal\Tests\svg_upload_sanitizer\Unit\HelperView source
class FileHelperTest extends UnitTestCase {
/**
* The mocked file system.
*
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $fileSystem;
/**
* The logger.
*
* @var \Drupal\Tests\svg_upload_sanitizer\TestLogger
*/
private $logger;
/**
* The file helper to test.
*
* @var \Drupal\svg_upload_sanitizer\Helper\FileHelper
*/
private $fileHelper;
/**
* {@inheritdoc}
*/
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));
}
/**
* Prepare the context for the updateSize() method tests.
*
* @param bool $pathIsResolved
* TRUE if the file path was resolved, FALSE otherwise.
* @param bool $filePathExists
* TRUE if the file path exists, FALSE otherwise.
* @param bool $fileSaveIsSuccessful
* TRUE if the file save was successful, FALSE otherwise.
*
* @return \PHPUnit\Framework\MockObject\MockObject
* A mocked optimizer.
*/
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,
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileHelperTest:: |
private | property | The file helper to test. | |
FileHelperTest:: |
private | property | The mocked file system. | |
FileHelperTest:: |
private | property | The logger. | |
FileHelperTest:: |
private | function | Prepare the context for the updateSize() method tests. | |
FileHelperTest:: |
public | function |
Overrides UnitTestCase:: |
|
FileHelperTest:: |
public | function | ||
FileHelperTest:: |
public | function | ||
FileHelperTest:: |
public | function | ||
FileHelperTest:: |
public | function | ||
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |