From 534c8cdbe672d2e6540539d9df256d54d6456210 Mon Sep 17 00:00:00 2001 From: Jens True Date: Mon, 10 Jul 2023 07:47:28 +0000 Subject: [PATCH] More testing. --- Makefile | 2 +- config.example.yml | 2 +- src/CommandBackup.php | 1 + tests/CommandBackupTest.php | 70 ++++++++++++++++++++++++++++++++++--- tests/CommandShowTest.php | 45 ++++++++++++++++++++++++ tests/Rclone/RcloneTest.php | 5 ++- 6 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 tests/CommandShowTest.php diff --git a/Makefile b/Makefile index 207662c..b893394 100644 --- a/Makefile +++ b/Makefile @@ -21,5 +21,5 @@ install-dev: test: vendor/bin/phpunit tests test-coverage: - vendor/bin/phpunit tests --testdox --coverage-filter src --coverage-html output/coverage --coverage-text --path-coverage --testdox-html output/test.html + vendor/bin/phpunit tests --testdox --coverage-filter src --coverage-html output/coverage --coverage-text --branch-coverage --testdox-html output/test.html \ No newline at end of file diff --git a/config.example.yml b/config.example.yml index 51ef4a3..0193593 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,7 +1,7 @@ notification: - type: Ntfy domain: https://ntfy.jcktrue.dk - topic: backup + topic: testing log: output.log rclone: options: diff --git a/src/CommandBackup.php b/src/CommandBackup.php index abb0522..a663bc2 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -75,6 +75,7 @@ class CommandBackup extends Command $message = $render->render('notify', $template); } catch (\Exception $e) { $message = $e->getMessage(); + $sio->error($message); } $notification->send($title, $message); diff --git a/tests/CommandBackupTest.php b/tests/CommandBackupTest.php index a5f8780..22c5c1b 100644 --- a/tests/CommandBackupTest.php +++ b/tests/CommandBackupTest.php @@ -1,4 +1,5 @@ -&1'); + + } + + protected function tearDown(): void + { + exec('rclone purge temp 2>&1', $output); + } + + + public function testBadConfig() + { + $applicationd = new Application('backup', "1.1.1"); + + $applicationd->add(new CommandBackup()); + + + $command = $applicationd->find('backup'); + $commandTester = new CommandTester($command); + $commandTester->execute(['config'=>"bad_file"]); + + $output = $commandTester->getDisplay(); + $this->assertStringContainsString('[ERROR] Configuration error:File "bad_file" does not exist.', $output); + } + + public function testGoodConfig() { $applicationd = new Application('backup', "1.1.1"); @@ -16,9 +46,41 @@ class CommandBackupTest extends \PHPUnit\Framework\TestCase $command = $applicationd->find('backup'); $commandTester = new CommandTester($command); - $commandTester->execute(['config'=>"empty"]); + $commandTester->execute(['config'=>"config.example.yml"]); $output = $commandTester->getDisplay(); - $this->assertStringContainsString('backup', $output); + $this->assertStringContainsString('[OK] Complete ', $output); + } + + public function testNoDestinationFolder() + { + exec('rclone purge temp/destination 2>&1', $output); + $applicationd = new Application('backup', "1.1.1"); + + $applicationd->add(new CommandBackup()); + + + $command = $applicationd->find('backup'); + $commandTester = new CommandTester($command); + $commandTester->execute(['config'=>"config.example.yml"]); + + $output = $commandTester->getDisplay(); + $this->assertStringContainsString('[OK] Complete ', $output); + } + + public function testNoSourceFolder() + { + exec('rclone purge temp/source 2>&1', $output); + $applicationd = new Application('backup', "1.1.1"); + + $applicationd->add(new CommandBackup()); + + + $command = $applicationd->find('backup'); + $commandTester = new CommandTester($command); + $commandTester->execute(['config'=>"config.example.yml"]); + + $output = $commandTester->getDisplay(); + $this->assertStringContainsString('[OK] Complete ', $output); } } \ No newline at end of file diff --git a/tests/CommandShowTest.php b/tests/CommandShowTest.php new file mode 100644 index 0000000..2682fdb --- /dev/null +++ b/tests/CommandShowTest.php @@ -0,0 +1,45 @@ +add(new CommandShow()); + + + $command = $applicationd->find('show'); + $commandTester = new CommandTester($command); + $commandTester->execute(['config'=>"bad_file"]); + + $output = $commandTester->getDisplay(); + $this->assertStringContainsString('[ERROR] Configuration error: File "bad_file" does not exist.', $output); + } + + public function testGoodConfig() + { + exec('rclone test makefiles temp/source 2>&1'); + + $applicationd = new Application('backup', "1.1.1"); + + $applicationd->add(new CommandShow()); + + + $command = $applicationd->find('show'); + $commandTester = new CommandTester($command); + $commandTester->execute(['config'=>"config.example.yml"]); + + $output = $commandTester->getDisplay(); + $this->assertStringContainsString('Example', $output); + $this->assertStringContainsString('temp/source', $output); + $this->assertStringContainsString('temp/destination', $output); + } + + +} \ No newline at end of file diff --git a/tests/Rclone/RcloneTest.php b/tests/Rclone/RcloneTest.php index 27638db..9be950a 100644 --- a/tests/Rclone/RcloneTest.php +++ b/tests/Rclone/RcloneTest.php @@ -7,7 +7,10 @@ final class RcloneTest extends TestCase { protected function setUp(): void { - shell_exec('rclone test makefiles temp/source 2>&1'); + if(!is_dir('temp')) { + mkdir('temp'); + } + exec('rclone test makefiles temp/source 2>&1'); } protected function tearDown(): void