diff --git a/.gitignore b/.gitignore index 79aaad1..431ea80 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ reports/ /Icinga /Zend /vendor +test/testdata/views/deleteme* diff --git a/library/Toplevelview/ViewConfig.php b/library/Toplevelview/ViewConfig.php index aac5fd1..3d56bde 100644 --- a/library/Toplevelview/ViewConfig.php +++ b/library/Toplevelview/ViewConfig.php @@ -327,7 +327,6 @@ public function delete($view): void * * @param $view The view to store * @param $force Stores a backup even if the content hasn't changed - * @throws ProgrammingError if the file with timestamp already exists * @throws NotReadableError if the file cannot be read */ protected function storeBackup($view, $force = false): void @@ -338,10 +337,6 @@ protected function storeBackup($view, $force = false): void $ts = (string) time(); $backup = $backup_dir . DIRECTORY_SEPARATOR . $ts . '.' . $view->getFormat(); - if (file_exists($backup)) { - throw new ProgrammingError('History file with timestamp already present: %s', $backup); - } - $existing_file = $this->getConfigDir() . DIRECTORY_SEPARATOR . $view->getName() . '.' . $view->getFormat(); $oldText = file_get_contents($existing_file); diff --git a/test/php/library/Toplevelview/Tree/TLVStatusTest.php b/test/php/library/Toplevelview/Tree/TLVStatusTest.php new file mode 100644 index 0000000..0f976ca --- /dev/null +++ b/test/php/library/Toplevelview/Tree/TLVStatusTest.php @@ -0,0 +1,58 @@ +add('ok', 1); + $this->assertSame('ok', $t->getOverall()); + + $t->add('missing', 1); + $this->assertSame('ok', $t->getOverall()); + + $t->add('critical_handled', 1); + $this->assertSame('critical handled', $t->getOverall()); + + $t->zero(); + $t->add('total'); + + $this->assertSame(1, $t->get('total')); + $this->assertSame(0, $t->get('missing')); + } + + public function testGetterSetter() + { + $t = new TLVStatus(); + $t->set('missing', 123); + $this->assertSame(123, $t->get('missing')); + } + + public function testMerge() + { + $b = new TLVStatus(); + $b->set('ok', 1); + $b->set('missing', 2); + $b->set('warning_handled', 3); + $b->set('critical_unhandled', 4); + + $a = new TLVStatus(); + $a->set('ok', 3); + $a->set('unknown_unhandled', 2); + $a->set('missing', 1); + + $a->merge($b); + $this->assertSame(3, $a->get('missing')); + $this->assertSame(4, $a->get('ok')); + $this->assertSame(3, $a->get('warning_handled')); + $this->assertSame(4, $a->get('critical_unhandled')); + $this->assertSame(2, $a->get('unknown_unhandled')); + $this->assertSame(null, $a->get('warning_unhandled')); + } +} diff --git a/test/php/library/Toplevelview/ViewConfigTest.php b/test/php/library/Toplevelview/ViewConfigTest.php index a278edb..f165781 100644 --- a/test/php/library/Toplevelview/ViewConfigTest.php +++ b/test/php/library/Toplevelview/ViewConfigTest.php @@ -3,6 +3,7 @@ namespace Tests\Icinga\Module\Toplevelview; use Icinga\Module\Toplevelview\ViewConfig; +use Icinga\Module\Toplevelview\Model\View; use Icinga\Exception\NotReadableError; use Icinga\Exception\NotFoundError; @@ -18,6 +19,34 @@ public function testViewConfigWithNoSuchView() $view = $c->loadByName('nosuchview'); } + public function testViewConfigDelete() + { + $c = new ViewConfig('test/testdata'); + $v = new View('deleteme', 'yml'); + + // Generate the file + $c->storeToFile($v); + $this->assertFileExists('test/testdata/views/deleteme.yml'); + // Generate the backup dir + $c->storeToFile($v); + $this->assertDirectoryExists('test/testdata/views/deleteme'); + // Delete the file + $c->delete($v); + $this->assertFalse(file_exists('test/testdata/views/deleteme.yml')); + + // Remove generated files afterwards + array_map('unlink', array_filter((array) glob("test/testdata/views/deleteme/*"))); + rmdir('test/testdata/views/deleteme/'); + } + + public function testViewConfigLoadAll() + { + $c = new ViewConfig('test/testdata'); + $views = $c->loadAll(); + + $this->assertArrayHasKey('example', $views); + } + public function testViewConfigWithNoError() { $c = new ViewConfig('test/testdata');