backupscript/tests/CommandBackupTest.php

99 lines
3.0 KiB
PHP
Raw Normal View History

2023-07-10 09:20:36 +00:00
<?php
declare(strict_types=1);
namespace App\Tests;
2023-07-10 07:47:28 +00:00
2023-06-15 10:15:44 +00:00
use PHPUnit\Framework\TestCase;
2023-07-10 09:20:36 +00:00
use PHPUnit\Framework\Attributes\CoversClass;
2023-06-15 10:15:44 +00:00
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use App\CommandBackup;
2023-07-10 09:20:36 +00:00
#[CoversClass(CommandBackup::class)]
final class CommandBackupTest extends \PHPUnit\Framework\TestCase
2023-06-15 10:15:44 +00:00
{
2023-07-10 07:47:28 +00:00
protected function setUp(): void
{
mkdir('temp');
mkdir('temp/destination');
exec('rclone test makefiles temp/source 2>&1');
}
protected function tearDown(): void
{
2023-07-10 09:20:36 +00:00
exec('rclone purge temp 2>&1');
2023-07-10 07:47:28 +00:00
}
2023-07-10 09:20:36 +00:00
public function testBadConfig(): void
2023-06-15 10:15:44 +00:00
{
2023-07-10 07:47:28 +00:00
$applicationd = new Application('backup', "1.1.1");
$applicationd->add(new CommandBackup());
$command = $applicationd->find('backup');
$commandTester = new CommandTester($command);
2023-07-10 09:20:36 +00:00
$commandTester->execute(['config' => "bad_file"]);
2023-07-10 07:47:28 +00:00
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[ERROR] Configuration error:File "bad_file" does not exist.', $output);
}
2023-08-15 11:43:12 +00:00
public function testNoCommand(): void
{
$applicationd = new Application('backup', "1.1.1");
$applicationd->add(new CommandBackup());
$command = $applicationd->find('backup');
$commandTester = new CommandTester($command);
$this->expectException(\Exception::class);
$commandTester->execute([]);
$this->fail('Exception was not thrown');
}
2023-07-10 09:20:36 +00:00
public function testGoodConfig(): void
2023-07-10 07:47:28 +00:00
{
$applicationd = new Application('backup', "1.1.1");
2023-06-15 10:15:44 +00:00
2023-07-10 07:47:28 +00:00
$applicationd->add(new CommandBackup());
$command = $applicationd->find('backup');
$commandTester = new CommandTester($command);
2023-07-10 09:20:36 +00:00
$commandTester->execute(['config' => "config.example.yml"]);
2023-07-10 07:47:28 +00:00
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] Complete ', $output);
}
2023-07-10 09:20:36 +00:00
public function testNoDestinationFolder(): void
2023-07-10 07:47:28 +00:00
{
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);
2023-07-10 09:20:36 +00:00
$commandTester->execute(['config' => "config.example.yml"]);
2023-07-10 07:47:28 +00:00
$output = $commandTester->getDisplay();
$this->assertStringContainsString('[OK] Complete ', $output);
}
2023-07-10 09:20:36 +00:00
public function testNoSourceFolder(): void
2023-07-10 07:47:28 +00:00
{
exec('rclone purge temp/source 2>&1', $output);
2023-06-15 10:15:44 +00:00
$applicationd = new Application('backup', "1.1.1");
$applicationd->add(new CommandBackup());
$command = $applicationd->find('backup');
$commandTester = new CommandTester($command);
2023-07-10 09:20:36 +00:00
$commandTester->execute(['config' => "config.example.yml"]);
2023-06-15 10:15:44 +00:00
$output = $commandTester->getDisplay();
2023-07-10 07:47:28 +00:00
$this->assertStringContainsString('[OK] Complete ', $output);
2023-06-15 10:15:44 +00:00
}
2023-07-10 09:20:36 +00:00
}