Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ldaspt committed Apr 22, 2024
1 parent 3afbf2f commit add59ef
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Tests/Services/JWTManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token\JWTUserToken;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManager;
use Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichmentInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Tests\Stubs\User as CustomUser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\User\InMemoryUser;
Expand Down Expand Up @@ -48,6 +49,25 @@ public function testCreate()
$this->assertEquals('secrettoken', $manager->create($this->createUser('user', 'password')));
}

public function testCreateWithPayloadEnrichment()
{
$dispatcher = $this->getEventDispatcherMock();
$encoder = $this->getJWTEncoderMock();
$encoder
->method('encode')
->with($this->arrayHasKey('baz'))
->willReturn('secrettoken');

$manager = new JWTManager($encoder, $dispatcher, 'username', new class () implements PayloadEnrichmentInterface {
public function enrich(UserInterface $user, array &$payload): void
{
$payload['baz'] = 'qux';
}
});

$this->assertEquals('secrettoken', $manager->create($this->createUser('user', 'password')));
}

/**
* test create.
*/
Expand All @@ -74,6 +94,26 @@ public function testCreateFromPayload()
$this->assertEquals('secrettoken', $manager->createFromPayload($this->createUser('user', 'password'), $payload));
}

public function testCreateFromPayloadWithPayloadEnrichment()
{
$dispatcher = $this->getEventDispatcherMock();

$encoder = $this->getJWTEncoderMock();
$encoder
->method('encode')
->with($this->arrayHasKey('baz'))
->willReturn('secrettoken');

$manager = new JWTManager($encoder, $dispatcher, 'username', new class () implements PayloadEnrichmentInterface {
public function enrich(UserInterface $user, array &$payload): void
{
$payload['baz'] = 'qux';
}
});
$payload = ['foo' => 'bar'];
$this->assertEquals('secrettoken', $manager->createFromPayload($this->createUser('user', 'password'), $payload));
}

/**
* test decode.
*/
Expand Down

0 comments on commit add59ef

Please sign in to comment.