PdfEncoderTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. use App\Service\Export\Encoder\PdfEncoder;
  3. use Knp\Snappy\Pdf;
  4. use PHPUnit\Framework\MockObject\MockObject;
  5. use PHPUnit\Framework\TestCase;
  6. class PdfEncoderTest extends TestCase
  7. {
  8. private MockObject | Pdf $knpSnappy;
  9. public function setUp(): void
  10. {
  11. $this->knpSnappy = $this->getMockBuilder(Pdf::class)->getMock();
  12. }
  13. public function testSupport(): void
  14. {
  15. $encoder = $this->getMockBuilder(PdfEncoder::class)
  16. ->setConstructorArgs([$this->knpSnappy])
  17. ->setMethodsExcept(['support'])
  18. ->getMock();
  19. $this->assertTrue($encoder->support('pdf'));
  20. $this->assertFalse($encoder->support('txt'));
  21. }
  22. public function testGetDefaultOptions(): void
  23. {
  24. $encoder = $this->getMockBuilder(PdfEncoder::class)
  25. ->setConstructorArgs([$this->knpSnappy])
  26. ->setMethodsExcept(['getDefaultOptions'])
  27. ->getMock();
  28. $this->assertIsArray($encoder->getDefaultOptions());
  29. }
  30. public function testEncode(): void
  31. {
  32. $encoder = $this->getMockBuilder(PdfEncoder::class)
  33. ->setConstructorArgs([$this->knpSnappy])
  34. ->setMethodsExcept(['encode'])
  35. ->getMock();
  36. $encoder->method('getDefaultOptions')->willReturn(['defaultOption' => 1]);
  37. $this->knpSnappy
  38. ->expects(self::once())
  39. ->method('getOutputFromHtml')
  40. ->with('<div>content</div>', ['defaultOption' => 1, 'additionalOption' => 2])
  41. ->willReturn('%%encoded%%');
  42. $this->assertEquals(
  43. '%%encoded%%',
  44. $encoder->encode('<div>content</div>', ['additionalOption' => 2])
  45. );
  46. }
  47. }