123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699 |
- <?php
- /**
- * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
- *
- * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
- */
- namespace Lcobucci\JWT;
- use Lcobucci\JWT\Claim\Factory as ClaimFactory;
- use Lcobucci\JWT\Parsing\Encoder;
- /**
- * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
- * @since 0.1.0
- */
- class BuilderTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var Encoder|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $encoder;
- /**
- * @var ClaimFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $claimFactory;
- /**
- * @var Claim|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $defaultClaim;
- /**
- * {@inheritdoc}
- */
- protected function setUp()
- {
- $this->encoder = $this->getMock(Encoder::class);
- $this->claimFactory = $this->getMock(ClaimFactory::class, [], [], '', false);
- $this->defaultClaim = $this->getMock(Claim::class);
- $this->claimFactory->expects($this->any())
- ->method('create')
- ->willReturn($this->defaultClaim);
- }
- /**
- * @return Builder
- */
- private function createBuilder()
- {
- return new Builder($this->encoder, $this->claimFactory);
- }
- /**
- * @test
- *
- * @covers Lcobucci\JWT\Builder::__construct
- */
- public function constructMustInitializeTheAttributes()
- {
- $builder = $this->createBuilder();
- $this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
- $this->assertAttributeEquals([], 'claims', $builder);
- $this->assertAttributeEquals(null, 'signature', $builder);
- $this->assertAttributeSame($this->encoder, 'encoder', $builder);
- $this->assertAttributeSame($this->claimFactory, 'claimFactory', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setAudience
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setAudienceMustChangeTheAudClaim()
- {
- $builder = $this->createBuilder();
- $builder->setAudience('test');
- $this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
- $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setAudience
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setAudienceCanReplicateItemOnHeader()
- {
- $builder = $this->createBuilder();
- $builder->setAudience('test', true);
- $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $builder);
- $this->assertAttributeEquals(
- ['alg' => 'none', 'typ' => 'JWT', 'aud' => $this->defaultClaim],
- 'headers',
- $builder
- );
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setAudience
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setAudienceMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->setAudience('test'));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setExpiration
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setExpirationMustChangeTheExpClaim()
- {
- $builder = $this->createBuilder();
- $builder->setExpiration('2');
- $this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
- $this->assertAttributeEquals(['exp' => $this->defaultClaim], 'claims', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setExpiration
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setExpirationCanReplicateItemOnHeader()
- {
- $builder = $this->createBuilder();
- $builder->setExpiration('2', true);
- $this->assertAttributeEquals(['exp' => $this->defaultClaim], 'claims', $builder);
- $this->assertAttributeEquals(
- ['alg' => 'none', 'typ' => 'JWT', 'exp' => $this->defaultClaim],
- 'headers',
- $builder
- );
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setExpiration
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setExpirationMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->setExpiration('2'));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setId
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIdMustChangeTheJtiClaim()
- {
- $builder = $this->createBuilder();
- $builder->setId('2');
- $this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
- $this->assertAttributeEquals(['jti' => $this->defaultClaim], 'claims', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setId
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIdCanReplicateItemOnHeader()
- {
- $builder = $this->createBuilder();
- $builder->setId('2', true);
- $this->assertAttributeEquals(['jti' => $this->defaultClaim], 'claims', $builder);
- $this->assertAttributeEquals(
- ['alg' => 'none', 'typ' => 'JWT', 'jti' => $this->defaultClaim],
- 'headers',
- $builder
- );
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setId
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIdMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->setId('2'));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setIssuedAt
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIssuedAtMustChangeTheIatClaim()
- {
- $builder = $this->createBuilder();
- $builder->setIssuedAt('2');
- $this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
- $this->assertAttributeEquals(['iat' => $this->defaultClaim], 'claims', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setIssuedAt
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIssuedAtCanReplicateItemOnHeader()
- {
- $builder = $this->createBuilder();
- $builder->setIssuedAt('2', true);
- $this->assertAttributeEquals(['iat' => $this->defaultClaim], 'claims', $builder);
- $this->assertAttributeEquals(
- ['alg' => 'none', 'typ' => 'JWT', 'iat' => $this->defaultClaim],
- 'headers',
- $builder
- );
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setIssuedAt
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIssuedAtMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->setIssuedAt('2'));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setIssuer
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIssuerMustChangeTheIssClaim()
- {
- $builder = $this->createBuilder();
- $builder->setIssuer('2');
- $this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
- $this->assertAttributeEquals(['iss' => $this->defaultClaim], 'claims', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setIssuer
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIssuerCanReplicateItemOnHeader()
- {
- $builder = $this->createBuilder();
- $builder->setIssuer('2', true);
- $this->assertAttributeEquals(['iss' => $this->defaultClaim], 'claims', $builder);
- $this->assertAttributeEquals(
- ['alg' => 'none', 'typ' => 'JWT', 'iss' => $this->defaultClaim],
- 'headers',
- $builder
- );
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setIssuer
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setIssuerMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->setIssuer('2'));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setNotBefore
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setNotBeforeMustChangeTheNbfClaim()
- {
- $builder = $this->createBuilder();
- $builder->setNotBefore('2');
- $this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
- $this->assertAttributeEquals(['nbf' => $this->defaultClaim], 'claims', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setNotBefore
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setNotBeforeCanReplicateItemOnHeader()
- {
- $builder = $this->createBuilder();
- $builder->setNotBefore('2', true);
- $this->assertAttributeEquals(['nbf' => $this->defaultClaim], 'claims', $builder);
- $this->assertAttributeEquals(
- ['alg' => 'none', 'typ' => 'JWT', 'nbf' => $this->defaultClaim],
- 'headers',
- $builder
- );
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setNotBefore
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setNotBeforeMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->setNotBefore('2'));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setSubject
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setSubjectMustChangeTheSubClaim()
- {
- $builder = $this->createBuilder();
- $builder->setSubject('2');
- $this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
- $this->assertAttributeEquals(['sub' => $this->defaultClaim], 'claims', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setSubject
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setSubjectCanReplicateItemOnHeader()
- {
- $builder = $this->createBuilder();
- $builder->setSubject('2', true);
- $this->assertAttributeEquals(['sub' => $this->defaultClaim], 'claims', $builder);
- $this->assertAttributeEquals(
- ['alg' => 'none', 'typ' => 'JWT', 'sub' => $this->defaultClaim],
- 'headers',
- $builder
- );
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- *
- * @covers Lcobucci\JWT\Builder::setSubject
- * @covers Lcobucci\JWT\Builder::setRegisteredClaim
- */
- public function setSubjectMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->setSubject('2'));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- *
- * @covers Lcobucci\JWT\Builder::set
- */
- public function setMustConfigureTheGivenClaim()
- {
- $builder = $this->createBuilder();
- $builder->set('userId', 2);
- $this->assertAttributeEquals(['userId' => $this->defaultClaim], 'claims', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- *
- * @covers Lcobucci\JWT\Builder::set
- */
- public function setMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->set('userId', 2));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- *
- * @covers Lcobucci\JWT\Builder::setHeader
- */
- public function setHeaderMustConfigureTheGivenClaim()
- {
- $builder = $this->createBuilder();
- $builder->setHeader('userId', 2);
- $this->assertAttributeEquals(
- ['alg' => 'none', 'typ' => 'JWT', 'userId' => $this->defaultClaim],
- 'headers',
- $builder
- );
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- *
- * @covers Lcobucci\JWT\Builder::setHeader
- */
- public function setHeaderMustKeepAFluentInterface()
- {
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->setHeader('userId', 2));
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::getToken
- * @uses Lcobucci\JWT\Token
- *
- * @covers Lcobucci\JWT\Builder::sign
- */
- public function signMustChangeTheSignature()
- {
- $signer = $this->getMock(Signer::class);
- $signature = $this->getMock(Signature::class, [], [], '', false);
- $signer->expects($this->any())
- ->method('sign')
- ->willReturn($signature);
- $builder = $this->createBuilder();
- $builder->sign($signer, 'test');
- $this->assertAttributeSame($signature, 'signature', $builder);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::getToken
- * @uses Lcobucci\JWT\Token
- *
- * @covers Lcobucci\JWT\Builder::sign
- */
- public function signMustKeepAFluentInterface()
- {
- $signer = $this->getMock(Signer::class);
- $signature = $this->getMock(Signature::class, [], [], '', false);
- $signer->expects($this->any())
- ->method('sign')
- ->willReturn($signature);
- $builder = $this->createBuilder();
- $this->assertSame($builder, $builder->sign($signer, 'test'));
- return $builder;
- }
- /**
- * @test
- *
- * @depends signMustKeepAFluentInterface
- *
- * @covers Lcobucci\JWT\Builder::unsign
- */
- public function unsignMustRemoveTheSignature(Builder $builder)
- {
- $builder->unsign();
- $this->assertAttributeSame(null, 'signature', $builder);
- }
- /**
- * @test
- *
- * @depends signMustKeepAFluentInterface
- *
- * @covers Lcobucci\JWT\Builder::unsign
- */
- public function unsignMustKeepAFluentInterface(Builder $builder)
- {
- $this->assertSame($builder, $builder->unsign());
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::sign
- * @uses Lcobucci\JWT\Builder::getToken
- * @uses Lcobucci\JWT\Token
- *
- * @covers Lcobucci\JWT\Builder::set
- *
- * @expectedException BadMethodCallException
- */
- public function setMustRaiseExceptionWhenTokenHasBeenSigned()
- {
- $signer = $this->getMock(Signer::class);
- $signature = $this->getMock(Signature::class, [], [], '', false);
- $signer->expects($this->any())
- ->method('sign')
- ->willReturn($signature);
- $builder = $this->createBuilder();
- $builder->sign($signer, 'test');
- $builder->set('test', 123);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::sign
- * @uses Lcobucci\JWT\Builder::getToken
- * @uses Lcobucci\JWT\Token
- *
- * @covers Lcobucci\JWT\Builder::setHeader
- *
- * @expectedException BadMethodCallException
- */
- public function setHeaderMustRaiseExceptionWhenTokenHasBeenSigned()
- {
- $signer = $this->getMock(Signer::class);
- $signature = $this->getMock(Signature::class, [], [], '', false);
- $signer->expects($this->any())
- ->method('sign')
- ->willReturn($signature);
- $builder = $this->createBuilder();
- $builder->sign($signer, 'test');
- $builder->setHeader('test', 123);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Builder::__construct
- * @uses Lcobucci\JWT\Builder::set
- * @uses Lcobucci\JWT\Token
- *
- * @covers Lcobucci\JWT\Builder::getToken
- */
- public function getTokenMustReturnANewTokenWithCurrentConfiguration()
- {
- $signature = $this->getMock(Signature::class, [], [], '', false);
- $this->encoder->expects($this->exactly(2))
- ->method('jsonEncode')
- ->withConsecutive([['typ'=> 'JWT', 'alg' => 'none']], [['test' => $this->defaultClaim]])
- ->willReturnOnConsecutiveCalls('1', '2');
- $this->encoder->expects($this->exactly(3))
- ->method('base64UrlEncode')
- ->withConsecutive(['1'], ['2'], [$signature])
- ->willReturnOnConsecutiveCalls('1', '2', '3');
- $builder = $this->createBuilder()->set('test', 123);
- $builderSign = new \ReflectionProperty($builder, 'signature');
- $builderSign->setAccessible(true);
- $builderSign->setValue($builder, $signature);
- $token = $builder->getToken();
- $tokenSign = new \ReflectionProperty($token, 'signature');
- $tokenSign->setAccessible(true);
- $this->assertAttributeEquals(['1', '2', '3'], 'payload', $token);
- $this->assertAttributeEquals($token->getHeaders(), 'headers', $builder);
- $this->assertAttributeEquals($token->getClaims(), 'claims', $builder);
- $this->assertAttributeSame($tokenSign->getValue($token), 'signature', $builder);
- }
- }
|