diff --git a/Tests/Services/JWTManagerTest.php b/Tests/Services/JWTManagerTest.php index 1954a43a..2a0b010f 100644 --- a/Tests/Services/JWTManagerTest.php +++ b/Tests/Services/JWTManagerTest.php @@ -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; @@ -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. */ @@ -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. */