class EntityPrintTest in Entity Print 8
Same name in this branch
- 8 src/Tests/EntityPrintTest.php \Drupal\entity_print\Tests\EntityPrintTest
- 8 tests/src/Unit/EntityPrintTest.php \Drupal\Tests\entity_print\Unit\EntityPrintTest
@coversDefaultClass \Drupal\entity_print\EntityPrintPdfBuilder @group entity_print
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\entity_print\Unit\EntityPrintTest
Expanded class hierarchy of EntityPrintTest
File
- tests/
src/ Unit/ EntityPrintTest.php, line 11
Namespace
Drupal\Tests\entity_print\UnitView source
class EntityPrintTest extends UnitTestCase {
/**
* Test safe file generation.
*
* @covers ::generateFilename
* @dataProvider generateFilenameDataProvider
*/
public function testGenerateFilename($entity_label, $expected_filename) {
$entity = $this
->getMockEntity($entity_label);
$pdf_builder = $this
->getMockPdfBuilder();
$reflection = new \ReflectionClass($pdf_builder);
$method = $reflection
->getMethod('generateFilename');
$method
->setAccessible(true);
$this
->assertEquals($expected_filename, $method
->invoke($pdf_builder, $entity));
}
/**
* Test multiple file generation.
*
* @covers ::generateMultiFilename
* @dataProvider generateMultipleFilenameDataProvider
*/
public function testGenerateMultipleFilename($entity_labels, $expected_filename) {
$entities = [];
foreach ($entity_labels as $entity_label) {
$entities[] = $this
->getMockEntity($entity_label);
}
$pdf_builder = $this
->getMockPdfBuilder();
$reflection = new \ReflectionClass($pdf_builder);
$method = $reflection
->getMethod('generateMultiFilename');
$method
->setAccessible(true);
$this
->assertEquals($expected_filename, $method
->invoke($pdf_builder, $entities));
}
/**
* Get the data for testing filename generation.
*
* @return array
* An array of data rows for testing filename generation.
*/
public function generateFilenameDataProvider() {
return [
// $node_title, $expected_filename.
[
'Random Node Title',
'Random Node Title.pdf',
],
[
'Title -=with special chars&*#',
'Title with special chars.pdf',
],
[
'Title 5 with Nums 2',
'Title 5 with Nums 2.pdf',
],
];
}
/**
* Data provider for multiple filename generation.
*
* @return array
* An array of data rows for testing filename generation.
*/
public function generateMultipleFilenameDataProvider() {
return [
[
[
'Node Title1',
'Node Title2',
'Node Title3',
],
'Node Title1-Node Title2-Node Title3.pdf',
],
[
[
'Title1 -=with special chars&*#',
'Title2 -=with special chars&*#',
'Title3 -=with special chars&*#',
],
'Title1 with special chars-Title2 with special chars-Title3 with special chars.pdf',
],
];
}
/**
* Get a mock pdf builder.
*
* @return \Drupal\entity_print\EntityPrintPdfBuilder
* The entity pdf builder mock.
*/
protected function getMockPdfBuilder() {
$pdf_builder = $this
->getMockBuilder('Drupal\\entity_print\\EntityPrintPdfBuilder')
->disableOriginalConstructor()
->setMethods([])
->getMock();
return $pdf_builder;
}
/**
* Get a mock entity for testing.
*
* @param string $entity_label
* (optional) The label title for the entity.
*
* @return \PHPUnit_Framework_MockObject_MockObject
* The content entity mock.
*/
protected function getMockEntity($entity_label = '') {
$entity = $this
->getMock('Drupal\\Core\\Entity\\EntityInterface');
if ($entity_label) {
$entity
->expects($this
->any())
->method('label')
->willReturn($entity_label);
}
return $entity;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityPrintTest:: |
public | function | Get the data for testing filename generation. | |
EntityPrintTest:: |
public | function | Data provider for multiple filename generation. | |
EntityPrintTest:: |
protected | function | Get a mock entity for testing. | |
EntityPrintTest:: |
protected | function | Get a mock pdf builder. | |
EntityPrintTest:: |
public | function | Test safe file generation. | |
EntityPrintTest:: |
public | function | Test multiple file generation. | |
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. | |
UnitTestCase:: |
protected | function | 340 |