2019-11-07 03:42:18 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\tests\unit\components\OAuth2\Entities;
|
|
|
|
|
|
|
|
use api\tests\unit\TestCase;
|
2024-12-06 06:04:09 +05:30
|
|
|
use common\components\OAuth2\Entities\AccessTokenEntity;
|
2019-12-06 21:37:08 +05:30
|
|
|
use DateTimeImmutable;
|
2019-11-07 03:42:18 +05:30
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface;
|
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface;
|
|
|
|
|
|
|
|
class AccessTokenEntityTest extends TestCase {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testToString(): void {
|
2019-11-07 03:42:18 +05:30
|
|
|
/** @var ClientEntityInterface|\PHPUnit\Framework\MockObject\MockObject $client */
|
|
|
|
$client = $this->createMock(ClientEntityInterface::class);
|
|
|
|
$client->method('getIdentifier')->willReturn('mockClientId');
|
|
|
|
|
|
|
|
$entity = new AccessTokenEntity();
|
|
|
|
$entity->setClient($client);
|
2019-12-06 21:37:08 +05:30
|
|
|
$entity->setExpiryDateTime(new DateTimeImmutable());
|
2019-11-07 03:42:18 +05:30
|
|
|
$entity->addScope($this->createScopeEntity('first'));
|
|
|
|
$entity->addScope($this->createScopeEntity('second'));
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
$token = $entity->toString();
|
2019-11-07 03:42:18 +05:30
|
|
|
$payloads = json_decode(base64_decode(explode('.', $token)[1]), true);
|
2019-12-13 16:25:09 +05:30
|
|
|
$this->assertSame('first second', $payloads['scope']);
|
2019-12-06 21:37:08 +05:30
|
|
|
}
|
|
|
|
|
2019-11-07 03:42:18 +05:30
|
|
|
private function createScopeEntity(string $id): ScopeEntityInterface {
|
|
|
|
/** @var ScopeEntityInterface|\PHPUnit\Framework\MockObject\MockObject $entity */
|
|
|
|
$entity = $this->createMock(ScopeEntityInterface::class);
|
|
|
|
$entity->method('getIdentifier')->willReturn($id);
|
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|