菜谱项目

PayloadTest.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of jwt-auth.
  4. *
  5. * (c) Sean Tymon <tymon148@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Tymon\JWTAuth\Test\Providers\JWT;
  11. use Mockery;
  12. use Carbon\Carbon;
  13. use Tymon\JWTAuth\Payload;
  14. use Tymon\JWTAuth\Claims\JwtId;
  15. use Tymon\JWTAuth\Claims\Issuer;
  16. use Tymon\JWTAuth\Claims\Subject;
  17. use Tymon\JWTAuth\Claims\Audience;
  18. use Tymon\JWTAuth\Claims\IssuedAt;
  19. use Tymon\JWTAuth\Claims\NotBefore;
  20. use Tymon\JWTAuth\Claims\Expiration;
  21. class PayloadTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function setUp()
  24. {
  25. Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
  26. $claims = [
  27. new Subject(1),
  28. new Issuer('http://example.com'),
  29. new Expiration(123 + 3600),
  30. new NotBefore(123),
  31. new IssuedAt(123),
  32. new JwtId('foo'),
  33. ];
  34. $this->validator = Mockery::mock('Tymon\JWTAuth\Validators\PayloadValidator');
  35. $this->validator->shouldReceive('setRefreshFlow->check');
  36. $this->payload = new Payload($claims, $this->validator);
  37. }
  38. public function tearDown()
  39. {
  40. Mockery::close();
  41. }
  42. /** @test */
  43. public function it_throws_an_exception_when_trying_to_add_to_the_payload()
  44. {
  45. $this->setExpectedException('Tymon\JWTAuth\Exceptions\PayloadException');
  46. $this->payload['foo'] = 'bar';
  47. }
  48. /** @test */
  49. public function it_throws_an_exception_when_trying_to_remove_a_key_from_the_payload()
  50. {
  51. $this->setExpectedException('Tymon\JWTAuth\Exceptions\PayloadException');
  52. unset($this->payload['foo']);
  53. }
  54. /** @test */
  55. public function it_should_cast_the_payload_to_a_string_as_json()
  56. {
  57. $this->assertEquals((string) $this->payload, json_encode($this->payload->get()));
  58. $this->assertJsonStringEqualsJsonString((string) $this->payload, json_encode($this->payload->get()));
  59. }
  60. /** @test */
  61. public function it_should_allow_array_access_on_the_payload()
  62. {
  63. $this->assertTrue(isset($this->payload['iat']));
  64. $this->assertEquals($this->payload['sub'], 1);
  65. $this->assertArrayHasKey('exp', $this->payload);
  66. }
  67. /** @test */
  68. public function it_should_get_properties_of_payload_via_get_method()
  69. {
  70. $this->assertInternalType('array', $this->payload->get());
  71. $this->assertEquals($this->payload->get('sub'), 1);
  72. }
  73. /** @test */
  74. public function it_should_get_multiple_properties_when_passing_an_array_to_the_get_method()
  75. {
  76. $values = $this->payload->get(['sub', 'jti']);
  77. list($sub, $jti) = $values;
  78. $this->assertInternalType('array', $values);
  79. $this->assertEquals($sub, 1);
  80. $this->assertEquals($jti, 'foo');
  81. }
  82. /** @test */
  83. public function it_should_determine_whether_the_payload_has_a_claim()
  84. {
  85. $this->assertTrue($this->payload->has(new Subject(1)));
  86. $this->assertFalse($this->payload->has(new Audience(1)));
  87. }
  88. /** @test */
  89. public function it_should_magically_get_a_property()
  90. {
  91. $sub = $this->payload->getSubject();
  92. $jti = $this->payload->getJwtId();
  93. $iss = $this->payload->getIssuer();
  94. $this->assertEquals($sub, 1);
  95. $this->assertEquals($jti, 'foo');
  96. $this->assertEquals($iss, 'http://example.com');
  97. }
  98. /** @test */
  99. public function it_should_throw_an_exception_when_magically_getting_a_property_that_does_not_exist()
  100. {
  101. $this->setExpectedException('\BadMethodCallException');
  102. $this->payload->getFoo();
  103. }
  104. /** @test */
  105. public function it_should_get_the_claims()
  106. {
  107. $claims = $this->payload->getClaims();
  108. $this->assertInstanceOf('Tymon\JWTAuth\Claims\Expiration', $claims[2]);
  109. $this->assertInstanceOf('Tymon\JWTAuth\Claims\JwtId', $claims[5]);
  110. }
  111. }