RequestIdTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\Http;
  8. use OC\AppFramework\Http\RequestId;
  9. use OCP\Security\ISecureRandom;
  10. use PHPUnit\Framework\MockObject\MockObject;
  11. /**
  12. * Class RequestIdTest
  13. *
  14. * @package OC\AppFramework\Http
  15. */
  16. class RequestIdTest extends \Test\TestCase {
  17. /** @var ISecureRandom|MockObject */
  18. protected $secureRandom;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->secureRandom = $this->createMock(ISecureRandom::class);
  22. }
  23. public function testGetIdWithModUnique(): void {
  24. $requestId = new RequestId(
  25. 'GeneratedUniqueIdByModUnique',
  26. $this->secureRandom
  27. );
  28. $this->secureRandom->expects($this->never())
  29. ->method('generate');
  30. $this->assertSame('GeneratedUniqueIdByModUnique', $requestId->getId());
  31. $this->assertSame('GeneratedUniqueIdByModUnique', $requestId->getId());
  32. }
  33. public function testGetIdWithoutModUnique(): void {
  34. $requestId = new RequestId(
  35. '',
  36. $this->secureRandom
  37. );
  38. $this->secureRandom->expects($this->once())
  39. ->method('generate')
  40. ->with('20')
  41. ->willReturnOnConsecutiveCalls(
  42. 'GeneratedByNextcloudItself1',
  43. 'GeneratedByNextcloudItself2',
  44. 'GeneratedByNextcloudItself3'
  45. );
  46. $this->assertSame('GeneratedByNextcloudItself1', $requestId->getId());
  47. $this->assertSame('GeneratedByNextcloudItself1', $requestId->getId());
  48. }
  49. }