From 9459045e6c282106bccbcf1dd7dd5bdaac4088fe Mon Sep 17 00:00:00 2001 From: Jens True Date: Mon, 24 Jul 2023 13:32:21 +0000 Subject: [PATCH] More testing. Removed Gitea actions. --- .gitea/workflows/build.yml | 37 ---------------------------- src/CommandBackup.php | 3 +-- src/Rclone/Rclone.php | 25 +++++++++---------- tests/Rclone/RcloneTest.php | 29 +++++++++++++++++----- tests/Template/TwigExtensionTest.php | 1 - tests/Template/TwigTest.php | 20 +++++++++++++++ 6 files changed, 55 insertions(+), 60 deletions(-) delete mode 100644 .gitea/workflows/build.yml create mode 100644 tests/Template/TwigTest.php diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml deleted file mode 100644 index 438295b..0000000 --- a/.gitea/workflows/build.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Build and test -run-name: Perform a regular build and test -on: push - -jobs: - requirements: - runs-on: ubuntu-latest - steps: - - name: Check out repository code - uses: actions/checkout@v3 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: '8.2' - extensions: mbstring, xml, curl, zip - tools: composer, phpdoc - coverage: xdebug - - name: Install dependencies - run: | - composer config github-oauth.github.com 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 - composer install - wget https://downloads.rclone.org/rclone-current-linux-amd64.deb - dpkg -i rclone-current-linux-amd64.deb - - name: Dry run - run: ./backup show config.example.yml - - name: Static analysis - run: composer analyze - - name: Test - run: composer test-coverage - - uses: actions/upload-artifact@v3 - with: - path: output/test.html - - name: Document - run: phpdoc run -# - uses: actions/upload-artifact@v3 -# with: -# path: output/docs \ No newline at end of file diff --git a/src/CommandBackup.php b/src/CommandBackup.php index 8504f34..e106306 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -54,8 +54,7 @@ class CommandBackup extends Command return Command::FAILURE; } - $rclone = new Rclone((string)$app->getConfig('rclone.path')); - $rclone->setLogger($app->getLogger()->withName('rclone')); + $rclone = new Rclone($app->getLogger()->withName('rclone'), (string)$app->getConfig('rclone.path')); $notification = new Notification(); /** @var array> */ diff --git a/src/Rclone/Rclone.php b/src/Rclone/Rclone.php index b2976f1..d58054b 100644 --- a/src/Rclone/Rclone.php +++ b/src/Rclone/Rclone.php @@ -4,8 +4,6 @@ declare(strict_types=1); namespace App\Rclone; -use Psr\Log\LoggerAwareTrait; -use Psr\Log\NullLogger; use Psr\Log\LoggerInterface; use Symfony\Component\Process\Process; use Symfony\Component\Process\Exception\ProcessFailedException; @@ -20,7 +18,10 @@ use Exception; */ class Rclone { - use LoggerAwareTrait; + /** + * The logger instance. + */ + protected LoggerInterface $logger; protected string $rclonePath; /** @@ -40,10 +41,10 @@ class Rclone * * @param string $rclonePath Relative or absolute path */ - public function __construct(string $rclonePath = "rclone") + public function __construct(LoggerInterface $logger, string $rclonePath = "rclone") { $this->rclonePath = $rclonePath; - $this->setLogger(new NullLogger()); + $this->logger = $logger; $process = $this->exec('--version'); if (!$process->isSuccessful()) { @@ -130,22 +131,18 @@ class Rclone $options ) ); - if ($this->logger instanceof LoggerInterface) { - $this->logger->info("Execute command", [$process->getCommandLine()]); - } + + $this->logger->info("Execute command", [$process->getCommandLine()]); + $process->setTimeout(4 * 3600); $process->run(); // executes after the command finishes if (!$process->isSuccessful()) { - if ($this->logger instanceof LoggerInterface) { - $this->logger->error("Failed execution"); - } - } - if ($this->logger instanceof LoggerInterface) { - $this->logger->info("Return code", [$process->getExitCode()]); + $this->logger->error("Failed execution"); } + $this->logger->info("Return code", [$process->getExitCode()]); return $process; } } diff --git a/tests/Rclone/RcloneTest.php b/tests/Rclone/RcloneTest.php index 61be9df..975297c 100644 --- a/tests/Rclone/RcloneTest.php +++ b/tests/Rclone/RcloneTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Tests; use App\Rclone\Rclone; +use Psr\Log\NullLogger; use PHPUnit\Framework\TestCase; final class RcloneTest extends TestCase @@ -23,26 +24,26 @@ final class RcloneTest extends TestCase public function testRclonePath(): void { $this->expectException(\Exception::class); - new Rclone('invalid'); + new Rclone(new NullLogger(), 'invalid'); $this->fail('Exception was not thrown'); } public function testRcloneInvalidVersion(): void { $this->expectException(\Exception::class); - new Rclone('uname'); + new Rclone(new NullLogger(), 'uname'); $this->fail('Exception was not thrown'); } public function testRcloneValidVersion(): void { - $rclone = new Rclone(); + $rclone = new Rclone(new NullLogger()); $this->assertStringStartsWith('rclone', $rclone->getVersion()); } public function testRcloneSize(): void { - $rclone = new Rclone(); + $rclone = new Rclone(new NullLogger()); $size = $rclone->getSize('temp/source'); $this->assertGreaterThan(10000, $size); @@ -54,17 +55,33 @@ final class RcloneTest extends TestCase public function testRcloneCopy(): void { - $rclone = new Rclone(); + $rclone = new Rclone(new NullLogger()); $rclone->copy('temp/source', 'temp/destination'); $this->assertDirectoryExists('temp/destination'); + } - $rclone = new Rclone(); + public function testRcloneCopyParam(): void + { + $rclone = new Rclone(new NullLogger()); $rclone->copy('temp/source', 'temp/destination', ['bwlimit' => '6M']); $this->assertDirectoryExists('temp/destination'); + } + public function testRcloneCopyBad(): void + { + $rclone = new Rclone(new NullLogger()); $this->expectException(\Exception::class); $this->expectExceptionMessage("ERROR"); $rclone->copy('temp/bogus-source', 'temp/bogus-destination'); $this->fail('Exception was not thrown'); } + + public function testRcloneCopyBadParam(): void + { + $rclone = new Rclone(new NullLogger()); + $this->expectException(\Exception::class); + $this->expectExceptionMessage("ERROR"); + $rclone->copy('temp/bogus-source', 'temp/bogus-destination', ['bwlimit' => '6M']); + $this->fail('Exception was not thrown'); + } } diff --git a/tests/Template/TwigExtensionTest.php b/tests/Template/TwigExtensionTest.php index 92e5438..343e92e 100644 --- a/tests/Template/TwigExtensionTest.php +++ b/tests/Template/TwigExtensionTest.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace App\Tests; use PHPUnit\Framework\TestCase; -use PHPUnit\Framework\Attributes\DataProvider; use App\Template\TwigExtension; use Twig\TwigFilter; diff --git a/tests/Template/TwigTest.php b/tests/Template/TwigTest.php new file mode 100644 index 0000000..2dd456f --- /dev/null +++ b/tests/Template/TwigTest.php @@ -0,0 +1,20 @@ + 'start {{ var }} end']); + $template = $obj->load('template'); + $output = $template->render(['var' => 'middle']); + $this->assertEquals('start middle end', $output); + } +}