-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathExecution.php
More file actions
176 lines (139 loc) · 3.99 KB
/
Execution.php
File metadata and controls
176 lines (139 loc) · 3.99 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Pest\Browser;
use Amp\ByteStream\ReadableResourceStream;
use Pest\Browser\Playwright\Playwright;
use Pest\Support\Container;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestStatus\TestStatus;
use ReflectionClass;
use Symfony\Component\Console\Output\OutputInterface;
use function Amp\async;
use function Amp\delay;
/**
* @internal
*/
final class Execution
{
/**
* Either the current context
*/
private bool $waiting = false;
/**
* The current context instance, or null if not set.
*/
private static ?Execution $instance = null;
/**
* Creates a new context instance.
*/
public static function instance(): self
{
if (! self::$instance instanceof self) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Debugs the test execution by waiting for a key press.
*/
public function debug(TestStatus $status): void
{
$this->waiting = true;
// @phpstan-ignore-next-line
$testName = str_replace('__pest_evaluable_', '', test()->name());
$message = $status->message();
// @phpstan-ignore-next-line
Container::getInstance()->get(OutputInterface::class)->writeln(
"\n <info>Test [{$testName}] failed with the message:</info> {$message}\n"
);
// @phpstan-ignore-next-line
Container::getInstance()->get(OutputInterface::class)->writeln(
' <info>Press any key to continue...</info>'
);
$stdin = new ReadableResourceStream(STDIN);
async(function () use ($stdin): void {
while ($stdin->read() !== null) {
$stdin->close();
delay(0.1);
break;
}
})->await();
}
/**
* Pauses the execution.
*/
public function wait(int|float $seconds = 1): void
{
async(function () use ($seconds): void {
$this->waiting = true;
delay($seconds);
$this->waiting = false;
})->await();
}
/**
* Checks if the execution is paused.
*/
public function isWaiting(): bool
{
return $this->waiting;
}
/**
* Ticks the execution.
*/
public function tick(): void
{
delay(0);
}
/**
* Waits for a key press.
*/
public function waitForKey(): void
{
$this->waiting = true;
// @phpstan-ignore-next-line
Container::getInstance()->get(OutputInterface::class)->writeln(
' <info>Press any key to continue...</info>'
);
$stdin = new ReadableResourceStream(STDIN);
async(function () use ($stdin): void {
while ($stdin->read() !== null) {
$stdin->close();
delay(0.1);
break;
}
})->await();
}
/**
* Awaits for a condition to be met, retrying until the timeout is reached.
*/
public function waitForExpectation(callable $callback): mixed
{
$timeout = Playwright::timeout();
$originalCount = Assert::getCount();
$start = microtime(true);
$end = $start + ($timeout / 1_000);
while (microtime(true) < $end) {
try {
return Playwright::usingTimeout(1_000, $callback);
} catch (ExpectationFailedException) {
//
}
$this->resetAssertions($originalCount);
self::instance()->tick();
}
return $callback();
}
/**
* Resets the assertion count to the original value.
*/
private function resetAssertions(int $originalCount): void
{
if (Assert::getCount() === $originalCount) {
return;
}
$reflector = new ReflectionClass(Assert::class);
$property = $reflector->getProperty('count');
$property->setValue(null, $originalCount);
}
}