You are here

class FileHelperTest in SVG Upload Sanitizer 8

Unit test class for the FileHelper class.

@package Drupal\Tests\svg_upload_sanitizer\Unit\Helper

@internal

Hierarchy

Expanded class hierarchy of FileHelperTest

File

tests/src/Unit/Helper/FileHelperTest.php, line 19

Namespace

Drupal\Tests\svg_upload_sanitizer\Unit\Helper
View 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

Namesort descending Modifiers Type Description Overrides
FileHelperTest::$fileHelper private property The file helper to test.
FileHelperTest::$fileSystem private property The mocked file system.
FileHelperTest::$logger private property The logger.
FileHelperTest::prepareUpdateSize private function Prepare the context for the updateSize() method tests.
FileHelperTest::setUp public function Overrides UnitTestCase::setUp
FileHelperTest::testUpdateSize public function
FileHelperTest::testUpdateSizeWhenTheFileCouldNotBeSaved public function
FileHelperTest::testUpdateSizeWhenTheFilePathCouldNotBeResolved public function
FileHelperTest::testUpdateSizeWhenTheFileSizeCouldNotBeGotten public function
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.