-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifecycleTest.php
178 lines (146 loc) · 5.86 KB
/
LifecycleTest.php
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
<?php
/**
* Enable, disable, and uninstall an extension. Ensure that various local example is enabled and disabled appropriately.
*
* The substantive assertions are split across various files in `tests/mixins/*.php`.
*
* @group e2e
* @see cv
*/
class E2E_Shimmy_LifecycleTest extends \PHPUnit\Framework\TestCase implements \Civi\Test\EndToEndInterface {
/**
* @var array
*/
protected $mixinTests;
protected function setUp(): void {
$this->assertNotEquals('UnitTests', getenv('CIVICRM_UF'), 'This is an end-to-end test involving CLI and HTTP. CIVICRM_UF should not be set to UnitTests.');
parent::setUp();
$this->mixinTests = [];
$mixinTestFiles = (array) glob($this->getPath('/tests/mixin/*Test.php'));
foreach ($mixinTestFiles as $file) {
require_once $file;
$class = '\\Civi\Shimmy\\Mixins\\' . preg_replace(';\.php$;', '', basename($file));
$this->mixinTests[] = new $class();
}
}
/**
* Install and uninstall the extension. Ensure that various mixins+artifacts work correctly.
*
* This interacts with Civi by running many subprocesses (`cv api3` and `cv api4` commands).
* This style of interaction is a better representation of how day-to-day sysadmin works.
*/
public function testLifecycleWithSubprocesses(): void {
$this->runLifecycle($this->createCvWithSubprocesses());
}
/**
* Install and uninstall the extension. Ensure that various mixins+artifacts work correctly.
*
* This interacts with Civi by calling local PHP functions (`civicrm_api3(` and `civicrm_api4()`).
* This style of interaction reveals whether the install/uninstall mechanics have data-leaks that
* may cause subtle/buggy interactions during the transitions.
*/
public function testLifecycleWithLocalFunctions(): void {
$this->runLifecycle($this->createCvWithLocalFunctions());
}
/**
* @param $cv
*/
private function runLifecycle($cv): void {
$this->runMethods('testPreConditions', $cv);
// Clear out anything from previous runs.
$cv->api3('Extension', 'disable', ['key' => 'shimmy']);
$cv->api3('Extension', 'uninstall', ['key' => 'shimmy']);
// The main show.
$cv->api3('Extension', 'enable', ['key' => 'shimmy']);
$this->runMethods('testInstalled', $cv);
// This is a duplicate - make sure things still work after an extra run.
$cv->api3('Extension', 'enable', ['key' => 'shimmy']);
$this->runMethods('testInstalled', $cv);
// OK, how's the cleanup?
$cv->api3('Extension', 'disable', ['key' => 'shimmy']);
$this->runMethods('testDisabled', $cv);
$cv->api3('Extension', 'uninstall', ['key' => 'shimmy']);
$this->runMethods('testUninstalled', $cv);
}
protected static function getPath($suffix = ''): string {
return dirname(__DIR__, 4) . $suffix;
}
protected function runMethods(string $method, ...$args) {
if (empty($this->mixinTests)) {
$this->fail('Cannot run methods. No mixin tests found.');
}
foreach ($this->mixinTests as $test) {
$test->$method(...$args);
}
}
protected function createCvWithLocalFunctions() {
return new class {
public function api3($entity, $action, $params) {
return civicrm_api3($entity, $action, $params);
}
public function api4($entity, $action, $params): array {
$params = array_merge(['checkPermissions' => FALSE], $params);
return (array) civicrm_api4($entity, $action, $params);
}
};
}
protected function createCvWithSubprocesses() {
return new class {
public function api3($entity, $action, $params) {
return $this->cv('api3 --in=json ' . escapeshellarg("$entity.$action"), json_encode($params));
}
public function api4($entity, $action, $params): array {
$params = array_merge(['checkPermissions' => FALSE], $params);
return $this->cv('api4 --in=json ' . escapeshellarg("$entity.$action"), json_encode($params));
}
/**
* Call the "cv" command.
*
* @param string $cmd
* The rest of the command to send.
* @param string|NULL $pipeData
* Optional data to send to `cv` via pipe.
* @param string $decode
* Ex: 'json' or 'phpcode'.
* @return string
* Response output (if the command executed normally).
* @throws \RuntimeException
* If the command terminates abnormally.
*/
protected function cv(string $cmd, ?string $pipeData = NULL, string $decode = 'json') {
$cmd = 'cv ' . $cmd;
$descriptorSpec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => STDERR);
$oldOutput = getenv('CV_OUTPUT');
putenv("CV_OUTPUT=json");
// Execute `cv` in the original folder. This is a work-around for
// phpunit/codeception, which seem to manipulate PWD.
$cmd = sprintf('cd %s; %s', escapeshellarg(getenv('PWD')), $cmd);
$process = proc_open($cmd, $descriptorSpec, $pipes, __DIR__);
putenv("CV_OUTPUT=$oldOutput");
if ($pipeData !== NULL) {
fwrite($pipes[0], $pipeData);
}
fclose($pipes[0]);
$result = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) !== 0) {
throw new RuntimeException("Command failed ($cmd):\n$result");
}
switch ($decode) {
case 'raw':
return $result;
case 'phpcode':
// If the last output is /*PHPCODE*/, then we managed to complete execution.
if (substr(trim($result), 0, 12) !== "/*BEGINPHP*/" || substr(trim($result), -10) !== "/*ENDPHP*/") {
throw new \RuntimeException("Command failed ($cmd):\n$result");
}
return $result;
case 'json':
return json_decode($result, 1);
default:
throw new RuntimeException("Bad decoder format ($decode)");
}
}
};
}
}