| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- 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;
- 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();
- }
- public function testGetFunctions(): void
- {
- $assetsExtension = $this
- ->getMockBuilder(AssetsExtension::class)
- ->setConstructorArgs([$this->projectDir, $this->fileManager])
- ->setMethodsExcept(['getFunctions'])
- ->getMock();
- $functions = $assetsExtension->getFunctions();
- $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->projectDir, $this->fileManager])
- ->setMethodsExcept(['absPath'])
- ->getMock();
- $publicDir = PathUtils::getProjectDir().'/public';
- $this->assertEquals(
- $publicDir.'/foo',
- $assetsExtension->absPath('foo')
- );
- $this->assertEquals(
- $publicDir.'/foo/bar',
- $assetsExtension->absPath('foo/bar')
- );
- $this->assertEquals(
- $publicDir.'/',
- $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');
- }
- }
|