1
0

RequestIdTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Http;
  23. use OC\AppFramework\Http\RequestId;
  24. use OCP\Security\ISecureRandom;
  25. use PHPUnit\Framework\MockObject\MockObject;
  26. /**
  27. * Class RequestIdTest
  28. *
  29. * @package OC\AppFramework\Http
  30. */
  31. class RequestIdTest extends \Test\TestCase {
  32. /** @var ISecureRandom|MockObject */
  33. protected $secureRandom;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->secureRandom = $this->createMock(ISecureRandom::class);
  37. }
  38. public function testGetIdWithModUnique(): void {
  39. $requestId = new RequestId(
  40. 'GeneratedUniqueIdByModUnique',
  41. $this->secureRandom
  42. );
  43. $this->secureRandom->expects($this->never())
  44. ->method('generate');
  45. $this->assertSame('GeneratedUniqueIdByModUnique', $requestId->getId());
  46. $this->assertSame('GeneratedUniqueIdByModUnique', $requestId->getId());
  47. }
  48. public function testGetIdWithoutModUnique(): void {
  49. $requestId = new RequestId(
  50. '',
  51. $this->secureRandom
  52. );
  53. $this->secureRandom->expects($this->once())
  54. ->method('generate')
  55. ->with('20')
  56. ->willReturnOnConsecutiveCalls(
  57. 'GeneratedByNextcloudItself1',
  58. 'GeneratedByNextcloudItself2',
  59. 'GeneratedByNextcloudItself3'
  60. );
  61. $this->assertSame('GeneratedByNextcloudItself1', $requestId->getId());
  62. $this->assertSame('GeneratedByNextcloudItself1', $requestId->getId());
  63. }
  64. }