Skip to content

Instantly share code, notes, and snippets.

@dersonsena
Last active November 21, 2019 21:14
Show Gist options
  • Save dersonsena/1f0b804b3df7eacf96a052e246ab2559 to your computer and use it in GitHub Desktop.
Save dersonsena/1f0b804b3df7eacf96a052e246ab2559 to your computer and use it in GitHub Desktop.
<?php
namespace Tests\Unit\Domain\Auth;
use App\Domain\Auth\JWTHelper;
use App\Domain\Client\Client;
use App\Domain\Group;
use App\Domain\User\User;
use PHPUnit\Framework\TestCase;
final class JWTHelperTest extends TestCase
{
/**
* @var User
*/
private $user;
/**
* @var Group
*/
private $group;
/**
* @var Client
*/
private $client;
protected function setUp(): void
{
$this->user = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->setMethods(['attributes'])
->getMock();
$this->user->method('attributes')->willReturn(['uuid', 'name', 'email']);
$this->user->uuid = '2283000c-0c60-11ea-8d71-362b9e155667';
$this->user->name = 'Unit Test User';
$this->user->email = 'unittest@domain.com';
$this->group = $this->getMockBuilder(Group::class)
->disableOriginalConstructor()
->setMethods(['attributes'])
->getMock();
$this->group->method('attributes')->willReturn(['uuid', 'name']);
$this->group->uuid = 'fd3dc7b8-0c65-11ea-8d71-362b9e155667';
$this->group->name = 'Unit test Group';
$this->client = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->setMethods(['attributes'])
->getMock();
$this->client->method('attributes')->willReturn(['uuid', 'name']);
$this->client->uuid = '1cfaaa26-0c66-11ea-8d71-362b9e155667';
$this->client->name = 'Unit test Client';
}
public function testUserTokenEncoding()
{
$token = (new JWTHelper())->encode($this->user, $this->group, $this->client);
$this->assertEquals(2, substr_count($token, '.'));
$this->assertEquals(557, strlen($token));
}
public function testIfTokenIsValid()
{
$JWTHelper = new JWTHelper();
$JWTHelper->encode($this->user, $this->group, $this->client);
$this->assertTrue($JWTHelper->validate());
}
public function testTokenPayloadDataInserted()
{
$JWTHelper = new JWTHelper();
$JWTHelper->encode($this->user, $this->group, $this->client);
$payload = $JWTHelper->decode();
$this->assertEquals('2283000c-0c60-11ea-8d71-362b9e155667', $payload->uuid);
$this->assertEquals('Unit Test User', $payload->name);
$this->assertEquals('unittest@domain.com', $payload->email);
$this->assertEquals('fd3dc7b8-0c65-11ea-8d71-362b9e155667', $payload->group_id);
$this->assertEquals('Unit test Group', $payload->group_name);
$this->assertEquals('1cfaaa26-0c66-11ea-8d71-362b9e155667', $payload->client_id);
$this->assertEquals('Unit test Client', $payload->client_name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment