AssetsExtensionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Tests\Unit\Service\Twig;
  3. use App\Entity\Core\File;
  4. use App\Service\File\FileManager;
  5. use App\Service\Twig\AssetsExtension;
  6. use App\Service\Utils\PathUtils;
  7. use PHPUnit\Framework\TestCase;
  8. class AssetsExtensionTest extends TestCase
  9. {
  10. private FileManager $fileManager;
  11. private string $projectDir;
  12. public function setUp(): void
  13. {
  14. $this->projectDir = PathUtils::getProjectDir();
  15. $this->fileManager = $this->getMockBuilder(FileManager::class)->disableOriginalConstructor()->getMock();
  16. }
  17. public function testGetFunctions(): void
  18. {
  19. $assetsExtension = $this
  20. ->getMockBuilder(AssetsExtension::class)
  21. ->setConstructorArgs([$this->projectDir, $this->fileManager])
  22. ->setMethodsExcept(['getFunctions'])
  23. ->getMock();
  24. $functions = $assetsExtension->getFunctions();
  25. $this->assertCount(4, $functions);
  26. $this->assertEquals('absPath', $functions[0]->getName());
  27. $this->assertEquals('fileImagePath', $functions[1]->getName());
  28. $this->assertEquals('asset_absolute', $functions[2]->getName());
  29. $this->assertEquals('asset_base64', $functions[3]->getName());
  30. }
  31. public function testAbsPath(): void
  32. {
  33. $assetsExtension = $this
  34. ->getMockBuilder(AssetsExtension::class)
  35. ->setConstructorArgs([$this->projectDir, $this->fileManager])
  36. ->setMethodsExcept(['absPath'])
  37. ->getMock();
  38. $publicDir = PathUtils::getProjectDir().'/public';
  39. $this->assertEquals(
  40. $publicDir.'/foo',
  41. $assetsExtension->absPath('foo')
  42. );
  43. $this->assertEquals(
  44. $publicDir.'/foo/bar',
  45. $assetsExtension->absPath('foo/bar')
  46. );
  47. $this->assertEquals(
  48. $publicDir.'/',
  49. $assetsExtension->absPath('')
  50. );
  51. }
  52. public function testFileImagePath(): void
  53. {
  54. $file = $this->getMockBuilder(File::class)->disableOriginalConstructor()->getMock();
  55. $size = 'sm';
  56. $expectedUrl = '/path/to/image.jpg';
  57. $this->fileManager
  58. ->expects($this->once())
  59. ->method('getImageUrl')
  60. ->with($file, $size, true)
  61. ->willReturn($expectedUrl);
  62. $assetsExtension = new AssetsExtension($this->projectDir, $this->fileManager);
  63. $this->assertEquals(
  64. ltrim($expectedUrl, '/'),
  65. $assetsExtension->fileImagePath($file, $size)
  66. );
  67. }
  68. public function testGetAssetAbsolutePath(): void
  69. {
  70. $assetsExtension = new AssetsExtension($this->projectDir, $this->fileManager);
  71. $publicDir = $this->projectDir.'/public';
  72. // Test with path starting with /
  73. $this->assertEquals(
  74. $publicDir.'/images/logo.png',
  75. $assetsExtension->getAssetAbsolutePath('/images/logo.png')
  76. );
  77. // Test with path not starting with /
  78. $this->assertEquals(
  79. $publicDir.'/images/logo.png',
  80. $assetsExtension->getAssetAbsolutePath('images/logo.png')
  81. );
  82. // Test with empty path
  83. $this->assertEquals(
  84. $publicDir.'/',
  85. $assetsExtension->getAssetAbsolutePath('')
  86. );
  87. }
  88. public function testGetAssetBase64(): void
  89. {
  90. // Create a mock for AssetsExtension with partial methods
  91. $assetsExtension = $this->getMockBuilder(AssetsExtension::class)
  92. ->setConstructorArgs([$this->projectDir, $this->fileManager])
  93. ->onlyMethods(['getAssetAbsolutePath'])
  94. ->getMock();
  95. // Set up the mock to return a specific path
  96. $testFilePath = sys_get_temp_dir().'/test_image.png';
  97. $assetsExtension
  98. ->method('getAssetAbsolutePath')
  99. ->willReturn($testFilePath);
  100. // Create a test file
  101. $imageContent = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==');
  102. file_put_contents($testFilePath, $imageContent);
  103. try {
  104. // Test the method
  105. $result = $assetsExtension->getAssetBase64('test_image.png');
  106. // Check the result
  107. $this->assertStringStartsWith('data:image/png;base64,', $result);
  108. $this->assertStringEndsWith(base64_encode($imageContent), $result);
  109. } finally {
  110. // Clean up
  111. if (file_exists($testFilePath)) {
  112. unlink($testFilePath);
  113. }
  114. }
  115. }
  116. public function testGetAssetBase64WithNonExistentFile(): void
  117. {
  118. // Create a mock for AssetsExtension with partial methods
  119. $assetsExtension = $this->getMockBuilder(AssetsExtension::class)
  120. ->setConstructorArgs([$this->projectDir, $this->fileManager])
  121. ->onlyMethods(['getAssetAbsolutePath'])
  122. ->getMock();
  123. // Set up the mock to return a non-existent file path
  124. $nonExistentFilePath = sys_get_temp_dir().'/non_existent_file.png';
  125. $assetsExtension->method('getAssetAbsolutePath')
  126. ->willReturn($nonExistentFilePath);
  127. // Make sure the file doesn't exist
  128. if (file_exists($nonExistentFilePath)) {
  129. unlink($nonExistentFilePath);
  130. }
  131. // Test that an exception is thrown
  132. $this->expectException(\RuntimeException::class);
  133. $this->expectExceptionMessage("Asset not found: {$nonExistentFilePath}");
  134. $assetsExtension->getAssetBase64('non_existent_file.png');
  135. }
  136. }