Add first tests

This commit is contained in:
Clement Desmidt 2020-04-16 18:02:46 +02:00
parent 0952cf2db3
commit 02ad4f286e
14 changed files with 1958 additions and 58 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
.idea
/vendor
/app/temp/*
!/app/temp/.gitkeep

View File

@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
namespace Shikiryu\Backup;
@ -6,17 +6,29 @@ use Shikiryu\Backup\Backup\BackupAbstract;
use Shikiryu\Backup\Transport\TransportAbstract;
use Shikiryu\Backup\Backup\Factory as BackupFactory;
use Shikiryu\Backup\Transport\Factory as TransportFactory;
use Shikiryu\Exceptions\BackupException;
use Shikiryu\Exceptions\ComposerNotFoundException;
use Shikiryu\Exceptions\ScenarioException;
use Shikiryu\Exceptions\ScenarioNotFoundException;
class Scenario
final class Scenario
{
/* @var $backup BackupAbstract */
private $backup;
/* @var $to TransportAbstract */
private $transport;
/**
* @throws ComposerNotFoundException
*/
public static function register()
{
include __DIR__.'/../../vendor/autoload.php';
$autoload_file = __DIR__ . '/../../vendor/autoload.php';
if (file_exists($autoload_file)) {
include $autoload_file;
} else {
throw new ComposerNotFoundException(sprintf('Autoloadfile «%s» not found.', $autoload_file));
}
}
/**
@ -32,18 +44,19 @@ class Scenario
/**
* check if backup is valid and then launch the transfer
*
* @return bool
* @throws BackupException
* @see BackupAbstract::isValid
* @see TransportAbstract::send
*
* @throws \Exception
*/
public function send()
public function send(): bool
{
if ($this->backup->isValid()) {
$this->transport->send();
} else {
throw new \Exception('Backup configuration is invalid.');
return $this->transport->send();
}
throw new BackupException('Backup configuration is invalid.');
}
/**
@ -51,9 +64,13 @@ class Scenario
*
* @param $scenario
*
* @throws \Exception
* @return bool
* @throws BackupException
* @throws ComposerNotFoundException
* @throws ScenarioException
* @throws ScenarioNotFoundException
*/
public static function launch($scenario)
public static function launch($scenario): bool
{
// add autoloader
static::register();
@ -64,15 +81,15 @@ class Scenario
if ($scenario !== null && static::isValid($scenario)) {
try {
$scenario = new self($scenario);
$scenario->send();
} catch (\Exception $e) {
return $scenario->send();
} catch (BackupException $e) {
throw $e;
}
exit;
}
throw new \Exception('invalid scenario.');
throw new ScenarioException('invalid scenario.');
}
throw new \Exception('scenario not found.');
throw new ScenarioNotFoundException(sprintf('scenario «%s» not found.', $scenario));
}
/**
@ -82,7 +99,7 @@ class Scenario
*
* @return bool
*/
public static function isValid(array $scenario)
public static function isValid(array $scenario): bool
{
return
isset($scenario['backup'], $scenario['transport']) &&

View File

@ -0,0 +1,10 @@
<?php
namespace Shikiryu\Exceptions;
use Exception;
class BackupException extends Exception
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace Shikiryu\Exceptions;
use Exception;
class ComposerNotFoundException extends Exception
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace Shikiryu\Exceptions;
use Exception;
class DistantException extends Exception
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace Shikiryu\Exceptions;
use Exception;
class LocalException extends Exception
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace Shikiryu\Exceptions;
use Exception;
class ScenarioException extends Exception
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace Shikiryu\Exceptions;
use Exception;
class ScenarioNotFoundException extends Exception
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace Shikiryu\Exceptions;
use Exception;
class TransportException extends Exception
{
}

View File

@ -6,7 +6,7 @@
{
"name": "Shikiryu",
"email": "backup@desmidt.fr",
"homepage": "http://shikiryu.com",
"homepage": "https://shikiryu.com",
"role": "Developer"
}
],
@ -21,9 +21,10 @@
"spatie/dropbox-api": "^1.11"
},
"require-dev": {
"roave/security-advisories": "dev-master"
"roave/security-advisories": "dev-master",
"phpunit/phpunit": "^9"
},
"autoload": {
"classmap": ["app/code/", "vendor/"]
"classmap": ["app/code/", "vendor/", "tests/"]
}
}

