You are here

class PDFPreviewGeneratorTest in PDFPreview 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/PdfPreviewGeneratorTest.php \Drupal\Tests\pdfpreview\Unit\PdfPreviewGeneratorTest

@coversDefaultClass \Drupal\pdfpreview\PDFPreviewGenerator

@group pdfpreview

Hierarchy

Expanded class hierarchy of PDFPreviewGeneratorTest

File

tests/src/Unit/PDFPreviewGeneratorTest.php, line 16

Namespace

Drupal\Tests\pdfpreview\Unit
View source
class PDFPreviewGeneratorTest extends UnitTestCase {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $configFactory;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->configFactory = $this
      ->prophesize(ConfigFactoryInterface::class);
  }

  /**
   * @covers ::getDestinationURI
   */
  public function testGetDestinationUri() {
    $filename = 'Test File.pdf';
    $id = '1234';
    $md5 = '117aa294817277a5c090bf18d515d885';
    $file = $this
      ->getFileMock($filename, $id);
    $generator = $this
      ->getPdfPreviewGeneratorMock();
    $reflection = new \ReflectionClass(PDFPreviewGenerator::class);
    $method = $reflection
      ->getMethod('getDestinationURI');
    $method
      ->setAccessible(TRUE);
    $file_config = $this
      ->prophesize(ImmutableConfig::class);
    $this->configFactory
      ->get('system.file')
      ->willReturn($file_config
      ->reveal());
    $file_config
      ->get('default_scheme')
      ->willReturn('public');
    $config = $this
      ->prophesize(ImmutableConfig::class);
    $this->configFactory
      ->get('pdfpreview.settings')
      ->willReturn($config
      ->reveal());
    $config
      ->get('path')
      ->willReturn('pdfpreview');

    // MD5 filename, JPG.
    $config
      ->get('filenames')
      ->willReturn('md5');
    $config
      ->get('type')
      ->willReturn('jpg');
    $filename_md5_jpg = 'public://pdfpreview/' . $md5 . '.jpg';
    $this
      ->assertEquals($filename_md5_jpg, $method
      ->invoke($generator, $file));

    // MD5 filename, PNG.
    $config
      ->get('type')
      ->willReturn('png');
    $filename_md5_png = 'public://pdfpreview/' . $md5 . '.png';
    $this
      ->assertEquals($filename_md5_png, $method
      ->invoke($generator, $file));

    // Human filename, PNG.
    $config
      ->get('filenames')
      ->willReturn('human');
    $filename_human_png = 'public://pdfpreview/1234-test-file.png';
    $this
      ->assertEquals($filename_human_png, $method
      ->invoke($generator, $file));
  }

  /**
   * Gets a mocked PDF Preview Generator for testing.
   *
   * @return \Drupal\pdfpreview\PDFPreviewGenerator
   *   Mocked PDF Preview Generator.
   */
  protected function getPdfPreviewGeneratorMock() {
    $file_system = $this
      ->getMockBuilder('\\Drupal\\Core\\File\\FileSystem')
      ->disableOriginalConstructor()
      ->getMock();
    $file_system
      ->expects($this
      ->any())
      ->method('basename')
      ->with('public://Test File.pdf', '.pdf')
      ->willReturn('Test File');
    $transliteration = $this
      ->getMockBuilder(TransliterationInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $transliteration
      ->expects($this
      ->any())
      ->method('transliterate')
      ->with('Test File', 'en')
      ->willReturn('test-file');
    $image_toolkit_manager = $this
      ->createMock('\\Drupal\\Core\\ImageToolkit\\ImageToolkitManager');
    $language = $this
      ->getMockBuilder('Drupal\\Core\\Language\\Language')
      ->disableOriginalConstructor()
      ->getMock();
    $language
      ->expects($this
      ->any())
      ->method('getId')
      ->willReturn('en');
    $language_manager = $this
      ->getMockBuilder('Drupal\\Core\\Language\\LanguageManager')
      ->disableOriginalConstructor()
      ->getMock();
    $language_manager
      ->expects($this
      ->any())
      ->method('getCurrentLanguage')
      ->willReturn($language);
    $generator = $this
      ->getMockBuilder('\\Drupal\\pdfpreview\\PDFPreviewGenerator')
      ->setConstructorArgs([
      $this->configFactory
        ->reveal(),
      $file_system,
      $transliteration,
      $image_toolkit_manager,
      $language_manager,
    ])
      ->getMock();
    return $generator;
  }

  /**
   * Gets a mocked file for testing.
   *
   * @param string $filename
   *   The filename.
   * @param int $id
   *   The file id.
   *
   * @return \Drupal\file\FileInterface
   *   The mocked file.
   */
  protected function getFileMock($filename, $id) {
    $methods = [
      'id',
      'getFileUri',
    ];
    $file = $this
      ->getMockBuilder('\\Drupal\\file\\Entity\\File')
      ->disableOriginalConstructor()
      ->setMethods($methods)
      ->getMock();
    $file
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn($id);
    $file
      ->expects($this
      ->any())
      ->method('getFileUri')
      ->willReturn('public://' . $filename);
    return $file;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PDFPreviewGeneratorTest::$configFactory protected property The config factory.
PDFPreviewGeneratorTest::getFileMock protected function Gets a mocked file for testing.
PDFPreviewGeneratorTest::getPdfPreviewGeneratorMock protected function Gets a mocked PDF Preview Generator for testing.
PDFPreviewGeneratorTest::setUp public function Overrides UnitTestCase::setUp
PDFPreviewGeneratorTest::testGetDestinationUri public function @covers ::getDestinationURI
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.