Skip to content

Commit

Permalink
Merge pull request #89 from ans-group/fix-ftp-check
Browse files Browse the repository at this point in the history
Fix ftp check
  • Loading branch information
phily245 authored Jul 22, 2024
2 parents 1a11d48 + f49b9a8 commit c70e6c0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"ext-json": "*",
"illuminate/console": "^10.0|^11.0",
"illuminate/http": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0"
"illuminate/support": "^10.0|^11.0",
"league/flysystem-ftp": "^3.28"
},
"require-dev": {
"phpunit/phpunit": "^10.0|^11.0",
Expand All @@ -75,6 +76,7 @@
"illuminate/log": "Allows the log health check to function.",
"illuminate/redis": "Allows the redis health check to function.",
"league/flysystem": "Allows the ftp health check to function.",
"league/flysystem-ftp": "Allows the ftp health check to function.",
"guzzlehttp/guzzle": "Allows the http health check to function.",
"enlightn/security-checker": "Allows the package security health check to function."
}
Expand Down
20 changes: 8 additions & 12 deletions src/Checks/FtpHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@

namespace UKFast\HealthCheck\Checks;

use League\Flysystem\Adapter\Ftp;
use League\Flysystem\FilesystemException;
use League\Flysystem\Ftp\FtpAdapter;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class FtpHealthCheck extends HealthCheck
{
protected string $name = 'ftp';

/**
* @var \Illuminate\Contracts\Filesystem\Filesystem
*/
protected $ftp;

public function __construct(Ftp $ftp)
{
$this->ftp = $ftp;
public function __construct(
protected FtpAdapter $ftpAdapter,
) {
}

public function status(): Status
{
try {
$this->ftp->getConnection();
} catch (\RuntimeException $e) {
$this->ftpAdapter->listContents('', false);
} catch (FilesystemException $exception) {
return $this->problem('Could not connect to FTP server', [
'exception' => $this->exceptionContext($e),
'exception' => $this->exceptionContext($exception),
]);
}

Expand Down
14 changes: 7 additions & 7 deletions src/HealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace UKFast\HealthCheck;

use Exception;
use Throwable;

abstract class HealthCheck
{
Expand Down Expand Up @@ -42,14 +42,14 @@ public function okay($context = []): Status
/**
* @return array<string, string|array|int>
*/
protected function exceptionContext(Exception $e): array
protected function exceptionContext(Throwable $exception): array
{
return [
'error' => $e->getMessage(),
'class' => get_class($e),
'line' => $e->getLine(),
'file' => $e->getFile(),
'trace' => explode("\n", $e->getTraceAsString()),
'error' => $exception->getMessage(),
'class' => get_class($exception),
'line' => $exception->getLine(),
'file' => $exception->getFile(),
'trace' => explode("\n", $exception->getTraceAsString()),
];
}
}
28 changes: 17 additions & 11 deletions tests/Checks/FtpHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,44 @@

namespace Tests\Unit\HealthCheck;

use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Ftp\UnableToConnectToFtpHost;
use UKFast\HealthCheck\Checks\FtpHealthCheck;
use RuntimeException;
use League\Flysystem\Adapter\Ftp;
use Tests\TestCase;
use Mockery as m;
use Mockery;

class FtpHealthCheckTest extends TestCase
{
public function testShowsProblemWhenCantConnectToFtpServer(): void
{
$ftp = m::mock(Ftp::class)
->expects('getConnection')
->andThrow(new RuntimeException('uwu'))
$ftp = Mockery::mock(FtpAdapter::class)
->expects('listContents')
->andThrow(new UnableToConnectToFtpHost('uwu'))
->getMock();

$status = (new FtpHealthCheck($ftp))->status();

$this->assertTrue($status->isProblem());

m::close();
Mockery::close();
}

public function testShowsOkayWhenCanConnectToFtpServer(): void
{
$ftp = m::mock(Ftp::class)
->expects('getConnection')
->andReturn(true)
function generator(): iterable {
yield 'foo';
yield 'bar';
yield 'baz';
};

$ftp = Mockery::mock(FtpAdapter::class)
->expects('listContents')
->andReturn(generator())
->getMock();

$status = (new FtpHealthCheck($ftp))->status();
$this->assertTrue($status->isOkay());

m::close();
Mockery::close();
}
}

0 comments on commit c70e6c0

Please sign in to comment.