Skip to content

Commit

Permalink
Adding type cast to ValidationData (fixes #28)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Sep 2, 2015
1 parent 9874909 commit 589ff72
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 38 deletions.
14 changes: 7 additions & 7 deletions src/ValidationData.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct($currentTime = null)
*/
public function setId($id)
{
$this->items['jti'] = $id;
$this->items['jti'] = (string) $id;
}

/**
Expand All @@ -59,7 +59,7 @@ public function setId($id)
*/
public function setIssuer($issuer)
{
$this->items['iss'] = $issuer;
$this->items['iss'] = (string) $issuer;
}

/**
Expand All @@ -69,7 +69,7 @@ public function setIssuer($issuer)
*/
public function setAudience($audience)
{
$this->items['aud'] = $audience;
$this->items['aud'] = (string) $audience;
}

/**
Expand All @@ -79,7 +79,7 @@ public function setAudience($audience)
*/
public function setSubject($subject)
{
$this->items['sub'] = $subject;
$this->items['sub'] = (string) $subject;
}

/**
Expand All @@ -89,9 +89,9 @@ public function setSubject($subject)
*/
public function setCurrentTime($currentTime)
{
$this->items['iat'] = $currentTime;
$this->items['nbf'] = $currentTime;
$this->items['exp'] = $currentTime;
$this->items['iat'] = (int) $currentTime;
$this->items['nbf'] = (int) $currentTime;
$this->items['exp'] = (int) $currentTime;
}

/**
Expand Down
97 changes: 66 additions & 31 deletions test/ValidationDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,83 +20,82 @@ class ValidationDataTest extends \PHPUnit_Framework_TestCase
*/
public function constructorShouldConfigureTheItems()
{
$expected = $this->createExpectedData();
$data = new ValidationData(1);
$items = ['jti' => null, 'iss' => null, 'aud' => null, 'sub' => null];
$items['iat'] = $items['nbf'] = $items['exp'] = 1;

$this->assertAttributeEquals($items, 'items', $data);
$this->assertAttributeSame($expected, 'items', $data);
}

/**
* @test
*
* @dataProvider claimValues
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setId
*/
public function setIdShouldChangeTheId()
public function setIdShouldChangeTheId($id)
{
$expected = $this->createExpectedData($id);
$data = new ValidationData(1);
$data->setId(1);

$items = ['jti' => 1, 'iss' => null, 'aud' => null, 'sub' => null];
$items['iat'] = $items['nbf'] = $items['exp'] = 1;
$data->setId($id);

$this->assertAttributeEquals($items, 'items', $data);
$this->assertAttributeSame($expected, 'items', $data);
}

/**
* @test
*
* @dataProvider claimValues
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setIssuer
*/
public function setIssuerShouldChangeTheIssuer()
public function setIssuerShouldChangeTheIssuer($iss)
{
$expected = $this->createExpectedData(null, null, $iss);
$data = new ValidationData(1);
$data->setIssuer('test');
$data->setIssuer($iss);

$items = ['jti' => null, 'iss' => 'test', 'aud' => null, 'sub' => null];
$items['iat'] = $items['nbf'] = $items['exp'] = 1;

$this->assertAttributeEquals($items, 'items', $data);
$this->assertAttributeSame($expected, 'items', $data);
}

/**
* @test
*
* @dataProvider claimValues
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setAudience
*/
public function setAudienceShouldChangeTheAudience()
public function setAudienceShouldChangeTheAudience($aud)
{
$expected = $this->createExpectedData(null, null, null, $aud);
$data = new ValidationData(1);
$data->setAudience('test');
$data->setAudience($aud);

$items = ['jti' => null, 'iss' => null, 'aud' => 'test', 'sub' => null];
$items['iat'] = $items['nbf'] = $items['exp'] = 1;

$this->assertAttributeEquals($items, 'items', $data);
$this->assertAttributeSame($expected, 'items', $data);
}

/**
* @test
*
* @dataProvider claimValues
*
* @uses Lcobucci\JWT\ValidationData::__construct
*
* @covers Lcobucci\JWT\ValidationData::setSubject
*/
public function setSubjectShouldChangeTheSubject()
public function setSubjectShouldChangeTheSubject($sub)
{
$expected = $this->createExpectedData(null, $sub);
$data = new ValidationData(1);
$data->setSubject('test');

$items = ['jti' => null, 'iss' => null, 'aud' => null, 'sub' => 'test'];
$items['iat'] = $items['nbf'] = $items['exp'] = 1;
$data->setSubject($sub);

$this->assertAttributeEquals($items, 'items', $data);
$this->assertAttributeSame($expected, 'items', $data);
}

/**
Expand All @@ -108,13 +107,11 @@ public function setSubjectShouldChangeTheSubject()
*/
public function setCurrentTimeShouldChangeTheTimeBasedValues()
{
$expected = $this->createExpectedData(null, null, null, null, 2);
$data = new ValidationData(1);
$data->setCurrentTime(2);

$items = ['jti' => null, 'iss' => null, 'aud' => null, 'sub' => null];
$items['iat'] = $items['nbf'] = $items['exp'] = 2;

$this->assertAttributeEquals($items, 'items', $data);
$this->assertAttributeSame($expected, 'items', $data);
}

/**
Expand Down Expand Up @@ -186,4 +183,42 @@ public function getShouldReturnNullWhenItemIsNotDefined()

$this->assertNull($data->get('test'));
}

/**
* @return array
*/
public function claimValues()
{
return [
[1],
['test']
];
}

/**
* @param string $id
* @param string $sub
* @param string $iss
* @param string $aud
* @param int $time
*
* @return array
*/
private function createExpectedData(
$id = null,
$sub = null,
$iss = null,
$aud = null,
$time = 1
) {
return [
'jti' => $id !== null ? (string) $id : null,
'iss' => $iss !== null ? (string) $iss : null,
'aud' => $aud !== null ? (string) $aud : null,
'sub' => $sub !== null ? (string) $sub : null,
'iat' => $time,
'nbf' => $time,
'exp' => $time
];
}
}

0 comments on commit 589ff72

Please sign in to comment.