Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core_ai\aiactions;
18
 
19
use core_ai\aiactions\responses\response_base;
20
use core_ai\aiactions;
21
use ReflectionClass;
22
 
23
/**
24
 * Test response_base action methods.
25
 *
26
 * @package    core_ai
27
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @covers     \core_ai\aiactions\base
30
 */
31
final class base_test extends \advanced_testcase {
32
    /**
33
     * Test get_basename.
34
     */
35
    public function test_get_basename(): void {
36
        $basename = aiactions\generate_text::get_basename();
37
        $this->assertEquals('generate_text', $basename);
38
    }
39
 
40
    /**
41
     * Test get_name.
42
     */
43
    public function test_get_name(): void {
44
        $this->assertEquals(
45
            get_string('action_generate_text', 'core_ai'),
46
            aiactions\generate_text::get_name()
47
        );
48
    }
49
 
50
    /**
51
     * Test get_description.
52
     */
53
    public function test_get_description(): void {
54
        $this->assertEquals(
55
            get_string('action_generate_text_desc', 'core_ai'),
56
            aiactions\generate_text::get_description()
57
        );
58
    }
59
 
60
    /**
61
     * Test that every action class implements a constructor.
62
     */
63
    public function test_constructor(): void {
64
        $classes = [];
65
 
66
        $contextid = 1;
67
        // Create an anonymous class that extends the base class.
68
        $base = new class($contextid) extends \core_ai\aiactions\base {
69
            /**
70
             * Store the response.
71
             * @param response_base $response
72
             * @return int
73
             */
74
            public function store(response_base $response): int {
75
                return 0;
76
            }
77
        };
78
 
79
        $reflection = new ReflectionClass($base); // Create a ReflectionClass for the anonymous class.
80
 
81
        // Use the location of the base class to get the AI action classes.
82
        $filepath = $reflection->getParentClass()->getFileName();
83
        $directory = dirname($filepath);
84
        $files = scandir($directory);
85
 
86
        foreach ($files as $file) {
87
            // Match files that are PHP files and not specifically just 'base.*.php'.
88
            if (str_ends_with($file, '.php')) {
89
                $classname = str_replace('.php', '', $file);
90
                $classes[] = 'core_ai\\aiactions\\' . $classname;
91
            }
92
        }
93
 
94
        // Ensure that some classes were found.
95
        $this->assertNotEmpty($classes, 'No classes were found for testing.');
96
 
97
        // For each action class, check that they have a constructor.
98
        foreach ($classes as $class) {
99
            $reflection = new ReflectionClass($class);
100
            $constructor = $reflection->getConstructor();
101
            $this->assertNotNull($constructor, 'Class ' . $class . ' does not have a constructor.');
102
        }
103
    }
104
 
105
}