PDFPreviewGeneratorTest.php in PDFPreview 8
File
tests/src/Unit/PDFPreviewGeneratorTest.php
View source
<?php
namespace Drupal\Tests\pdfpreview\Unit;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\pdfpreview\PDFPreviewGenerator;
use Drupal\Tests\UnitTestCase;
class PDFPreviewGeneratorTest extends UnitTestCase {
protected $configFactory;
public function setUp() {
parent::setUp();
$this->configFactory = $this
->prophesize(ConfigFactoryInterface::class);
}
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');
$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));
$config
->get('type')
->willReturn('png');
$filename_md5_png = 'public://pdfpreview/' . $md5 . '.png';
$this
->assertEquals($filename_md5_png, $method
->invoke($generator, $file));
$config
->get('filenames')
->willReturn('human');
$filename_human_png = 'public://pdfpreview/1234-test-file.png';
$this
->assertEquals($filename_human_png, $method
->invoke($generator, $file));
}
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;
}
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;
}
}