菜谱项目

NamshiAdapterTest.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Providers\JWT\NamshiAdapter;
  14. class NamshiAdapterTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function setUp()
  17. {
  18. Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
  19. $this->jws = Mockery::mock('Namshi\JOSE\JWS');
  20. $this->provider = new NamshiAdapter('secret', 'HS256', $this->jws);
  21. }
  22. public function tearDown()
  23. {
  24. Mockery::close();
  25. }
  26. /** @test */
  27. public function it_should_return_the_token_when_passing_a_valid_subject_to_encode()
  28. {
  29. $payload = ['sub' => 1, 'exp' => 123, 'iat' => 123, 'iss' => '/foo'];
  30. $this->jws->shouldReceive('setPayload')->once()->with($payload)->andReturn(Mockery::self());
  31. $this->jws->shouldReceive('sign')->once()->with('secret')->andReturn(Mockery::self());
  32. $this->jws->shouldReceive('getTokenString')->once()->andReturn('foo.bar.baz');
  33. $token = $this->provider->encode($payload);
  34. $this->assertEquals('foo.bar.baz', $token);
  35. }
  36. /** @test */
  37. public function it_should_throw_an_invalid_exception_when_the_payload_could_not_be_encoded()
  38. {
  39. $this->setExpectedException('Tymon\JWTAuth\Exceptions\JWTException');
  40. $this->jws->shouldReceive('sign')->andThrow(new \Exception);
  41. $payload = ['sub' => 1, 'exp' => 123, 'iat' => 123, 'iss' => '/foo'];
  42. $this->provider->encode($payload);
  43. }
  44. /** @test */
  45. // public function it_should_return_the_payload_when_passing_a_valid_token_to_decode()
  46. // {
  47. // $this->jws->shouldReceive('load')->once()->with('foo.bar.baz')->andReturn(true);
  48. // $this->jws->shouldReceive('verify')->andReturn(true);
  49. // $payload = $this->provider->decode('foo.bar.baz');
  50. // }
  51. /** @test */
  52. public function it_should_throw_a_token_invalid_exception_when_the_token_could_not_be_decoded()
  53. {
  54. $this->setExpectedException('Tymon\JWTAuth\Exceptions\TokenInvalidException');
  55. $this->jws->shouldReceive('verify')->andReturn(false);
  56. $token = $this->provider->decode('foo');
  57. }
  58. }