|
| 1 | +<?php |
| 2 | + |
| 3 | +// This file is part of the Moodle Plugin CI package. |
| 4 | +// |
| 5 | +// Moodle is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// Moodle is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +namespace MoodlePluginCI\Tests\Command; |
| 19 | + |
| 20 | +use MoodlePluginCI\Bridge\MoodleConfig; |
| 21 | +use MoodlePluginCI\Command\PHPDocCommand; |
| 22 | +use MoodlePluginCI\Installer\Database\MySQLDatabase; |
| 23 | +use MoodlePluginCI\Tests\Fake\Bridge\DummyMoodlePHPDoc; |
| 24 | +use MoodlePluginCI\Tests\Fake\Bridge\DummyMoodlePlugin; |
| 25 | +use MoodlePluginCI\Tests\Fake\Process\DummyExecute; |
| 26 | +use MoodlePluginCI\Tests\MoodleTestCase; |
| 27 | +use Symfony\Component\Console\Application; |
| 28 | +use Symfony\Component\Console\Tester\CommandTester; |
| 29 | + |
| 30 | +/** |
| 31 | + * Unit tests for PHPDocCommand. |
| 32 | + * |
| 33 | + * @copyright 2023 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com} |
| 34 | + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 35 | + */ |
| 36 | +class PHPDocCommandTest extends MoodleTestCase |
| 37 | +{ |
| 38 | + protected function executeCommand($pluginDir = null, $maxWarnings = -1, $mockOutput = ''): CommandTester |
| 39 | + { |
| 40 | + if ($pluginDir === null) { |
| 41 | + $pluginDir = $this->pluginDir; |
| 42 | + } |
| 43 | + |
| 44 | + $command = new PHPDocCommand(); |
| 45 | + $command->moodle = new DummyMoodlePHPDoc($this->moodleDir); |
| 46 | + $command->plugin = new DummyMoodlePlugin($pluginDir); |
| 47 | + $command->execute = new DummyExecute(); // Mocked execution. |
| 48 | + $command->execute->returnOutput = $mockOutput; // Mocked output. |
| 49 | + |
| 50 | + // Note: we leave this here as reference for any other command test needing a config.php file, |
| 51 | + // but it's not needed for this unit tess that is using a mocked execute() method. |
| 52 | + // Create a basic config.php file. This command requires it. |
| 53 | + // $config = new MoodleConfig(); |
| 54 | + // $configContents = $config->createContents(new MySQLDatabase(), '/path/to/moodledata'); |
| 55 | + // $config->dump($this->moodleDir . DIRECTORY_SEPARATOR . 'config.php', $configContents); |
| 56 | + |
| 57 | + $application = new Application(); |
| 58 | + $application->add($command); |
| 59 | + |
| 60 | + $options = ['plugin' => $pluginDir]; |
| 61 | + if ($maxWarnings >= 0) { |
| 62 | + $options['--max-warnings'] = $maxWarnings; |
| 63 | + } |
| 64 | + |
| 65 | + $commandTester = new CommandTester($application->find('phpdoc')); |
| 66 | + $commandTester->execute($options); |
| 67 | + |
| 68 | + return $commandTester; |
| 69 | + } |
| 70 | + |
| 71 | + public function testExecute() |
| 72 | + { |
| 73 | + $commandTester = $this->executeCommand(); |
| 74 | + $this->assertSame(0, $commandTester->getStatusCode()); |
| 75 | + |
| 76 | + // Verify various parts of the output. |
| 77 | + $output = $commandTester->getDisplay(); |
| 78 | + |
| 79 | + // Also verify display info is correct. |
| 80 | + $this->assertMatchesRegularExpression('/RUN Moodle PHPDoc Checker on local_ci/', $output); |
| 81 | + } |
| 82 | + |
| 83 | + public function testExecuteFail() |
| 84 | + { |
| 85 | + $mockOutput = ' Line 12: Some error happened'; |
| 86 | + $commandTester = $this->executeCommand($this->pluginDir, -1, $mockOutput); |
| 87 | + $this->assertSame(1, $commandTester->getStatusCode()); |
| 88 | + |
| 89 | + // Verify various parts of the output. |
| 90 | + // Note: Because this is a mocked process, with mocked output, we cannot inspect the command output |
| 91 | + // as we do in other tests. Only the title is available. |
| 92 | + // Some day all the mocks should be refactored to allow this. |
| 93 | + // $output = $commandTester->getDisplay(); |
| 94 | + // $this->assertMatchesRegularExpression('xxxxxxx/', $output); // Progress. |
| 95 | + |
| 96 | + // Also verify display info is correct. |
| 97 | + $this->assertMatchesRegularExpression('/RUN Moodle PHPDoc Checker/', $commandTester->getDisplay()); |
| 98 | + } |
| 99 | + |
| 100 | + public function testExecuteWithWarningsAndThreshold() |
| 101 | + { |
| 102 | + // Let's add a file with 2 warnings, and verify how the max-warnings affects the outcome. |
| 103 | + $mockOutput = <<<'EOT' |
| 104 | + Line 2: Empty line found after PHP open tag (warning) |
| 105 | + Line 12: Function someclass::somefunc is not documented (warning) |
| 106 | + |
| 107 | +EOT; |
| 108 | + // By default it passes. |
| 109 | + $commandTester = $this->executeCommand($this->pluginDir, -1, $mockOutput); |
| 110 | + $this->assertSame(0, $commandTester->getStatusCode()); |
| 111 | + |
| 112 | + // Allowing 0 warning, it fails. |
| 113 | + $commandTester = $this->executeCommand($this->pluginDir, 0, $mockOutput); |
| 114 | + $this->assertSame(1, $commandTester->getStatusCode()); |
| 115 | + |
| 116 | + // Allowing 1 warning, it fails. |
| 117 | + $commandTester = $this->executeCommand($this->pluginDir, 1, $mockOutput); |
| 118 | + $this->assertSame(1, $commandTester->getStatusCode()); |
| 119 | + |
| 120 | + // Allowing 2 warnings, it passes. |
| 121 | + $commandTester = $this->executeCommand($this->pluginDir, 2, $mockOutput); |
| 122 | + $this->assertSame(0, $commandTester->getStatusCode()); |
| 123 | + |
| 124 | + // Allowing 3 warnings, it passes. |
| 125 | + $commandTester = $this->executeCommand($this->pluginDir, 3, $mockOutput); |
| 126 | + $this->assertSame(0, $commandTester->getStatusCode()); |
| 127 | + } |
| 128 | + |
| 129 | + public function testExecuteNoFiles() |
| 130 | + { |
| 131 | + // Just random directory with no PHP files. |
| 132 | + $commandTester = $this->executeCommand($this->pluginDir . '/tests/behat'); |
| 133 | + $this->assertSame(0, $commandTester->getStatusCode()); |
| 134 | + $this->assertMatchesRegularExpression('/No relevant files found to process, free pass!/', $commandTester->getDisplay()); |
| 135 | + } |
| 136 | + |
| 137 | + public function testExecuteNoPlugin() |
| 138 | + { |
| 139 | + $this->expectException(\InvalidArgumentException::class); |
| 140 | + $this->executeCommand('/path/to/no/plugin'); |
| 141 | + } |
| 142 | +} |
0 commit comments