Skip to content

Commit

Permalink
Use psr/clock instead of protected method
Browse files Browse the repository at this point in the history
  • Loading branch information
sstok committed Nov 27, 2023
1 parent 8e8b29b commit c3d65f8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ext-mbstring": "*",
"ext-openssl": "*",
"mlocati/ocsp": "^1.0",
"psr/clock": "^1.0",
"rollerworks/pdb-symfony-bridge": "^1.0",
"rollerworks/pdb-validator": "^1.0",
"symfony/translation-contracts": "^2.5 || ^3.0"
Expand All @@ -30,6 +31,7 @@
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^10.4.2",
"rollerscapes/standards": "^1.0",
"symfony/clock": "^6.3",
"symfony/error-handler": "^6.3",
"symfony/http-client": "^6.3",
"symfony/phpunit-bridge": "^6.3 || ^7.0",
Expand Down
9 changes: 5 additions & 4 deletions src/CertificateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rollerworks\Component\X509Validator;

use Pdp\Domain;
use Psr\Clock\ClockInterface;
use Rollerworks\Component\PdbSfBridge\PdpManager as PublicSuffixManager;
use Rollerworks\Component\X509Validator\Violation\CertificateHasExpired;
use Rollerworks\Component\X509Validator\Violation\GlobalWildcard;
Expand Down Expand Up @@ -41,7 +42,8 @@ class CertificateValidator
public function __construct(
private readonly PublicSuffixManager $suffixManager,
X509DataExtractor $dataExtractor = null,
CAResolver $caResolver = null
CAResolver $caResolver = null,
private ?ClockInterface $clock = null
) {
$this->extractor = $dataExtractor ?? new X509DataExtractor();
$this->caResolver = $caResolver ?? new CAResolverImpl();
Expand Down Expand Up @@ -171,9 +173,8 @@ public function validateCertificateSupport(string $certificate, callable $valida
$validator($data, $certificate, $this);
}

/** @internal used for testing */
protected function getNow(): \DateTimeImmutable
private function getNow(): \DateTimeImmutable
{
return new \DateTimeImmutable();
return $this->clock?->now() ?? new \DateTimeImmutable();
}
}
18 changes: 13 additions & 5 deletions tests/CertificateValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psr\Clock\ClockInterface;
use Rollerworks\Component\PdbSfBridge\PdpManager;
use Rollerworks\Component\PdbSfBridge\PdpMockProvider;
use Rollerworks\Component\X509Validator\CertificateValidator;
Expand All @@ -28,30 +29,37 @@
use Rollerworks\Component\X509Validator\Violation\UnsupportedPurpose;
use Rollerworks\Component\X509Validator\Violation\WeakSignatureAlgorithm;
use Rollerworks\Component\X509Validator\X509Info;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\Test\ClockSensitiveTrait;

/**
* @internal
*/
final class CertificateValidatorTest extends TestCase
{
use ClockSensitiveTrait;

private ClockInterface $clock;
private ?CertificateValidator $certificateValidator = null;
private ?PdpManager $pdpManager;

protected function setUp(): void
{
$this->pdpManager = PdpMockProvider::getPdpManager();
$this->certificateValidator = new class($this->pdpManager) extends CertificateValidator {
$this->clock = new class() implements ClockInterface {
public ?string $now = '2013-05-29T14:12:14.000000+0000';

protected function getNow(): \DateTimeImmutable
public function now(): \DateTimeImmutable
{
if (isset($this->now)) {
return new \DateTimeImmutable($this->now);
}

return parent::getNow();
return Clock::get()->now();
}
};

$this->pdpManager = PdpMockProvider::getPdpManager();
$this->certificateValidator = new CertificateValidator($this->pdpManager, clock: $this->clock);
}

#[Test]
Expand Down Expand Up @@ -124,7 +132,7 @@ public function validate_certificate_is_expired(): void
CA;

try {
$this->certificateValidator->now = null;
$this->clock->now = null;

Check failure on line 135 in tests/CertificateValidatorTest.php

View workflow job for this annotation

GitHub Actions / PhpStan (8.2)

Access to an undefined property Psr\Clock\ClockInterface::$now.
$this->certificateValidator->validateCertificate($certContents, ['root' => $ca]);

Check failure on line 136 in tests/CertificateValidatorTest.php

View workflow job for this annotation

GitHub Actions / PhpStan (8.2)

Cannot call method validateCertificate() on Rollerworks\Component\X509Validator\CertificateValidator|null.

self::fail('Exception was expected.');
Expand Down
2 changes: 2 additions & 0 deletions tests/OCSPValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Rollerworks\Component\PdbSfBridge\PdpManager;
use Rollerworks\Component\PdbSfBridge\PdpMockProvider;
use Rollerworks\Component\X509Validator\OCSPValidator;
use Rollerworks\Component\X509Validator\TranslatableArgument;
Expand All @@ -41,6 +42,7 @@ final class OCSPValidatorTest extends TestCase
{
use ProphecyTrait;

private PdpManager $pdpManager;
private ?OCSPValidator $certificateValidator = null;
private static ?Psr16Cache $cache;

Expand Down

0 comments on commit c3d65f8

Please sign in to comment.