Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert all tests to pest #180

Merged
merged 10 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"spatie/macroable": "^1.0|^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"pestphp/pest": "^1.22",
"spatie/pest-plugin-snapshots": "^1.1",
"spatie/phpunit-snapshot-assertions": "^4.2.3",
"spatie/ray": "^1.30"
},
Expand All @@ -51,9 +52,12 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/pest"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
}
2 changes: 1 addition & 1 deletion src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected function fetchCertificates(string $hostName): array

if (! $client) {
$clientErrorMessage = ($this->usingIpAddress)
? "Could not connect to `{$connectTo}` or it does not have a certificate matching `${hostName}`."
? "Could not connect to `{$connectTo}` or it does not have a certificate matching `{$hostName}`."
: "Could not connect to `{$connectTo}`.";

throw CouldNotDownloadCertificate::unknownError($hostName, $clientErrorMessage);
Expand Down
268 changes: 107 additions & 161 deletions tests/DownloaderTest.php
Original file line number Diff line number Diff line change
@@ -1,168 +1,114 @@
<?php

namespace Spatie\SslCertificate\Test;

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

class DownloaderTest extends TestCase
{
protected $domainWithDifferentPort;
protected $ipDomainWithDifferentPort;
protected $differentPort;

protected function setUp(): void
{
parent::setUp();

$this->domainWithDifferentPort = 'psd2.b2b.belfius.be';
$this->ipDomainWithDifferentPort = '141.96.1.12';
$this->differentPort = 8443;
}

/** @test */
public function it_can_download_a_certificate_from_a_host_name()
{
$sslCertificate = Downloader::downloadCertificateFromUrl('spatie.be');

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_can_download_a_certificate_from_a_host_name_with_hostport()
{
$sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort);

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_can_download_a_certificate_from_a_host_name_with_strange_characters()
{
$sslCertificate = Downloader::downloadCertificateFromUrl('https://www.hüpfburg.de');

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_can_download_a_certificate_from_a_host_name_with_strange_characters_with_hostport()
{
$sslCertificate = Downloader::downloadCertificateFromUrl('https://' . $this->domainWithDifferentPort . ':' . $this->differentPort);

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_can_download_a_certificate_for_a_host_name_from_an_ip_address()
{
$sslCertificate = SslCertificate::download()
->fromIpAddress('164.92.244.169')
->forHost('spatie.be');

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_can_download_a_certificate_for_a_host_name_from_an_ip_address_with_hostport()
{
$sslCertificate = SslCertificate::download()
->fromIpAddress($this->ipDomainWithDifferentPort)
->forHost($this->domainWithDifferentPort . ':' . $this->differentPort);

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_can_download_a_certificate_for_a_host_name_from_an_ipv6_address()
{
if (getenv('GITHUB_ACTIONS')) {
$this->markTestSkipped('Github Actions have no IPv6 Support');
}
$sslCertificate = SslCertificate::download()
->fromIpAddress('2607:f8b0:4004:c07::64')
->forHost('google.com');

$this->assertInstanceOf(SslCertificate::class, $sslCertificate);
}

/** @test */
public function it_sets_a_fingerprint_on_the_downloaded_certificate()
{
$sslCertificate = Downloader::downloadCertificateFromUrl('spatie.be');

$this->assertNotEmpty($sslCertificate->getFingerprint());
}

/** @test */
public function it_sets_a_fingerprint_on_the_downloaded_certificate_with_hostport()
{
$sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort);

$this->assertNotEmpty($sslCertificate->getFingerprint());
}

/** @test */
public function it_can_download_all_certificates_from_a_host_name()
{
$sslCertificates = (new Downloader())->getCertificates('spatie.be');

$this->assertCount(1, $sslCertificates);
}

/** @test */
public function it_can_download_all_certificates_from_a_host_name_with_socket_context_options()
{
$sslCertificates = (new Downloader())
->withSocketContextOptions([
'bindto' => '0:0',
])
->getCertificates('spatie.be');

$this->assertCount(1, $sslCertificates);
}

/** @test */
public function it_throws_an_exception_for_non_existing_host()
{
$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(UnknownError::class);

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

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

$sslCertificate = SslCertificate::download()
->fromIpAddress('138.197.187.74')
->forHost('fake.subdomain.spatie.be');
}

/** @test */
public function it_can_retrieve_the_ip_address_of_the_server_that_served_the_certificates()
{
$sslCertificate = Downloader::downloadCertificateFromUrl('spatie.be');

$this->assertEquals('164.92.244.169:443', $sslCertificate->getRemoteAddress());
}

/** @test */
public function it_can_retrieve_the_ip_address_of_the_server_that_served_the_certificates_with_hostport()
{
$sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort);

$this->assertEquals($this->ipDomainWithDifferentPort . ':' . $this->differentPort, $sslCertificate->getRemoteAddress());
}
}
beforeEach(function () {
$this->domainWithDifferentPort = 'psd2.b2b.belfius.be';
$this->ipDomainWithDifferentPort = '141.96.1.12';
$this->differentPort = 8443;
});

it('can download a certificate from a host name', function () {
$sslCertificate = Downloader::downloadCertificateFromUrl('spatie.be');

expect($sslCertificate)->toBeInstanceOf(SslCertificate::class);
});

it('can download a certificate from a host name with hostport ', function () {
$sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort);

expect($sslCertificate)->toBeInstanceOf(SslCertificate::class);
});

it('can download a certificate from a host name with strange characters', function () {
$sslCertificate = Downloader::downloadCertificateFromUrl('https://www.hüpfburg.de');

expect($sslCertificate)->toBeInstanceOf(SslCertificate::class);
});

test('can download a certificate from a host name with strange characters with hostport', function () {
$sslCertificate = Downloader::downloadCertificateFromUrl('https://' . $this->domainWithDifferentPort . ':' . $this->differentPort);

expect($sslCertificate)->toBeInstanceOf(SslCertificate::class);
});

it('can download a certificate for a host name from an ip address', function () {
$sslCertificate = SslCertificate::download()
->fromIpAddress('164.92.244.169')
->forHost('spatie.be');

expect($sslCertificate)->toBeInstanceOf(SslCertificate::class);
});

it('can download a certificate for a host name from an ip address with hostport', function () {
$sslCertificate = SslCertificate::download()
->fromIpAddress($this->ipDomainWithDifferentPort)
->forHost($this->domainWithDifferentPort . ':' . $this->differentPort);

expect($sslCertificate)->toBeInstanceOf(SslCertificate::class);
});

it('can download a certificate for a host name from an ipv6 address', function () {
$sslCertificate = SslCertificate::download()
->fromIpAddress('2607:f8b0:4004:c07::64')
->forHost('google.com');

expect($sslCertificate)->toBeInstanceOf(SslCertificate::class);
})->skip(getenv('GITHUB_ACTIONS'), 'Github Actions have no IPv6 Support');

it('sets a fingerprint on the downloaded certificate', function () {
$sslCertificate = Downloader::downloadCertificateFromUrl('spatie.be');

expect($sslCertificate->getFingerprint())->notg19->toBeEmpty();
});

it('sets a fingerprint on the downloaded certificate with hostport', function () {
$sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort);

expect($sslCertificate->getFingerprint())->not->toBeEmpty();
});

it('can download all certificates from a host name', function () {
$sslCertificates = (new Downloader())->getCertificates('spatie.be');

expect($sslCertificates)->toHaveCount(1);
});

it('can download all certificates from a host name with socket context options', function () {
$sslCertificates = (new Downloader())
->withSocketContextOptions([
'bindto' => '0:0',
])
->getCertificates('spatie.be');

expect($sslCertificates)->toHaveCount(1);
});

it('throws an exception for non existing host')
->tap(fn () => Downloader::downloadCertificateFromUrl('spatie-non-existing.be'))
->throws(HostDoesNotExist::class);

it('throws an exception when downloading a certificate from a host that contains none')
->tap(fn () => Downloader::downloadCertificateFromUrl('3564020356.org'))
->throws(UnknownError::class);

it('throws an exception when downloading a certificate for a missing host name from an ip address', function () {
$sslCertificate = SslCertificate::download()
->fromIpAddress('138.197.187.74')
->forHost('fake.subdomain.spatie.be');
})->throws(UnknownError::class);

it('can retrieve the ip address of the server that served the certificates', function () {
$sslCertificate = Downloader::downloadCertificateFromUrl('spatie.be');

expect($sslCertificate->getRemoteAddress())->toEqual('164.92.244.169:443');
});

it('can retrieve the ip address of the server that served the certificates with hostport', function () {
$sslCertificate = Downloader::downloadCertificateFromUrl($this->domainWithDifferentPort . ':' . $this->differentPort);

expect($sslCertificate->getRemoteAddress())->toEqual($this->ipDomainWithDifferentPort . ':' . $this->differentPort);
});
Loading