Skip to content

Commit

Permalink
Shutdown bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed May 17, 2022
1 parent 1337749 commit 091c1de
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Changelog

All notable changes to `laravel-minio-testing-tools` will be documented in this file.

## 1.0.1 - 2022-05-17

- Cleanup + bugfix

## 1.0.9 - 2022-05-17

- First release
26 changes: 24 additions & 2 deletions src/UsesMinIOServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ trait UsesMinIOServer

public Collection $minIODiskConfig;

public bool $minIODestroyed = false;

public bool $minIOEnvironmentRestored = false;

public function bootUsesMinIOServer()
{
$this->startMinIOServer();
Expand Down Expand Up @@ -120,14 +124,24 @@ public function startMinIOServer(): bool

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

register_shutdown_function(function () use ($pid, $temporaryDirectory) {
$killMinIOAndDeleteStorage = function () use ($pid, $temporaryDirectory) {
if ($this->minIODestroyed) {
return;
}

if ($pid) {
$this->exec("kill {$pid}");
}

// deleting the directory might fail when minio is not killed yet
rescue(fn () => $temporaryDirectory->delete());
});

$this->minIODestroyed = true;
};

$this->beforeApplicationDestroyed($killMinIOAndDeleteStorage);

register_shutdown_function($killMinIOAndDeleteStorage);

$tries = 0;

Expand Down Expand Up @@ -216,6 +230,8 @@ public function updateEnvirionmentFile()

file_put_contents($envFilename, $env);

$this->beforeApplicationDestroyed(fn () => $this->restoreEnvironmentFile());

register_shutdown_function(fn () => $this->restoreEnvironmentFile());
}

Expand All @@ -227,6 +243,10 @@ public function updateEnvirionmentFile()
*/
public function restoreEnvironmentFile()
{
if ($this->minIOEnvironmentRestored) {
return;
}

$envFilename = base_path('.env');

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

file_put_contents($envFilename, $env);

$this->minIOEnvironmentRestored = true;
}
}
5 changes: 4 additions & 1 deletion tests/UsesMinIOServerTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Http;
use Orchestra\Testbench\Concerns\CreatesApplication;
use ProtoneMedia\LaravelMinioTestingTools\UsesMinIOServer;

class DummyTestCase
class DummyTestCase extends TestCase
{
use CreatesApplication;
use UsesMinIOServer;
}

Expand Down

0 comments on commit 091c1de

Please sign in to comment.