Skip to content

Commit

Permalink
Add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanra committed Feb 16, 2021
1 parent ed33c92 commit b7c2c6f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Models/LetsEncryptCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public function getHasExpiredAttribute(): bool

public function renew(): PendingDispatch
{
return LetsEncrypt::renew($this->domain);
return LetsEncrypt::renew($this);
}

public function renewNow(): self
{
return LetsEncrypt::renewNow($this->domain);
return LetsEncrypt::renewNow($this);
}
}
16 changes: 16 additions & 0 deletions tests/Encoders/PemEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Tests\Encoders;

use Daanra\LaravelLetsEncrypt\Encoders\PemEncoder;
use Daanra\LaravelLetsEncrypt\Tests\TestCase;

class PemEncoderTest extends TestCase
{
/** @test */
public function test_encoder()
{
$this->assertEquals("test test\n", PemEncoder::encode(' test test '));
$this->assertEquals("2test1\n", PemEncoder::encode('2test1'));
}
}
102 changes: 102 additions & 0 deletions tests/Models/LetsEncryptCertificateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Tests\Models;

use Daanra\LaravelLetsEncrypt\Builders\LetsEncryptCertificateBuilder;
use Daanra\LaravelLetsEncrypt\Collections\LetsEncryptCertificateCollection;
use Daanra\LaravelLetsEncrypt\Facades\LetsEncrypt;
use Daanra\LaravelLetsEncrypt\Jobs\RegisterAccount;
use Daanra\LaravelLetsEncrypt\Jobs\RequestAuthorization;
use Daanra\LaravelLetsEncrypt\Jobs\RequestCertificate;
use Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate;
use Daanra\LaravelLetsEncrypt\Tests\TestCase;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Facades\Queue;

class LetsEncryptCertificateTest extends TestCase
{
/** @test */
public function test_correct_builder_instance()
{
$builder = LetsEncryptCertificate::query();

$this->assertInstanceOf(LetsEncryptCertificateBuilder::class, $builder);
}

/** @test */
public function test_correct_collection_instance()
{
$collection = LetsEncryptCertificate::all();

$this->assertInstanceOf(LetsEncryptCertificateCollection::class, $collection);
}

/** @test */
public function test_has_expired_attribute()
{
$certificate = LetsEncryptCertificate::create([
'domain' => 'test1.test',
]);

$this->assertFalse(
$certificate->has_expired,
'Certificate has not been issued so it should not be marked as expired.'
);

$certificate->update([
'last_renewed_at' => now(),
'created' => true,
]);

$this->assertFalse($certificate->has_expired, 'Certificate has just been issued so should not be considered as expired.');

$certificate->update([
'last_renewed_at' => now()->subMonths(4),
]);

$this->assertTrue($certificate->has_expired, 'Certificate has been issued 4 months ago so should be marked as expired.');
}

/** @test */
public function test_renew()
{
$certificate = LetsEncryptCertificate::create([
'domain' => 'test2.test',
]);

Queue::fake();

$pendingDispatch = $certificate->renew();

$this->assertInstanceOf(PendingDispatch::class, $pendingDispatch);

// Jobs are only pushed after the pending dispatch leaves memory.
$pendingDispatch->__destruct();

Queue::assertPushedWithChain(RegisterAccount::class, [
new RequestAuthorization($certificate),
new RequestCertificate($certificate),
]);
}

/** @test */
public function test_renew_now()
{
$certificate = LetsEncryptCertificate::create([
'domain' => 'test3.test',
]);

Queue::fake();

LetsEncrypt::shouldReceive('renewNow')
->once()
->with($certificate)
->andReturn($certificate);

$instance = $certificate->renewNow();

Queue::assertNothingPushed();

$this->assertEquals($certificate, $instance);
}
}

0 comments on commit b7c2c6f

Please sign in to comment.