|
|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Tests\Unit\Service\Twig;
|
|
|
|
|
|
+use App\Entity\Core\File;
|
|
|
use App\Service\File\FileManager;
|
|
|
use App\Service\Twig\AssetsExtension;
|
|
|
use App\Service\Utils\PathUtils;
|
|
|
@@ -10,9 +11,11 @@ use PHPUnit\Framework\TestCase;
|
|
|
class AssetsExtensionTest extends TestCase
|
|
|
{
|
|
|
private FileManager $fileManager;
|
|
|
+ private string $projectDir;
|
|
|
|
|
|
public function setUp(): void
|
|
|
{
|
|
|
+ $this->projectDir = PathUtils::getProjectDir();
|
|
|
$this->fileManager = $this->getMockBuilder(FileManager::class)->disableOriginalConstructor()->getMock();
|
|
|
}
|
|
|
|
|
|
@@ -20,23 +23,25 @@ class AssetsExtensionTest extends TestCase
|
|
|
{
|
|
|
$assetsExtension = $this
|
|
|
->getMockBuilder(AssetsExtension::class)
|
|
|
- ->setConstructorArgs([$this->fileManager])
|
|
|
+ ->setConstructorArgs([$this->projectDir, $this->fileManager])
|
|
|
->setMethodsExcept(['getFunctions'])
|
|
|
->getMock();
|
|
|
|
|
|
$functions = $assetsExtension->getFunctions();
|
|
|
|
|
|
- $this->assertCount(2, $functions);
|
|
|
+ $this->assertCount(4, $functions);
|
|
|
|
|
|
$this->assertEquals('absPath', $functions[0]->getName());
|
|
|
$this->assertEquals('fileImagePath', $functions[1]->getName());
|
|
|
+ $this->assertEquals('asset_absolute', $functions[2]->getName());
|
|
|
+ $this->assertEquals('asset_base64', $functions[3]->getName());
|
|
|
}
|
|
|
|
|
|
public function testAbsPath(): void
|
|
|
{
|
|
|
$assetsExtension = $this
|
|
|
->getMockBuilder(AssetsExtension::class)
|
|
|
- ->setConstructorArgs([$this->fileManager])
|
|
|
+ ->setConstructorArgs([$this->projectDir, $this->fileManager])
|
|
|
->setMethodsExcept(['absPath'])
|
|
|
->getMock();
|
|
|
|
|
|
@@ -57,4 +62,106 @@ class AssetsExtensionTest extends TestCase
|
|
|
$assetsExtension->absPath('')
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ public function testFileImagePath(): void
|
|
|
+ {
|
|
|
+ $file = $this->getMockBuilder(File::class)->disableOriginalConstructor()->getMock();
|
|
|
+ $size = 'sm';
|
|
|
+ $expectedUrl = '/path/to/image.jpg';
|
|
|
+
|
|
|
+ $this->fileManager
|
|
|
+ ->expects($this->once())
|
|
|
+ ->method('getImageUrl')
|
|
|
+ ->with($file, $size, true)
|
|
|
+ ->willReturn($expectedUrl);
|
|
|
+
|
|
|
+ $assetsExtension = new AssetsExtension($this->projectDir, $this->fileManager);
|
|
|
+
|
|
|
+ $this->assertEquals(
|
|
|
+ ltrim($expectedUrl, '/'),
|
|
|
+ $assetsExtension->fileImagePath($file, $size)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGetAssetAbsolutePath(): void
|
|
|
+ {
|
|
|
+ $assetsExtension = new AssetsExtension($this->projectDir, $this->fileManager);
|
|
|
+ $publicDir = $this->projectDir.'/public';
|
|
|
+
|
|
|
+ // Test with path starting with /
|
|
|
+ $this->assertEquals(
|
|
|
+ $publicDir.'/images/logo.png',
|
|
|
+ $assetsExtension->getAssetAbsolutePath('/images/logo.png')
|
|
|
+ );
|
|
|
+
|
|
|
+ // Test with path not starting with /
|
|
|
+ $this->assertEquals(
|
|
|
+ $publicDir.'/images/logo.png',
|
|
|
+ $assetsExtension->getAssetAbsolutePath('images/logo.png')
|
|
|
+ );
|
|
|
+
|
|
|
+ // Test with empty path
|
|
|
+ $this->assertEquals(
|
|
|
+ $publicDir.'/',
|
|
|
+ $assetsExtension->getAssetAbsolutePath('')
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGetAssetBase64(): void
|
|
|
+ {
|
|
|
+ // Create a mock for AssetsExtension with partial methods
|
|
|
+ $assetsExtension = $this->getMockBuilder(AssetsExtension::class)
|
|
|
+ ->setConstructorArgs([$this->projectDir, $this->fileManager])
|
|
|
+ ->onlyMethods(['getAssetAbsolutePath'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ // Set up the mock to return a specific path
|
|
|
+ $testFilePath = sys_get_temp_dir().'/test_image.png';
|
|
|
+ $assetsExtension
|
|
|
+ ->method('getAssetAbsolutePath')
|
|
|
+ ->willReturn($testFilePath);
|
|
|
+
|
|
|
+ // Create a test file
|
|
|
+ $imageContent = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==');
|
|
|
+ file_put_contents($testFilePath, $imageContent);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // Test the method
|
|
|
+ $result = $assetsExtension->getAssetBase64('test_image.png');
|
|
|
+
|
|
|
+ // Check the result
|
|
|
+ $this->assertStringStartsWith('data:image/png;base64,', $result);
|
|
|
+ $this->assertStringEndsWith(base64_encode($imageContent), $result);
|
|
|
+ } finally {
|
|
|
+ // Clean up
|
|
|
+ if (file_exists($testFilePath)) {
|
|
|
+ unlink($testFilePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testGetAssetBase64WithNonExistentFile(): void
|
|
|
+ {
|
|
|
+ // Create a mock for AssetsExtension with partial methods
|
|
|
+ $assetsExtension = $this->getMockBuilder(AssetsExtension::class)
|
|
|
+ ->setConstructorArgs([$this->projectDir, $this->fileManager])
|
|
|
+ ->onlyMethods(['getAssetAbsolutePath'])
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ // Set up the mock to return a non-existent file path
|
|
|
+ $nonExistentFilePath = sys_get_temp_dir().'/non_existent_file.png';
|
|
|
+ $assetsExtension->method('getAssetAbsolutePath')
|
|
|
+ ->willReturn($nonExistentFilePath);
|
|
|
+
|
|
|
+ // Make sure the file doesn't exist
|
|
|
+ if (file_exists($nonExistentFilePath)) {
|
|
|
+ unlink($nonExistentFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Test that an exception is thrown
|
|
|
+ $this->expectException(\RuntimeException::class);
|
|
|
+ $this->expectExceptionMessage("Asset not found: {$nonExistentFilePath}");
|
|
|
+
|
|
|
+ $assetsExtension->getAssetBase64('non_existent_file.png');
|
|
|
+ }
|
|
|
}
|