Browse Source

fix and complete unit tests

Olivier Massot 5 months ago
parent
commit
be10143e3d

+ 42 - 3
tests/Unit/Service/Doctrine/FiltersConfigurationServiceTest.php

@@ -44,6 +44,11 @@ class TestableFiltersConfigurationService extends FiltersConfigurationService
     {
         return $this->previousTimeConstraintState;
     }
+
+    public function setFiltersConfigured(bool $value): void
+    {
+        $this->filtersConfigured = $value;
+    }
 }
 
 class FiltersConfigurationServiceTest extends TestCase
@@ -64,7 +69,7 @@ class FiltersConfigurationServiceTest extends TestCase
         return $this
             ->getMockBuilder(TestableFiltersConfigurationService::class)
             ->setConstructorArgs([$this->em, $this->dateTimeConstraint, $this->activityYearConstraint])
-            ->setMethodsExcept([$methodName, 'getPreviousTimeConstraintState', 'setPreviousTimeConstraintState'])
+            ->setMethodsExcept([$methodName, 'getPreviousTimeConstraintState', 'setPreviousTimeConstraintState', 'setFiltersConfigured'])
             ->getMock();
     }
 
@@ -117,6 +122,7 @@ class FiltersConfigurationServiceTest extends TestCase
     public function testSuspendTimeConstraintFilters(): void
     {
         $filterConfigurationService = $this->getFiltersConfigurationServiceMockFor('suspendTimeConstraintFilters');
+        $filterConfigurationService->setFiltersConfigured(true);
 
         $filterConfigurationService->expects(self::once())->method('timeFiltersAlreadyDisabled')->willReturn(false);
         $filterConfigurationService->expects(self::exactly(2))->method('disableFilter')->withConsecutive(['date_time_filter'], ['activity_year_filter']);
@@ -132,11 +138,12 @@ class FiltersConfigurationServiceTest extends TestCase
     public function testSuspendTimeConstraintFiltersAlreadySuspended(): void
     {
         $filterConfigurationService = $this->getFiltersConfigurationServiceMockFor('suspendTimeConstraintFilters');
+        $filterConfigurationService->setFiltersConfigured(true);
 
         $filterConfigurationService->setPreviousTimeConstraintState(true);
 
         $this->expectException(\RuntimeException::class);
-        $this->expectExceptionMessage('time constraints is already suspended');
+        $this->expectExceptionMessage('The time constraints are already suspended');
 
         $filterConfigurationService->suspendTimeConstraintFilters();
     }
@@ -144,6 +151,7 @@ class FiltersConfigurationServiceTest extends TestCase
     public function testSuspendTimeConstraintFiltersAlreadyDisabled(): void
     {
         $filterConfigurationService = $this->getFiltersConfigurationServiceMockFor('suspendTimeConstraintFilters');
+        $filterConfigurationService->setFiltersConfigured(true);
 
         $filterConfigurationService->expects(self::once())->method('timeFiltersAlreadyDisabled')->willReturn(true);
 
@@ -237,6 +245,7 @@ class FiltersConfigurationServiceTest extends TestCase
     public function testRestoreTimeConstraintFilters(): void
     {
         $filterConfigurationService = $this->getFiltersConfigurationServiceMockFor('restoreTimeConstraintFilters');
+        $filterConfigurationService->setFiltersConfigured(true);
 
         $filterConfigurationService->setPreviousTimeConstraintState(true);
 
@@ -253,10 +262,40 @@ class FiltersConfigurationServiceTest extends TestCase
     public function testRestoreTimeConstraintFiltersNotAlreadySuspended(): void
     {
         $filterConfigurationService = $this->getFiltersConfigurationServiceMockFor('restoreTimeConstraintFilters');
+        $filterConfigurationService->setFiltersConfigured(true);
 
         $this->expectException(\RuntimeException::class);
-        $this->expectExceptionMessage('time constraints has not been suspended, can not be restored');
+        $this->expectExceptionMessage('The time constraints have not been suspended, can not be restored');
 
         $filterConfigurationService->restoreTimeConstraintFilters();
     }
+
+    public function testSuspendTimeConstraintFiltersWhenFiltersNotConfigured(): void
+    {
+        $filterConfigurationService = $this->getFiltersConfigurationServiceMockFor('suspendTimeConstraintFilters');
+        $filterConfigurationService->setFiltersConfigured(false);
+
+        // No methods should be called when filtersConfigured is false
+        $filterConfigurationService->expects(self::never())->method('timeFiltersAlreadyDisabled');
+        $filterConfigurationService->expects(self::never())->method('disableFilter');
+
+        $filterConfigurationService->suspendTimeConstraintFilters();
+
+        // previousTimeConstraintState should remain null
+        $this->assertNull($filterConfigurationService->getPreviousTimeConstraintState());
+    }
+
+    public function testRestoreTimeConstraintFiltersWhenFiltersNotConfigured(): void
+    {
+        $filterConfigurationService = $this->getFiltersConfigurationServiceMockFor('restoreTimeConstraintFilters');
+        $filterConfigurationService->setFiltersConfigured(false);
+
+        // No methods should be called when filtersConfigured is false
+        $filterConfigurationService->expects(self::never())->method('enableFilter');
+
+        $filterConfigurationService->restoreTimeConstraintFilters();
+
+        // previousTimeConstraintState should remain null
+        $this->assertNull($filterConfigurationService->getPreviousTimeConstraintState());
+    }
 }

+ 2 - 2
tests/Unit/Service/File/Storage/ApiLegacyStorageTest.php

@@ -75,7 +75,7 @@ class ApiLegacyStorageTest extends TestCase
         $this->apiLegacyRequestService
             ->expects(self::once())
             ->method('getContent')
-            ->with('api/files/123/download/md?relativePath=1')
+            ->with('api/public/files/123/download/md?relativePath=1')
             ->willReturn('xyz');
 
         $this->assertEquals(
@@ -94,7 +94,7 @@ class ApiLegacyStorageTest extends TestCase
         $this->apiLegacyRequestService
             ->expects(self::once())
             ->method('getContent')
-            ->with('api/files/123/download/lg?relativePath=1')
+            ->with('api/public/files/123/download/lg?relativePath=1')
             ->willReturn('xyz');
 
         $this->assertEquals(

+ 110 - 3
tests/Unit/Service/Twig/AssetsExtensionTest.php

@@ -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');
+    }
 }