ToBase64ExtensionTest.php 1.7 KB

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