forked from moodlehq/moodle-plugin-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |