RandomLibAdapterTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Generator;
  4. use Mockery;
  5. use Mockery\MockInterface;
  6. use Ramsey\Uuid\Generator\RandomLibAdapter;
  7. use Ramsey\Uuid\Test\TestCase;
  8. use RandomLib\Factory as RandomLibFactory;
  9. use RandomLib\Generator;
  10. class RandomLibAdapterTest extends TestCase
  11. {
  12. /**
  13. * @runInSeparateProcess
  14. * @preserveGlobalState disabled
  15. */
  16. public function testAdapterWithGeneratorDoesNotCreateGenerator(): void
  17. {
  18. $factory = Mockery::mock('overload:' . RandomLibFactory::class);
  19. $factory->shouldNotReceive('getHighStrengthGenerator');
  20. $generator = $this->getMockBuilder(Generator::class)
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $this->assertInstanceOf(RandomLibAdapter::class, new RandomLibAdapter($generator));
  24. }
  25. /**
  26. * @runInSeparateProcess
  27. * @preserveGlobalState disabled
  28. */
  29. public function testAdapterWithoutGeneratorGreatesGenerator(): void
  30. {
  31. $generator = Mockery::mock(Generator::class);
  32. /** @var RandomLibFactory&MockInterface $factory */
  33. $factory = Mockery::mock('overload:' . RandomLibFactory::class);
  34. $factory->expects()->getHighStrengthGenerator()->andReturns($generator);
  35. $this->assertInstanceOf(RandomLibAdapter::class, new RandomLibAdapter());
  36. }
  37. public function testGenerateUsesGenerator(): void
  38. {
  39. $length = 10;
  40. $generator = $this->getMockBuilder(Generator::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $generator->expects($this->once())
  44. ->method('generate')
  45. ->with($length)
  46. ->willReturn('foo');
  47. $adapter = new RandomLibAdapter($generator);
  48. $adapter->generate($length);
  49. }
  50. public function testGenerateReturnsString(): void
  51. {
  52. $generator = $this->getMockBuilder(Generator::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $generator->expects($this->once())
  56. ->method('generate')
  57. ->willReturn('random-string');
  58. $adapter = new RandomLibAdapter($generator);
  59. $result = $adapter->generate(1);
  60. $this->assertSame('random-string', $result);
  61. }
  62. }