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

Use psr/clock instead of protected method #2

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: 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
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Access to an undefined property Rollerworks\\\\Component\\\\X509Validator\\\\CertificateValidator\\:\\:\\$now\\.$#"
message: "#^Access to an undefined property Psr\\\\Clock\\\\ClockInterface\\:\\:\\$now\\.$#"
count: 1
path: tests/CertificateValidatorTest.php
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();
}
}
15 changes: 10 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,34 @@
use Rollerworks\Component\X509Validator\Violation\UnsupportedPurpose;
use Rollerworks\Component\X509Validator\Violation\WeakSignatureAlgorithm;
use Rollerworks\Component\X509Validator\X509Info;
use Symfony\Component\Clock\Clock;

/**
* @internal
*/
final class CertificateValidatorTest extends TestCase
{
private ClockInterface $clock;
private CertificateValidator $certificateValidator;
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 +129,7 @@ public function validate_certificate_is_expired(): void
CA;

try {
$this->certificateValidator->now = null;
$this->clock->now = null;
$this->certificateValidator->validateCertificate($certContents, ['root' => $ca]);

self::fail('Exception was expected.');
Expand Down