Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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;
18
 
19
use frozen_clock;
20
use incrementing_clock;
21
 
22
/**
23
 * Tests for testing clocks.
24
 *
25
 * @package    core
26
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
final class clock_test extends \advanced_testcase {
30
    /**
31
     * Test the incrementing mock clock.
32
     *
33
     * @covers \incrementing_clock
34
     */
35
    public function test_clock_with_incrementing(): void {
36
        require_once(__DIR__ . '/../classes/incrementing_clock.php');
37
 
38
        $clock = new incrementing_clock();
39
        $this->assertInstanceOf(\incrementing_clock::class, $clock);
40
 
41
        $initialtime = $clock->now()->getTimestamp();
42
 
43
        // Test the functionality.
44
        $this->assertEquals($initialtime + 1, $clock->now()->getTimestamp());
45
        $this->assertEquals($initialtime + 2, $clock->time());
46
        $this->assertEquals($initialtime + 3, $clock->now()->getTimestamp());
47
 
48
        // Specify a specific start time.
49
        $clock = new incrementing_clock(12345);
50
 
51
        $this->assertEquals(12345, $clock->now()->getTimestamp());
52
        $this->assertEquals(12346, $clock->time());
53
        $this->assertEquals(12347, $clock->now()->getTimestamp());
54
 
55
        $clock->set_to(12345);
56
        $this->assertEquals(12345, $clock->time());
57
        $this->assertEquals(12346, $clock->time());
58
 
59
        $clock->bump();
60
        $this->assertEquals(12348, $clock->time());
61
        $clock->bump();
62
        $this->assertEquals(12350, $clock->time());
63
        $clock->bump(5);
64
        $this->assertEquals(12356, $clock->time());
65
    }
66
 
67
    /**
68
     * Test the incrementing mock clock.
69
     *
70
     * @covers \frozen_clock
71
     */
72
    public function test_mock_clock_with_frozen(): void {
73
        require_once(__DIR__ . '/../classes/frozen_clock.php');
74
 
75
        $clock = new frozen_clock();
76
 
77
        // Test the functionality.
78
        $initialtime = $clock->now()->getTimestamp();
79
        $this->assertEquals($initialtime, $clock->now()->getTimestamp());
80
        $this->assertEquals($initialtime, $clock->now()->getTimestamp());
81
        $this->assertEquals($initialtime, $clock->now()->getTimestamp());
82
        $this->assertEquals($initialtime, $clock->time());
83
 
84
        // Specify a specific start time.
85
        $clock = new frozen_clock(12345);
86
 
87
        $initialtime = $clock->now();
88
        $this->assertEquals($initialtime, $clock->now());
89
        $this->assertEquals($initialtime, $clock->now());
90
        $this->assertEquals($initialtime, $clock->now());
91
 
92
        $clock->set_to(12345);
93
        $this->assertEquals(12345, $clock->now()->getTimestamp());
94
        $this->assertEquals(12345, $clock->now()->getTimestamp());
95
        $this->assertEquals(12345, $clock->now()->getTimestamp());
96
 
97
        $this->assertEquals(12345, $clock->time());
98
 
99
        $clock->bump();
100
        $this->assertEquals(12346, $clock->time());
101
        $clock->bump();
102
        $this->assertEquals(12347, $clock->time());
103
        $clock->bump(5);
104
        $this->assertEquals(12352, $clock->time());
105
    }
106
}