class PDFPreviewGeneratorTest in PDFPreview 8
Same name and namespace in other branches
- 2.0.x tests/src/Unit/PdfPreviewGeneratorTest.php \Drupal\Tests\pdfpreview\Unit\PdfPreviewGeneratorTest
@coversDefaultClass \Drupal\pdfpreview\PDFPreviewGenerator
@group pdfpreview
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\pdfpreview\Unit\PDFPreviewGeneratorTest
Expanded class hierarchy of PDFPreviewGeneratorTest
File
- tests/
src/ Unit/ PDFPreviewGeneratorTest.php, line 16
Namespace
Drupal\Tests\pdfpreview\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PDFPreviewGeneratorTest:: |
protected | property | The config factory. | |
PDFPreviewGeneratorTest:: |
protected | function | Gets a mocked file for testing. | |
PDFPreviewGeneratorTest:: |
protected | function | Gets a mocked PDF Preview Generator for testing. | |
PDFPreviewGeneratorTest:: |
public | function |
Overrides UnitTestCase:: |
|
PDFPreviewGeneratorTest:: |
public | function | @covers ::getDestinationURI | |
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. |