Skip to content

Commit edc1da6

Browse files
committed
[docs] Document new externallib_testcase
1 parent 70cdc6a commit edc1da6

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

docs/apis/subsystems/external/testing.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ Writing unit tests for an external service function is no different to writing u
2020

2121
## How to write an external function PHPUnit test
2222

23-
You should create one unit test testcase for each external service file, and it should be named after the file that it tests.
23+
You should create one unit test testcase for each external service class, and it should be named after the class that it tests.
2424

2525
For example, if you have written a service function in `[componentfolder]/classes/external/get_fruit.php`, you should write a unit test in `[componentfolder]/tests/external/get_fruit_test.php`.
2626

27+
:::note External Services Testcase
28+
29+
An external testcase has been created to make testing external services easier.
30+
31+
When creating your tests, you can extend the `\core_external\tests\externallib_testcase` class instead of `\advanced_testcase`.
32+
33+
:::
34+
2735
```php title="mod/kitchen/tests/external/get_fruit_test.php"
2836
<?php
2937
// This file is part of Moodle - http://moodle.org/
@@ -41,28 +49,20 @@ For example, if you have written a service function in `[componentfolder]/classe
4149
// You should have received a copy of the GNU General Public License
4250
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
4351

52+
namespace mod_kitchen\external;
53+
4454
/**
4555
* Unit tests for the get_fruit function of the kitchen.
4656
*
4757
* @package mod_kitchen
4858
* @category external
49-
* @copyright 20XX Your Name
59+
* @copyright Your Name
5060
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5161
*/
52-
53-
namespace mod_kitchen\external;
54-
55-
defined('MOODLE_INTERNAL') || die();
56-
57-
global $CFG;
58-
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
59-
60-
class get_fruit_test extends externallib_advanced_testcase {
61-
62+
#[\PHPUnit\Framework\Attributes\CoversClass(get_fruit::class)]
63+
class get_fruit_test extends \core_external\tests\externallib_testcase {
6264
/**
6365
* Test the execute function when capabilities are present.
64-
*
65-
* @covers \mod_fruit\external\get_fruit::execute
6666
*/
6767
public function test_capabilities(): void {
6868
$this->resetAfterTest(true);
@@ -96,8 +96,6 @@ class get_fruit_test extends externallib_advanced_testcase {
9696

9797
/**
9898
* Test the execute function when capabilities are missing.
99-
*
100-
* @covers \mod_fruit\external\get_fruit::execute
10199
*/
102100
public function test_capabilities_missing(): void {
103101
global $USER;

0 commit comments

Comments
 (0)