Skip to content

Commit

Permalink
Add tests to cover what is testable in SelfUpdate command
Browse files Browse the repository at this point in the history
Note that only helper methods can be tested for that command, not
the execution of the update itself (it has to happen within a PHAR).
And it doesn't make much sense to start mocking lots of stuff for that.

Some integration tests @ CIS will be in charge of
covering the real execution instead. Coming soon.
  • Loading branch information
stronk7 committed Feb 5, 2024
1 parent 8124fe7 commit 9fad129
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Command/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

/**
* Calculate the full path where the old PHAR file will be backup (to be able to roll back to it).
*
* @param string|null $directory the directory where the backup will be stored
*
* @return string
*/
protected function getBackupPath(): string
protected function getBackupPath(?string $directory = null): string
{
$directory = getenv('HOME');
$directory = $directory ?? getenv('HOME'); // Default to $HOME as base directory if not provided.
if (empty($directory) || !is_dir($directory)) {
throw new \RuntimeException('Your $HOME enviroment variable is either not set or is not a directory');
throw new \RuntimeException("The {$directory} path is not an existing directory");
}
$directory .= '/.moodle-plugin-ci';

Expand Down
62 changes: 62 additions & 0 deletions tests/Command/SelfUpdateCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Moodle Plugin CI package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2023 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/

namespace Command;

use MoodlePluginCI\Command\SelfUpdateCommand;
use Symfony\Component\Filesystem\Filesystem;

/**
* Tests for the SelfUpdateCommand class.
*
* There isn't much to test here, only helper methods. Note that the utility itself
* will be covered by some integration tests @ CIs.
*/
class SelfUpdateCommandTest extends \PHPUnit\Framework\TestCase
{
/**
* @covers \MoodlePluginCI\Command\SelfUpdateCommand::getBackupPath
*/
public function testGetBackupPathNotExists()
{
$command = new SelfUpdateCommand();

// Try with a non-existing directory.
$rollBackDir = sys_get_temp_dir() . '/not_existing_dir';
$rollBackFile = $rollBackDir . '/.moodle-plugin-ci/moodle-plugin-ci-old.phar';

$this->expectException(\RuntimeException::class);

// Use reflection to test the protected method.
$method = new \ReflectionMethod($command, 'getBackupPath');
$method->setAccessible(true);
$this->assertSame($rollBackFile, $method->invoke($command, $rollBackDir));
}

/**
* @covers \MoodlePluginCI\Command\SelfUpdateCommand::getBackupPath
*/
public function testGetBackupPathExists()
{
$command = new SelfUpdateCommand();

// Try with a existing directory.
$rollBackDir = sys_get_temp_dir() . '/existing_dir';
(new Filesystem())->mkdir($rollBackDir); // Let's create the directory.
$rollBackFile = $rollBackDir . '/.moodle-plugin-ci/moodle-plugin-ci-old.phar';

// Use reflection to test the protected method.
$method = new \ReflectionMethod($command, 'getBackupPath');
$method->setAccessible(true);
$this->assertSame($rollBackFile, $method->invoke($command, $rollBackDir));
}
}

0 comments on commit 9fad129

Please sign in to comment.