<?php

declare(strict_types=1);

namespace App\Tests;

use App\Rclone\Rclone;
use Psr\Log\NullLogger;
use PHPUnit\Framework\TestCase;

final class RcloneTest extends TestCase
{
    protected function setUp(): void
    {
        exec('rclone test makefiles temp/source 2>&1');
    }

    protected function tearDown(): void
    {
        exec('rclone purge temp 2>&1');
    }

    public function testRclonePath(): void
    {
        $this->expectException(\Exception::class);
        new Rclone(new NullLogger(), 'invalid');
        $this->fail('Exception was not thrown');
    }

    public function testRcloneInvalidVersion(): void
    {
        $this->expectException(\Exception::class);
        new Rclone(new NullLogger(), 'uname');
        $this->fail('Exception was not thrown');
    }

    public function testRcloneValidVersion(): void
    {
        $rclone = new Rclone(new NullLogger());
        $this->assertStringStartsWith('rclone', $rclone->getVersion());
    }

    public function testRcloneSize(): void
    {
        $rclone = new Rclone(new NullLogger());
        $size = $rclone->getSize('temp/source');
        $this->assertGreaterThan(10000, $size);

        $this->expectException(\Exception::class);
        $this->expectExceptionMessage("ERROR");
        $rclone->getSize('temp/bogus-source');
        $this->fail('Exception was not thrown');
    }

    public function testRcloneCopy(): void
    {
        $rclone = new Rclone(new NullLogger());
        $rclone->copy('temp/source', 'temp/destination');
        $this->assertDirectoryExists('temp/destination');
    }

    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');
    }
}