ValidationDataTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
  4. *
  5. * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
  6. */
  7. namespace Lcobucci\JWT;
  8. /**
  9. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  10. * @since 2.0.0
  11. */
  12. class ValidationDataTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @test
  16. *
  17. * @covers Lcobucci\JWT\ValidationData::__construct
  18. */
  19. public function constructorShouldConfigureTheItems()
  20. {
  21. $expected = $this->createExpectedData();
  22. $data = new ValidationData(1);
  23. $this->assertAttributeSame($expected, 'items', $data);
  24. }
  25. /**
  26. * @test
  27. *
  28. * @dataProvider claimValues
  29. *
  30. * @uses Lcobucci\JWT\ValidationData::__construct
  31. *
  32. * @covers Lcobucci\JWT\ValidationData::setId
  33. */
  34. public function setIdShouldChangeTheId($id)
  35. {
  36. $expected = $this->createExpectedData($id);
  37. $data = new ValidationData(1);
  38. $data->setId($id);
  39. $this->assertAttributeSame($expected, 'items', $data);
  40. }
  41. /**
  42. * @test
  43. *
  44. * @dataProvider claimValues
  45. *
  46. * @uses Lcobucci\JWT\ValidationData::__construct
  47. *
  48. * @covers Lcobucci\JWT\ValidationData::setIssuer
  49. */
  50. public function setIssuerShouldChangeTheIssuer($iss)
  51. {
  52. $expected = $this->createExpectedData(null, null, $iss);
  53. $data = new ValidationData(1);
  54. $data->setIssuer($iss);
  55. $this->assertAttributeSame($expected, 'items', $data);
  56. }
  57. /**
  58. * @test
  59. *
  60. * @dataProvider claimValues
  61. *
  62. * @uses Lcobucci\JWT\ValidationData::__construct
  63. *
  64. * @covers Lcobucci\JWT\ValidationData::setAudience
  65. */
  66. public function setAudienceShouldChangeTheAudience($aud)
  67. {
  68. $expected = $this->createExpectedData(null, null, null, $aud);
  69. $data = new ValidationData(1);
  70. $data->setAudience($aud);
  71. $this->assertAttributeSame($expected, 'items', $data);
  72. }
  73. /**
  74. * @test
  75. *
  76. * @dataProvider claimValues
  77. *
  78. * @uses Lcobucci\JWT\ValidationData::__construct
  79. *
  80. * @covers Lcobucci\JWT\ValidationData::setSubject
  81. */
  82. public function setSubjectShouldChangeTheSubject($sub)
  83. {
  84. $expected = $this->createExpectedData(null, $sub);
  85. $data = new ValidationData(1);
  86. $data->setSubject($sub);
  87. $this->assertAttributeSame($expected, 'items', $data);
  88. }
  89. /**
  90. * @test
  91. *
  92. * @uses Lcobucci\JWT\ValidationData::__construct
  93. *
  94. * @covers Lcobucci\JWT\ValidationData::setCurrentTime
  95. */
  96. public function setCurrentTimeShouldChangeTheTimeBasedValues()
  97. {
  98. $expected = $this->createExpectedData(null, null, null, null, 2);
  99. $data = new ValidationData(1);
  100. $data->setCurrentTime(2);
  101. $this->assertAttributeSame($expected, 'items', $data);
  102. }
  103. /**
  104. * @test
  105. *
  106. * @uses Lcobucci\JWT\ValidationData::__construct
  107. *
  108. * @covers Lcobucci\JWT\ValidationData::has
  109. */
  110. public function hasShouldReturnTrueWhenItemIsNotEmpty()
  111. {
  112. $data = new ValidationData(1);
  113. $this->assertTrue($data->has('iat'));
  114. }
  115. /**
  116. * @test
  117. *
  118. * @uses Lcobucci\JWT\ValidationData::__construct
  119. *
  120. * @covers Lcobucci\JWT\ValidationData::has
  121. */
  122. public function hasShouldReturnFalseWhenItemIsEmpty()
  123. {
  124. $data = new ValidationData(1);
  125. $this->assertFalse($data->has('jti'));
  126. }
  127. /**
  128. * @test
  129. *
  130. * @uses Lcobucci\JWT\ValidationData::__construct
  131. *
  132. * @covers Lcobucci\JWT\ValidationData::has
  133. */
  134. public function hasShouldReturnFalseWhenItemIsNotDefined()
  135. {
  136. $data = new ValidationData(1);
  137. $this->assertFalse($data->has('test'));
  138. }
  139. /**
  140. * @test
  141. *
  142. * @uses Lcobucci\JWT\ValidationData::__construct
  143. *
  144. * @covers Lcobucci\JWT\ValidationData::get
  145. */
  146. public function getShouldReturnTheItemValue()
  147. {
  148. $data = new ValidationData(1);
  149. $this->assertEquals(1, $data->get('iat'));
  150. }
  151. /**
  152. * @test
  153. *
  154. * @uses Lcobucci\JWT\ValidationData::__construct
  155. *
  156. * @covers Lcobucci\JWT\ValidationData::get
  157. */
  158. public function getShouldReturnNullWhenItemIsNotDefined()
  159. {
  160. $data = new ValidationData(1);
  161. $this->assertNull($data->get('test'));
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function claimValues()
  167. {
  168. return [
  169. [1],
  170. ['test']
  171. ];
  172. }
  173. /**
  174. * @param string $id
  175. * @param string $sub
  176. * @param string $iss
  177. * @param string $aud
  178. * @param int $time
  179. *
  180. * @return array
  181. */
  182. private function createExpectedData(
  183. $id = null,
  184. $sub = null,
  185. $iss = null,
  186. $aud = null,
  187. $time = 1
  188. ) {
  189. return [
  190. 'jti' => $id !== null ? (string) $id : null,
  191. 'iss' => $iss !== null ? (string) $iss : null,
  192. 'aud' => $aud !== null ? (string) $aud : null,
  193. 'sub' => $sub !== null ? (string) $sub : null,
  194. 'iat' => $time,
  195. 'nbf' => $time,
  196. 'exp' => $time
  197. ];
  198. }
  199. }