PdfEncoderTest.php 1.9 KB

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