AbstractSourceTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * The RandomLib library for securely generating random numbers and strings in PHP
  4. *
  5. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  6. * @copyright 2011 The Authors
  7. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  8. * @version Build @@version@@
  9. */
  10. namespace RandomLib\Source;
  11. use SecurityLib\Strength;
  12. abstract class AbstractSourceTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function setUp()
  15. {
  16. $class = static::getTestedClass();
  17. if (!$class::isSupported()) {
  18. $this->markTestSkipped();
  19. }
  20. }
  21. protected static function getTestedClass()
  22. {
  23. return preg_replace('/Test$/', '', get_called_class());
  24. }
  25. protected static function getExpectedStrength()
  26. {
  27. return new Strength(Strength::VERYLOW);
  28. }
  29. public static function provideGenerate()
  30. {
  31. $data = array();
  32. for ($i = 0; $i < 100; $i += 5) {
  33. $not = $i > 0 ? str_repeat(chr(0), $i) : chr(0);
  34. $data[] = array($i, $not);
  35. }
  36. return $data;
  37. }
  38. public function testGetStrength()
  39. {
  40. $class = static::getTestedClass();
  41. $strength = static::getExpectedStrength();
  42. $actual = $class::getStrength();
  43. $this->assertEquals($actual, $strength);
  44. }
  45. /**
  46. * @dataProvider provideGenerate
  47. * @group slow
  48. */
  49. public function testGenerate($length, $not)
  50. {
  51. $class = static::getTestedClass();
  52. $rand = new $class();
  53. $stub = $rand->generate($length);
  54. $this->assertEquals($length, strlen($stub));
  55. $this->assertNotEquals($not, $stub);
  56. }
  57. }