Skip to content

Commit

Permalink
add specific exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Feb 19, 2019
1 parent 7bc8b06 commit a603046
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `ssl-certificate` will be documented in this file

## 1.13.0 - 2019-02-19

- add specific exceptions

## 1.12.11 - 2018-12-06

- add support for detecting pre-certificates
Expand Down
2 changes: 1 addition & 1 deletion src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Spatie\SslCertificate;

use Throwable;
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate;
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate\CouldNotDownloadCertificate;

class Downloader
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?php

namespace Spatie\SslCertificate\Exceptions;
namespace Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate;

use Exception;

class CouldNotDownloadCertificate extends Exception
{
public static function hostDoesNotExist(string $hostName): self
{
return new static("The host named `{$hostName}` does not exist.");
return new HostDoesNotExist($hostName);
}

public static function noCertificateInstalled(string $hostName): self
{
return new static("Could not find a certificate on host named `{$hostName}`.");
return new NoCertificateInstalled($hostName);
}

public static function unknownError(string $hostName, string $errorMessage): self
{
return new static("Could not download certificate for host `{$hostName}` because {$errorMessage}");
return new UnknownError($hostName, $errorMessage);
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/CouldNotDownloadCertificate/HostDoesNotExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate;

use Throwable;

class HostDoesNotExist extends CouldNotDownloadCertificate
{
public function __construct(string $hostName)
{
parent::__construct("The host named `{$hostName}` does not exist.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate;

class NoCertificateInstalled extends CouldNotDownloadCertificate
{
public function __construct(string $hostName)
{
parent::__construct("Could not find a certificate on host named `{$hostName}`.");
}

}
13 changes: 13 additions & 0 deletions src/Exceptions/CouldNotDownloadCertificate/UnknownError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate;

class UnknownError extends CouldNotDownloadCertificate
{
public function __construct(string $hostName, string $errorMessage)
{
parent::__construct("Could not download certificate for host `{$hostName}` because {$errorMessage}");
}


}
16 changes: 13 additions & 3 deletions tests/DownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use PHPUnit\Framework\TestCase;
use Spatie\SslCertificate\Downloader;
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate\HostDoesNotExist;
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate\NoCertificateInstalled;
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate\UnknownError;
use Spatie\SslCertificate\SslCertificate;
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate;
use Spatie\SslCertificate\Exceptions\CouldNotDownloadCertificate\CouldNotDownloadCertificate;

class DownloaderTest extends TestCase
{
Expand Down Expand Up @@ -44,16 +47,23 @@ public function it_can_download_all_certificates_from_a_host_name()
/** @test */
public function it_throws_an_exception_for_non_existing_host()
{
$this->expectException(CouldNotDownloadCertificate::class);
$this->expectException(HostDoesNotExist::class);

Downloader::downloadCertificateFromUrl('spatie-non-existing.be');
}

/** @test */
public function it_throws_an_exception_when_downloading_a_certificate_from_a_host_that_contains_none()
{
$this->expectException(CouldNotDownloadCertificate::class);
$this->expectException(UnknownError::class);

Downloader::downloadCertificateFromUrl('3564020356.org');
}

/** @test */
public function it_tests()
{
$this->expectException(NoCertificateInstalled::class);

Downloader::downloadCertificateFromUrl('hipsteadresjes.gent');}
}

0 comments on commit a603046

Please sign in to comment.