FactoryTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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;
  11. use SecurityLib\Strength;
  12. class FactoryTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstruct()
  15. {
  16. $factory = new Factory();
  17. $this->assertTrue($factory instanceof Factory);
  18. }
  19. public function testGetGeneratorFallback()
  20. {
  21. $factory = new Factory();
  22. $generator = $factory->getGenerator(new Strength(Strength::VERYLOW));
  23. $mixer = call_user_func(array(
  24. get_class($generator->getMixer()),
  25. 'getStrength',
  26. ));
  27. $this->assertTrue($mixer->compare(new Strength(Strength::VERYLOW)) <= 0);
  28. }
  29. /**
  30. * @covers RandomLib\Factory::getMediumStrengthGenerator
  31. * @covers RandomLib\Factory::getGenerator
  32. * @covers RandomLib\Factory::findMixer
  33. * @covers RandomLib\Factory::findSources
  34. */
  35. public function testGetMediumStrengthGenerator()
  36. {
  37. $factory = new Factory();
  38. $generator = $factory->getMediumStrengthGenerator();
  39. $this->assertTrue($generator instanceof Generator);
  40. $mixer = call_user_func(array(
  41. get_class($generator->getMixer()),
  42. 'getStrength',
  43. ));
  44. $this->assertTrue($mixer->compare(new Strength(Strength::MEDIUM)) <= 0);
  45. foreach ($generator->getSources() as $source) {
  46. $strength = call_user_func(array(get_class($source), 'getStrength'));
  47. $this->assertTrue($strength->compare(new Strength(Strength::MEDIUM)) >= 0);
  48. }
  49. }
  50. /**
  51. * @expectedException RuntimeException
  52. * @expectedExceptionMessage Could not find sources
  53. */
  54. public function testNoAvailableSource()
  55. {
  56. $factory = new Factory();
  57. $sources = new \ReflectionProperty($factory, 'sources');
  58. $sources->setAccessible(true);
  59. $sources->setValue($factory, array());
  60. $factory->getMediumStrengthGenerator();
  61. }
  62. }