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