Skip to content

Commit 9fad129

Browse files
committed
Add tests to cover what is testable in SelfUpdate command
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.
1 parent 8124fe7 commit 9fad129

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/Command/SelfUpdateCommand.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9393
}
9494

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Moodle Plugin CI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @copyright 2023 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
10+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
11+
*/
12+
13+
namespace Command;
14+
15+
use MoodlePluginCI\Command\SelfUpdateCommand;
16+
use Symfony\Component\Filesystem\Filesystem;
17+
18+
/**
19+
* Tests for the SelfUpdateCommand class.
20+
*
21+
* There isn't much to test here, only helper methods. Note that the utility itself
22+
* will be covered by some integration tests @ CIs.
23+
*/
24+
class SelfUpdateCommandTest extends \PHPUnit\Framework\TestCase
25+
{
26+
/**
27+
* @covers \MoodlePluginCI\Command\SelfUpdateCommand::getBackupPath
28+
*/
29+
public function testGetBackupPathNotExists()
30+
{
31+
$command = new SelfUpdateCommand();
32+
33+
// Try with a non-existing directory.
34+
$rollBackDir = sys_get_temp_dir() . '/not_existing_dir';
35+
$rollBackFile = $rollBackDir . '/.moodle-plugin-ci/moodle-plugin-ci-old.phar';
36+
37+
$this->expectException(\RuntimeException::class);
38+
39+
// Use reflection to test the protected method.
40+
$method = new \ReflectionMethod($command, 'getBackupPath');
41+
$method->setAccessible(true);
42+
$this->assertSame($rollBackFile, $method->invoke($command, $rollBackDir));
43+
}
44+
45+
/**
46+
* @covers \MoodlePluginCI\Command\SelfUpdateCommand::getBackupPath
47+
*/
48+
public function testGetBackupPathExists()
49+
{
50+
$command = new SelfUpdateCommand();
51+
52+
// Try with a existing directory.
53+
$rollBackDir = sys_get_temp_dir() . '/existing_dir';
54+
(new Filesystem())->mkdir($rollBackDir); // Let's create the directory.
55+
$rollBackFile = $rollBackDir . '/.moodle-plugin-ci/moodle-plugin-ci-old.phar';
56+
57+
// Use reflection to test the protected method.
58+
$method = new \ReflectionMethod($command, 'getBackupPath');
59+
$method->setAccessible(true);
60+
$this->assertSame($rollBackFile, $method->invoke($command, $rollBackDir));
61+
}
62+
}

0 commit comments

Comments
 (0)