| 1 | efrain | 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_analytics;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | require_once(__DIR__ . '/fixtures/test_indicator_max.php');
 | 
        
           |  |  | 22 | require_once(__DIR__ . '/fixtures/test_indicator_discrete.php');
 | 
        
           |  |  | 23 | require_once(__DIR__ . '/fixtures/test_indicator_min.php');
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | /**
 | 
        
           |  |  | 26 |  * Unit tests for the model.
 | 
        
           |  |  | 27 |  *
 | 
        
           |  |  | 28 |  * @package   core_analytics
 | 
        
           |  |  | 29 |  * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
 | 
        
           |  |  | 30 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 31 |  */
 | 
        
           | 1441 | ariadna | 32 | final class indicator_test extends \advanced_testcase {
 | 
        
           | 1 | efrain | 33 |     /**
 | 
        
           |  |  | 34 |      * test_validate_calculated_value
 | 
        
           |  |  | 35 |      *
 | 
        
           |  |  | 36 |      * @param string $indicatorclass
 | 
        
           |  |  | 37 |      * @param array $returnedvalue
 | 
        
           |  |  | 38 |      * @dataProvider validate_calculated_value
 | 
        
           |  |  | 39 |      * @return null
 | 
        
           |  |  | 40 |      */
 | 
        
           | 11 | efrain | 41 |     public function test_validate_calculated_value($indicatorclass, $returnedvalue): void {
 | 
        
           | 1 | efrain | 42 |         $indicator = new $indicatorclass();
 | 
        
           |  |  | 43 |         list($values, $unused) = $indicator->calculate([1], 'notrelevanthere');
 | 
        
           |  |  | 44 |         $this->assertEquals($returnedvalue, $values[0]);
 | 
        
           |  |  | 45 |     }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |     /**
 | 
        
           |  |  | 48 |      * Data provider for test_validate_calculated_value
 | 
        
           |  |  | 49 |      *
 | 
        
           |  |  | 50 |      * @return array
 | 
        
           |  |  | 51 |      */
 | 
        
           | 1441 | ariadna | 52 |     public static function validate_calculated_value(): array {
 | 
        
           | 1 | efrain | 53 |         return [
 | 
        
           |  |  | 54 |             'max' => ['test_indicator_max', [1]],
 | 
        
           |  |  | 55 |             'min' => ['test_indicator_min', [-1]],
 | 
        
           |  |  | 56 |             'discrete' => ['test_indicator_discrete', [0, 0, 0, 0, 1]],
 | 
        
           |  |  | 57 |         ];
 | 
        
           |  |  | 58 |     }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |     /**
 | 
        
           |  |  | 61 |      * test_validate_calculated_value_exceptions
 | 
        
           |  |  | 62 |      *
 | 
        
           |  |  | 63 |      * @param string $indicatorclass
 | 
        
           |  |  | 64 |      * @param string $willreturn
 | 
        
           |  |  | 65 |      * @dataProvider validate_calculated_value_exceptions
 | 
        
           |  |  | 66 |      * @return null
 | 
        
           |  |  | 67 |      */
 | 
        
           | 11 | efrain | 68 |     public function test_validate_calculated_value_exceptions($indicatorclass, $willreturn): void {
 | 
        
           | 1 | efrain | 69 |   | 
        
           |  |  | 70 |         $indicator = new $indicatorclass();
 | 
        
           |  |  | 71 |         $indicatormock = $this->getMockBuilder(get_class($indicator))
 | 
        
           |  |  | 72 |             ->onlyMethods(['calculate_sample'])
 | 
        
           |  |  | 73 |             ->getMock();
 | 
        
           |  |  | 74 |         $indicatormock->method('calculate_sample')->willReturn($willreturn);
 | 
        
           |  |  | 75 |         $this->expectException(\coding_exception::class);
 | 
        
           |  |  | 76 |         list($values, $unused) = $indicatormock->calculate([1], 'notrelevanthere');
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |     }
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |     /**
 | 
        
           |  |  | 81 |      * Data provider for test_validate_calculated_value_exceptions
 | 
        
           |  |  | 82 |      *
 | 
        
           |  |  | 83 |      * @return array
 | 
        
           |  |  | 84 |      */
 | 
        
           | 1441 | ariadna | 85 |     public static function validate_calculated_value_exceptions(): array {
 | 
        
           | 1 | efrain | 86 |         return [
 | 
        
           |  |  | 87 |             'max' => ['test_indicator_max', 2],
 | 
        
           |  |  | 88 |             'min' => ['test_indicator_min', -2],
 | 
        
           |  |  | 89 |             'discrete' => ['test_indicator_discrete', 7],
 | 
        
           |  |  | 90 |         ];
 | 
        
           |  |  | 91 |     }
 | 
        
           |  |  | 92 | }
 |