createdTemplateIds as $id) { $file = TemplateManager::getTemplate($id); if ($file !== null) { $file->delete(); } } $this->createdTemplateIds = []; parent::tearDown(); } private function addTemplateFile(string $name, string $content = "test"): File { $dir = TemplateManager::getGlobalTemplateDir(); $file = $dir->newFile($name, $content); $this->createdTemplateIds[] = $file->getId(); return $file; } /** * Returns a Folder instance pointing to the global template directory. */ public function testGetGlobalTemplateDirReturnsFolder(): void { $dir = TemplateManager::getGlobalTemplateDir(); $this->assertInstanceOf(Folder::class, $dir); } /** * Creates the template directory if it does not already exist. */ public function testGetGlobalTemplateDirCreatesDirectoryIfMissing(): void { $dir = TemplateManager::getGlobalTemplateDir(); $this->assertTrue($dir->isReadable()); } /** * Returns an empty array when the template directory contains no files. */ public function testGetGlobalTemplatesReturnsEmptyWhenNoTemplatesExist(): void { $dir = TemplateManager::getGlobalTemplateDir(); foreach ($dir->getDirectoryListing() as $node) { $node->delete(); } $templates = TemplateManager::getGlobalTemplates(); $this->assertSame([], $templates); } /** * Returns all files in the template directory when no mime filter is applied. */ public function testGetGlobalTemplatesReturnsAllFiles(): void { $this->addTemplateFile("test.docx"); $this->addTemplateFile("test.xlsx"); $templates = TemplateManager::getGlobalTemplates(); $names = array_map(fn($f) => $f->getName(), $templates); $this->assertContains("test.docx", $names); $this->assertContains("test.xlsx", $names); } /** * Returns only files matching the given mime type when a filter is applied. */ public function testGetGlobalTemplatesFiltersByMimeType(): void { $this->addTemplateFile("word.docx"); $this->addTemplateFile("sheet.xlsx"); $templates = TemplateManager::getGlobalTemplates( "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ); $names = array_map(fn($f) => $f->getName(), $templates); $this->assertContains("word.docx", $names); $this->assertNotContains("sheet.xlsx", $names); } /** * Returns null when the template ID is zero. */ public function testGetTemplateReturnsNullForZeroId(): void { $this->assertNull(TemplateManager::getTemplate(0)); } /** * Returns null when no template with the given ID exists. */ public function testGetTemplateReturnsNullForNonExistentId(): void { $this->assertNull(TemplateManager::getTemplate(PHP_INT_MAX)); } /** * Returns the File when a template with the given ID exists. */ public function testGetTemplateReturnsFileWhenExists(): void { $created = $this->addTemplateFile("find-me.docx"); $found = TemplateManager::getTemplate($created->getId()); $this->assertInstanceOf(File::class, $found); $this->assertSame("find-me.docx", $found->getName()); } /** * Returns false for a file ID that is not in the template directory. */ public function testIsTemplateReturnsFalseForNonExistentId(): void { $this->assertFalse(TemplateManager::isTemplate(PHP_INT_MAX)); } /** * Returns true when the given file ID belongs to a template. */ public function testIsTemplateReturnsTrueForKnownTemplate(): void { $file = $this->addTemplateFile("check-me.docx"); $this->assertTrue(TemplateManager::isTemplate($file->getId())); } /** * Returns the binary content of the bundled default docx template. */ public function testGetEmptyTemplateReturnsContentForDocx(): void { $content = TemplateManager::getEmptyTemplate("new.docx"); $this->assertNotFalse($content); $this->assertNotEmpty($content); } /** * Returns the binary content of the bundled default xlsx template. */ public function testGetEmptyTemplateReturnsContentForXlsx(): void { $content = TemplateManager::getEmptyTemplate("new.xlsx"); $this->assertNotFalse($content); $this->assertNotEmpty($content); } /** * Returns the binary content of the bundled default pptx template. */ public function testGetEmptyTemplateReturnsContentForPptx(): void { $content = TemplateManager::getEmptyTemplate("new.pptx"); $this->assertNotFalse($content); $this->assertNotEmpty($content); } /** * Returns false when the requested extension has no bundled template. */ public function testGetEmptyTemplateReturnsFalseForUnknownExtension(): void { $this->assertFalse(TemplateManager::getEmptyTemplate("new.unknown")); } }