From f41571cfd2a5e4b0ab6eb5c99e8abb904db6646c Mon Sep 17 00:00:00 2001 From: Jens True Date: Thu, 15 Jun 2023 14:10:17 +0000 Subject: [PATCH] More tests and refactoring --- .woodpecker.yml | 19 +++++++----- src/CommandBackup.php | 11 +++---- src/Notification/Notification.php | 3 +- src/Notification/Ntfy.php | 30 ++++++++++++++----- src/Template/Twig.php | 20 +++++++++++++ .../TwigExtension.php} | 8 ++--- tests/CommandBackupTest.php | 2 -- tests/Notification/NtfyTest.php | 24 +++++++++++++++ tests/Rclone/RcloneTest.php | 13 ++++---- 9 files changed, 92 insertions(+), 38 deletions(-) create mode 100644 src/Template/Twig.php rename src/{Twig/AppExtension.php => Template/TwigExtension.php} (80%) create mode 100644 tests/Notification/NtfyTest.php diff --git a/.woodpecker.yml b/.woodpecker.yml index 836281f..6a5c67c 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -4,17 +4,20 @@ pipeline: commands: - composer install --no-dev run: - image: php + image: php:cli-bookworm commands: - ./backup show config.example.yml - test: + dependencies: image: composer commands: + - composer install + analyze: + image: php:cli-bookworm + commands: + - make analyze + test: + image: php:cli-bookworm + commands: - apt update - apt install rclone - - composer install - - vendor/bin/phpunit tests - tools: - image: php - commands: - - make analyze \ No newline at end of file + - vendor/bin/phpunit tests \ No newline at end of file diff --git a/src/CommandBackup.php b/src/CommandBackup.php index b574adf..a6bc37e 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -8,11 +8,10 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -use Twig\Environment; -use Twig\Loader\ArrayLoader; -use DateTime; +use App\Template\Twig; use App\Notification\Notification; use App\Rclone\Rclone; +use DateTime; #[AsCommand( name: 'backup', @@ -47,9 +46,7 @@ class CommandBackup extends Command $notification = new Notification(); $notification->loadMany($app->getConfig()['notification']); - $loader = new ArrayLoader($app->getConfig()['templates']); - $twig = new Environment($loader); - $twig->addExtension(new Twig\AppExtension()); + $render = new Twig($app->getConfig()['templates']); foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) { $title = $conf['title']; @@ -66,7 +63,7 @@ class CommandBackup extends Command $template['destination_size_after'] = $rclone->getSize($conf['destination']); $template['end'] = new DateTime(); - $message = $twig->render('notify', $template); + $message = $render->render('notify', $template); } catch (\Exception $e) { $message = $e->getMessage(); } diff --git a/src/Notification/Notification.php b/src/Notification/Notification.php index 344092c..d172cdb 100644 --- a/src/Notification/Notification.php +++ b/src/Notification/Notification.php @@ -28,6 +28,7 @@ class Notification * * @param string $key Notification class * @param string[] $config Implementation specific configuration + * @SuppressWarnings(PHPMD) */ public function loadSingle(string $key, array $config): void { @@ -35,7 +36,7 @@ class Notification case 'ntfy': case 'Ntfy': case 'NTFY': - $this->notifiers[] = new Ntfy($config); + $this->notifiers[] = Ntfy::factory($config); break; default: break; diff --git a/src/Notification/Ntfy.php b/src/Notification/Ntfy.php index eea38dc..d9aeff2 100644 --- a/src/Notification/Ntfy.php +++ b/src/Notification/Ntfy.php @@ -8,27 +8,41 @@ use Ntfy\Client; class Ntfy implements NotificationInterface { - /** @var string[] $config */ - private array $config; - private Client $client; - + private string $topic = 'default'; /** * Initialize with configuration. * * @param string[] $config Configuration */ - public function __construct(array $config) + public static function factory(array $config): self { - $this->config = $config; + $instance = new self(new Client(new Server($config['domain']))); + if (isset($config['topic'])) { + $instance->setTopic($config['topic']); + } + return $instance; + } - $this->client = new Client(new Server($config['domain'])); + public function __construct(Client $client) + { + $this->client = $client; + } + + public function setTopic(string $topic): void + { + $this->topic = $topic; } public function send(string $title, string $message): void { + assert(strlen($title) > 0); + assert(strlen($title) < 256); + assert(strlen($message) > 0); + assert(strlen($message) < 4096); + $msg = new Message(); - $msg->topic($this->config['topic']); + $msg->topic($this->topic); $msg->title($title); $msg->body($message); $this->client->send($msg); diff --git a/src/Template/Twig.php b/src/Template/Twig.php new file mode 100644 index 0000000..0b199b9 --- /dev/null +++ b/src/Template/Twig.php @@ -0,0 +1,20 @@ +addExtension(new TwigExtension()); + } +} diff --git a/src/Twig/AppExtension.php b/src/Template/TwigExtension.php similarity index 80% rename from src/Twig/AppExtension.php rename to src/Template/TwigExtension.php index a3729df..ef85b7a 100644 --- a/src/Twig/AppExtension.php +++ b/src/Template/TwigExtension.php @@ -1,6 +1,6 @@ - * @license https://opensource.org/licenses/gpl-license.php GNU Public License - * @link https://jcktrue.dk */ -class AppExtension extends AbstractExtension +class TwigExtension extends AbstractExtension { /** * Extend the filters diff --git a/tests/CommandBackupTest.php b/tests/CommandBackupTest.php index 51501d0..a5f8780 100644 --- a/tests/CommandBackupTest.php +++ b/tests/CommandBackupTest.php @@ -1,6 +1,4 @@ createStub(Client::class); + + $sut = new Ntfy($client); + + $client->expects($this->once())->method('send'); + $sut->send('title','text'); + } +} \ No newline at end of file diff --git a/tests/Rclone/RcloneTest.php b/tests/Rclone/RcloneTest.php index e895065..27638db 100644 --- a/tests/Rclone/RcloneTest.php +++ b/tests/Rclone/RcloneTest.php @@ -1,5 +1,6 @@ expectException(\Exception::class); - $rclone = new \App\Rclone\Rclone('invalid'); + $rclone = new Rclone('invalid'); $this->assertString('', $rclone->getVersion()); } public function testRcloneInvalidVersion(): void { $this->expectException(\Exception::class); - $rclone = new \App\Rclone\Rclone('uname'); + $rclone = new Rclone('uname'); $this->assertString('', $rclone->getVersion()); } public function testRcloneValidVersion(): void { - $rclone = new \App\Rclone\Rclone(); + $rclone = new Rclone(); $this->assertStringContainsString('rclone', $rclone->getVersion()); } public function testRcloneSize(): void { - $rclone = new \App\Rclone\Rclone(); + $rclone = new Rclone(); $size = $rclone->getSize('temp/source'); $this->assertGreaterThan(10000, $size); @@ -49,11 +50,11 @@ final class RcloneTest extends TestCase public function testRcloneCopy(): void { - $rclone = new \App\Rclone\Rclone(); + $rclone = new Rclone(); $result = $rclone->copy('temp/source','temp/destination'); $this->assertDirectoryExists('temp/destination'); - $rclone = new \App\Rclone\Rclone(); + $rclone = new Rclone(); $result = $rclone->copy('temp/source','temp/destination',['bwlimit'=>'6M']); $this->assertDirectoryExists('temp/destination');