|
| 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