Skip to content

Commit

Permalink
Add runtime exception, if status endpoint was not found, #4
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodotme committed Jun 10, 2019
1 parent edbd607 commit 4ef3943
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ClusterProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ static function ( ProvidesResponseData $response )
}
);

$request->addFailureCallbacks(
static function ( Throwable $e )
{
throw $e;
}
);

$this->proxy->sendAsyncRequest( $request );
}

Expand Down
17 changes: 17 additions & 0 deletions src/Responses/PhpFpmStatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use hollodotme\FastCGI\Interfaces\ProvidesServerStatus;
use hollodotme\FastCGI\Responses\PhpFpm\Process;
use hollodotme\FastCGI\Responses\PhpFpm\Status;
use RuntimeException;
use function array_filter;
use function array_shift;
use function explode;
Expand All @@ -33,6 +34,7 @@ final class PhpFpmStatusResponse implements ProvidesServerStatus
* @param ProvidesResponseData $response
* @param ConfiguresSocketConnection $connection
*
* @throws RuntimeException
* @throws Exception
*/
public function __construct( ProvidesResponseData $response, ConfiguresSocketConnection $connection )
Expand All @@ -41,9 +43,24 @@ public function __construct( ProvidesResponseData $response, ConfiguresSocketCon
$this->connection = $connection;
$this->processes = [];

$this->guardResponseIsValid();
$this->parseBody();
}

/**
* @throws RuntimeException
*/
private function guardResponseIsValid() : void
{
if ( 'File not found.' === trim( $this->response->getBody() ) )
{
throw new RuntimeException(
"Could not find server's status path."
. " Please check for typos and if the status endpoint is enabled in your server's config."
);
}
}

/**
* @throws Exception
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/ClusterStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use hollodotme\FastCGI\Tests\Traits\SocketDataProviding;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use Throwable;

Expand Down Expand Up @@ -55,6 +56,7 @@ protected function tearDown() : void
*/
public function testGetStatus() : void
{
/** @var array|PhpFpmStatusResponse[] $statusResponses */
$statusResponses = $this->clusterProxy->getStatus(
'/status',
PhpFpmStatusResponse::class
Expand All @@ -70,4 +72,28 @@ public function testGetStatus() : void

$this->assertSame( $expectedPoolNames, $poolNames );
}

/**
* @throws ConnectException
* @throws ReadFailedException
* @throws Throwable
* @throws TimedoutException
* @throws WriteFailedException
*/
public function testThrowsExceptionIfStatusEndpointCannotBeFound() : void
{
$this->expectException( RuntimeException::class );
$this->expectExceptionMessage(
"Could not find server's status path."
. " Please check for typos and if the status endpoint is enabled in your server's config."
);

/** @noinspection UnusedFunctionResultInspection */
$this->clusterProxy->getStatus(
'/not-existing-status-endpoint',
PhpFpmStatusResponse::class
);

$this->fail( 'Expected runtime exception to be thrown.' );
}
}

0 comments on commit 4ef3943

Please sign in to comment.