Skip to content

Commit

Permalink
Introduce fake implementation of signer for tests
Browse files Browse the repository at this point in the history
This allows us to stop using PHPUnit mocks in every place that needs,
making tests more obvious.

Signed-off-by: Luís Cobucci <lcobucci@gmail.com>
  • Loading branch information
lcobucci committed Nov 20, 2023
1 parent df58dd5 commit 800dbdd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<rule ref="Lcobucci" />

<rule ref="SlevomatCodingStandard.Functions.UnusedParameter">
<exclude-pattern>tests/UnsupportedParser.php</exclude-pattern>
<exclude-pattern>tests</exclude-pattern>
</rule>
</ruleset>

30 changes: 30 additions & 0 deletions tests/Signer/FakeSigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Lcobucci\JWT\Tests\Signer;

use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Key;

final class FakeSigner implements Signer
{
/** @param non-empty-string $signature */
public function __construct(private readonly string $signature)
{
}

public function algorithmId(): string
{
return 'FAKE-' . $this->signature;
}

public function sign(string $payload, Key $key): string
{
return $this->signature . '-' . $key->contents();
}

public function verify(string $expected, string $payload, Key $key): bool
{
return $this->signature . '-' . $key->contents() === $expected;
}
}
14 changes: 14 additions & 0 deletions tests/Validation/Constraint/ConstraintTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

namespace Lcobucci\JWT\Tests\Validation\Constraint;

use Closure;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\JwtFacade;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Token\DataSet;
use Lcobucci\JWT\Token\Plain;
use Lcobucci\JWT\Token\Signature;
use Lcobucci\JWT\UnencryptedToken;
use PHPUnit\Framework\TestCase;

abstract class ConstraintTestCase extends TestCase
Expand All @@ -25,4 +30,13 @@ protected function buildToken(
$signature ?? new Signature('sig+hash', 'sig+encoded'),
);
}

protected function issueToken(Signer $signer, Signer\Key $key, ?Closure $customization = null): UnencryptedToken
{
return (new JwtFacade())->issue(
$signer,
$key,
$customization ?? static fn (Builder $builder) => $builder,
);
}
}

0 comments on commit 800dbdd

Please sign in to comment.