UriSafeTokenGeneratorTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Security\Csrf\Tests\TokenGenerator;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class UriSafeTokenGeneratorTest extends TestCase
  17. {
  18. const ENTROPY = 1000;
  19. /**
  20. * A non alpha-numeric byte string.
  21. *
  22. * @var string
  23. */
  24. private static $bytes;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $random;
  29. /**
  30. * @var UriSafeTokenGenerator
  31. */
  32. private $generator;
  33. public static function setUpBeforeClass()
  34. {
  35. self::$bytes = base64_decode('aMf+Tct/RLn2WQ==');
  36. }
  37. protected function setUp()
  38. {
  39. $this->generator = new UriSafeTokenGenerator(self::ENTROPY);
  40. }
  41. protected function tearDown()
  42. {
  43. $this->random = null;
  44. $this->generator = null;
  45. }
  46. public function testGenerateToken()
  47. {
  48. $token = $this->generator->generateToken();
  49. $this->assertTrue(ctype_print($token), 'is printable');
  50. $this->assertStringNotMatchesFormat('%S+%S', $token, 'is URI safe');
  51. $this->assertStringNotMatchesFormat('%S/%S', $token, 'is URI safe');
  52. $this->assertStringNotMatchesFormat('%S=%S', $token, 'is URI safe');
  53. }
  54. }