-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathIntegrationTest.php
More file actions
274 lines (218 loc) · 7.76 KB
/
Copy pathIntegrationTest.php
File metadata and controls
274 lines (218 loc) · 7.76 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
namespace Tempest\Framework\Testing;
use Closure;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Tempest\Auth\Authentication\Authenticatable;
use Tempest\Auth\Authentication\Authenticator;
use Tempest\Auth\OAuth\Testing\OAuthTester;
use Tempest\Cache\Testing\CacheTester;
use Tempest\Clock\Clock;
use Tempest\Clock\MockClock;
use Tempest\Console\Output\MemoryOutputBuffer;
use Tempest\Console\Output\StdoutOutputBuffer;
use Tempest\Console\OutputBuffer;
use Tempest\Console\Testing\ConsoleTester;
use Tempest\Container\GenericContainer;
use Tempest\Core\Exceptions\ExceptionTester;
use Tempest\Core\FrameworkKernel;
use Tempest\Database\DatabaseInitializer;
use Tempest\Database\Testing\DatabaseTester;
use Tempest\DateTime\DateTimeInterface;
use Tempest\Discovery\DiscoveryLocation;
use Tempest\EventBus\Testing\EventBusTester;
use Tempest\Framework\Testing\Http\HttpRouterTester;
use Tempest\Framework\Testing\View\ViewTester;
use Tempest\Http\GenericRequest;
use Tempest\Http\Method;
use Tempest\Http\Request;
use Tempest\Mail\Testing\MailTester;
use Tempest\Mail\Testing\TestingMailer;
use Tempest\Process\Testing\ProcessTester;
use Tempest\Storage\Testing\StorageTester;
use Throwable;
use function Tempest\env;
use function Tempest\Support\Path\normalize;
use function Tempest\Support\Path\to_absolute_path;
/**
* @mago-expect lint:too-many-properties
*/
abstract class IntegrationTest extends TestCase
{
protected string $root;
protected string $internalStorage;
/** @var \Tempest\Discovery\DiscoveryLocation[] */
protected array $discoveryLocations = [];
protected FrameworkKernel $kernel;
protected GenericContainer $container;
protected ConsoleTester $console;
/**
* Provides utilities for testing HTTP routes.
*/
protected HttpRouterTester $http;
/**
* Provides utilities for testing installers.
*/
protected InstallerTester $installer;
/**
* Provides utilities for testing the Vite integration.
*/
protected ViteTester $vite;
/**
* Provides utilities for testing the event bus.
*/
protected EventBusTester $eventBus;
/**
* Provides utilities for testing storage management.
*/
protected StorageTester $storage;
/**
* Provides utilities for testing emails.
*/
protected MailTester $mailer;
/**
* Provides utilities for testing the cache.
*/
protected CacheTester $cache;
/**
* Provides utilities for testing exception reporting.
*/
protected ExceptionTester $exceptions;
/**
* Provides utilities for testing process execution.
*/
protected ProcessTester $process;
/**
* Provides utilities for testing OAuth flows.
*/
protected OAuthTester $oauth;
/**
* Provides utilities for testing the database.
*/
protected DatabaseTester $database;
/**
* Provides utilities for testing views.
*/
protected ViewTester $view;
protected function setUp(): void
{
parent::setUp();
$this
->setupKernel()
->setupConsole()
->setupTesters()
->setupBaseRequest();
}
/**
* Returns an array of DiscoveryLocations that should be discovered only during testing
* @return \Tempest\Discovery\DiscoveryLocation[]
*/
protected function discoverTestLocations(): array
{
$discoveryLocations = [];
$testsPath = to_absolute_path($this->root, 'tests');
if (is_dir($testsPath)) {
$discoveryLocations[] = new DiscoveryLocation('Tests', $testsPath);
}
return $discoveryLocations;
}
protected function setupKernel(): self
{
// We force forward slashes for consistency even on Windows.
$this->root ??= normalize(realpath(getcwd()));
$this->internalStorage = $this->root . '/.tempest/test_internal_storage/' . env('TEST_TOKEN', 'default');
$discoveryLocations = [...$this->discoveryLocations, ...$this->discoverTestLocations()];
$this->kernel ??= FrameworkKernel::boot(
root: $this->root,
discoveryLocations: $discoveryLocations,
internalStorage: $this->internalStorage,
);
/** @var GenericContainer $container */
$container = $this->kernel->container;
$this->container = $container;
return $this;
}
protected function setupConsole(): self
{
$this->console = new ConsoleTester($this->container);
$this->container->singleton(OutputBuffer::class, fn () => new MemoryOutputBuffer());
$this->container->singleton(StdoutOutputBuffer::class, fn () => new MemoryOutputBuffer());
return $this;
}
protected function setupTesters(): self
{
$this->http = new HttpRouterTester($this->container);
$this->installer = new InstallerTester($this->container);
$this->eventBus = new EventBusTester($this->container);
$this->storage = new StorageTester($this->container);
$this->cache = new CacheTester($this->container);
$this->mailer = new MailTester(new TestingMailer());
$this->process = $this->container->get(ProcessTester::class);
$this->process->disableProcessExecution();
$this->exceptions = $this->container->get(ExceptionTester::class);
$this->exceptions->preventProcessing();
$this->vite = $this->container->get(ViteTester::class);
$this->vite->preventTagResolution();
$this->vite->clearCaches();
$this->oauth = new OAuthTester($this->container);
$this->database = new DatabaseTester($this->container);
$this->view = new ViewTester($this->container);
return $this;
}
protected function setupBaseRequest(): self
{
$request = new GenericRequest(Method::GET, '/', []);
$this->container->singleton(Request::class, fn () => $request);
$this->container->singleton(GenericRequest::class, fn () => $request);
return $this;
}
protected function useTestingDatabase(): self
{
$this->container->removeInitializer(DatabaseInitializer::class);
$this->container->addInitializer(TestingDatabaseInitializer::class);
return $this;
}
protected function clock(DateTimeInterface|string $now = 'now'): MockClock
{
$clock = new MockClock($now);
$this->container->singleton(Clock::class, fn () => $clock);
return $clock;
}
protected function tearDown(): void
{
parent::tearDown();
/** @phpstan-ignore-next-line */
unset($this->root);
/** @phpstan-ignore-next-line */
unset($this->discoveryLocations);
/** @phpstan-ignore-next-line */
unset($this->kernel);
/** @phpstan-ignore-next-line */
unset($this->container);
/** @phpstan-ignore-next-line */
unset($this->console);
/** @phpstan-ignore-next-line */
unset($this->http);
/** @phpstan-ignore-next-line */
unset($this->oauth);
GenericContainer::setInstance(null);
}
protected function assertException(string $expectedExceptionClass, Closure $handler, ?Closure $assertException = null, ?string $message = null): void
{
try {
$handler();
} catch (Throwable $throwable) {
$this->assertInstanceOf($expectedExceptionClass, $throwable);
if ($assertException instanceof Closure) {
$assertException($throwable);
}
return;
}
Assert::fail($message ?? "Expected exception {$expectedExceptionClass} was not thrown");
}
protected function login(Authenticatable $user): void
{
$this->container->get(Authenticator::class)->authenticate($user);
}
}