Skip to content

Commit

Permalink
Merge branch 'feature/token-defaults' into 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Nov 15, 2015
2 parents 92476b1 + 166d23b commit 8819b1a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 32 deletions.
32 changes: 27 additions & 5 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,34 @@ public function hasHeader($name)
* Returns the value of a token header
*
* @param string $name
* @param mixed $default
*
* @return mixed
*
* @throws OutOfBoundsException
*/
public function getHeader($name)
public function getHeader($name, $default = null)
{
if (!isset($this->headers[$name])) {
if ($this->hasHeader($name)) {
return $this->getHeaderValue($name);
}

if ($default === null) {
throw new OutOfBoundsException('Requested header is not configured');
}

return $default;
}

/**
* Returns the value stored in header
*
* @param string $name
*
* @return mixed
*/
private function getHeaderValue($name)
{
$header = $this->headers[$name];

if ($header instanceof Claim) {
Expand Down Expand Up @@ -140,18 +157,23 @@ public function hasClaim($name)
* Returns the value of a token claim
*
* @param string $name
* @param mixed $default
*
* @return mixed
*
* @throws OutOfBoundsException
*/
public function getClaim($name)
public function getClaim($name, $default = null)
{
if (!isset($this->claims[$name])) {
if ($this->hasClaim($name)) {
return $this->claims[$name]->getValue();
}

if ($default === null) {
throw new OutOfBoundsException('Requested claim is not configured');
}

return $this->claims[$name]->getValue();
return $default;
}

/**
Expand Down
77 changes: 50 additions & 27 deletions test/unit/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function hasHeaderMustReturnFalseWhenItIsNotConfigured()
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasHeader
*
* @covers Lcobucci\JWT\Token::getHeader
*
Expand All @@ -81,9 +82,26 @@ public function getHeaderMustRaiseExceptionWhenHeaderIsNotConfigured()
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasHeader
*
* @covers Lcobucci\JWT\Token::getHeader
*/
public function getHeaderMustReturnTheDefaultValueWhenIsNotConfigured()
{
$token = new Token(['test' => 'testing']);

$this->assertEquals('blah', $token->getHeader('testing', 'blah'));
}

/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasHeader
*
* @covers Lcobucci\JWT\Token::getHeader
* @covers Lcobucci\JWT\Token::getHeaderValue
*/
public function getHeaderMustReturnTheRequestedHeader()
{
$token = new Token(['test' => 'testing']);
Expand All @@ -95,10 +113,11 @@ public function getHeaderMustReturnTheRequestedHeader()
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\Token::hasHeader
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::getHeader
* @covers Lcobucci\JWT\Token::getHeaderValue
*/
public function getHeaderMustReturnValueWhenItIsAReplicatedClaim()
{
Expand Down Expand Up @@ -139,6 +158,7 @@ public function getClaimsMustReturnTheConfiguredClaims()
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::hasClaim
*/
Expand All @@ -153,10 +173,9 @@ public function hasClaimMustReturnTrueWhenItIsConfigured()
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::hasClaim
*
* @uses Lcobucci\JWT\Claim\Basic
*/
public function hasClaimMustReturnFalseWhenItIsNotConfigured()
{
Expand All @@ -169,7 +188,24 @@ public function hasClaimMustReturnFalseWhenItIsNotConfigured()
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Token::hasClaim
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::getClaim
*/
public function getClaimMustReturnTheDefaultValueWhenIsNotConfigured()
{
$token = new Token([], ['test' => new Basic('test', 'testing')]);

$this->assertEquals('blah', $token->getClaim('testing', 'blah'));
}

/**
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasClaim
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::getClaim
*
Expand All @@ -185,6 +221,7 @@ public function getClaimShouldRaiseExceptionWhenClaimIsNotConfigured()
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\Token::hasClaim
* @uses Lcobucci\JWT\Claim\Basic
*
* @covers Lcobucci\JWT\Token::getClaim
Expand Down Expand Up @@ -302,15 +339,9 @@ public function validateShouldReturnTrueWhenThereAreNoValidatableClaims()
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::get
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\Claim\EqualsTo::__construct
* @uses Lcobucci\JWT\Claim\EqualsTo::validate
* @uses Lcobucci\JWT\ValidationData
* @uses Lcobucci\JWT\Claim\Basic
* @uses Lcobucci\JWT\Claim\EqualsTo
*
* @covers Lcobucci\JWT\Token::validate
* @covers Lcobucci\JWT\Token::getValidatableClaims
Expand All @@ -335,19 +366,11 @@ public function validateShouldReturnFalseWhenThereIsAtLeastOneFailedValidatableC
* @test
*
* @uses Lcobucci\JWT\Token::__construct
* @uses Lcobucci\JWT\ValidationData::__construct
* @uses Lcobucci\JWT\ValidationData::get
* @uses Lcobucci\JWT\ValidationData::has
* @uses Lcobucci\JWT\ValidationData::setIssuer
* @uses Lcobucci\JWT\Claim\Basic::__construct
* @uses Lcobucci\JWT\Claim\Basic::getName
* @uses Lcobucci\JWT\Claim\Basic::getValue
* @uses Lcobucci\JWT\Claim\EqualsTo::__construct
* @uses Lcobucci\JWT\Claim\EqualsTo::validate
* @uses Lcobucci\JWT\Claim\LesserOrEqualsTo::__construct
* @uses Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo::__construct
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
* @uses Lcobucci\JWT\ValidationData
* @uses Lcobucci\JWT\Claim\Basic
* @uses Lcobucci\JWT\Claim\EqualsTo
* @uses Lcobucci\JWT\Claim\LesserOrEqualsTo
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
*
* @covers Lcobucci\JWT\Token::validate
* @covers Lcobucci\JWT\Token::getValidatableClaims
Expand Down

0 comments on commit 8819b1a

Please sign in to comment.