BaseSignerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Signer;
  8. use Lcobucci\JWT\Signature;
  9. /**
  10. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  11. * @since 0.1.0
  12. */
  13. class BaseSignerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var BaseSigner|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $signer;
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp()
  23. {
  24. $this->signer = $this->getMockForAbstractClass(BaseSigner::class);
  25. $this->signer->method('getAlgorithmId')
  26. ->willReturn('TEST123');
  27. }
  28. /**
  29. * @test
  30. *
  31. * @covers Lcobucci\JWT\Signer\BaseSigner::modifyHeader
  32. */
  33. public function modifyHeaderShouldChangeAlgorithm()
  34. {
  35. $headers = ['typ' => 'JWT'];
  36. $this->signer->modifyHeader($headers);
  37. $this->assertEquals($headers['typ'], 'JWT');
  38. $this->assertEquals($headers['alg'], 'TEST123');
  39. }
  40. /**
  41. * @test
  42. *
  43. * @uses Lcobucci\JWT\Signature::__construct
  44. * @uses Lcobucci\JWT\Signer\Key
  45. *
  46. * @covers Lcobucci\JWT\Signer\BaseSigner::sign
  47. * @covers Lcobucci\JWT\Signer\BaseSigner::getKey
  48. */
  49. public function signMustReturnANewSignature()
  50. {
  51. $key = new Key('123');
  52. $this->signer->expects($this->once())
  53. ->method('createHash')
  54. ->with('test', $key)
  55. ->willReturn('test');
  56. $this->assertEquals(new Signature('test'), $this->signer->sign('test', $key));
  57. }
  58. /**
  59. * @test
  60. *
  61. * @uses Lcobucci\JWT\Signature::__construct
  62. * @uses Lcobucci\JWT\Signer\Key
  63. *
  64. * @covers Lcobucci\JWT\Signer\BaseSigner::sign
  65. * @covers Lcobucci\JWT\Signer\BaseSigner::getKey
  66. */
  67. public function signShouldConvertKeyWhenItsNotAnObject()
  68. {
  69. $this->signer->expects($this->once())
  70. ->method('createHash')
  71. ->with('test', new Key('123'))
  72. ->willReturn('test');
  73. $this->assertEquals(new Signature('test'), $this->signer->sign('test', '123'));
  74. }
  75. /**
  76. * @test
  77. *
  78. * @uses Lcobucci\JWT\Signature::__construct
  79. * @uses Lcobucci\JWT\Signer\Key
  80. *
  81. * @covers Lcobucci\JWT\Signer\BaseSigner::verify
  82. * @covers Lcobucci\JWT\Signer\BaseSigner::getKey
  83. */
  84. public function verifyShouldDelegateTheCallToAbstractMethod()
  85. {
  86. $key = new Key('123');
  87. $this->signer->expects($this->once())
  88. ->method('doVerify')
  89. ->with('test', 'test', $key)
  90. ->willReturn(true);
  91. $this->assertTrue($this->signer->verify('test', 'test', $key));
  92. }
  93. /**
  94. * @test
  95. *
  96. * @uses Lcobucci\JWT\Signature::__construct
  97. * @uses Lcobucci\JWT\Signer\Key
  98. *
  99. * @covers Lcobucci\JWT\Signer\BaseSigner::verify
  100. * @covers Lcobucci\JWT\Signer\BaseSigner::getKey
  101. */
  102. public function verifyShouldConvertKeyWhenItsNotAnObject()
  103. {
  104. $this->signer->expects($this->once())
  105. ->method('doVerify')
  106. ->with('test', 'test', new Key('123'))
  107. ->willReturn(true);
  108. $this->assertTrue($this->signer->verify('test', 'test', '123'));
  109. }
  110. }