Skip to content

Commit 091c1de

Browse files
committed
Shutdown bugfix
1 parent 1337749 commit 091c1de

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
# Changelog
22

33
All notable changes to `laravel-minio-testing-tools` will be documented in this file.
4+
5+
## 1.0.1 - 2022-05-17
6+
7+
- Cleanup + bugfix
8+
9+
## 1.0.9 - 2022-05-17
10+
11+
- First release

src/UsesMinIOServer.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ trait UsesMinIOServer
1717

1818
public Collection $minIODiskConfig;
1919

20+
public bool $minIODestroyed = false;
21+
22+
public bool $minIOEnvironmentRestored = false;
23+
2024
public function bootUsesMinIOServer()
2125
{
2226
$this->startMinIOServer();
@@ -120,14 +124,24 @@ public function startMinIOServer(): bool
120124

121125
$pid = $this->exec("minio server {$temporaryDirectory->path()} --address :{$this->minIOPort} > /dev/null 2>&1 & echo $!");
122126

123-
register_shutdown_function(function () use ($pid, $temporaryDirectory) {
127+
$killMinIOAndDeleteStorage = function () use ($pid, $temporaryDirectory) {
128+
if ($this->minIODestroyed) {
129+
return;
130+
}
131+
124132
if ($pid) {
125133
$this->exec("kill {$pid}");
126134
}
127135

128136
// deleting the directory might fail when minio is not killed yet
129137
rescue(fn () => $temporaryDirectory->delete());
130-
});
138+
139+
$this->minIODestroyed = true;
140+
};
141+
142+
$this->beforeApplicationDestroyed($killMinIOAndDeleteStorage);
143+
144+
register_shutdown_function($killMinIOAndDeleteStorage);
131145

132146
$tries = 0;
133147

@@ -216,6 +230,8 @@ public function updateEnvirionmentFile()
216230

217231
file_put_contents($envFilename, $env);
218232

233+
$this->beforeApplicationDestroyed(fn () => $this->restoreEnvironmentFile());
234+
219235
register_shutdown_function(fn () => $this->restoreEnvironmentFile());
220236
}
221237

@@ -227,6 +243,10 @@ public function updateEnvirionmentFile()
227243
*/
228244
public function restoreEnvironmentFile()
229245
{
246+
if ($this->minIOEnvironmentRestored) {
247+
return;
248+
}
249+
230250
$envFilename = base_path('.env');
231251

232252
if (!file_exists($envFilename)) {
@@ -244,5 +264,7 @@ public function restoreEnvironmentFile()
244264
});
245265

246266
file_put_contents($envFilename, $env);
267+
268+
$this->minIOEnvironmentRestored = true;
247269
}
248270
}

tests/UsesMinIOServerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?php
22

3+
use Illuminate\Foundation\Testing\TestCase;
34
use Illuminate\Support\Facades\Artisan;
45
use Illuminate\Support\Facades\Http;
6+
use Orchestra\Testbench\Concerns\CreatesApplication;
57
use ProtoneMedia\LaravelMinioTestingTools\UsesMinIOServer;
68

7-
class DummyTestCase
9+
class DummyTestCase extends TestCase
810
{
11+
use CreatesApplication;
912
use UsesMinIOServer;
1013
}
1114

0 commit comments

Comments
 (0)