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
/**
18
 * Unit tests for the join trait
19
 *
20
 * @package     core_reportbuilder
21
 * @copyright   2024 Paul Holden <paulh@moodle.com>
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
declare(strict_types=1);
26
 
27
namespace core_reportbuilder\local\helpers;
28
 
29
use advanced_testcase;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
/**
34
 * Unit tests for the join trait
35
 *
36
 * @package     core_reportbuilder
37
 * @covers      \core_reportbuilder\local\helpers\join_trait
38
 * @copyright   2024 Paul Holden <paulh@moodle.com>
39
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
final class join_trait_test extends advanced_testcase {
42
 
43
    /**
44
     * Test adding single join
45
     */
46
    public function test_add_join(): void {
47
        $trait = new join_trait_mock();
48
        $trait->add_join('JOIN {test} t ON t.id = a.id');
49
 
50
        $this->assertEquals(['JOIN {test} t ON t.id = a.id'], $trait->get_joins());
51
    }
52
 
53
    /**
54
     * Test adding single join multiple times
55
     */
56
    public function test_add_join_multiple(): void {
57
        $trait = new join_trait_mock();
58
 
59
        // Add multiple joins, two of which are duplicates.
60
        $trait->add_join('JOIN {test} t1 ON t1.id = a.id')
61
            ->add_join('JOIN {test} t2 ON t2.id = b.id')
62
            ->add_join('JOIN {test} t1 ON t1.id = a.id');
63
 
64
        // The duplicated join is normalised away.
65
        $this->assertEquals([
66
            'JOIN {test} t1 ON t1.id = a.id',
67
            'JOIN {test} t2 ON t2.id = b.id',
68
        ], $trait->get_joins());
69
    }
70
 
71
    /**
72
     * Test adding multiple joins
73
     */
74
    public function test_add_joins(): void {
75
        $trait = new join_trait_mock();
76
 
77
        // Add multiple joins, two of which are duplicates.
78
        $trait->add_joins([
79
            'JOIN {test} t1 ON t1.id = a.id',
80
            'JOIN {test} t2 ON t2.id = b.id',
81
            'JOIN {test} t1 ON t1.id = a.id',
82
        ]);
83
 
84
        // The duplicated join is normalised away.
85
        $this->assertEquals([
86
            'JOIN {test} t1 ON t1.id = a.id',
87
            'JOIN {test} t2 ON t2.id = b.id',
88
        ], $trait->get_joins());
89
    }
90
}
91
 
92
/**
93
 * Simple implementation of a class using the trait
94
 */
95
final class join_trait_mock {
96
    use join_trait;
97
}