1744
composer.lock generated

File diff suppressed because it is too large Load Diff

5
phpunit.xml Normal file
View File

@ -0,0 +1,5 @@
<testsuites>
<testsuite name="ShikiryuBackup">
<directory>/test/**/*Test.php</directory>
</testsuite>
</testsuites>

96
tests/ScenarioTest.php Normal file
View File

@ -0,0 +1,96 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Shikiryu\Backup\Scenario;
use Shikiryu\Exceptions\ScenarioException;
use Shikiryu\Exceptions\ScenarioNotFoundException;
final class ScenarioTest extends TestCase
{
private $scenario_path = __DIR__.'/scenario.json';
protected function setUp(): void
{
$this->scenario_path = __DIR__.'/scenario.json';
}
protected function tearDown(): void
{
if (file_exists($this->scenario_path)) {
unlink($this->scenario_path);
}
}
public function testLaunchWithNotFoundFileScenario()
{
$this->expectException(ScenarioNotFoundException::class);
Scenario::launch('file_not_found.json');
}
/**
* @ignore
*/
/*public function testNoComposerInstallDone()
{
$this->expectException(\Shikiryu\Exceptions\ComposerNotFoundException::class);
rename(__DIR__ . '/../vendor', __DIR__ . '/../vendor');
Scenario::launch('file_not_found.json');
rename(__DIR__ . '/../vendor', __DIR__ . '/../vendor');
}*/
public function testInvalidScenarioWithNoBackup()
{
$this->expectException(ScenarioException::class);
file_put_contents($this->scenario_path, json_encode([
'transport' => [
'Folder' => [
'folder' => '/test',
]
]
]));
Scenario::launch($this->scenario_path);
}
public function testInvalidScenarioWithNoTransport()
{
$this->expectException(ScenarioException::class);
file_put_contents($this->scenario_path, json_encode([
'backup' => [
'SFTP' => [
'host' => 'localhost',
'login' => 'login',
'password' => 'password',
'files' => [
'/home/login/file1',
'/home/login/file2'
]
]
]
]));
Scenario::launch($this->scenario_path);
}
public function testIsNotValid()
{
$is_valid = ['backup' => [], 'transport' => []];
$is_valid = Scenario::isValid($is_valid);
$this->assertFalse($is_valid);
}
public function testIsValid()
{
$is_valid = ['backup' => ['SFTP' => [
'host' => 'localhost',
'login' => 'login',
'password' => 'password',
'files' => [
'/home/login/file1',
'/home/login/file2'
]
]], 'transport' => ['Folder' => [
'folder' => '/test',
]]];
$is_valid = Scenario::isValid($is_valid);
$this->assertTrue($is_valid);
}
}

View File

@ -0,0 +1,43 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Shikiryu\Backup\Backup\Files;
use Shikiryu\Exceptions\BackupException;
class FilesTest extends TestCase
{
public function testIsValid()
{
$files = [
__DIR__ . '/file1',
__DIR__ . '/file2',
];
foreach ($files as $file) {
touch($file);
}
$file = new Files([
'files' => $files
]);
$this->assertTrue($file->isValid());
foreach ($files as $file) {
unlink($file);
}
}
public function testIsNotValid()
{
$file = new Files([
'files' => [
'/file1',
'/file2',
],
]);
$this->assertFalse($file->isValid());
}
public function testInvalidConstruct()
{
$this->expectException(BackupException::class);
$file = new Files([]);
}
}