More tests and refactoring
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Jens True 2023-06-15 14:10:17 +00:00
parent 72058335ca
commit f41571cfd2
9 changed files with 92 additions and 38 deletions

@ -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
- vendor/bin/phpunit tests

@ -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();
}

@ -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;

@ -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);

20
src/Template/Twig.php Normal file

@ -0,0 +1,20 @@
<?php
namespace App\Template;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use App\Template\TwigExtension;
class Twig extends Environment
{
/**
* @param string[] $templates Array of templates
*/
public function __construct(array $templates)
{
$loader = new ArrayLoader($templates);
parent::__construct($loader);
$this->addExtension(new TwigExtension());
}
}

@ -1,6 +1,6 @@
<?php
namespace App\Twig;
namespace App\Template;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Extension\AbstractExtension;
@ -10,12 +10,8 @@ use Twig\TwigFilter;
* Twig extension
*
* Additional formatters for templates
*
* @author Jens True <jens.chr.true@gmail.com>
* @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

@ -1,6 +1,4 @@
<?php
namespace App\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use App\Notification\Ntfy;
use Ntfy\Server;
use Ntfy\Client;
use Ntfy\Message;
final class NtfyTest extends TestCase
{
/**
* @doesNotPerformAssertions
*/
public function testDoesSomething(): void
{
//Dependency
$client = $this->createStub(Client::class);
$sut = new Ntfy($client);
$client->expects($this->once())->method('send');
$sut->send('title','text');
}
}

@ -1,5 +1,6 @@
<?php declare(strict_types=1);
use App\Rclone\Rclone;
use PHPUnit\Framework\TestCase;
final class RcloneTest extends TestCase
@ -18,26 +19,26 @@ final class RcloneTest extends TestCase
public function testRclonePath(): void
{
$this->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');