Skip to content

Commit 5ce30a1

Browse files
committed
Add more test coverage
1 parent 410be88 commit 5ce30a1

File tree

1 file changed

+128
-9
lines changed

1 file changed

+128
-9
lines changed

tests/Terremoth/AsyncTest/ProcessTest.php

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,27 @@ public function testSendHandlesShmopCollisionAndRecreation(): void
118118

119119
$process = $this->getMockBuilder(Process::class)
120120
->setConstructorArgs([$key])
121-
->onlyMethods(['openShmop', 'deleteShmop', 'writeToShmop'])
121+
->onlyMethods(['openShmop', 'writeToShmop'])
122122
->getMock();
123123

124124
$process->expects($this->exactly(3))
125125
->method('openShmop')
126-
->willReturnCallback(function (int $key, string $mode) {
127-
unset($key);
126+
->willReturnCallback(function (int $k, string $mode) {
127+
unset($k);
128128
if ($mode === 'n') {
129129
throw new Exception('shmop already exists');
130130
}
131131

132132
$tempKey = random_int(1000000, 9999999);
133-
$dummyShmop = shmop_open($tempKey, 'c', 0660, 1);
133+
$dummy = shmop_open($tempKey, 'c', 0660, 1);
134134

135-
$this->assertTrue(shmop_delete($dummyShmop));
135+
if ($mode === 'c') {
136+
$this->assertTrue(shmop_delete($dummy));
137+
}
136138

137-
return $dummyShmop;
139+
return $dummy;
138140
});
139141

140-
$process->expects($this->once())
141-
->method('deleteShmop')
142-
->willReturn(true);
143142

144143
$process->expects($this->once())
145144
->method('writeToShmop')
@@ -150,4 +149,124 @@ public function testSendHandlesShmopCollisionAndRecreation(): void
150149

151150
$process->send(fn() => 'test');
152151
}
152+
153+
public function testSendThrowsExceptionWhenExistingShmopCannotBeAccessed(): void
154+
{
155+
$process = $this->getMockBuilder(Process::class)
156+
->onlyMethods(['openShmop'])
157+
->getMock();
158+
159+
$process->expects($this->exactly(2))
160+
->method('openShmop')
161+
->willReturnCallback(function (int $key, string $mode) {
162+
unset($key);
163+
if ($mode === 'n') {
164+
throw new Exception('Simulated creation failure');
165+
}
166+
if ($mode === 'a') {
167+
return false;
168+
}
169+
return false;
170+
});
171+
172+
$this->expectException(Exception::class);
173+
$this->expectExceptionMessage('Could not access existing shmop instance');
174+
175+
$process->send(fn() => true);
176+
}
177+
178+
public function testSendThrowsExceptionWhenExistingShmopCannotBeDeleted(): void
179+
{
180+
$process = $this->getMockBuilder(Process::class)
181+
->onlyMethods(['openShmop', 'deleteShmop'])
182+
->getMock();
183+
184+
$process->expects($this->exactly(2))
185+
->method('openShmop')
186+
->willReturnCallback(function (int $k, string $mode) {
187+
unset($k);
188+
if ($mode === 'n') {
189+
throw new Exception('Simulated creation failure');
190+
}
191+
192+
$tempKey = random_int(1000000, 9999999);
193+
$dummy = shmop_open($tempKey, 'c', 0660, 1);
194+
195+
// Fix: Usa o retorno para satisfazer o Psalm e limpa a memória
196+
$this->assertTrue(shmop_delete($dummy));
197+
198+
return $dummy;
199+
});
200+
201+
$process->expects($this->once())
202+
->method('deleteShmop')
203+
->willReturn(false);
204+
205+
$this->expectException(Exception::class);
206+
$this->expectExceptionMessage('Could not delete existing shmop instance');
207+
208+
$process->send(fn() => true);
209+
}
210+
211+
public function testSendThrowsExceptionWhenShmopCannotBeRecreated(): void
212+
{
213+
$process = $this->getMockBuilder(Process::class)
214+
->onlyMethods(['openShmop', 'deleteShmop'])
215+
->getMock();
216+
217+
$process->expects($this->exactly(3))
218+
->method('openShmop')
219+
->willReturnCallback(function (int $k, string $mode) {
220+
unset($k);
221+
if ($mode === 'n') {
222+
throw new Exception('Simulated creation failure');
223+
}
224+
if ($mode === 'c') {
225+
return false;
226+
}
227+
228+
$tempKey = random_int(1000000, 9999999);
229+
$dummy = shmop_open($tempKey, 'c', 0660, 1);
230+
231+
// Fix: Usa o retorno para satisfazer o Psalm e limpa a memória
232+
$this->assertTrue(shmop_delete($dummy));
233+
234+
return $dummy;
235+
});
236+
237+
$process->expects($this->once())
238+
->method('deleteShmop')
239+
->willReturn(true);
240+
241+
$this->expectException(Exception::class);
242+
$this->expectExceptionMessage('Could not recreate shmop instance');
243+
244+
$process->send(fn() => true);
245+
}
246+
247+
public function testErrorHandlerCapturesWarnings(): void
248+
{
249+
$process = $this->getMockBuilder(Process::class)
250+
->onlyMethods(['openShmop'])
251+
->getMock();
252+
253+
$process->expects($this->exactly(2))
254+
->method('openShmop')
255+
->willReturnCallback(function (int $key, string $mode) {
256+
unset($key);
257+
if ($mode === 'n') {
258+
throw new Exception('First failure');
259+
}
260+
if ($mode === 'a') {
261+
trigger_error('Native shmop warning', E_USER_WARNING);
262+
return false;
263+
}
264+
return false;
265+
});
266+
267+
$this->expectException(Exception::class);
268+
$this->expectExceptionMessage('Native shmop warning');
269+
270+
$process->send(fn() => true);
271+
}
153272
}

0 commit comments

Comments
 (0)