KeyTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 org\bovigo\vfs\vfsStream;
  9. /**
  10. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  11. * @since 3.0.4
  12. */
  13. class KeyTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @before
  17. */
  18. public function configureRootDir()
  19. {
  20. vfsStream::setup(
  21. 'root',
  22. null,
  23. [
  24. 'test.pem' => 'testing',
  25. 'emptyFolder' => []
  26. ]
  27. );
  28. }
  29. /**
  30. * @test
  31. *
  32. * @covers Lcobucci\JWT\Signer\Key::__construct
  33. * @covers Lcobucci\JWT\Signer\Key::setContent
  34. */
  35. public function constructShouldConfigureContentAndPassphrase()
  36. {
  37. $key = new Key('testing', 'test');
  38. $this->assertAttributeEquals('testing', 'content', $key);
  39. $this->assertAttributeEquals('test', 'passphrase', $key);
  40. }
  41. /**
  42. * @test
  43. *
  44. * @covers Lcobucci\JWT\Signer\Key::__construct
  45. * @covers Lcobucci\JWT\Signer\Key::setContent
  46. * @covers Lcobucci\JWT\Signer\Key::readFile
  47. */
  48. public function constructShouldBeAbleToConfigureContentFromFile()
  49. {
  50. $key = new Key('file://' . vfsStream::url('root/test.pem'));
  51. $this->assertAttributeEquals('testing', 'content', $key);
  52. $this->assertAttributeEquals(null, 'passphrase', $key);
  53. }
  54. /**
  55. * @test
  56. *
  57. * @expectedException \InvalidArgumentException
  58. *
  59. * @covers Lcobucci\JWT\Signer\Key::__construct
  60. * @covers Lcobucci\JWT\Signer\Key::setContent
  61. * @covers Lcobucci\JWT\Signer\Key::readFile
  62. */
  63. public function constructShouldRaiseExceptionWhenFileDoesNotExists()
  64. {
  65. new Key('file://' . vfsStream::url('root/test2.pem'));
  66. }
  67. /**
  68. * @test
  69. *
  70. * @expectedException \InvalidArgumentException
  71. *
  72. * @covers Lcobucci\JWT\Signer\Key::__construct
  73. * @covers Lcobucci\JWT\Signer\Key::setContent
  74. * @covers Lcobucci\JWT\Signer\Key::readFile
  75. */
  76. public function constructShouldRaiseExceptionWhenFileGetContentsFailed()
  77. {
  78. new Key('file://' . vfsStream::url('root/emptyFolder'));
  79. }
  80. /**
  81. * @test
  82. *
  83. * @uses Lcobucci\JWT\Signer\Key::__construct
  84. * @uses Lcobucci\JWT\Signer\Key::setContent
  85. *
  86. * @covers Lcobucci\JWT\Signer\Key::getContent
  87. */
  88. public function getContentShouldReturnConfiguredData()
  89. {
  90. $key = new Key('testing', 'test');
  91. $this->assertEquals('testing', $key->getContent());
  92. }
  93. /**
  94. * @test
  95. *
  96. * @uses Lcobucci\JWT\Signer\Key::__construct
  97. * @uses Lcobucci\JWT\Signer\Key::setContent
  98. *
  99. * @covers Lcobucci\JWT\Signer\Key::getPassphrase
  100. */
  101. public function getPassphraseShouldReturnConfiguredData()
  102. {
  103. $key = new Key('testing', 'test');
  104. $this->assertEquals('test', $key->getPassphrase());
  105. }
  106. }