You are here

class EntityPrintTest in Entity Print 8

Same name in this branch
  1. 8 src/Tests/EntityPrintTest.php \Drupal\entity_print\Tests\EntityPrintTest
  2. 8 tests/src/Unit/EntityPrintTest.php \Drupal\Tests\entity_print\Unit\EntityPrintTest

@coversDefaultClass \Drupal\entity_print\EntityPrintPdfBuilder @group entity_print

Hierarchy

Expanded class hierarchy of EntityPrintTest

File

tests/src/Unit/EntityPrintTest.php, line 11

Namespace

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

Namesort descending Modifiers Type Description Overrides
EntityPrintTest::generateFilenameDataProvider public function Get the data for testing filename generation.
EntityPrintTest::generateMultipleFilenameDataProvider public function Data provider for multiple filename generation.
EntityPrintTest::getMockEntity protected function Get a mock entity for testing.
EntityPrintTest::getMockPdfBuilder protected function Get a mock pdf builder.
EntityPrintTest::testGenerateFilename public function Test safe file generation.
EntityPrintTest::testGenerateMultipleFilename public function Test multiple file generation.
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.
UnitTestCase::setUp protected function 340