InitialStateServiceTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use JsonSerializable;
  9. use OC\AppFramework\Bootstrap\Coordinator;
  10. use OC\InitialStateService;
  11. use OCP\IServerContainer;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Psr\Log\LoggerInterface;
  14. use stdClass;
  15. use function json_encode;
  16. class InitialStateServiceTest extends TestCase {
  17. /** @var InitialStateService */
  18. private $service;
  19. /** @var MockObject|LoggerInterface|(LoggerInterface&MockObject) */
  20. protected $logger;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->logger = $this->createMock(LoggerInterface::class);
  24. $this->service = new InitialStateService(
  25. $this->logger,
  26. $this->createMock(Coordinator::class),
  27. $this->createMock(IServerContainer::class)
  28. );
  29. }
  30. public function staticData(): array {
  31. return [
  32. ['string'],
  33. [23],
  34. [2.3],
  35. [new class implements JsonSerializable {
  36. public function jsonSerialize(): int {
  37. return 3;
  38. }
  39. }],
  40. ];
  41. }
  42. /**
  43. * @dataProvider staticData
  44. */
  45. public function testStaticData(mixed $value): void {
  46. $this->service->provideInitialState('test', 'key', $value);
  47. $data = $this->service->getInitialStates();
  48. $this->assertEquals(
  49. ['test-key' => json_encode($value)],
  50. $data
  51. );
  52. }
  53. public function testValidDataButFailsToJSONEncode(): void {
  54. $this->logger->expects($this->once())
  55. ->method('error');
  56. $this->service->provideInitialState('test', 'key', ['upload' => INF]);
  57. $data = $this->service->getInitialStates();
  58. $this->assertEquals(
  59. [],
  60. $data
  61. );
  62. }
  63. public function testStaticButInvalidData(): void {
  64. $this->logger->expects($this->once())
  65. ->method('warning');
  66. $this->service->provideInitialState('test', 'key', new stdClass());
  67. $data = $this->service->getInitialStates();
  68. $this->assertEquals(
  69. [],
  70. $data
  71. );
  72. }
  73. /**
  74. * @dataProvider staticData
  75. */
  76. public function testLazyData(mixed $value): void {
  77. $this->service->provideLazyInitialState('test', 'key', function () use ($value) {
  78. return $value;
  79. });
  80. $data = $this->service->getInitialStates();
  81. $this->assertEquals(
  82. ['test-key' => json_encode($value)],
  83. $data
  84. );
  85. }
  86. }