Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarchalemisys committed Nov 2, 2024
1 parent 492d1da commit 1a09a46
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Tests/Services/JWTManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Lexik\Bundle\JWTAuthenticationBundle\Services\PayloadEnrichmentInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\TestBrowserToken;
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -137,6 +139,56 @@ public function testDecode()
$this->assertSame(['foo' => 'bar'], $manager->decode($this->getJWTUserTokenMock()));
}

/**
* test decode a TokenInterface without getCredentials.
*/
public function testDecode_noGetCredentials()
{
$dispatcher = $this->getEventDispatcherMock();
$dispatcher
->expects($this->never())
->method('dispatch')
->with(
$this->isInstanceOf(JWTDecodedEvent::class),
$this->equalTo(Events::JWT_DECODED)
);

$encoder = $this->getJWTEncoderMock();
$encoder
->expects($this->never())
->method('decode')
->willReturn(['foo' => 'bar']);

$token = new NullToken();
$manager = new JWTManager($encoder, $dispatcher, 'username');
$this->assertFalse($manager->decode($token));
}

/**
* test decode a TokenInterface with getCredentials returning null.
*/
public function testDecode_nullGetCredentials()
{
$dispatcher = $this->getEventDispatcherMock();
$dispatcher
->expects($this->never())
->method('dispatch')
->with(
$this->isInstanceOf(JWTDecodedEvent::class),
$this->equalTo(Events::JWT_DECODED)
);

$encoder = $this->getJWTEncoderMock();
$encoder
->expects($this->never())
->method('decode')
->willReturn(['foo' => 'bar']);

$token = new TestBrowserToken();
$manager = new JWTManager($encoder, $dispatcher, 'username');
$this->assertFalse($manager->decode($token));
}

public function testParse()
{
$dispatcher = $this->getEventDispatcherMock();
Expand Down

0 comments on commit 1a09a46

Please sign in to comment.