mirror of
https://github.com/Chouchen/Shikiryu_Backup.git
synced 2021-06-30 16:02:14 +02:00
✅ Add first tests
This commit is contained in:
parent
0952cf2db3
commit
02ad4f286e
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
||||
.idea
|
||||
/vendor
|
||||
/app/temp/*
|
||||
!/app/temp/.gitkeep
|
||||
|
@ -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']) &&
|
||||
|
10
app/code/exceptions/BackupException.php
Normal file
10
app/code/exceptions/BackupException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class BackupException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/ComposerNotFoundException.php
Normal file
10
app/code/exceptions/ComposerNotFoundException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ComposerNotFoundException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/DistantException.php
Normal file
10
app/code/exceptions/DistantException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class DistantException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/LocalException.php
Normal file
10
app/code/exceptions/LocalException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class LocalException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/ScenarioException.php
Normal file
10
app/code/exceptions/ScenarioException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ScenarioException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/ScenarioNotFoundException.php
Normal file
10
app/code/exceptions/ScenarioNotFoundException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ScenarioNotFoundException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/TransportException.php
Normal file
10
app/code/exceptions/TransportException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class TransportException extends Exception
|
||||
{
|
||||
|
||||
}
|
@ -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
1744
composer.lock
generated
File diff suppressed because it is too large
Load Diff
5
phpunit.xml
Normal file
5
phpunit.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<testsuites>
|
||||
<testsuite name="ShikiryuBackup">
|
||||
<directory>/test/**/*Test.php</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
96
tests/ScenarioTest.php
Normal file
96
tests/ScenarioTest.php
Normal 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);
|
||||
}
|
||||
}
|
43
tests/backup/FilesTest.php
Normal file
43
tests/backup/FilesTest.php
Normal 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([]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user