FactoryTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\Claim;
  8. /**
  9. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  10. * @since 2.0.0
  11. */
  12. class FactoryTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @test
  16. *
  17. * @covers Lcobucci\JWT\Claim\Factory::__construct
  18. */
  19. public function constructMustConfigureTheCallbacks()
  20. {
  21. $callback = function () {
  22. };
  23. $factory = new Factory(['test' => $callback]);
  24. $expected = [
  25. 'iat' => [$factory, 'createLesserOrEqualsTo'],
  26. 'nbf' => [$factory, 'createLesserOrEqualsTo'],
  27. 'exp' => [$factory, 'createGreaterOrEqualsTo'],
  28. 'iss' => [$factory, 'createEqualsTo'],
  29. 'aud' => [$factory, 'createEqualsTo'],
  30. 'sub' => [$factory, 'createEqualsTo'],
  31. 'jti' => [$factory, 'createEqualsTo'],
  32. 'test' => $callback
  33. ];
  34. $this->assertAttributeEquals($expected, 'callbacks', $factory);
  35. }
  36. /**
  37. * @test
  38. *
  39. * @uses Lcobucci\JWT\Claim\Factory::__construct
  40. * @uses Lcobucci\JWT\Claim\Basic::__construct
  41. *
  42. * @covers Lcobucci\JWT\Claim\Factory::create
  43. * @covers Lcobucci\JWT\Claim\Factory::createLesserOrEqualsTo
  44. */
  45. public function createShouldReturnALesserOrEqualsToClaimForIssuedAt()
  46. {
  47. $claim = new Factory();
  48. $this->assertInstanceOf(LesserOrEqualsTo::class, $claim->create('iat', 1));
  49. }
  50. /**
  51. * @test
  52. *
  53. * @uses Lcobucci\JWT\Claim\Factory::__construct
  54. * @uses Lcobucci\JWT\Claim\Basic::__construct
  55. *
  56. * @covers Lcobucci\JWT\Claim\Factory::create
  57. * @covers Lcobucci\JWT\Claim\Factory::createLesserOrEqualsTo
  58. */
  59. public function createShouldReturnALesserOrEqualsToClaimForNotBefore()
  60. {
  61. $claim = new Factory();
  62. $this->assertInstanceOf(LesserOrEqualsTo::class, $claim->create('nbf', 1));
  63. }
  64. /**
  65. * @test
  66. *
  67. * @uses Lcobucci\JWT\Claim\Factory::__construct
  68. * @uses Lcobucci\JWT\Claim\Basic::__construct
  69. *
  70. * @covers Lcobucci\JWT\Claim\Factory::create
  71. * @covers Lcobucci\JWT\Claim\Factory::createGreaterOrEqualsTo
  72. */
  73. public function createShouldReturnAGreaterOrEqualsToClaimForExpiration()
  74. {
  75. $claim = new Factory();
  76. $this->assertInstanceOf(GreaterOrEqualsTo::class, $claim->create('exp', 1));
  77. }
  78. /**
  79. * @test
  80. *
  81. * @uses Lcobucci\JWT\Claim\Factory::__construct
  82. * @uses Lcobucci\JWT\Claim\Basic::__construct
  83. *
  84. * @covers Lcobucci\JWT\Claim\Factory::create
  85. * @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
  86. */
  87. public function createShouldReturnAnEqualsToClaimForId()
  88. {
  89. $claim = new Factory();
  90. $this->assertInstanceOf(EqualsTo::class, $claim->create('jti', 1));
  91. }
  92. /**
  93. * @test
  94. *
  95. * @uses Lcobucci\JWT\Claim\Factory::__construct
  96. * @uses Lcobucci\JWT\Claim\Basic::__construct
  97. *
  98. * @covers Lcobucci\JWT\Claim\Factory::create
  99. * @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
  100. */
  101. public function createShouldReturnAnEqualsToClaimForIssuer()
  102. {
  103. $claim = new Factory();
  104. $this->assertInstanceOf(EqualsTo::class, $claim->create('iss', 1));
  105. }
  106. /**
  107. * @test
  108. *
  109. * @uses Lcobucci\JWT\Claim\Factory::__construct
  110. * @uses Lcobucci\JWT\Claim\Basic::__construct
  111. *
  112. * @covers Lcobucci\JWT\Claim\Factory::create
  113. * @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
  114. */
  115. public function createShouldReturnAnEqualsToClaimForAudience()
  116. {
  117. $claim = new Factory();
  118. $this->assertInstanceOf(EqualsTo::class, $claim->create('aud', 1));
  119. }
  120. /**
  121. * @test
  122. *
  123. * @uses Lcobucci\JWT\Claim\Factory::__construct
  124. * @uses Lcobucci\JWT\Claim\Basic::__construct
  125. *
  126. * @covers Lcobucci\JWT\Claim\Factory::create
  127. * @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
  128. */
  129. public function createShouldReturnAnEqualsToClaimForSubject()
  130. {
  131. $claim = new Factory();
  132. $this->assertInstanceOf(EqualsTo::class, $claim->create('sub', 1));
  133. }
  134. /**
  135. * @test
  136. *
  137. * @uses Lcobucci\JWT\Claim\Factory::__construct
  138. * @uses Lcobucci\JWT\Claim\Basic::__construct
  139. *
  140. * @covers Lcobucci\JWT\Claim\Factory::create
  141. * @covers Lcobucci\JWT\Claim\Factory::createBasic
  142. */
  143. public function createShouldReturnABasiclaimForOtherClaims()
  144. {
  145. $claim = new Factory();
  146. $this->assertInstanceOf(Basic::class, $claim->create('test', 1));
  147. }
  148. }