diff --git a/src/Models/LetsEncryptCertificate.php b/src/Models/LetsEncryptCertificate.php index 966b068..7161940 100644 --- a/src/Models/LetsEncryptCertificate.php +++ b/src/Models/LetsEncryptCertificate.php @@ -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); } } diff --git a/tests/Encoders/PemEncoderTest.php b/tests/Encoders/PemEncoderTest.php new file mode 100644 index 0000000..9a9fc6d --- /dev/null +++ b/tests/Encoders/PemEncoderTest.php @@ -0,0 +1,16 @@ +assertEquals("test test\n", PemEncoder::encode(' test test ')); + $this->assertEquals("2test1\n", PemEncoder::encode('2test1')); + } +} diff --git a/tests/Models/LetsEncryptCertificateTest.php b/tests/Models/LetsEncryptCertificateTest.php new file mode 100644 index 0000000..69198e7 --- /dev/null +++ b/tests/Models/LetsEncryptCertificateTest.php @@ -0,0 +1,102 @@ +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); + } +}