PagePdfTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of Chrome PHP.
  4. *
  5. * (c) Soufiane Ghzal <sghzal@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace HeadlessChromium\Test;
  11. use HeadlessChromium\PageUtils\PagePdf;
  12. /**
  13. * @covers \HeadlessChromium\PagePdf
  14. */
  15. class PagePdfTest extends BaseTestCase
  16. {
  17. private const TYPES_STRING = [
  18. 'string',
  19. '',
  20. ];
  21. private const TYPES_NUMERIC = [
  22. 1,
  23. 1.1,
  24. ];
  25. private const TYPES_BOOLEAN = [
  26. true,
  27. false,
  28. ];
  29. /**
  30. * @var PagePdfForTests
  31. */
  32. private $pagePdf;
  33. /**
  34. * @before
  35. */
  36. public function createEmptyPagePdf(): void
  37. {
  38. $this->pagePdf = new PagePdfForTests();
  39. }
  40. public function invalidPdfOptionsProvider(): array
  41. {
  42. return \array_merge(
  43. $this->getOptionsDataset('landscape', self::TYPES_STRING),
  44. $this->getOptionsDataset('headerTemplate', self::TYPES_NUMERIC),
  45. $this->getOptionsDataset('scale', self::TYPES_STRING),
  46. [['headerTemplate', new \stdClass()]],
  47. [['footerTemplate', []]],
  48. [['unknown_field', 1]],
  49. );
  50. }
  51. public function validPdfOptionsProvider(): array
  52. {
  53. return \array_merge(
  54. $this->getOptionsDataset('landscape', self::TYPES_BOOLEAN),
  55. $this->getOptionsDataset('printBackground', self::TYPES_BOOLEAN),
  56. $this->getOptionsDataset('displayHeaderFooter', self::TYPES_BOOLEAN),
  57. $this->getOptionsDataset('headerTemplate', self::TYPES_STRING),
  58. $this->getOptionsDataset('footerTemplate', self::TYPES_STRING),
  59. $this->getOptionsDataset('paperWidth', self::TYPES_NUMERIC),
  60. $this->getOptionsDataset('paperHeight', self::TYPES_NUMERIC),
  61. $this->getOptionsDataset('marginTop', self::TYPES_NUMERIC),
  62. $this->getOptionsDataset('marginBottom', self::TYPES_NUMERIC),
  63. $this->getOptionsDataset('marginLeft', self::TYPES_NUMERIC),
  64. $this->getOptionsDataset('marginRight', self::TYPES_NUMERIC),
  65. $this->getOptionsDataset('pageRanges', self::TYPES_STRING),
  66. $this->getOptionsDataset('ignoreInvalidPageRanges', self::TYPES_BOOLEAN),
  67. $this->getOptionsDataset('preferCSSPageSize', self::TYPES_BOOLEAN),
  68. $this->getOptionsDataset('scale', self::TYPES_NUMERIC),
  69. );
  70. }
  71. /**
  72. * @dataProvider invalidPdfOptionsProvider
  73. */
  74. public function testInvalidOptions(string $optionName, $optionValue): void
  75. {
  76. $this->expectException(\InvalidArgumentException::class);
  77. $this->pagePdf->setOptions([$optionName => $optionValue]);
  78. }
  79. /**
  80. * @dataProvider validPdfOptionsProvider
  81. */
  82. public function testValidOptions(string $optionName, $optionValue): void
  83. {
  84. self::assertInstanceOf(PagePdf::class, $this->pagePdf->setOptions([$optionName => $optionValue]));
  85. }
  86. private function getOptionsDataset(string $optionName, array $optionValues): array
  87. {
  88. return \array_reduce(
  89. $optionValues,
  90. function ($carry, $value) use ($optionName) {
  91. $carry[] = [$optionName, $value];
  92. return $carry;
  93. },
  94. []
  95. );
  96. }
  97. }