Browse Source

Add first tests

master
Clement Desmidt 2 months ago
parent
commit
02ad4f286e
14 changed files with 2139 additions and 239 deletions
  1. +2
    -0
      .gitignore
  2. +33
    -16
      app/code/Scenario.php
  3. +10
    -0
      app/code/exceptions/BackupException.php
  4. +10
    -0
      app/code/exceptions/ComposerNotFoundException.php
  5. +10
    -0
      app/code/exceptions/DistantException.php
  6. +10
    -0
      app/code/exceptions/LocalException.php
  7. +10
    -0
      app/code/exceptions/ScenarioException.php
  8. +10
    -0
      app/code/exceptions/ScenarioNotFoundException.php
  9. +10
    -0
      app/code/exceptions/TransportException.php
  10. +4
    -3
      composer.json
  11. +1886
    -220
      composer.lock
  12. +5
    -0
      phpunit.xml
  13. +96
    -0
      tests/ScenarioTest.php
  14. +43
    -0
      tests/backup/FilesTest.php

+ 2
- 0
.gitignore View File

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

+ 33
- 16
app/code/Scenario.php 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']) &&


+ 10
- 0
app/code/exceptions/BackupException.php View File

@@ -0,0 +1,10 @@
<?php

namespace Shikiryu\Exceptions;

use Exception;

class BackupException extends Exception
{

}

+ 10
- 0
app/code/exceptions/ComposerNotFoundException.php View File

@@ -0,0 +1,10 @@
<?php

namespace Shikiryu\Exceptions;

use Exception;

class ComposerNotFoundException extends Exception
{

}

+ 10
- 0
app/code/exceptions/DistantException.php View File

@@ -0,0 +1,10 @@
<?php

namespace Shikiryu\Exceptions;

use Exception;

class DistantException extends Exception
{

}

+ 10
- 0
app/code/exceptions/LocalException.php View File

@@ -0,0 +1,10 @@
<?php

namespace Shikiryu\Exceptions;

use Exception;

class LocalException extends Exception
{

}

+ 10
- 0
app/code/exceptions/ScenarioException.php View File

@@ -0,0 +1,10 @@
<?php

namespace Shikiryu\Exceptions;

use Exception;

class ScenarioException extends Exception
{

}

+ 10
- 0
app/code/exceptions/ScenarioNotFoundException.php View File

@@ -0,0 +1,10 @@
<?php

namespace Shikiryu\Exceptions;

use Exception;

class ScenarioNotFoundException extends Exception
{

}

+ 10
- 0
app/code/exceptions/TransportException.php View File

@@ -0,0 +1,10 @@
<?php

namespace Shikiryu\Exceptions;

use Exception;

class TransportException extends Exception
{

}

+ 4
- 3
composer.json 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/"]
}
}

+ 1886
- 220
composer.lock
File diff suppressed because it is too large
View File


+ 5
- 0
phpunit.xml View File

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

+ 96
- 0
tests/ScenarioTest.php 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);
}
}

+ 43
- 0
tests/backup/FilesTest.php 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([]);
}
}

Loading…
Cancel
Save