-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathClock.php
More file actions
138 lines (114 loc) · 3.77 KB
/
Clock.php
File metadata and controls
138 lines (114 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace Pest\Browser\Playwright;
use DateTimeInterface;
use Generator;
use Pest\Browser\Playwright\Concerns\InteractsWithPlaywright;
/**
* @internal
*/
final readonly class Clock
{
use InteractsWithPlaywright;
/**
* Creates a new clock instance.
*/
public function __construct(
private string $targetGuid,
) {
//
}
/**
* Install fake implementations for time-related functions.
*
* @param array<string, mixed>|null $options Options including time to initialize with
*/
public function install(?array $options = null): void
{
$params = [];
if (isset($options['time'])) {
$params['time'] = $this->normalizeTime($options['time']);
}
$response = $this->sendMessage('clockInstall', $params);
$this->processVoidResponse($response);
}
/**
* Advance the clock, firing all the time-related callbacks.
*
* @param int|string $ticks Time in milliseconds or human-readable string like "30:00"
*/
public function runFor(int|string $ticks): void
{
$response = $this->sendMessage('clockRunFor', ['ticks' => $ticks]);
$this->processVoidResponse($response);
}
/**
* Advance the clock by jumping forward in time.
* Only fires due timers at most once.
*
* @param int|string $ticks Time in milliseconds or human-readable string like "30:00"
*/
public function fastForward(int|string $ticks): void
{
$response = $this->sendMessage('clockFastForward', ['ticks' => $ticks]);
$this->processVoidResponse($response);
}
/**
* Advance the clock by jumping forward in time and pause the time.
* Once called, no timers are fired unless other clock methods are called.
*
* @param int|string|DateTimeInterface $time Time to pause at
*/
public function pauseAt(int|string|DateTimeInterface $time): void
{
$response = $this->sendMessage('clockPauseAt', ['time' => $this->normalizeTime($time)]);
$this->processVoidResponse($response);
}
/**
* Resumes timers. Once called, time resumes flowing and timers fire as usual.
*/
public function resume(): void
{
$response = $this->sendMessage('clockResume');
$this->processVoidResponse($response);
}
/**
* Makes Date.now and new Date() return fixed fake time at all times.
* Keeps all the timers running.
*
* @param int|string|DateTimeInterface $time Time to be set
*/
public function setFixedTime(int|string|DateTimeInterface $time): void
{
$response = $this->sendMessage('clockSetFixedTime', ['time' => $this->normalizeTime($time)]);
$this->processVoidResponse($response);
}
/**
* Sets system time but does not trigger any timers.
* Use this to test how the web page reacts to a time shift.
*
* @param int|string|DateTimeInterface $time Time to be set
*/
public function setSystemTime(int|string|DateTimeInterface $time): void
{
$response = $this->sendMessage('clockSetSystemTime', ['time' => $this->normalizeTime($time)]);
$this->processVoidResponse($response);
}
/**
* Normalize time parameter to the format expected by Playwright.
*/
private function normalizeTime(int|string|DateTimeInterface $time): int|string
{
if ($time instanceof DateTimeInterface) {
return $time->format('c'); // ISO 8601 format
}
return $time;
}
/**
* @param array<string, mixed> $params
*/
private function sendMessage(string $method, array $params = []): Generator
{
return Client::instance()->execute($this->targetGuid, $method, $params);
}
}