. */ namespace SP\Tests\Modules\Cli\Commands; use DI\DependencyException; use DI\NotFoundException; use SP\Config\Config; use SP\Core\Exceptions\FileNotFoundException; use SP\Modules\Cli\Commands\BackupCommand; use SP\Services\Backup\FileBackupService; use SP\Tests\Modules\Cli\CliTestCase; use function SP\Tests\recreateDir; /** * */ class BackupCommandTest extends CliTestCase { /** * @var string */ protected static string $currentConfig; /** * @var string[] */ protected static array $commandInputData = [ '--path' => TMP_PATH ]; /** * @throws DependencyException * @throws NotFoundException */ public function testBackupFails(): void { $inputData = ['--path' => '/non/existant/path']; $commandTester = $this->executeCommandTest( BackupCommand::class, $inputData ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Unable to create the backups directory', $output); } /** * @throws DependencyException * @throws FileNotFoundException * @throws NotFoundException */ public function testBackupIsSuccessful(): void { $this->setupDatabase(); $commandTester = $this->executeCommandTest( BackupCommand::class, self::$commandInputData ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Application and database backup completed successfully', $output); $this->checkBackupFilesAreCreated(); // Recreate cache directory to avoid unwanted behavior recreateDir(CACHE_PATH); } private function checkBackupFilesAreCreated(): void { $configData = self::$dic->get(Config::class)->getConfigData(); $this->assertFileExists( FileBackupService::getAppBackupFilename( TMP_PATH, $configData->getBackupHash(), true ) ); $this->assertFileExists( FileBackupService::getDbBackupFilename( TMP_PATH, $configData->getBackupHash(), true ) ); } /** * @throws DependencyException * @throws NotFoundException */ public function testBackupFromEnvironmentVarIsAbort(): void { putenv(sprintf('%s=%s', BackupCommand::$envVarsMapping['path'], '/non/existant/path') ); $commandTester = $this->executeCommandTest( BackupCommand::class, null, false ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Unable to create the backups directory', $output); } /** * @throws DependencyException * @throws NotFoundException * @throws FileNotFoundException */ public function testBackupFromEnvironmentVarIsSuccessful(): void { $this->setEnvironmentVariables(); $commandTester = $this->executeCommandTest( BackupCommand::class, null, false ); // the output of the command in the console $output = $commandTester->getDisplay(); $this->assertStringContainsString('Application and database backup completed successfully', $output); $this->checkBackupFilesAreCreated(); // Recreate cache directory to avoid unwanted behavior recreateDir(CACHE_PATH); } private function setEnvironmentVariables(): void { putenv(sprintf('%s=%s', BackupCommand::$envVarsMapping['path'], TMP_PATH) ); } protected function setUp(): void { $this->unsetEnvironmentVariables(); parent::setUp(); } private function unsetEnvironmentVariables(): void { foreach (BackupCommand::$envVarsMapping as $envVar) { putenv($envVar); } } }