Repository navigation
Expand file tree
/
Copy pathTestCase.php
More file actions
93 lines (81 loc) · 2.86 KB
/
Copy pathTestCase.php
File metadata and controls
93 lines (81 loc) · 2.86 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
<?php
declare(strict_types=1);
namespace Crustum\Stimulus\Test;
use Cake\Http\BaseApplication;
use Cake\TestSuite\TestCase as CakeTestCase;
use Crustum\Stimulus\Test\Support\FakeClient;
use Crustum\Stimulus\Test\Support\FakeWorker;
use Crustum\Stimulus\Worker\ApplicationFactory;
use Mockery;
use TestApp\Application;
/**
* Default test case for Stimulus plugin tests.
*
* Boots the test application through the worker application factory and
* returns the booted fake worker. Test classes must extend this base
* instead of the raw PHPUnit case so worker construction stays in one place.
*/
abstract class TestCase extends CakeTestCase
{
/**
* Builds a booted worker context for the given requests.
*
* Returns an `[$app, $worker, $client]` triple. The factory boots the test
* application, the fake worker extends the real worker, and the client
* records responses and errors in memory.
*
* @param array<int, mixed> $requests Requests or task payloads to serve.
* @return array{0: \Cake\Http\BaseApplication, 1: \Crustum\Stimulus\Test\Support\FakeWorker, 2: \Crustum\Stimulus\Test\Support\FakeClient} Booted application, worker, and client triple.
*/
protected function createStimulusContext(array $requests): array
{
$factory = new ApplicationFactory(Application::class, CONFIG);
$client = new FakeClient($requests);
$worker = new FakeWorker($factory, $client);
$worker->boot();
return [$worker->application(), $worker, $client];
}
/**
* Builds the application factory booting the test application.
*
* Hands tests the factory used for the application under test.
*
* @return \Crustum\Stimulus\Worker\ApplicationFactory Factory booting the test application.
*/
protected function appFactory(): ApplicationFactory
{
return new ApplicationFactory(Application::class, CONFIG);
}
/**
* Boots the test application through the application factory.
*
* Applies the default warm services, so factory tests share the worker boot path.
*
* @return \Cake\Http\BaseApplication Booted test application.
*/
protected function createApplication(): BaseApplication
{
return $this->appFactory()->createApplication();
}
/**
* Loads the plugin configuration array.
*
* Returns the plugin configuration file contents for Swoole table sizing.
*
* @return array<string, mixed> Plugin configuration file contents.
*/
protected function config(): array
{
return require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'stimulus.php';
}
/**
* Closes Mockery expectations after each test.
*
* @return void
*/
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}