ToBase64ExtensionTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\Twig;
  4. use App\Service\Twig\ToBase64Extension;
  5. use Path\Path;
  6. use PHPUnit\Framework\MockObject\MockObject;
  7. use PHPUnit\Framework\TestCase;
  8. use Twig\TwigFilter;
  9. class ToBase64ExtensionTest extends TestCase
  10. {
  11. private string $projectDir = '/test/project';
  12. private function getToBase64ExtensionMockFor(string $methodName): ToBase64Extension|MockObject
  13. {
  14. return $this->getMockBuilder(ToBase64Extension::class)
  15. ->setConstructorArgs([$this->projectDir])
  16. ->setMethodsExcept([$methodName])
  17. ->getMock();
  18. }
  19. /**
  20. * @see ToBase64Extension::getFilters()
  21. */
  22. public function testGetFilters(): void
  23. {
  24. $extension = $this->getToBase64ExtensionMockFor('getFilters');
  25. $filters = $extension->getFilters();
  26. $this->assertIsArray($filters);
  27. $this->assertCount(1, $filters);
  28. $this->assertInstanceOf(TwigFilter::class, $filters[0]);
  29. $this->assertSame('img_to_base64', $filters[0]->getName());
  30. // Test that the callable is correctly set
  31. $callable = $filters[0]->getCallable();
  32. $this->assertIsArray($callable);
  33. $this->assertSame($extension, $callable[0]);
  34. $this->assertSame('imgToBase64', $callable[1]);
  35. }
  36. /**
  37. * @see ToBase64Extension::imgToBase64()
  38. */
  39. public function testImgToBase64WithNonExistentFile(): void
  40. {
  41. $extension = $this->getToBase64ExtensionMockFor('imgToBase64');
  42. // Test with a non-existent file - this should return empty string without file operations
  43. $result = $extension->imgToBase64('non-existent-image.jpg');
  44. $this->assertSame('', $result);
  45. }
  46. }